subject

Modify the recursive Fibonacci function to employ the memoization technique discussed in this chapter. The function creates a dictionary and then defines a nested recursive helper function named memoizedFib. The base case is the same as before. However, before making a recursive call, the helper function looks up the value for the function’s current argument in the dictionary (use the method get, with None as the default value). If the value exists, the function returns it. Otherwise, after the helper function adds the results of its two recursive calls, it saves the sum in the dictionary with the current argument of the function as the key. Also use the Counterobject discussed in this chapter to count the number of recursive calls of the helper function. Included code:def fib(n):"""Returns the nth Fibonacci number."""if n < 3:return 1else:return fib(n - 1) + fib(n - 2)def main():"""Tests the function with some powers of 2."""problemSize = 2print("%4s%12s" % ("n", "fib(n)"))for count in range(5):print("%4d%12d" % (problemSize, fib(problemSize)))problemSize *= 2if __name__ == "__main__":main()

ansver
Answers: 3

Another question on Computers and Technology

question
Computers and Technology, 23.06.2019 16:30
How to do this programming flowchart?
Answers: 3
question
Computers and Technology, 24.06.2019 13:00
Which one of the following functions is not available on the autosum tool? sum average if max
Answers: 3
question
Computers and Technology, 24.06.2019 19:00
Luis is cloud-based( microsoft bot framework). true false
Answers: 1
question
Computers and Technology, 26.06.2019 02:00
How do you measure text with an e-gauge
Answers: 1
You know the right answer?
Modify the recursive Fibonacci function to employ the memoization technique discussed in this chapte...
Questions
question
History, 03.05.2020 13:31
question
Computers and Technology, 03.05.2020 13:31
question
Mathematics, 03.05.2020 13:31
question
Chemistry, 03.05.2020 13:31
Questions on the website: 13722360