January 2020
Intermediate to advanced
432 pages
10h 18m
English
A key concept in DP is to break down larger problems into smaller subproblems, then solve said smaller problems and store the result. The fancy name for this activity is called memoization and the best way to showcase how this works is with an example. Open up Chapter_2_3.py and follow the exercise:
fibSequence = [0,1]def Fibonacci(n): if n<0: print("Outside bounds") elif n<= len(fibSequence): return fibSequence[n-1] else: print("Solving for {}".format(n)) fibN = Fibonacci(n-1) + Fibonacci(n-2) fibSequence.append(fibN) return fibN print(Fibonacci(9))
Read now
Unlock full access