-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursion_In_class.py
More file actions
55 lines (46 loc) · 1.03 KB
/
recursion_In_class.py
File metadata and controls
55 lines (46 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def fac(n):
if n == 1:
return 1
else:
return n * fac(n-1)
print(fac(10))
def reverse(s):
if s == "":
return s
else:
return reverse(s[1:]) + s[0]
print(reverse('hello'))
# Plandrome
# 'abcba' return True. if first element doesnot equal last return wrong
# other wise return True.
# 'abcd' return False
def palidrome(s):
if len(s) == 0:
return True
elif s[0] != s[-1]:
return False
else:
print(s)
return palidrome(s[1:-1])
print(palidrome('helloolleh'))
# Fib(n) = n-1
def fib(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fib(n-1) + fib(n-2)
name = input('enter something: ')
result = fib(int(name))
print('fib(%s) = %d' % (name, result))
import turtle
def draw (myturtle, linelen):
if linelen > 0:
myturtle.forward(linelen)
myturtle.right(90)
draw(myturtle, linelen - 5)
myturtle = turtle.Turtle()
mywin = turtle.Screen()
draw(myturtle, 150)
mywin.exitonclick()