-
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
8 changed files
with
179 additions
and
6 deletions.
There are no files selected for viewing
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,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 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,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)) |
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
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