-
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
63 changed files
with
7,451 additions
and
107 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": "10/4/24", "link": "lab/lab05/", "name": "Lab 05: Python Lists", "piazza_name": "Lab 05", "release": "9/30/24"}, {"due": "10/9/24", "link": "hw/hw05/", "name": "HW 05: Tree Recursion", "piazza_name": "HW 05", "release": "10/2/24"}, {"due": "10/16/24", "link": "proj/cats/", "name": "Cats", "piazza_name": "Cats", "release": "10/2/24"}] | ||
[{"due": "10/9/24", "link": "hw/hw05/", "name": "HW 05: Tree Recursion", "piazza_name": "HW 05", "release": "10/2/24"}, {"due": "10/11/24", "link": "lab/lab06/", "name": "Lab 06: Mutability", "piazza_name": "Lab 06", "release": "10/7/24"}, {"due": "10/16/24", "link": "proj/cats/", "name": "Cats", "piazza_name": "Cats", "release": "10/2/24"}] |
Binary file not shown.
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 |
---|---|---|
@@ -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
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.