Recursion, Backtracking and Dynamic Programming in Python