Advertisement
pavelperc

memoization

Nov 26th, 2018
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.29 KB | None | 0 0
  1. def memoize(f):
  2.     t = dict()
  3.     def lambda1(n):
  4.         if n not in t:
  5.             t[n] = f(n)
  6.         return t[n]
  7.  
  8.     return lambda1
  9.  
  10. fibfast = memoize(lambda n : 1 if n < 2 else fibfast(n-1) + fibfast(n-2))
  11.  
  12.  
  13. print(fibfast(1))
  14. print(fibfast(2))
  15. print(fibfast(3))
  16. print(fibfast(4))
  17. print(fibfast(5))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement