diff --git a/_internal/targets.json b/_internal/targets.json index 4ee101bfe..12f5b3a1d 100644 --- a/_internal/targets.json +++ b/_internal/targets.json @@ -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"}] diff --git a/assets/slides/12-Objects.pdf b/assets/slides/12-Objects.pdf new file mode 100644 index 000000000..ea1faa957 Binary files /dev/null and b/assets/slides/12-Objects.pdf differ diff --git a/assets/slides/12.py b/assets/slides/12.py new file mode 100644 index 000000000..285ff8d8c --- /dev/null +++ b/assets/slides/12.py @@ -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' diff --git a/disc/disc06/index.html b/disc/disc06/index.html index ad3010381..62d87c203 100644 --- a/disc/disc06/index.html +++ b/disc/disc06/index.html @@ -303,7 +303,7 @@

Q3: Word Rope

Reminder: s[-1] evaluates to the last element of a sequence s.

-
+
Run in 61A Code