Python Projects for Beginners a ten-Week Bootcamp Approach to Python Programming



Download 2,61 Mb.
bet142/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   138   139   140   141   142   143   144   145   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

Using @lru_cache


Now that we know how to create a caching system ourselves, let’s use Python’s built-in method for memoization. It’s known as “lru_cache” or Least Recently Used Cache. It performs the same way our memoization technique did earlier; however, it’ll do it in less lines of code because we apply it as a decorator. Let’s check it out:

# using @lru_cache, Python’s default moization/caching technique from functools import lru_cache @lru_cache( ) # python’s built-in memoization/caching system def fib(n): if n <= 1:
return n else:
return fib( n – 1 ) + fib( n – 2 ) fib(50) # calculates almost instantly

Go ahead and run the cell. We’ll get the same output as we did in the preceding cell but this time with less lines. It’s performing the exact same technique, except it’s applied as a decorator rather than directly within the function. There’s no better way, as far as performance goes, but using lru_cache is much easier on the eyes.

THURSDAY EXERCISES


  1. Factorial Caching: apply either the lru_cache built-in decorator to the factorial function that we created previously, or set up your own caching system.

  2. Searching Data: Create a function that takes in two arguments, a list of data and an item to search for. search through the list of data passed in and return true if the item to search for appears, otherwise, return False. If one of the items is another list, create a recursive call so that you don’t need to create another loop. Use the example call in the following as a reference on what data to expect:

>>> searchList( [ 2, 3, [ 18, 22 ], 6 ], 22 )

today, we learned all about recursive functions and how to improve them with the concept of memoization. We were able to use a simple caching technique in order to store previously computed values. Recursive functions can be useful when it makes sense to use them, but in most cases a simple for loop would suffice, since recursive functions can become slow over time.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   138   139   140   141   142   143   144   145   ...   200




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish