Python: Learn Python (Advanced) in 7 Days and Ace It Well. Hands On Challenges INCLUDED! (Python Programming Book Series 2) by Ronald Olsen

Python: Learn Python (Advanced) in 7 Days and Ace It Well. Hands On Challenges INCLUDED! (Python Programming Book Series 2) by Ronald Olsen

Author:Ronald Olsen [Olsen, Ronald]
Language: eng
Format: azw3
Tags: Data Analysis, Python, Python programming, Computer programming, Learn coding, Machine Learning
Publisher: AUVA PRESS
Published: 2017-06-26T04:00:00+00:00


Solution for Challenge 8

def memoize(func):

func.cache = {}

def wrapper(n):

try:

ans = func.cache[n]

except KeyError:

ans = func.cache[n] = func(n)

return ans

return wrapper

@conceptualize

def myfibonacci(n):

"""

>>> print(myfibonacci.cache)

{}

>>> myfibonacci(1)

1

>>> myfibonacci(2)

1

>>> myfibonacci(10)

55

>>> myfibonacci.cache[10]

55

>>> myfibonacci(40)

102334155

"""

assert n >= 0

if n < 2:

return n

else:

return myfibonacci(n-1) + myfibonacci(n-2)

Chapter 8

Python Descriptors

This chapter explores Python descriptors, their syntax, how they are called and several built-in Python descriptors, including the functions, the properties, and the static methods. Let’s jump right in.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.