-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecursion.py
194 lines (181 loc) · 4.79 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# Recursive Python program to...
# determine how many times a given letter occurs in a string
def countLetter(string, l, i=0, count=0):
string = string.lower()
if len(string) > 0 and string[i] == l:
count += 1
if i == len(string) - 1 or len(string) == 0:
return count
return countLetter(string, l, i+1, count)
# find the Fibonacci series till a specific term
def fibonacciSeries(n, x=0, y=1):
if type(n) != int:
raise TypeError("n must be a positive int")
if n < 0:
raise ValueError("n must be a positive int")
if n == 0:
return
if x == 0:
if n == 1:
return 0
if n == 2:
print(0)
return 1
print(0)
print(1)
n -= 2
x += y
y += x
if n != 0:
if n == 1:
return x
print(x)
n -= 1
if n != 0:
if n == 1:
return y
print(y)
return fibonacciSeries(n-1, x, y)
# find the sum of elements in a list
def sumOfList(arr, i=0):
if len(arr) == 0:
return 0
if i == len(arr) - 1:
return arr[i]
return arr[i] + sumOfList(arr, i+1)
# find the binary equivalent of a number
def decToBin(d):
if type(d) != int:
raise TypeError("d must be an int")
if d == 0:
return 0
if d == 1:
return 1
if d == -1:
return -1
return int(str(decToBin(int(d/2))) + str(d%2))
# find the sum of the digits of the number
def sumDigits(n, i=0):
n = str(n)
if n[i] == "-":
i += 1
if n[i] == ".":
i += 1
if i == len(n) - 1:
if n[0] == "-":
return -int(n[i])
return int(n[i])
if n[0] == "-":
return -int(n[i]) + sumDigits(n, i+1)
return int(n[i]) + sumDigits(n, i+1)
# find the LCM of two numbers
def lcm(x, y, i=1):
x, y = abs(x), abs(y)
if x == 0 or y == 0:
return 0
if x == y:
return x
if x > y:
if i*x % y == 0:
return int(i * x)
if x < y:
if i*y % x == 0:
return int(i * y)
return lcm(x, y, i+1)
# find if a number is prime
def prime(n, i=2):
if type(n) != int:
raise TypeError("n must be an int")
#function will throw error if value is too large
if n > 1998:
raise ValueError("n must be lesser than 1999")
if n == 2:
return True
if n < 2 or n%2 == 0:
return False
if n%i == 0:
return False
if i == n//2 + 1:
return True
return prime(n, i+1)
# find the product of two numbers
def multiply(x, y, a=0):
if x == 0:
return 0
if type(y) == float:
if type(x) == float:
raise TypeError("can only accept one float")
x, y = y, x
if y < 0:
return multiply(x, y+1, a-x)
if y == 0:
return round(a, 9)
return multiply(x, y-1, a+x)
# find the power of a number
def power(x, n, a=1):
if type(n) != int:
raise TypeError("n must be an int")
if x == 0 and n == 0:
raise ZeroDivisionError("power and base cannot both be zero")
if x == 0:
return 0
if n < 0:
return power(x, n+1, a*(1/x))
if n == 0:
return a
return power(x, n-1, a*x)
# check whether a string is a palindrome
def palindrome(string, i=0):
if len(string) == 0 or len(string) == 1:
return True
string = string.lower()
if string[i] != string[-(i+1)]:
return False
if i == len(string) // 2 - 1:
return True
return palindrome(string, i+1)
# reverse a string
def reverse(string, n):
if n == 0:
return string
if n - 1 == 0:
return string
return string[n-1] + reverse(string[:-1], n-1)
# flatten a nested list
def flatten(arr, i=0):
if len(arr) == 0:
return arr
if type(arr[i]) == list:
if len(arr[i]) == 0:
arr.pop(i)
i -= 1
else:
k = 0
for j in range(len(arr[i])-1, -1, -1):
arr.insert(i, arr[i+k][j])
k += 1
arr.pop(i+k)
if type(arr[i]) == list:
flatten(arr, i)
if i == len(arr) - 1:
return arr
return flatten(arr, i+1)
# find the total sum of a nested list
def flatSum(arr, i=0):
if len(arr) == 0:
return 0
if type(arr[i]) == list:
if len(arr[i]) == 0:
arr[i].append(0)
arr = arr + arr.pop(i)
return flatSum(arr, i)
if i == len(arr) - 1:
return arr[i]
return arr[i] + flatSum(arr, i+1)
# find the length of a list
def length(arr, l=0):
try:
arr[l]
except IndexError:
return l
return length(arr, l+1)