Grokking Algorithms



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

Chapter 3
 
 
I
 
 
Recursion
his chapter also includes a lot of pseudocode. 
Pseudocode
is a
high-level description of the problem you’re trying to solve, in code.
It’s written like code, but it’s meant to be closer to human speech.
Recursion
Suppose you’re digging through your grandma’s attic and come across a 
mysterious locked suitcase.
Grandma tells you that the key for the suitcase is probably in this
other box.
his box contains more boxes, with more boxes inside those boxes. he 
key is in a box somewhere. What’s your algorithm to search for the key? 
hink of an algorithm before you read on.


39
Recursion
Here’s one approach.
1. Make a pile of boxes to look through.
2. Grab a box, and look through it.
3. If yo
u ind a box, add it to the pile to look through later.
4. If you ind a key, you’re done!
5. Repeat.
Here’s an alternate approach.
1. Look through the box.
2. If you ind a box, go to step 1.
3. If you ind a key, you’re done!


40
Chapter 3
 
 
I
 
 
Recursion
Which approach seems easier to yo
u? he irst approach uses a 
while
loop. While the pile isn’t empty, grab a box and look through it:
def
look_for_key(main_box):
pile = main_box.make_a_pile_to_look_through()
while
pile is not empty:
box = pile.grab_a_box()
for
item in box:
if
item.is_a_box():
pile.append(item)
elif
item.is_a_key():
print “found the key!”
he second way uses recursion. 
Recursion 
is where a function calls itself. 
Here’s the second way in pseudocode:

Download 6,4 Mb.

Do'stlaringiz bilan baham:
1   ...   21   22   23   24   25   26   27   28   ...   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