-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrecursion.py
More file actions
76 lines (65 loc) · 1.89 KB
/
recursion.py
File metadata and controls
76 lines (65 loc) · 1.89 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def sum(n):
if n <= 0:
return 0
else:
return n + sum(n - 1)
def is_palindrome(ss):
if len(ss) < 2:
return True
if ss[0] != ss[-1]:
return False
return is_palindrome(ss[1:-1])
def reverse(ss):
if len(ss) == 0:
return ""
return ss[-1] + reverse(ss[:-1])
def fib(n):
if n < 0:
return 0
if n == 1 or n == 2:
return 1
else:
return fib(n - 1) + fib(n - 2)
def pretty_print(dictionary, indent=" "):
# iterate through every key in the dictionary
for key in dictionary:
# get the value associated with the key
val = dictionary[key]
# check the type of the key to see if it's another dict
if isinstance(val, dict):
print(f"{indent}{key}:")
pretty_print(dictionary[key], indent + indent)
else:
# it's the val isn't a dict then just print out they key and val
print(f"{indent}{key}: {val}")
print(sum(3), "should be", 6)
print(sum(4), "should be", 10)
print()
print(is_palindrome(""), "should be", True)
print(is_palindrome("a"), "should be", True)
print(is_palindrome("aa"), "should be", True)
print(is_palindrome("tacocat"), "should be", True)
print(is_palindrome("lionoil"), "should be", True)
print(is_palindrome("firetruck"), "should be", False)
print()
print(reverse("cat"), "should be", "tac")
print(reverse(reverse("abcdefghijk")), "should be", "abcdefghijk")
print()
print(fib(0), "should be", 0)
print(fib(1), "should be", 1)
print(fib(2), "should be", 1)
print(fib(3), "should be", 2)
print(fib(4), "should be", 3)
print(fib(5), "should be", 5)
print(fib(6), "should be", 8)
print(fib(7), "should be", 13)
print()
d0 = {"foo": 42, "bar": "car"}
d1 = {"foo": 42, "bar": {"baz": "ace"}}
d2 = {"foo": 42, "bar": {"baz": "ace", "deep": {"val": 3333}}, "another": 90}
pretty_print(d0)
print()
pretty_print(d1)
print()
pretty_print(d2)
print()