Else: recurse(n-1, n+s)



Download 18,38 Kb.
Sana30.05.2022
Hajmi18,38 Kb.
#619895
Bog'liq
homework 2

Homework for Lesson #3 Valijonov Umirbek

1) def recurse(n, s):
if n == 0:
print(s)
else:
recurse(n-1, n+s)
recurse(3, 0)
1. What would happen if you called this function like this:
recurse(-1, 0) ?

2. Write a docstring that explains everything someone would need to know in


order to use this function (and nothing else).
"""
def recurse(n, s):
'''Recursive function for getting and printing the value of s;
n - positive integer;
s - integer.
'''
if n == 0:
print(s)
else:
recurse(n-1, n+s)

'''main :


None
recurse: n ---> 3 s ---> 0
recurse: n ---> 2 s ---> 3
recurse: n ---> 1 s ---> 5
recurse: n ---> 0 s ---> 6
'''
# Answer1:
# recurse(-1, 0) will be infinite recursion.
In [16]:
#!/usr/bin/env python
def check_fermat(a,b,c,n): if n <= 2:
print("Fermat's Last Theorem only holds for n>2")
return
else:
if a n + b n == c ** n:
print("Holy smokes, Fermat was wrong!")
else:
print("No, that doesn't work")
def get_user_input():
a = int(raw_input("Enter a: "))
b = int(raw_input("Enter b: "))
c = int(raw_input("Enter c: "))
n = int(raw_input("Enter n: "))
check_fermat(a, b, c, n)
if name == "__main__":
get_user_input()
Enter a: 5
Enter b: 6
Enter c: 9090
Enter n: 65
No, that doesn't work
3) radius = distance(xc, yc, xp, yp)
result = area(radius)
return result
def area2(xc, yc, xp, yp):
radius = distance(xc, yc, xp, yp)
result = area(radius)
return result
def area2(xc, yc, xp, yp):
return area(distance(xc, yc, xp, yp))

4) def factorial(n):


def factorial(n):
if n == 0:
return 1
def factorial(n):
if n == 0:
return 1
else: recurse = factorial(n-1) result = n * recurse
return result

5) def fibonacci (n):


if n == 0:
return 0
elif n == 1:
return 1
else:
return fibonacci(n-1) + fibonacci(n-2)
Download 18,38 Kb.

Do'stlaringiz bilan baham:




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