-
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
36 changed files
with
3,866 additions
and
462 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/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": "hw/hw06/", "name": "HW 06: Mutability", "piazza_name": "HW 06", "release": "10/9/24"}, {"due": "10/16/24", "link": "proj/cats/", "name": "Cats", "piazza_name": "Cats", "release": "10/2/24"}] | ||
[{"due": "10/16/24", "link": "hw/hw06/", "name": "HW 06: Mutability", "piazza_name": "HW 06", "release": "10/9/24"}, {"due": "10/16/24", "link": "proj/cats/", "name": "Cats", "piazza_name": "Cats", "release": "10/2/24"}, {"due": "10/18/24", "link": "lab/lab07/", "name": "Lab 07: Object-Oriented Programming", "piazza_name": "Lab 07", "release": "10/14/24"}] |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,110 +1,79 @@ | ||
|
||
# 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 | ||
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 | ||
""" | ||
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 | ||
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] | ||
|
||
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) | ||
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: | ||
print('Invalid choice!') | ||
return interactive(n) | ||
return (['%' + s for s in park(n-1)] + | ||
['.' + s for s in park(n-1)] + | ||
['<>' + s for s in park(n-2)]) | ||
|
||
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,107 @@ | ||
def demos(): | ||
"""Demos. | ||
>>> b = Account('Ada') | ||
>>> f = b.deposit | ||
>>> f(5) | ||
5 | ||
>>> f(25) | ||
30 | ||
>>> b.balance | ||
30 | ||
>>> a = Account('Alan') | ||
>>> [a.deposit(n) for n in range(10)] | ||
[0, 1, 3, 6, 10, 15, 21, 28, 36, 45] | ||
>>> m = map(a.deposit, range(10, 13)) | ||
>>> next(m) | ||
55 | ||
>>> a.balance | ||
55 | ||
>>> next(m) | ||
66 | ||
>>> next(m) | ||
78 | ||
>>> a.balance | ||
78 | ||
>>> d = {1: 10, 2: 5, 3: 15, 4: 8, 5: 4} | ||
>>> max(d.keys(), key=d.get) | ||
3 | ||
""" | ||
|
||
class Clown: | ||
"""An illustration of a class statement. This class is not useful. | ||
>>> Clown.nose | ||
'big and red' | ||
>>> Clown.dance() | ||
'No thanks' | ||
""" | ||
nose = 'big and red' | ||
def dance(): | ||
return 'No thanks' | ||
|
||
class Town: | ||
"""Waldo in town. | ||
>>> Town(1, 7).street[2] | ||
'Waldo' | ||
""" | ||
def __init__(self, w, aldo): | ||
if aldo == 7: | ||
self.street = {self.f(w): 'Waldo'} | ||
|
||
def f(self, x): | ||
return x + 1 | ||
|
||
|
||
class Beach: | ||
"""Waldo at the beach. | ||
>>> Beach().walk(0).wave(0) | ||
'Waldo' | ||
""" | ||
def __init__(self): | ||
sand = ['Wal', 'do'] | ||
self.dig = sand.pop | ||
|
||
def walk(self, x): | ||
self.wave = lambda y: self.dig(x) + self.dig(y) | ||
return self | ||
|
||
class Account: | ||
"""An account has a balance and a holder. | ||
All accounts share a common interest rate. | ||
>>> a = Account('John') | ||
>>> a.holder | ||
'John' | ||
>>> a.deposit(100) | ||
100 | ||
>>> a.withdraw(90) | ||
10 | ||
>>> a.withdraw(90) | ||
'Insufficient funds' | ||
>>> a.balance | ||
10 | ||
>>> a.interest | ||
0.02 | ||
>>> Account.interest | ||
0.02 | ||
""" | ||
interest = 0.02 | ||
|
||
def __init__(self, account_holder): | ||
self.holder = account_holder | ||
self.balance = 0 | ||
|
||
def deposit(self, amount): | ||
"""Add amount to balance.""" | ||
self.balance = self.balance + amount | ||
return self.balance | ||
|
||
def withdraw(self, amount): | ||
"""Subtract amount from balance if funds are available.""" | ||
if amount > self.balance: | ||
return 'Insufficient funds' | ||
self.balance = self.balance - amount | ||
return self.balance |
Oops, something went wrong.