Grokking Algorithms


def countdown(i): print i if



Download 24,82 Mb.
Pdf ko'rish
bet28/122
Sana22.07.2022
Hajmi24,82 Mb.
#839971
1   ...   24   25   26   27   28   29   30   31   ...   122
Bog'liq
grokking-algorithms-illustrated-programmers-curious

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 
This section covers the 
call stack
. It’s an important concept
in programming. The 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. The 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 off 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.
This data structure is called a 
stack
. The 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()
This function greets you and then calls two other functions. Here are 
those two functions:

Download 24,82 Mb.

Do'stlaringiz bilan baham:
1   ...   24   25   26   27   28   29   30   31   ...   122




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