-
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
21 changed files
with
1,552 additions
and
23 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": "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": "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"}] |
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,89 @@ | ||
class Account: | ||
"""An account has a balance and a holder. | ||
>>> a = Account('John') | ||
>>> a.holder | ||
'John' | ||
>>> a.deposit(100) | ||
100 | ||
>>> a.withdraw(90) | ||
10 | ||
>>> a.withdraw(90) | ||
'Insufficient funds' | ||
>>> a.balance | ||
10 | ||
""" | ||
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 | ||
|
||
def transfer(out_of, into, amount): | ||
"""Transfer amount between two accounts. | ||
>>> john = Account('John') | ||
>>> jack = Account('Jack') | ||
>>> john.deposit(100) | ||
100 | ||
>>> jack.deposit(100000) | ||
100000 | ||
>>> transfer(jack, john, 1000) | ||
'Transfer successful' | ||
>>> john.balance | ||
1100 | ||
>>> jack.balance | ||
99000 | ||
>>> transfer(john, jack, 10000) | ||
'Insufficient funds' | ||
>>> transfer(john, jack, 10) | ||
'Transfer successful' | ||
>>> john.balance | ||
1090 | ||
>>> jack.balance | ||
99010 | ||
""" | ||
result = out_of.withdraw(amount) | ||
if type(result) == str: # something went wrong | ||
return result | ||
else: | ||
into.deposit(amount) | ||
return 'Transfer successful' | ||
|
||
class Scam: | ||
"""A scam account has a balance and a holder. | ||
>>> a = Scam('John') | ||
>>> a.holder | ||
'John' | ||
>>> a.deposit(100) | ||
102.0 | ||
>>> a.withdraw(90) | ||
'We apologize for the delay' | ||
>>> a.withdraw(90) | ||
'We apologize for the delay' | ||
>>> a.balance | ||
102.0 | ||
""" | ||
def __init__(self, account_holder): | ||
self.holder = account_holder | ||
self.balance = 0 | ||
|
||
def deposit(self, amount): | ||
"""Add amount +2% to balance.""" | ||
self.balance = self.balance + amount * 1.02 | ||
return self.balance | ||
|
||
def withdraw(self, amount): | ||
"""Subtract amount from balance if funds are available.""" | ||
return 'We apologize for the delay' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "Homework 6", | ||
"endpoint": "cal/c88c/fa24/hw06", | ||
"src": [ | ||
"hw06.py" | ||
], | ||
"tests": { | ||
"hw*.py": "doctest", | ||
"tests/*.py": "ok_test" | ||
}, | ||
"default_tests": [ | ||
"shuffle", | ||
"deep_map" | ||
], | ||
"protocols": [ | ||
"restore", | ||
"file_contents", | ||
"analytics", | ||
"help", | ||
"unlock", | ||
"grading", | ||
"backup" | ||
] | ||
} |
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,41 @@ | ||
def shuffle(s): | ||
"""Return a shuffled list that interleaves the two halves of s. | ||
>>> shuffle(range(6)) | ||
[0, 3, 1, 4, 2, 5] | ||
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] | ||
>>> shuffle(letters) | ||
['a', 'e', 'b', 'f', 'c', 'g', 'd', 'h'] | ||
>>> shuffle(shuffle(letters)) | ||
['a', 'c', 'e', 'g', 'b', 'd', 'f', 'h'] | ||
>>> letters # Original list should not be modified | ||
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] | ||
""" | ||
assert len(s) % 2 == 0, 'len(seq) must be even' | ||
"*** YOUR CODE HERE ***" | ||
|
||
|
||
def deep_map(f, s): | ||
"""Replace all non-list elements x with f(x) in the nested list s. | ||
>>> six = [1, 2, [3, [4], 5], 6] | ||
>>> deep_map(lambda x: x * x, six) | ||
>>> six | ||
[1, 4, [9, [16], 25], 36] | ||
>>> # Check that you're not making new lists | ||
>>> s = [3, [1, [4, [1]]]] | ||
>>> s1 = s[1] | ||
>>> s2 = s1[1] | ||
>>> s3 = s2[1] | ||
>>> deep_map(lambda x: x + 1, s) | ||
>>> s | ||
[4, [2, [5, [2]]]] | ||
>>> s1 is s[1] | ||
True | ||
>>> s2 is s1[1] | ||
True | ||
>>> s3 is s2[1] | ||
True | ||
""" | ||
"*** YOUR CODE HERE ***" | ||
|
Binary file not shown.
Oops, something went wrong.