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, 22.06.2019 11:40, silviamgarcia
Pthreads programming: create and terminate a thread write a c++ program that creates a thread. the main will display a message “hello world from the main”. the main will create a thread that will display a message “hello world from the thread” and then terminates with a call to pthread_exit()
Answers: 3
image
Computers and Technology, 23.06.2019 11:30, kyraj21
Which excel file extension stores automated steps for repetitive tasks?
Answers: 1
image
Computers and Technology, 23.06.2019 12:30, legend101xD
Animations and transitions are added from the
Answers: 1
image
Computers and Technology, 24.06.2019 10:00, shrafe
Which two technologies support the building of single-page applications?
Answers: 2
Do you know the correct answer?
Modify the recursive Fibonacci function to employ the memoization technique discussed in this chapte...

Questions in other subjects: