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, 22.06.2019 00:20
What’s resistance in an electrical circuit ?
Answers: 1
question
Computers and Technology, 22.06.2019 08:40
What are the three parts to physical security standards for various types of army equipment and the risk level
Answers: 2
question
Computers and Technology, 22.06.2019 12:30
What characteristic of long period comets suggest they come directly from the oort cloud?
Answers: 2
question
Computers and Technology, 23.06.2019 02:00
For a typical middle-income family, what is the estimated cost of raising a child to the age of 18? $145,500 $245,340 $304,340 $455,500
Answers: 1
You know the right answer?
Modify the recursive Fibonacci function to employ the memoization technique discussed in this chapte...
Questions
question
Mathematics, 28.12.2019 18:31