Computers and Technology

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()

answer
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 23.06.2019 12:00, deflox74
What does the level 1 topic in a word outline become in powerpoint? a. first-level bullet item b. slide title c. third-level bullet item d. second-level bullet item
Answers: 1
image
Computers and Technology, 23.06.2019 15:00, puppylove899
Barbara is interested in pursuing a career in the science and math pathway. which qualifications will her reach that goal? a. an advanced knowledge of physics and math b. an advanced knowledge of engineering and math c. an advanced knowledge of physics and robotics an d. advanced knowledge of machinery and math
Answers: 1
image
Computers and Technology, 23.06.2019 17:20, Morehollie9428
What is the best assassins creed game?
Answers: 2
image
Computers and Technology, 24.06.2019 02:00, sameh0425
Which steps will open the system so that you can enter a question and do a search for
Answers: 1
Do you know the correct answer?
Modify the recursive Fibonacci function to employ the memoization technique discussed in this chapte...

Questions in other subjects: