Lab 7 Solutions
Solution Files
Topics
Consult this section if you need a refresher on the material for this lab. It's okay to skip directly to the questions and refer back here should you get stuck.
Generators
We can create our own custom iterators by writing a generator function, which
returns a special type of iterator called a generator. Generator functions
have yield
statements within the body of the function instead of return
statements. Calling a generator function will return a generator object and
will not execute the body of the function.
For example, let's consider the following generator function:
def countdown(n):
print("Beginning countdown!")
while n >= 0:
yield n
n -= 1
print("Blastoff!")
Calling countdown(k)
will return a generator object that counts down from k
to 0. Since generators are iterators, we can call iter
on the resulting
object, which will simply return the same object. Note that the body is not
executed at this point; nothing is printed and no numbers are outputted.
>>> c = countdown(5)
>>> c
<generator object countdown ...>
>>> c is iter(c)
True
So how is the counting done? Again, since generators are iterators, we call
next
on them to get the next element! The first time next
is called,
execution begins at the first line of the function body and continues until the
yield
statement is reached. The result of evaluating the expression in the
yield
statement is returned. The following interactive session continues
from the one above.
>>> next(c)
Beginning countdown!
5
Unlike functions we've seen before in this course, generator functions can
remember their state. On any consecutive calls to next
, execution picks up
from the line after the yield
statement that was previously executed. Like
the first call to next
, execution will continue until the next yield
statement is reached. Note that because of this, Beginning countdown!
doesn't
get printed again.
>>> next(c)
4
>>> next(c)
3
The next 3 calls to next
will continue to yield consecutive descending
integers until 0. On the following call, a StopIteration
error will be
raised because there are no more values to yield (i.e. the end of the function
body was reached before hitting a yield
statement).
>>> next(c)
2
>>> next(c)
1
>>> next(c)
0
>>> next(c)
Blastoff!
StopIteration
Separate calls to countdown
will create distinct generator objects with their
own state. Usually, generators shouldn't restart. If you'd like to reset the
sequence, create another generator object by calling the generator function
again.
>>> c1, c2 = countdown(5), countdown(5)
>>> c1 is c2
False
>>> next(c1)
5
>>> next(c2)
5
Here is a summary of the above:
- A generator function has a
yield
statement and returns a generator object. - Calling the
iter
function on a generator object returns the same object without modifying its current state. - The body of a generator function is not evaluated until
next
is called on a resulting generator object. Calling thenext
function on a generator object computes and returns the next object in its sequence. If the sequence is exhausted,StopIteration
is raised. A generator "remembers" its state for the next
next
call. Therefore,the first
next
call works like this:- Enter the function and run until the line with
yield
. - Return the value in the
yield
statement, but remember the state of the function for futurenext
calls.
- Enter the function and run until the line with
And subsequent
next
calls work like this:- Re-enter the function, start at the line after the
yield
statement that was previously executed, and run until the nextyield
statement. - Return the value in the
yield
statement, but remember the state of the function for futurenext
calls.
- Re-enter the function, start at the line after the
- Calling a generator function returns a brand new generator object (like
calling
iter
on an iterable object). - A generator should not restart unless it's defined that way. To start over from the first element in a generator, just call the generator function again to create a new generator.
Another useful tool for generators is the yield from
statement. yield from
will yield all values from an iterator or iterable.
>>> def gen_list(lst):
... yield from lst
...
>>> g = gen_list([1, 2, 3, 4])
>>> next(g)
1
>>> next(g)
2
>>> next(g)
3
>>> next(g)
4
>>> next(g)
StopIteration
Object-Oriented Programming
Object-oriented programming (OOP) is a style of programming that
allows you to think of code in terms of "objects." Here's an example of
a Car
class:
class Car:
num_wheels = 4
def __init__(self, color):
self.wheels = Car.num_wheels
self.color = color
def drive(self):
if self.wheels <= Car.num_wheels:
return self.color + ' car cannot drive!'
return self.color + ' car goes vroom!'
def pop_tire(self):
if self.wheels > 0:
self.wheels -= 1
Here's some terminology:
- class: a blueprint for how to build a certain type of object.
The
Car
class (shown above) describes the behavior and data that allCar
objects have. instance: a particular occurrence of a class. In Python, we create instances of a class like this:
>>> my_car = Car('red')
my_car
is an instance of theCar
class.data attributes: a variable that belongs to the instance (also called instance variables). Think of a data attribute as a quality of the object: cars have wheels and color, so we have given our
Car
instanceself.wheels
andself.color
attributes. We can access attributes using dot notation:>>> my_car.color 'red' >>> my_car.wheels 4
method: Methods are just like normal functions, except that they are bound to an instance. Think of a method as a "verb" of the class: cars can drive and also pop their tires, so we have given our
Car
instance the methodsdrive
andpop_tire
. We call methods using dot notation:>>> my_car = Car('red') >>> my_car.drive() 'red car goes vroom!'
constructor: Constructors build an instance of the class. The constructor for car objects is
Car(color)
. When Python calls that constructor, it immediately calls the__init__
method. That's where we initialize the data attributes:def __init__(self, color): self.wheels = Car.num_wheels self.color = color
The constructor takes in one argument,
color
. As you can see, this constructor also creates theself.wheels
andself.color
attributes.self
: in Python,self
is the first parameter for many methods (in this class, we will only use methods whose first parameter isself
). When a method is called,self
is bound to an instance of the class. For example:>>> my_car = Car('red') >>> car.drive()
Notice that the
drive
method takes inself
as an argument, but it looks like we didn't pass one in! This is because the dot notation implicitly passes incar
asself
for us.
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.
Generators
Q1: Apply That Again
Implement amplify
, a generator function that takes a one-argument function f
and a starting value x
. The element at index k that it yields (starting at 0) is the result of applying f
k times to x
. It terminates whenever the next value it would yield is a falsy value, such as 0
, ""
, []
, False
, etc.
def amplify(f, x):
"""Yield the longest sequence x, f(x), f(f(x)), ... that are all true values
>>> list(amplify(lambda s: s[1:], 'boxes'))
['boxes', 'oxes', 'xes', 'es', 's']
>>> list(amplify(lambda x: x//2-1, 14))
[14, 6, 2]
"""
while x:
yield x
x = f(x)
Use Ok to test your code:
python3 ok -q amplify
Object-Oriented Programming
Q2: WWPD: Classy Cars
Below is the definition of a Car
class that we will be using in the following WWPD questions.
class Car:
num_wheels = 4
gas = 30
headlights = 2
size = 'Tiny'
def __init__(self, make, model):
self.make = make
self.model = model
self.color = 'No color yet. You need to paint me.'
self.wheels = Car.num_wheels
self.gas = Car.gas
def paint(self, color):
self.color = color
return self.make + ' ' + self.model + ' is now ' + color
def drive(self):
if self.wheels < Car.num_wheels or self.gas <= 0:
return 'Cannot drive!'
self.gas -= 10
return self.make + ' ' + self.model + ' goes vroom!'
def pop_tire(self):
if self.wheels > 0:
self.wheels -= 1
def fill_gas(self):
self.gas += 20
return 'Gas level: ' + str(self.gas)
You can find the unlocking questions below.
Use Ok to test your knowledge with the following "What Would Python Display?" questions:
python3 ok -q no-wwpd-car -u
Important: For all WWPD questions, type
Function
if you believe the answer is<function...>
,Error
if it errors, andNothing
if nothing is displayed.
>>> from car import *
>>> deneros_car = Car('Tesla', 'Model S')
>>> deneros_car.model
______'Model S'
>>> deneros_car.gas
______30
>>> deneros_car.gas -= 20 # The car is leaking gas
>>> deneros_car.gas
______10
>>> deneros_car.drive()
______'Tesla Model S goes vroom!'
>>> deneros_car.drive()
______'Cannot drive!'
>>> deneros_car.fill_gas()
______'Gas level: 20'
>>> deneros_car.gas
______20
>>> Car.gas
______30
>>> Car.gas = 50 # Car manufacturer upgrades their cars to start with more gas
>>> ashleys_car = Car('Honda', 'HR-V')
>>> ashleys_car.gas
______50
>>> from car import *
>>> brandons_car = Car('Audi', 'A5')
>>> brandons_car.wheels = 2
>>> brandons_car.wheels
______2
>>> Car.num_wheels
______4
>>> brandons_car.drive() # Type Error if an error occurs and Nothing if nothing is displayed
______'Cannot drive!'
>>> Car.drive() # Type Error if an error occurs and Nothing if nothing is displayed
______Error
>>> Car.drive(brandons_car) # Type Error if an error occurs and Nothing if nothing is displayed
______'Cannot drive!'
Q3: Person
Modify the following Person
class to add a repeat
method, which
repeats the last thing said. See the doctests for an example of its
use.
Hint: you will have to modify other methods as well, not just the
repeat
method.
class Person:
"""Person class.
>>> steven = Person("Steven")
>>> steven.repeat() # initialized person has the below starting repeat phrase!
'I squirreled it away before it could catch on fire.'
>>> steven.say("Hello")
'Hello'
>>> steven.repeat()
'Hello'
>>> steven.greet()
'Hello, my name is Steven'
>>> steven.repeat()
'Hello, my name is Steven'
>>> steven.ask("preserve abstraction barriers")
'Would you please preserve abstraction barriers'
>>> steven.repeat()
'Would you please preserve abstraction barriers'
"""
def __init__(self, name):
self.name = name
self.previous = "I squirreled it away before it could catch on fire."
def say(self, stuff):
self.previous = stuff return stuff
def ask(self, stuff):
return self.say("Would you please " + stuff)
def greet(self):
return self.say("Hello, my name is " + self.name)
def repeat(self):
return self.say(self.previous)
Use Ok to test your code:
python3 ok -q Person
Q4: Smart Fridge
The SmartFridge
class is used by smart
refrigerators to track which items are in the fridge
and let owners know when an item has run out.
The class internally uses a dictionary to store items,
where each key is the item name and the value is the current quantity.
The add_item
method should add the given quantity
of the given item and report the current quantity.
You can assume that the use_item
method will only be called on
items that are already in the fridge, and it should use up
the given quantity of the given item. If the quantity would fall to or below zero, it should only use up to the remaining quantity, and remind the owner to buy more of that item.
Finish implementing the SmartFridge
class definition
so that its add_item
and use_item
methods work as specified by the doctests.
class SmartFridge:
""""
>>> fridgey = SmartFridge()
>>> fridgey.add_item('Mayo', 1)
'I now have 1 Mayo'
>>> fridgey.add_item('Mayo', 2)
'I now have 3 Mayo'
>>> fridgey.use_item('Mayo', 2.5)
'I have 0.5 Mayo left'
>>> fridgey.use_item('Mayo', 0.5)
'Oh no, we need more Mayo!'
>>> fridgey.add_item('Eggs', 12)
'I now have 12 Eggs'
>>> fridgey.use_item('Eggs', 15)
'Oh no, we need more Eggs!'
>>> fridgey.add_item('Eggs', 1)
'I now have 1 Eggs'
"""
def __init__(self):
self.items = {}
def add_item(self, item, quantity):
if item in self.items:
self.items[item] += quantity
else:
self.items[item] = quantity
return f'I now have {self.items[item]} {item}' def use_item(self, item, quantity):
self.items[item] -= min(quantity, self.items[item])
if self.items[item] == 0:
return f'Oh no, we need more {item}!'
return f'I have {self.items[item]} {item} left'
You may find Python's formatted string literals, or f-strings useful. A quick example:
>>> feeling = 'love' >>> course = '61A!' >>> f'I {feeling} {course}' 'I love 61A!'
Use Ok to test your code:
python3 ok -q SmartFridge
If you're curious about alternate methods of string formatting, you can also check out an older method of Python string formatting. A quick example:
>>> ten, twenty, thirty = 10, 'twenty', [30] >>> '{0} plus {1} is {2}'.format(ten, twenty, thirty) '10 plus twenty is [30]'
Submit
Make sure to submit this assignment by uploading any files you've edited to the appropriate Gradescope assignment. For a refresher on how to do this, refer to Lab 00.
Optional Questions
These questions are optional, but you must complete them in order to be checked off before the end of the lab period. They are also useful practice!
Q5: Cucumber
Cucumber is a card game. Cards are positive integers (no suits). Players are numbered from 0 up to players
(0, 1, 2, 3 in a 4-player game).
In each Round
, the players each play
one card, starting with the starter
and
in ascending order (player 0 follows player 3 in a 4-player game). If the card
played is as high or higher than
the highest
card played so far, that player takes control. The winner is the last player who took control
after every player has played once.
Implement Round
so that CucumberGame
behaves as described in the doctests below.
Hint: Here is an example of a try-catch with an
AssertionError
>> try: ... assert False, 'oh no!' ... except AssertionError as e: ... print(e) ... oh no!
class CucumberGame:
"""Play a round and return all winners so far. Cards is a list of pairs.
Each (who, card) pair in cards indicates who plays and what card they play.
>>> g = CucumberGame()
>>> g.play_round(3, [(3, 4), (0, 8), (1, 8), (2, 5)])
>>> g.winners
[1]
>>> g.play_round(1, [(3, 5), (1, 4), (2, 5), (0, 8), (3, 7), (0, 6), (1, 7)])
It is not your turn, player 3
It is not your turn, player 0
The round is over, player 1
>>> g.winners
[1, 3]
>>> g.play_round(3, [(3, 7), (2, 5), (0, 9)]) # Round is never completed
It is not your turn, player 2
>>> g.winners
[1, 3]
"""
def __init__(self):
self.winners = []
def play_round(self, starter, cards):
r = Round(starter)
for who, card in cards:
try:
r.play(who, card)
except AssertionError as e:
print(e)
if r.winner != None:
self.winners.append(r.winner)
class Round:
players = 4
def __init__(self, starter):
self.starter = starter
self.next_player = starter
self.highest = -1
self.winner = None
def play(self, who, card):
assert not self.is_complete(), f'The round is over, player {who}'
assert who == self.next_player, f'It is not your turn, player {who}'
self.next_player = (who + 1) % Round.players if card >= self.highest:
self.highest = card self.control = who if self.is_complete(): self.winner = self.control
def is_complete(self):
""" Checks if a game could end. """
return self.next_player == self.starter and self.highest > -1
Use Ok to test your code:
python3 ok -q CucumberGame