-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
100 changed files
with
1,323 additions
and
7,420 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
[{"due": "9/25/24", "link": "hw/hw03/", "name": "HW 03: Higher-Order Functions", "piazza_name": "HW 03", "release": "9/18/24"}, {"due": "9/27/24", "link": "lab/lab04/", "name": "Lab 04: Recursion", "piazza_name": "Lab 04", "release": "9/23/24"}] | ||
[{"due": "9/25/24", "link": "hw/hw03/", "name": "HW 03: Higher-Order Functions", "piazza_name": "HW 03", "release": "9/18/24"}, {"due": "9/27/24", "link": "lab/lab04/", "name": "Lab 04: Recursion", "piazza_name": "Lab 04", "release": "9/23/24"}, {"due": "10/2/24", "link": "hw/hw04/", "name": "HW 04: Recursion", "piazza_name": "HW 04", "release": "9/25/24"}] |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# Tree Recursion | ||
|
||
# ----------------------- # | ||
# Tracing factorial | ||
|
||
from ucb import trace | ||
|
||
@trace | ||
def fact(n): | ||
""" computes the factorial of n (n!) | ||
>>> fact(5) # 5! = 5 * 4 * 3 * 2 * 1 | ||
120 | ||
""" | ||
if n == 0 or n == 1: | ||
return 1 | ||
else: | ||
return n * fact(n-1) | ||
|
||
# ----------------------- # | ||
# Mutual Recursion | ||
# (Counting Unique Prime Factors Recursively) | ||
# https://pythontutor.com/render.html#code=def%20smallest_factor%28n%29%3A%0A%20%20%20%20if%20%28n%252%20%3D%3D%200%29%3A%0A%20%20%20%20%20%20%20%20return%202%0A%20%20%20%20k%20%3D%203%0A%20%20%20%20while%20%28k%20%3C%20n%29%3A%0A%20%20%20%20%20%20%20%20if%20%28n%25k%20%3D%3D%200%29%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20k%0A%20%20%20%20%20%20%20%20k%20%2B%3D%201%0A%20%20%20%20return%20n%0A%0A%0Adef%20unique_prime_factors%28n%29%3A%0A%20%20%20%20%22%22%22Return%20the%20number%20of%20unique%20prime%20factors%20of%20n.%0A%0A%20%20%20%20%3E%3E%3E%20unique_prime_factors%2851%29%20%20%23%203%20*%2017%0A%20%20%20%202%0A%20%20%20%20%3E%3E%3E%20unique_prime_factors%2827%29%20%20%20%23%203%20*%203%20*%203%0A%20%20%20%201%0A%20%20%20%20%3E%3E%3E%20unique_prime_factors%28120%29%20%23%202%20*%202%20*%202%20*%203%20*%205%0A%20%20%20%203%0A%20%20%20%20%22%22%22%0A%20%20%20%20k%20%3D%20smallest_factor%28n%29%0A%20%20%20%20def%20no_k%28n%29%3A%0A%20%20%20%20%20%20%20%20%22Return%20the%20number%20of%20unique%20prime%20factors%20of%20n%20other%20than%20k.%22%0A%20%20%20%20%20%20%20%20if%20n%20%3D%3D%201%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%200%0A%20%20%20%20%20%20%20%20elif%20n%20%25%20k%20!%3D%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20unique_prime_factors%28n%29%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20no_k%28n//k%29%0A%20%20%20%20return%201%2Bno_k%28n%29%0A%0Aunique_prime_factors%2860%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false | ||
def smallest_factor(n): | ||
if (n%2 == 0): | ||
return 2 | ||
k = 3 | ||
while (k < n): | ||
if (n%k == 0): | ||
return k | ||
k += 1 | ||
return n | ||
|
||
|
||
def unique_prime_factors(n): | ||
"""Return the number of unique prime factors of n. | ||
>>> unique_prime_factors(51) # 3 * 17 | ||
2 | ||
>>> unique_prime_factors(27) # 3 * 3 * 3 | ||
1 | ||
>>> unique_prime_factors(120) # 2 * 2 * 2 * 3 * 5 | ||
3 | ||
""" | ||
k = smallest_factor(n) | ||
def no_k(n): | ||
"Return the number of unique prime factors of n other than k." | ||
if n == 1: | ||
return 0 | ||
elif n % k != 0: | ||
return unique_prime_factors(n) | ||
else: | ||
return no_k(n//k) | ||
return 1+no_k(n) | ||
|
||
unique_prime_factors(120) | ||
|
||
# ----------------------- # | ||
# Count Partitions | ||
# https://pythontutor.com/cp/composingprograms.html#code=def%20count_partitions%28n,%20m%29%3A%0A%20%20%20%20if%20n%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20return%201%0A%20%20%20%20elif%20n%20%3C%200%3A%0A%20%20%20%20%20%20%20%20return%200%0A%20%20%20%20elif%20m%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20return%200%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20with_m%20%3D%20count_partitions%28n-m,%20m%29%20%0A%20%20%20%20%20%20%20%20without_m%20%3D%20count_partitions%28n,%20m-1%29%0A%20%20%20%20%20%20%20%20return%20with_m%20%2B%20without_m%0A%20%20%20%20%20%20%20%20%0Aresult%20%3D%20count_partitions%285,%203%29%0A%0A%23%201%20%2B%201%20%2B%201%20%2B%201%20%2B%201%20%3D%205%0A%23%201%20%2B%201%20%2B%201%20%2B%202%20%2B%20%20%20%3D%205%0A%23%201%20%2B%202%20%2B%202%20%2B%20%20%20%20%20%20%20%3D%205%0A%23%201%20%2B%201%20%2B%203%20%2B%20%20%20%20%20%20%20%3D%205%0A%23%202%20%2B%203%20%2B%20%20%20%20%20%20%20%20%20%20%20%3D%205&cumulative=false&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D | ||
|
||
def count_partitions(n, m): | ||
if n == 0: | ||
return 1 | ||
elif n < 0: | ||
return 0 | ||
elif m == 0: | ||
return 0 | ||
else: | ||
with_m = count_partitions(n-m, m) | ||
without_m = count_partitions(n, m-1) | ||
return with_m + without_m | ||
|
||
result = count_partitions(5, 3) | ||
|
||
# 1 + 1 + 1 + 1 + 1 = 5 | ||
# 1 + 1 + 1 + 2 + = 5 | ||
# 1 + 2 + 2 + = 5 | ||
# 1 + 1 + 3 + = 5 | ||
# 2 + 3 + = 5 | ||
|
||
# ----------------------- # | ||
|
||
def count_park(n): | ||
"""Count the ways to park cars and motorcycles in n adjacent spots. | ||
>>> count_park(1) # '.' or '%' | ||
2 | ||
>>> count_park(2) # '..', '.%', '%.', '%%', or '<>' | ||
5 | ||
>>> count_park(4) # some examples: '<><>', '.%%.', '%<>%', '%.<>' | ||
29 | ||
""" | ||
if n < 0: | ||
return 0 | ||
elif n == 0: | ||
return 1 | ||
else: | ||
return count_park(n-2) + count_park(n-1) + count_park(n-1) | ||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
|
||
# Lists practice | ||
|
||
digits = [1, 8, 2, 8] | ||
8 in digits | ||
[1, 8] in digits | ||
1 in digits and 8 in digits | ||
|
||
for x in digits: | ||
continue | ||
#print(100 * x) | ||
|
||
def f(x): | ||
for y in [x, x + 1, x + 2]: | ||
print('this is y:', y) | ||
if y > 0: | ||
return y | ||
return 1000 | ||
|
||
def print_negatives(s): | ||
for d in s: | ||
d = -d | ||
print(d) | ||
print(s) | ||
s = [2, 3, 4] | ||
return s | ||
|
||
# Range practice | ||
|
||
def range_practice(): | ||
range(4) | ||
list(range(4)) | ||
range(-2, 2) | ||
list(range(-2, 2)) | ||
range(2, 100000000) # 9 zeroes slows things down a lot | ||
list(range(2, 100000000)) | ||
len(range(2, range(0, 23)[9])) | ||
|
||
def fib(n): | ||
"""Return the nth fibonnaci number. | ||
>>> fib(0) | ||
0 | ||
>>> fib(2) | ||
1 | ||
>>> fib(10) | ||
55 | ||
""" | ||
fib_number = 0 | ||
what_to_add = 1 # to get to the next fib number | ||
for _ in range(n): | ||
what_to_add, fib_number = fib_number, fib_number + what_to_add | ||
return fib_number | ||
|
||
# List comprehension practice | ||
|
||
xs = range(-10, 11) | ||
ys = [x*x - 2*x + 1 for x in xs] | ||
|
||
xs_where_y_is_below_10 = [x for x in xs if x*x - 2*x + 1 < 10] | ||
xs_where_y_is_below_10 = [xs[i] for i in range(len(xs)) if ys[i] < 10] | ||
|
||
# Tree recursion practice | ||
|
||
goal = 21 | ||
|
||
def play(strategy0, strategy1, n=0, who = 0, announce=False): | ||
"Play twenty-one starting at n and return the index of the winner." | ||
strategies = [strategy0, strategy1] | ||
while n < goal: | ||
n = n + strategies[who](n) | ||
if announce: | ||
print('Player', who, 'increases n to', n) | ||
who = 1 - who | ||
return who | ||
|
||
def two_strat(n): | ||
"Always choose 2." | ||
return 2 | ||
|
||
def interactive(n): | ||
"Ask the user." | ||
choice = input('Pick 1, 2, or 3: ') | ||
if choice in ['1', '2', '3']: | ||
return int(choice) | ||
else: | ||
print('Invalid choice!') | ||
return interactive(n) | ||
|
||
def best(n, other_strategy): | ||
"""Return an best strategy against other_strategy. | ||
>>> beat_two_strat = lambda n: best(n, two_strat) | ||
>>> winner(20, beat_two_strat, two_strat) | ||
1 | ||
>>> winner(19, beat_two_strat, two_strat) | ||
0 | ||
>>> winner(15, beat_two_strat, two_strat) | ||
0 | ||
>>> winner(0, beat_two_strat, two_strat) | ||
0 | ||
""" | ||
plan = lambda future_n: best(future_n, other_strategy) | ||
for choice in range(1, 4): | ||
if play(plan, other_strategy, n + choice, 1) == 0: | ||
return choice | ||
return 1 | ||
|
||
perfect = lambda n: best(n, perfect) | ||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.