-
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
1 parent
6a5379c
commit cc49829
Showing
1,091 changed files
with
465,554 additions
and
100 deletions.
There are no files selected for viewing
Binary file not shown.
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 |
---|---|---|
@@ -1,98 +1,97 @@ | ||
# Tree Recursion | ||
|
||
# ----------------------- # | ||
# Tracing factorial | ||
# Lists practice | ||
|
||
from ucb import trace | ||
digits = [1, 8, 2, 8] | ||
8 in digits | ||
[1, 8] in digits | ||
1 in digits and 8 in digits | ||
|
||
@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 | ||
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 | ||
>>> unique_prime_factors(120) # 2 * 2 * 2 * 3 * 5 | ||
3 | ||
>>> fib(10) | ||
55 | ||
""" | ||
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 | ||
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: | ||
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: | ||
print('Invalid Choice!') | ||
return interactive(n) | ||
def best(n, other_strategy): | ||
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 | ||
else: | ||
return count_park(n-2) + count_park(n-1) + count_park(n-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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
|
||
def box_pointer_example(): | ||
def f(s): | ||
x = s[0] | ||
return [x] | ||
|
||
t = [3, [2+2, 5]] | ||
u = [f(t[1]), t] | ||
print(u) | ||
|
||
def double_eights(s): | ||
"""Return whether two consecutive items of list s are 8. | ||
>>> double_eights([1, 2, 8, 8]) | ||
True | ||
>>> double_eights([8, 8, 0]) | ||
True | ||
>>> double_eights([5, 3, 8, 8, 3, 5]) | ||
True | ||
>>> double_eights([2, 8, 4, 6, 8, 2]) | ||
False | ||
""" | ||
for i in range(len(s)-1): | ||
if s[i] == 8 and s[i+1] == 8: | ||
return True | ||
return False | ||
|
||
def double_eights_rec(s): | ||
"""Return whether two consecutive items of list s are 8. | ||
>>> double_eights_rec([1, 2, 8, 8]) | ||
True | ||
>>> double_eights_rec([8, 8, 0]) | ||
True | ||
>>> double_eights_rec([5, 3, 8, 8, 3, 5]) | ||
True | ||
>>> double_eights_rec([2, 8, 4, 6, 8, 2]) | ||
False | ||
""" | ||
if s[:2] == [8, 8]: | ||
return True | ||
elif len(s) < 2: | ||
return False | ||
else: | ||
return double_eights_rec(s[1:]) | ||
|
||
min(range(10), key=lambda i: abs(50 ** 0.5 - i)) | ||
sum([2, 3, 4], sum([20, 30, 40])) | ||
any([x > 10 for x in range(10)]) | ||
|
||
xs = range(-10, 11) | ||
ys = [x*x - 2*x + 1 for x in xs] | ||
min(xs, key=lambda x: x*x - 2*x + 1) | ||
xs[min(range(len(xs)), key=lambda i: ys[i])] | ||
|
||
|
||
def park(n): | ||
"""Return the ways to park cars and motorcycles in n adjacent spots. | ||
>>> park(1) | ||
['%', '.'] | ||
>>> park(2) | ||
['%%', '%.', '.%', '..', '<>'] | ||
>>> park(3) | ||
['%%%', '%%.', '%.%', '%..', '%<>', '.%%', '.%.', '..%', '...', '.<>', '<>%', '<>.'] | ||
>>> len(park(4)) | ||
29 | ||
""" | ||
if n < 0: | ||
return [] | ||
elif n == 0: | ||
return [''] | ||
else: | ||
return (['%' + s for s in park(n-1)] + | ||
['.' + s for s in park(n-1)] + | ||
['<>' + s for s in park(n-2)]) | ||
|
||
|
||
|
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,48 @@ | ||
xs = range(-10, 11) | ||
ys = [x*x - 2*x + 1 for x in xs] | ||
|
||
i = min(range(len(ys)), key=lambda i: ys[i]) | ||
xs[min(range(len(xs)), key=lambda i: ys[i])] | ||
|
||
|
||
def park(n): | ||
"""Return the ways to park cars and motorcycles in n adjacent spots. | ||
>>> park(1) | ||
['%', '.'] | ||
>>> park(2) | ||
['%%', '%.', '.%', '..', '<>'] | ||
>>> park(3) | ||
['%%%', '%%.', '%.%', '%..', '%<>', '.%%', '.%.', '..%', '...', '.<>', '<>%', '<>.'] | ||
>>> len(park(4)) | ||
29 | ||
""" | ||
if n < 0: | ||
return [] | ||
elif n == 0: | ||
return [''] | ||
else: | ||
return (['%' + s for s in park(n-1)] + | ||
['.' + s for s in park(n-1)] + | ||
['<>' + s for s in park(n-2)]) | ||
|
||
|
||
def dict_demos(): | ||
numerals = {'I': 1, 'V': 5, 'X': 10} | ||
numerals['X'] | ||
# numerals['X-ray'] | ||
# numerals[10] | ||
len(numerals) | ||
list(numerals) | ||
numerals.values() | ||
list(numerals.values()) | ||
sum(numerals.values()) | ||
dict([[3, 9], [4, 16]]) | ||
numerals.get('X', 0) | ||
numerals.get('X-ray', 0) | ||
numerals.get('X-ray') | ||
{1: 2, 1: 3} | ||
{[1]: 2} | ||
{1: [2]} | ||
|
||
|
Binary file not shown.
Oops, something went wrong.