Skip to content

Commit 84dde48

Browse files
Mr_heMr_he
Mr_he
authored and
Mr_he
committed
1.7 Recursive Functions
1 parent 4750464 commit 84dde48

File tree

15 files changed

+407
-0
lines changed

15 files changed

+407
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
def cascade(n):
2+
print(n)
3+
if n >= 10:
4+
cascade(n//10)
5+
print(n)
6+
7+
#cascade(233431)
8+
9+
def is_event(n):
10+
return n % 2 == 0
11+
12+
def play_alice(n):
13+
if n == 0:
14+
print("Bob win!")
15+
else:
16+
play_bob(n-1)
17+
18+
def play_bob(n):
19+
if n == 0:
20+
print("Alice win!")
21+
else:
22+
if is_event(n):
23+
play_alice(n-2)
24+
else:
25+
play_alice(n-1)
26+
27+
def inverse_cascade(n):
28+
grow(n)
29+
print(n)
30+
shrink(n)
31+
32+
def f_then_g(f, g, n):
33+
if n:
34+
f(n)
35+
g(n)
36+
37+
grow = lambda n: f_then_g(grow, print, n // 10)
38+
shrink = lambda n: f_then_g(print, shrink, n // 10)
39+
40+
def count_partitions(n, m):
41+
if n == 0:
42+
return 1
43+
elif n < 0:
44+
return 0
45+
elif m == 0:
46+
return 0
47+
else:
48+
return count_partitions(n-m, m) + count_partitions(n, m-1)
49+
50+
print(count_partitions(20,20))
51+
52+
53+
54+
55+
56+
57+
58+
59+
60+
61+
62+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
def split(n):
2+
return n // 10, n % 10
3+
4+
def sum_digits(n):
5+
if n < 10:
6+
return n
7+
else:
8+
all_but_last, last = split(n)
9+
return last + sum_digits(all_but_last)
10+
11+
def luhn_sum(n):
12+
if n < 10:
13+
return n
14+
else:
15+
all_but_last, last = split(n)
16+
return luhn_sum_double(n) + last
17+
18+
def luhn_sum_double(n):
19+
all_but_last, last = split(n)
20+
luhn_digit = sum_digits(2 * last)
21+
if n < 10:
22+
return luhn_digit
23+
else:
24+
return luhn_sum(all_but_last) + luhn_digit
25+

cs61a/lab/lab02/.ok_history

974 Bytes
Binary file not shown.

cs61a/lab/lab02/.ok_messages

6 Bytes
Binary file not shown.

cs61a/lab/lab02/lab02.ok

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "Lab 2",
3+
"endpoint": "cal/cs61a/sp16/lab02",
4+
"src": [
5+
"lab02.py",
6+
"lab02_extra.py"
7+
],
8+
"tests": {
9+
"tests/lambda.py": "ok_test",
10+
"tests/hof.py": "ok_test",
11+
"lab02.py:lambda_curry2": "doctest",
12+
"lab02.py:adder": "doctest",
13+
"tests/factorial_ok.py": "ok_test",
14+
"tests/skip_mul_ok.py": "ok_test",
15+
"lab02.py:skip_mul": "doctest",
16+
"tests/count_up_ok.py": "ok_test",
17+
"lab02.py:count_up": "doctest",
18+
"lab02.py:gcd": "doctest",
19+
"lab02_extra.py:hailstone": "doctest",
20+
"lab02_extra.py:count_cond": "doctest",
21+
"lab02_extra.py:cycle": "doctest",
22+
"lab02_extra.py:paths": "doctest"
23+
},
24+
"protocols": [
25+
"restore",
26+
"file_contents",
27+
"analytics",
28+
"unlock",
29+
"grading",
30+
"backup"
31+
]
32+
}

cs61a/lab/lab02/lab02.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
"""Lab 2: Higher Order Functions & Lambdas & Recursions"""
2+
3+
def lambda_curry2(func):
4+
"""
5+
Returns a Curried version of a two argument function func.
6+
>>> from operator import add
7+
>>> x = lambda_curry2(add)
8+
>>> y = x(3)
9+
>>> y(5)
10+
8
11+
"""
12+
"*** YOUR CODE HERE ***"
13+
return lambda x: lambda y: func(x, y)
14+
15+
def adder(f1, f2):
16+
"""
17+
Return a function that takes in a single variable x, and returns
18+
f1(x) + f2(x). You can assume the result of f1(x) and f2(x) can be
19+
added together, and they both take in one argument.
20+
21+
>>> identity = lambda x: x # returns inpuSettingst
22+
>>> square = lambda x: x**2
23+
>>> a1 = adder(identity, square) # x + x^2
24+
>>> a1(4)
25+
20
26+
>>> a2 = adder(a1, identity) # (x + x^2) + x
27+
>>> a2(4)
28+
24
29+
>>> a2(5)
30+
35
31+
>>> a3 = adder(a1, a2) # (x + x^2) + (x + x^2 + x)
32+
>>> a3(4)
33+
44
34+
"""
35+
return lambda x: f1(x) + f2(x)
36+
37+
def skip_mul(n):
38+
"""Return the product of n * (n - 2) * (n - 4) * ...
39+
40+
>>> skip_mul(5) # 5 * 3 * 1
41+
15
42+
>>> skip_mul(8) # 8 * 6 * 4 * 2 * 0
43+
0
44+
"""
45+
if n == 0:
46+
return 0
47+
elif n == 1:
48+
return 1
49+
else:
50+
return n * skip_mul(n - 2)
51+
52+
def count_up(n):
53+
"""Print out all numbers up to and including n in ascending order.
54+
55+
>>> count_up(5)
56+
1
57+
2
58+
3
59+
4
60+
5
61+
"""
62+
def counter(i):
63+
if i <= n:
64+
print(i)
65+
counter(i+1)
66+
counter(1)
67+
68+
def gcd(a, b):
69+
"""Returns the greatest common divisor of a and b.
70+
Should be implemented using recursion.
71+
72+
>>> gcd(34, 19)
73+
1
74+
>>> gcd(39, 91)
75+
13
76+
>>> gcd(20, 30)
77+
10
78+
>>> gcd(40, 40)
79+
40
80+
"""
81+
if a < b:
82+
a, b = b, a # a is lager number and b is smaller number
83+
if a % b == 0:
84+
return b
85+
else:
86+
return gcd(b, a % b)

cs61a/lab/lab02/lab02_extra.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
"""Optional program for lab02 """
2+
3+
from lab02 import *
4+
5+
# Extra Question
6+
7+
def hailstone(n):
8+
"""Print out the hailstone sequence starting at n, and return the
9+
number of elements in the sequence.
10+
11+
>>> a = hailstone(10)
12+
10
13+
5
14+
16
15+
8
16+
4
17+
2
18+
1
19+
>>> a
20+
7
21+
"""
22+
print(n)
23+
if n == 1:
24+
return 1
25+
if n % 2 == 0:
26+
return hailstone(n//2) + 1
27+
else:
28+
return hailstone(n*3+1) + 1
29+
30+
def count_cond(condition):
31+
"""
32+
>>> count_factors = count_cond(lambda n, i: n % i == 0)
33+
>>> count_factors(2) # 1, 2
34+
2
35+
>>> count_factors(4) # 1, 2, 4
36+
3
37+
>>> count_factors(12) # 1, 2, 3, 4, 6, 12
38+
6
39+
40+
>>> is_prime = lambda n, i: count_factors(i) == 2
41+
>>> count_primes = count_cond(is_prime)
42+
>>> count_primes(2) # 2
43+
1
44+
>>> count_primes(3) # 2, 3
45+
2
46+
>>> count_primes(4) # 2, 3
47+
2
48+
>>> count_primes(5) # 2, 3, 5
49+
3
50+
>>> count_primes(20) # 2, 3, 5, 7, 11, 13, 17, 19
51+
8
52+
"""
53+
def count(n):
54+
i, count = 1, 0
55+
while i <= n:
56+
if condition(n, i):
57+
count += 1
58+
i += 1
59+
return count
60+
return count
61+
62+
def cycle(f1, f2, f3):
63+
""" Returns a function that is itself a higher order function
64+
>>> def add1(x):
65+
... return x + 1
66+
>>> def times2(x):
67+
... return x * 2
68+
>>> def add3(x):
69+
... return x + 3
70+
>>> my_cycle = cycle(add1, times2, add3)
71+
>>> identity = my_cycle(0)
72+
>>> identity(5)
73+
5
74+
>>> add_one_then_double = my_cycle(2)
75+
>>> add_one_then_double(1)
76+
4
77+
>>> do_all_functions = my_cycle(3)
78+
>>> do_all_functions(2)
79+
9
80+
>>> do_more_than_a_cycle = my_cycle(4)
81+
>>> do_more_than_a_cycle(2)
82+
10
83+
>>> do_two_cycles = my_cycle(6)
84+
>>> do_two_cycles(1)
85+
19
86+
"""
87+
def g(n):
88+
def h(x):
89+
if n == 0:
90+
return x
91+
elif n == 1:
92+
return f1(x)
93+
elif n == 2:
94+
return f2(f1(x))
95+
elif n == 3:
96+
return f3(f2(f1(x)))
97+
else:
98+
c = (n-1) % 3 + 1
99+
out_most_function = g(c)
100+
rest_result = g(n-c)(x)
101+
return out_most_function(rest_result)
102+
return h
103+
return g
104+
105+
def paths(m, n):
106+
"""Return the number of paths from one corner of an
107+
M by N grid to the opposite corner.
108+
109+
>>> paths(2, 2)
110+
2
111+
>>> paths(5, 7)
112+
210
113+
>>> paths(117, 1)
114+
1
115+
>>> paths(1, 157)
116+
1
117+
"""
118+
if m == 1 or n == 1:
119+
return 1
120+
else:
121+
return paths(m-1, n) + paths(m, n-1)

cs61a/lab/lab02/ok

176 KB
Binary file not shown.

cs61a/projects/hog.zip

-202 KB
Binary file not shown.

cs61a/projects/hog_contest/hog.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
This is a minimal contest submission file. You may also submit the full
3+
hog.py from Project 1 as your contest entry.
4+
5+
Only this file will be submitted. Make sure to include any helper functions
6+
from `hog.py` that you'll need here! For example, if you have a function to
7+
calculate Free Bacon points, you should make sure it's added to this file
8+
as well.
9+
"""
10+
11+
def final_strategy(score, opponent_score):
12+
return 5

cs61a/projects/hog_contest/ok

176 KB
Binary file not shown.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "Project 1 Contest: Hog Strategy",
3+
"endpoint": "cal/cs61a/sp16/proj1contest",
4+
"src": [
5+
"hog.py"
6+
],
7+
"tests": {
8+
"tests/import.py": "ok_test",
9+
"tests/final.py": "ok_test"
10+
},
11+
"protocols": [
12+
"file_contents",
13+
"analytics",
14+
"grading",
15+
"backup"
16+
]
17+
}

cs61a/projects/hog_contest/tests/__init__.py

Whitespace-only changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
test = {
2+
'name': 'Time Check',
3+
'points': 0,
4+
'suites': [
5+
{
6+
'cases': [
7+
{
8+
'code': r"""
9+
>>> for i in range(100):
10+
... for j in range(100):
11+
... assert -1 <= final_strategy(i, j) <= 10
12+
""",
13+
'hidden': False,
14+
'locked': False
15+
}
16+
],
17+
'scored': True,
18+
'setup': r"""
19+
>>> from hog import final_strategy
20+
""",
21+
'teardown': '',
22+
'type': 'doctest'
23+
}
24+
]
25+
}

0 commit comments

Comments
 (0)