Grokking Algorithms


def look_for_key(box): for



Download 6,4 Mb.
Pdf ko'rish
bet26/120
Sana21.12.2022
Hajmi6,4 Mb.
#893167
1   ...   22   23   24   25   26   27   28   29   ...   120
Bog'liq
Grokking Algorithms An Illustrated Guide for Programmers and Other

def
look_for_key(box):
for
item in box:
if
item.is_a_box():
look_for_key(item) 
Recursion!
elif
item.is_a_key():
print “found the key!”
Both approaches accomplish the same thing, but the second approach 
is clearer to me. Recursion is used when it makes the solution clearer. 
here’s no performance beneit to using recursion; in fact, loops are 
sometimes better for performance. I like this quote by Leigh Caldwell 
on Stack Overlow: “Loops may achieve a performance gain for 
your program. Recursion may achieve a performance gain for your 
programmer. Choose which is more important in your situation!”
1
Many important algorithms use recursion, so it’s important to 
understand the concept.
Base case and recursive case
Because a recursive function calls itself, it’s easy to write a 
function incorrectly that ends up in an ininite loop. For 
example, suppose you want to write a function that prints a countdown
like this:
> 3...2...1
1
 http://stackoverlow.com/a/72694/139117.


41
Base case and recursive case
You can write it recursively, like so:
def countdown(i):
print i
countdown(i-1)
Write out this code and run it. You’ll notice a problem: this function 
will run forever!
> 3...2...1...0...-1...-2...
(Press Ctrl-C to kill your script.)
When you write a recursive function, you have to tell it when to stop 
recursing
. hat’s why 
every recursive function has two parts: the base 
case, and the recursive case.
he recursive case is when the function calls 
itself. he base case is when the function doesn’t call itself again … so it 
doesn’t go into an ininite loop.
Let’s add a base case to the countdown function:

Download 6,4 Mb.

Do'stlaringiz bilan baham:
1   ...   22   23   24   25   26   27   28   29   ...   120




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