Skip to content

Commit

Permalink
Wed Oct 9 13:52:34 PDT 2024
Browse files Browse the repository at this point in the history
  • Loading branch information
papajohn committed Oct 9, 2024
1 parent afe940f commit c4720e7
Show file tree
Hide file tree
Showing 21 changed files with 1,552 additions and 23 deletions.
2 changes: 1 addition & 1 deletion _internal/targets.json
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 added assets/slides/12-Objects.pdf
Binary file not shown.
89 changes: 89 additions & 0 deletions assets/slides/12.py
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'
4 changes: 2 additions & 2 deletions disc/disc06/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ <h3 id="q3-word-rope">Q3: Word Rope</h3>
<p><strong>Reminder:</strong> <code>s[&#x2d;1]</code> evaluates to the last element of a sequence <code>s</code>.</p>


<div class="monaco-storable" id="word_rope-input" style="height:396px;"></div>
<div class="monaco-storable" id="word_rope-input" style="height:360px;"></div>
<a href="javascript:void" id="modal-link-word_rope-input">Run in 61A Code</a>
<div class="modal fade" id="modal-word_rope-input" tabindex="-1" role="dialog">
<div class="modal-dialog modal-lg" role="document">
Expand All @@ -319,7 +319,7 @@ <h3 id="q3-word-rope">Q3: Word Rope</h3>
</div>
<div class="storable-status"></div>
<script>
$(() => activateEditor('def word_rope(s):\n &quot;&quot;&quot;Return a rope of the words in string s.\n\n &gt;&gt;&gt; word_rope(&#x27;test&#x27;)\n [&#x27;t&#x27;, &#x27;e&#x27;, &#x27;s&#x27;, &#x27;t&#x27;]\n &gt;&gt;&gt; word_rope(&#x27;the ref was wrong&#x27;)\n [&#x27;t&#x27;, &#x27;h&#x27;, &#x27;e&#x27;, [&#x27;r&#x27;, &#x27;e&#x27;, &#x27;f&#x27;, [&#x27;w&#x27;, &#x27;a&#x27;, &#x27;s&#x27;, [&#x27;w&#x27;, &#x27;r&#x27;, &#x27;o&#x27;, &#x27;n&#x27;, &#x27;g&#x27;]]]]\n &quot;&quot;&quot;\n assert s and s[0] != &#x27; &#x27; and s[&#x2d;1] != [ ]\n result = []\n word = _____\n for x in s:\n if x == &#x27; &#x27;:\n "*** YOUR CODE HERE ***"\n else:\n "*** YOUR CODE HERE ***"\n return result\n', "python", "word_rope-input"));
$(() => activateEditor('def word_rope(s):\n &quot;&quot;&quot;Return a rope of the words in string s.\n\n &gt;&gt;&gt; word_rope(&#x27;the ref was wrong&#x27;)\n [&#x27;t&#x27;, &#x27;h&#x27;, &#x27;e&#x27;, [&#x27;r&#x27;, &#x27;e&#x27;, &#x27;f&#x27;, [&#x27;w&#x27;, &#x27;a&#x27;, &#x27;s&#x27;, [&#x27;w&#x27;, &#x27;r&#x27;, &#x27;o&#x27;, &#x27;n&#x27;, &#x27;g&#x27;]]]]\n &quot;&quot;&quot;\n assert s and s[0] != &#x27; &#x27; and s[&#x2d;1] != [ ]\n result = []\n word = _____\n for x in s:\n if x == &#x27; &#x27;:\n "*** YOUR CODE HERE ***"\n else:\n "*** YOUR CODE HERE ***"\n return result\n', "python", "word_rope-input"));
</script>


Expand Down
24 changes: 24 additions & 0 deletions hw/hw06/hw06.ok
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"
]
}
41 changes: 41 additions & 0 deletions hw/hw06/hw06.py
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 added hw/hw06/hw06.zip
Binary file not shown.
Loading

0 comments on commit c4720e7

Please sign in to comment.