Python Solution of Recursion and Dynamic Programming part