Skip to content

Commit

Permalink
Mon Oct 21 14:12:17 PDT 2024
Browse files Browse the repository at this point in the history
  • Loading branch information
papajohn committed Oct 21, 2024
1 parent 48f94f3 commit c7d3be3
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 6 deletions.
Binary file added assets/slides/14-Inheritance.pdf
Binary file not shown.
97 changes: 97 additions & 0 deletions assets/slides/14.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Tracking Instances

class Transaction:
"""A logged transaction.
>>> s = [20, -3, -4]
>>> ts = [Transaction(x) for x in s]
>>> ts[1].balance()
17
>>> ts[2].balance()
13
"""
log = []

def __init__(self, amount):
self.amount = amount
self.prior = list(self.log)
self.log.append(self)

def balance(self):
return self.amount + sum([t.amount for t in self.prior])

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
>>> a.interest
0.02
"""

interest = 0.02 # A class attribute

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


class CheckingAccount(Account):
"""A bank account that charges for withdrawals.
>>> ch = CheckingAccount('Jack')
>>> ch.balance = 20
>>> ch.withdraw(5)
14
>>> ch.interest
0.01
"""

withdraw_fee = 1
interest = 0.01

def withdraw(self, amount):
return Account.withdraw(self, amount + self.withdraw_fee)
# Alternatively:
return super().withdraw(amount + self.withdraw_fee)

# Multiple Inheritance

class SavingsAccount(Account):
"""A bank account that charges for deposits."""

deposit_fee = 2

def deposit(self, amount):
return Account.deposit(self, amount - self.deposit_fee)


class AsSeenOnTVAccount(CheckingAccount, SavingsAccount):
"""A bank account that charges for everything."""

def __init__(self, account_holder):
self.holder = account_holder
self.balance = 1 # A free dollar!

supers = [c.__name__ for c in AsSeenOnTVAccount.mro()]
Binary file added assets/slides/15-Linked_Lists.pdf
Binary file not shown.
64 changes: 64 additions & 0 deletions assets/slides/15.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
class Link:
"""A linked list.
>>> s = Link(3, Link(4, Link(5)))
>>> s
Link(3, Link(4, Link(5)))
>>> print(s)
<3 4 5>
>>> s.first
3
>>> s.rest
Link(4, Link(5))
>>> s.rest.first
4
>>> s.rest.first = 7
>>> s
Link(3, Link(7, Link(5)))
>>> s.first = 6
>>> s.rest.rest = Link.empty
>>> s
Link(6, Link(7))
>>> print(s)
<6 7>
>>> print(s.rest)
<7>
>>> t = Link(1, Link(Link(2, Link(3)), Link(4)))
>>> t
Link(1, Link(Link(2, Link(3)), Link(4)))
>>> print(t)
<1 <2 3> 4>
"""
empty = ()

def __init__(self, first, rest=empty):
assert rest is Link.empty or isinstance(rest, Link)
self.first = first
self.rest = rest
#Not in scope!
def __repr__(self):
if self.rest:
rest_repr = ', ' + repr(self.rest)
else:
rest_repr = ''
return 'Link(' + repr(self.first) + rest_repr + ')'
#Not in scope!
def __str__(self):
string = '<'
while self.rest is not Link.empty:
string += str(self.first) + ' '
self = self.rest
return string + str(self.first) + '>'

def map_link(f, s):
"""Return a linked list of f(x) for each x in s.
>>> evens = Link(4, Link(2, Link(6)))
>>> map_link(lambda x: x + 1, evens)
Link(5, Link(3, Link(7)))
>>> evens
Link(4, Link(2, Link(6)))
"""
if s is Link.empty:
return s
return Link(f(s.first), map_link(f, s.rest))
10 changes: 7 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,8 @@ <h2 class="frontpage-header">Calendar</h2>
<ul class="list-inline">
<li><a href="https://bcourses.berkeley.edu/courses/1538122/pages/lecture-14-inheritance" class="label label-outline" target="_blank">Recording</a></li>
<li><a href="https://www.youtube.com/watch?v=Bqpe1iyq5vE&list=PL6BsET-8jgYXUT9nA2gvweTlheGbvp6qv&ab_channel=JohnDeNero" class="label label-outline" target="_blank">Videos</a></li>
<li><a href="/fa24/assets/slides/14-Inheritance.pdf" class="label label-outline">Slides (pdf)</a></li>
<li><a href="/fa24/assets/slides/14.py" class="label label-outline">14.py</a></li>
</ul>
</td>
<td>
Expand Down Expand Up @@ -1133,6 +1135,8 @@ <h2 class="frontpage-header">Calendar</h2>

<ul class="list-inline">
<li><a href="https://www.youtube.com/watch?v=yC4WPw_6ehY&list=PL6BsET-8jgYWnTLtlZ1ZWlCIWnagkNXrY&ab_channel=JohnDeNero" class="label label-outline" target="_blank">Videos</a></li>
<li><a href="/fa24/assets/slides/15-Linked_Lists.pdf" class="label label-outline">Slides (pdf)</a></li>
<li><a href="/fa24/assets/slides/15.py" class="label label-outline">15.py</a></li>
</ul>
</td>
<td>
Expand All @@ -1142,10 +1146,10 @@ <h2 class="frontpage-header">Calendar</h2>
</td>
<td>
<span class="">
<span class="assignment-text">Disc 08: Linked Lists</span>
<a href="/fa24/disc/disc08/" class="assignment-text">Disc 08: Linked Lists</a>

<ul class="list-inline">

<li><a href="https://www.youtube.com/playlist?list=PLx38hZJ5RLZd0WV3t8gQLmu_lbxr28HMG" class="label label-outline" target="_blank">Video</a></li>
</ul>

</span>
Expand Down Expand Up @@ -1571,7 +1575,7 @@ <h3><a href="/articles/about">Policies</a></h3>

});

let build_time = new Date(1000 * 1729541054.131282);
let build_time = new Date(1000 * 1729545120.543286);
</script>
<script>
$('.alwaystoggle').css('display', 'inline-block');
Expand Down
6 changes: 3 additions & 3 deletions lab/lab08/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ <h2 id="getting-started-videos" class="no-toc">Getting Started Videos</h2>
<blockquote><p>To see these videos, you should be logged into your berkeley.edu email.</p></blockquote>


<iframe width="560" height="315" src="https://youtube.com/embed/videoseries?list=PLx38hZJ5RLZdDSDb_kP0oSAv_XTF2qnG3&si=ZF41WsMcxhe_41M_"
frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" aria-label = "lab7-getstarted"
allowfullscreen></iframe><p><a href='https://youtu.be/playlist?list=PLx38hZJ5RLZdDSDb_kP0oSAv_XTF2qnG3&si=ZF41WsMcxhe_41M_'> YouTube link </a></p>
<iframe width="560" height="315" src="https://youtube.com/embed/videoseries?list=PLx38hZJ5RLZf75A1ctbQcDPT5wtWkNETl"
frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" aria-label = "c88c-lab8-getstarted"
allowfullscreen></iframe><p><a href='https://youtu.be/playlist?list=PLx38hZJ5RLZf75A1ctbQcDPT5wtWkNETl'> YouTube link </a></p>
</div>


Expand Down
4 changes: 4 additions & 0 deletions lecture/lec14/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ <h2 id="prerecorded"><a href="#playlist">Lecture Playlist</a></h2>

<a href="https://www.youtube.com/watch?v=Bqpe1iyq5vE&list=PL6BsET-8jgYXUT9nA2gvweTlheGbvp6qv&ab_channel=JohnDeNero" class="label label-outline" target="_blank">Videos</a>

<a href="/assets/slides/14-Inheritance.pdf" class="label label-outline" target="_blank">Slides (pdf)</a>

<a href="/assets/slides/14.py" class="label label-outline" target="_blank">14.py</a>

</ul>
</div>
</div>
Expand Down
4 changes: 4 additions & 0 deletions lecture/lec15/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ <h2 id="prerecorded"><a href="#playlist">Lecture Playlist</a></h2>

<a href="https://www.youtube.com/watch?v=yC4WPw_6ehY&list=PL6BsET-8jgYWnTLtlZ1ZWlCIWnagkNXrY&ab_channel=JohnDeNero" class="label label-outline" target="_blank">Videos</a>

<a href="/assets/slides/15-Linked_Lists.pdf" class="label label-outline" target="_blank">Slides (pdf)</a>

<a href="/assets/slides/15.py" class="label label-outline" target="_blank">15.py</a>

</ul>
</div>
</div>
Expand Down

0 comments on commit c7d3be3

Please sign in to comment.