-
Notifications
You must be signed in to change notification settings - Fork 0
/
recursion.py
79 lines (63 loc) · 1.8 KB
/
recursion.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# To check if given number or word is a palindrome
def is_palindrome(c):
'''Returns whether the given input is a palindrome or not'''
c= str(c)
if len(c) == 0 or len(c) == 1:
return True
else:
if c[0] == c[-1] and is_palindrome(c[1:-1]):
return True
else:
return False
# Fibonacci
def fibonacci(n):
'''Returns the nth fibonacci number'''
if n < 0 or isinstance(n, float):
return print ("Please enter a positive integer")
else:
if n == 0 or n == 1:
return 1
else:
return fibonacci(n-1) + fibonacci(n-2)
# Factorial of a number
def factorial(n):
'''Returns the factorial of n'''
if n < 0 or isinstance(n, float):
return print ("Please enter a positive integer")
else:
if n == 0 or n == 1:
return 1
else:
return n * factorial(n-1)
# Sum of numbers in list
c = [3,5,6,2]
def add_list(c):
'''Returns the sum of numbers in list'''
if len(c) == 1:
return c[0]
elif len(c) == 0:
print ("The list is empty")
return None
else:
return c[0] + add_list(c[1:])
# Maximum of a list
c = [300,5,6,20000,2334534,424545763,23432536,23,52,45,2]
def maximum_list(c):
'''Returns the maximum value in the given list'''
if len(c) == 1:
return c[0]
elif len(c) == 0:
print ("The list is empty")
return None
else:
if c[0] > maximum_list(c[1:]):
return c[0]
else:
return maximum_list(c[1:])
# Sum of all positive integers upto a given number
def add_integers_upto(num):
'''Returns the sum of all positive integers upto num'''
if num == 0: return 0
elif num == 1: return 1
else:
return num + add_integers_upto(num-1)