-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursivePractice.py
More file actions
60 lines (40 loc) · 1.26 KB
/
recursivePractice.py
File metadata and controls
60 lines (40 loc) · 1.26 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
55
56
57
58
59
60
# Recursively reverse a string
def reverse_string(s):
if len(s) < 2:
return s
return reverse_string(s[1:]) + s[0]
print(reverse_string("1234") == "4321") # True
# Maps a given function over nested list
def map_f(f, arr, result=None):
if result is None:
result = []
for x in arr:
if isinstance(x, list):
map_f(f, x, result)
else:
result.append(f(x))
return result
print(map_f(lambda x: x * x, [1, 2, [3, 4, [5]]]) == [1, 4, 9, 16, 25]) # True
# Count the number of ways to change any given amount
# Note: Not working right, counts duplicate denominations
def count_change(val, coins, count=None):
if count is None:
count = 0
if val < 0 or coins is None:
return 0
if val == 0:
return 1
for c in coins:
count += count_change(val - c, coins)
return count
print(count_change(10, [1, 5]) == 3)
# Generate all permutations of a list recursively
def permute(arr, start=0):
end = len(arr)
if start == end and arr is not None:
print(arr) # O(n)
for i in range(start, end): # O(n!)
arr[start], arr[i] = arr[i], arr[start]
permute(arr, start + 1)
arr[start], arr[i] = arr[i], arr[start]
permute([1, 2, 3])