Download hw08.zip. Inside the archive, you will find a file called
+hw08.py, along with a copy of the ok autograder.
+
+
Submission: When you are done, submit the assignment by uploading all code files you've edited to Gradescope. You may submit more than once before the deadline; only the
+final submission will be scored. Check that you have successfully submitted
+your code on Gradescope. See Lab 0 for more instructions on
+submitting assignments.
+
+
Using Ok: If you have any questions about using Ok, please
+refer to this guide.
+
+
+
+
Grading: Homework is graded based on
+correctness. Each incorrect problem will decrease the total score by one point.
+This homework is out of 2 points.
+
+
+
+
+
+
Required Questions
+
+
+
+
+
+
+
+
Getting Started Videos
+
+
+
These videos may provide some helpful direction for tackling the coding
+problems on this assignment.
+
+
To see these videos, you should be logged into your berkeley.edu email.
Write a function store_digits that takes in an integer n and returns
+a linked list containing the digits of n in the same order (from left to right).
+
+
Important: Do not use any string manipulation functions, such as str or reversed.
+
+
+
+
def store_digits(n):
+ """Stores the digits of a positive number n in a linked list.
+
+ >>> s = store_digits(1)
+ >>> s
+ Link(1)
+ >>> store_digits(2345)
+ Link(2, Link(3, Link(4, Link(5))))
+ >>> store_digits(876)
+ Link(8, Link(7, Link(6)))
+ >>> store_digits(2450)
+ Link(2, Link(4, Link(5, Link(0))))
+ >>> store_digits(20105)
+ Link(2, Link(0, Link(1, Link(0, Link(5)))))
+ >>> # a check for restricted functions
+ >>> import inspect, re
+ >>> cleaned = re.sub(r"#.*\\n", '', re.sub(r'"{3}[\s\S]*?"{3}', '', inspect.getsource(store_digits)))
+ >>> print("Do not use str or reversed!") if any([r in cleaned for r in ["str", "reversed"]]) else None
+ """
+ "*** YOUR CODE HERE ***"
+
+
+
+
+
Use Ok to test your code:
python3 ok -q store_digits
+
+
+
+
+
Q2: Mutable Mapping
+
+
+
Implement deep_map_mut(func, s), which applies the function func to each
+element in the linked list s. If an element is itself a linked list,
+recursively apply func to its elements as well.
+
+
Your implementation should mutate the original linked list. Do not
+create any new linked lists. The function returns None.
+
+
Hint: You can use the built-in isinstance function to determine if an element is a linked list.
Construct Check: The final test case for this problem checks
+that your function does not create any new linked lists.
+If you are failing this doctest, make sure that you are not
+creating link lists by calling the constructor, i.e.
+
+
s = Link(1)
+
+
+
+
def deep_map_mut(func, s):
+ """Mutates a deep link s by replacing each item found with the
+ result of calling func on the item. Does NOT create new Links (so
+ no use of Link's constructor).
+
+ Does not return the modified Link object.
+
+ >>> link1 = Link(3, Link(Link(4), Link(5, Link(6))))
+ >>> print(link1)
+ <3 <4> 5 6>
+ >>> # Disallow the use of making new Links before calling deep_map_mut
+ >>> Link.__init__, hold = lambda *args: print("Do not create any new Links."), Link.__init__
+ >>> try:
+ ... deep_map_mut(lambda x: x * x, link1)
+ ... finally:
+ ... Link.__init__ = hold
+ >>> print(link1)
+ <9 <16> 25 36>
+ """
+ "*** YOUR CODE HERE ***"
+
+
+
+
+
Use Ok to test your code:
python3 ok -q deep_map_mut
+
+
+
+
+
Check Your Score Locally
+
+
You can locally check your score on each question of this assignment by running
+
+
python3 ok --score
+
+
This does NOT submit the assignment! When you are satisfied with your score, submit the assignment to Gradescope to receive credit for it.
+
+
Submit Assignment
+
+
+
Submit this assignment by uploading any files you've edited to the appropriate Gradescope assignment.Lab 00 has detailed instructions.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/hw/hw08/ok b/hw/hw08/ok
new file mode 100644
index 000000000..b84a800fe
Binary files /dev/null and b/hw/hw08/ok differ
diff --git a/index.html b/index.html
index 8de984905..c6ab43840 100644
--- a/index.html
+++ b/index.html
@@ -1050,13 +1050,17 @@
Here's a refresher on Object-Oriented Programming. It's okay to skip directly to
+the questions and refer back here if you get stuck.
+
+
+
+
+
+
Object-oriented programming (OOP) uses objects and classes to organize programs. Here's an example of a class:
+
+
class Car:
+ max_tires = 4
+
+ def __init__(self, color):
+ self.tires = Car.max_tires
+ self.color = color
+
+ def drive(self):
+ if self.tires < Car.max_tires:
+ return self.color + ' car cannot drive!'
+ return self.color + ' car goes vroom!'
+
+ def pop_tire(self):
+ if self.tires > 0:
+ self.tires -= 1
+
+
Class: The type of an object. The Car class (shown above) describes the characteristics of all Car
+ objects.
+
+
Object: A single instance of a class. In Python, a new object is created by
+ calling a class.
+
+
>>> ferrari = Car('red')
+
+
Here, ferrari is a name bound to a Car object.
+
+
Class attribute: A variable that belongs to a class and is accessed via dot notation. The Car class has a max_tires attribute.
+
+
>>> Car.max_tires
+4
+
+
Instance attribute: A variable that belongs to a particular object. Each Car object has a tires attribute and a color attribute. Like class attributes, instance attributes are accessed via dot notation.
Method: A function that belongs to an object and is called via dot notation. By convention, the first parameter of a method is self.
+
+
When one of an object's methods is called, the object is implicitly provided as the argument for self. For example, the drive method of the ferrari object is called with empty parentheses because self is implicitly bound to the ferrari object.
+
+
>>> ferrari = Car('red')
+>>> ferrari.drive()
+'red car goes vroom!'
+
+
We can also call the original Car.drive function. The original function does not belong to any particular Car object, so we must provide an explicit argument for self.
+
+
>>> ferrari = Car('red')
+>>> Car.drive(ferrari)
+'red car goes vroom!'
+
+
__init__: A special function that is called automatically when a new instance of a class is created.
+
+
Notice how the drive method takes in self as an argument, but it
+ looks like we didn't pass one in! This is because the dot notation
+ implicitly passes in ferrari as self for us. So in this example, self is bound to the
+ object called ferrari in the global frame.
+
+
To evaluate the expression Car('red'), Python creates a new Car object. Then, Python calls the __init__ function of the Car class with self bound to the new object and color bound to 'red'.
+
+
+
+
+
Q1: Bank Account
+
+
+
Extend the BankAccount class to include a transactions attribute. This attribute should be a list that keeps track of each transaction made on the account. Whenever the deposit or withdraw method is called, a new Transaction instance should be created and added to the list, even if the action is not successful.
+
+
The Transaction class should have the following attributes:
+
+
+
before: The account balance before the transaction.
+
after: The account balance after the transaction.
+
id: The transaction ID, which is the number of previous transactions (deposits or withdrawals) made on that account. The transaction IDs for a specific BankAccount instance must be unique, but this id does not need to be unique across all accounts. In other words, you only need to ensure that no two Transaction objects made by the same BankAccount have the same id.
+
+
+
In addition, the Transaction class should have the following methods:
+
+
+
changed(): Returns True if the balance changed (i.e., before is different from after), otherwise returns False.
+
report(): Returns a string describing the transaction. The string should start with the transaction ID and describe the change in balance. Take a look at the doctests for the expected output.
+
+
+
+
+
class Transaction:
+ def __init__(self, id, before, after):
+ self.id = id
+ self.before = before
+ self.after = after
+
+ def changed(self):
+ """Return whether the transaction resulted in a changed balance."""
+
return str(self.id) + ': ' + msg
+
+class BankAccount:
+ """A bank account that tracks its transaction history.
+
+ >>> a = BankAccount('Eric')
+ >>> a.deposit(100) # Transaction 0 for a
+ 100
+ >>> b = BankAccount('Erica')
+ >>> a.withdraw(30) # Transaction 1 for a
+ 70
+ >>> a.deposit(10) # Transaction 2 for a
+ 80
+ >>> b.deposit(50) # Transaction 0 for b
+ 50
+ >>> b.withdraw(10) # Transaction 1 for b
+ 40
+ >>> a.withdraw(100) # Transaction 3 for a
+ 'Insufficient funds'
+ >>> len(a.transactions)
+ 4
+ >>> len([t for t in a.transactions if t.changed()])
+ 3
+ >>> for t in a.transactions:
+ ... print(t.report())
+ 0: increased 0->100
+ 1: decreased 100->70
+ 2: increased 70->80
+ 3: no change
+ >>> b.withdraw(100) # Transaction 2 for b
+ 'Insufficient funds'
+ >>> b.withdraw(30) # Transaction 3 for b
+ 10
+ >>> for t in b.transactions:
+ ... print(t.report())
+ 0: increased 0->50
+ 1: decreased 50->40
+ 2: no change
+ 3: decreased 40->10
+ """
+
+ # *** YOU NEED TO MAKE CHANGES IN SEVERAL PLACES IN THIS CLASS ***
+
def next_id(self):
+ # There are many ways to implement this counter, such as using an instance
+ # attribute to track the next ID.
+ return len(self.transactions)
+ def deposit(self, amount):
+ """Increase the account balance by amount, add the deposit
+ to the transaction history, and return the new balance.
+ """
+
self.balance = self.balance + amount
+ return self.balance
+
+ def withdraw(self, amount):
+ """Decrease the account balance by amount, add the withdraw
+ to the transaction history, and return the new balance.
+ """
+ if amount > self.balance:
+
A mint is a place where coins are made. In this question, you'll implement a Mint class that can output a Coin with the correct year and worth.
+
+
+
Each Mint instance has a year stamp. The update method sets the
+ year stamp of the instance to the present_year class attribute of the Mintclass.
+
The create method takes a subclass of Coin (not an instance!), then
+ creates and returns an instance of that subclass stamped with the Mint's year (which may be different from
+ Mint.present_year if it has not been updated.)
+
A Coin's worth method returns the cents value of the coin plus one
+ extra cent for each year of age beyond 50. A coin's age can be determined
+ by subtracting the coin's year from the present_year class attribute of
+ the Mint class.
+
+
+
+
+
class Mint:
+ """A mint creates coins by stamping on years.
+
+ The update method sets the mint's stamp to Mint.present_year.
+
+ >>> mint = Mint()
+ >>> mint.year
+ 2024
+ >>> dime = mint.create(Dime)
+ >>> dime.year
+ 2024
+ >>> Mint.present_year = 2104 # Time passes
+ >>> nickel = mint.create(Nickel)
+ >>> nickel.year # The mint has not updated its stamp yet
+ 2024
+ >>> nickel.worth() # 5 cents + (80 - 50 years)
+ 35
+ >>> mint.update() # The mint's year is updated to 2104
+ >>> Mint.present_year = 2179 # More time passes
+ >>> mint.create(Dime).worth() # 10 cents + (75 - 50 years)
+ 35
+ >>> Mint().create(Dime).worth() # A new mint has the current year
+ 10
+ >>> dime.worth() # 10 cents + (155 - 50 years)
+ 115
+ >>> Dime.cents = 20 # Upgrade all dimes!
+ >>> dime.worth() # 20 cents + (155 - 50 years)
+ 125
+ """
+ present_year = 2024
+
+ def __init__(self):
+ self.update()
+
+ def create(self, coin):
+
return coin(self.year)
+ def update(self):
+
self.year = Mint.present_year
+class Coin:
+ cents = None # will be provided by subclasses, but not by Coin itself
+
+ def __init__(self, year):
+ self.year = year
+
+ def worth(self):
+
You can locally check your score on each question of this assignment by running
+
+
python3 ok --score
+
+
This does NOT submit the assignment! When you are satisfied with your score, submit the assignment to Gradescope to receive credit for it.
+
+
+
Submit Assignment
+
+
+
If you are in a regular section of CS 61A, fill out this lab attendance and feedback form. (If you are in the mega section, you don't need to fill out the form.)
+
+
Then, submit this assignment by uploading any files you've edited to the appropriate Gradescope assignment.Lab 00 has detailed instructions.