Grokking Algorithms


def countdown(i): print i if



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

def
countdown(i):
print i
if
i <= 0: 
Base case
return
else:
Recursive case
countdown(i-1)
Now the function works as expected. It goes something like this.
Infinite loop


42
Chapter 3
 
 
I
 
 
Recursion
The stack 
his section covers the 
call stack
. It’s an important concept
in programming. he call stack is an important concept in 
general programming, and it’s also important to understand
when using recursion
.
Suppose you’re throwing a barbecue. You keep a todo list for the 
barbecue, in the form of a stack of sticky notes.
Remember back when we talked about arrays and lists, 
and you had a todo list? You could add todo items 
anywhere to the list or delete random items. he stack of 
sticky notes is much simpler. When you insert an item
it gets added to the top of the list. When you read an item, 
you only read the topmost item, and it’s taken of the list. So your todo 
list has only two actions:
push 
(insert) and
pop 
(remove and read).
Let’s see the todo list in action.
his data structure is called a 
stack
. he stack is a simple data structure. 
You’ve been using a stack this whole time without realizing it!


43
The stack
The call stack
Your computer uses a stack internally called the
call stack.
Let’s see it in 
action. Here’s a simple function:
def
greet(name):
print “hello, “ + name + “!”
greet2(name)
print “getting ready to say bye...”
bye()
his function greets you and then calls two other functions. Here are 
those two functions:

Download 6,4 Mb.

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