*Due by 11:59pm on Thursday, 3/5*

Download quiz02.zip. Inside the archive, you will find a file called quiz02.py, along with a copy of the OK autograder.

Complete the quiz and submit it before 11:59pm on Thursday, 3/5. **You must
work alone**, but you may talk to the course staff (see **Asking
Questions** below). You may use any course materials, including an
interpreter, course videos, slides, and readings. Please **do
not** discuss these specific questions with your classmates, and
**do not** scour the web for answers or post your answers
online.

Your submission will be graded automatically for correctness. Your
implementations **do not** need to be efficient, as long as they
are correct. We will apply additional correctness tests as well as the ones
provided. Passing these tests does not guarantee a perfect score.

**Asking Questions:** If you believe you need clarification on
a question, **make a private post** on Piazza. Please do not post
publicly about the quiz contents. If the staff discovers a problem with the
quiz or needs to clarify a question, we will email the class via Piazza. You
can also come to office hours to ask questions about the quiz or any other
course material, but no answers or hints will be provided in office hours.

**Submission:** When you are done, submit with
`python3 ok --submit`

. You may submit more than once before
the deadline; only the final submission will be scored.

The `ok`

program helps you test your code and track your progress.
The first time you run the autograder, you will be asked to log in with your
@berkeley.edu account using your web browser. Please do so. Each time you run
ok, it will back up your work and progress on our servers.
You can run all the doctests with the following command:

`python3 ok`

To test a specific question, use the `-q`

option with the
name of the function:

`python3 ok -q <function>`

By default, only tests that **fail** will appear. If you
want to see how you did on all tests, you can use the `-v`

option:

`python3 ok -v`

If you do not want to send your progress to our server or you have any
problems logging in, add the `--local`

flag to block all
communication:

`python3 ok --local`

When you are ready to submit, run `ok`

with the
`--submit`

option:

`python3 ok --submit`

**Readings:** You might find the following references
useful:

Implement `make_change`

, which takes a positive integer `amount`

and a
dictionary of `coins`

. The `coins`

dictionary keys are positive integer
denominations and its values are positive integer coin counts. For example,
`{1: 4, 5: 2}`

represents four pennies and two nickels. The `make_change`

function returns a list of coins that sum to `amount`

, where the count of
any denomination `k`

in the return value is at most `coins[k]`

.

If there are multiple ways to make change for `amount`

, prefer to use as many
of the smallest coins available and place the smallest coins first in the
returned list.

```
def make_change(amount, coins):
"""Return a list of coins that sum to amount, preferring the smallest coins
available and placing the smallest coins first in the returned list.
The coins argument is a dictionary with keys that are positive integer
denominations and values that are positive integer coin counts.
>>> make_change(2, {2: 1})
[2]
>>> make_change(2, {1: 2, 2: 1})
[1, 1]
>>> make_change(4, {1: 2, 2: 1})
[1, 1, 2]
>>> make_change(4, {2: 1}) == None
True
>>> coins = {2: 2, 3: 2, 4: 3, 5: 1}
>>> make_change(4, coins)
[2, 2]
>>> make_change(8, coins)
[2, 2, 4]
>>> make_change(25, coins)
[2, 3, 3, 4, 4, 4, 5]
>>> coins[8] = 1
>>> make_change(25, coins)
[2, 2, 4, 4, 5, 8]
"""
if not coins:
return None
smallest = min(coins)
rest = remove_one(coins, smallest)
"*** YOUR CODE HERE ***"
```

You can use the `remove_one`

function in your implementation:

```
def remove_one(coins, coin):
"""Remove one coin from a dictionary of coins. Return a new dictionary,
leaving the original dictionary coins unchanged.
>>> coins = {2: 5, 3: 2, 6: 1}
>>> remove_one(coins, 2) == {2: 4, 3: 2, 6: 1}
True
>>> remove_one(coins, 6) == {2: 5, 3: 2}
True
>>> coins == {2: 5, 3: 2, 6: 1} # Unchanged
True
"""
copy = dict(coins)
count = copy.pop(coin) - 1
if count:
copy[coin] = count
return copy
```

*Hint*: Try using the smallest coin to make change. If it turns out that there
is no way to make change using the smallest coin, then try making change
without the smallest coin.

*Hint*: The simplest solution does not involve defining any local functions,
but you can define additional functions if you wish.

Complete the `change`

method of the `ChangeMachine`

class. A `ChangeMachine`

instance holds some `coins`

, which are initially all pennies. The `change`

method takes a positive integer `coin`

, adds that coin to its `coins`

, and then
returns a list that sums to `coin`

. The machine prefers to return as many of
the smallest coins available, ordered from smallest to largest. The coins
returned by `change`

are removed from the machine's `coins`

.

```
class ChangeMachine:
"""A change machine holds a certain number of coins, initially all pennies.
The change method adds a single coin of some denomination X and returns a
list of coins that sums to X. The machine prefers to return the smallest
coins available. The total value in the machine never changes, and it can
always make change for any coin (perhaps by returning the coin passed in).
The coins attribute is a dictionary with keys that are positive integer
denominations and values that are positive integer coin counts.
>>> m = ChangeMachine(2)
>>> m.coins == {1: 2}
True
>>> m.change(2)
[1, 1]
>>> m.coins == {2: 1}
True
>>> m.change(2)
[2]
>>> m.coins == {2: 1}
True
>>> m.change(3)
[3]
>>> m.coins == {2: 1}
True
>>> m = ChangeMachine(10) # 10 pennies
>>> m.coins == {1: 10}
True
>>> m.change(5) # takes a nickel & returns 5 pennies
[1, 1, 1, 1, 1]
>>> m.coins == {1: 5, 5: 1} # 5 pennies & a nickel remain
True
>>> m.change(3)
[1, 1, 1]
>>> m.coins == {1: 2, 3: 1, 5: 1}
True
>>> m.change(2)
[1, 1]
>>> m.change(2) # not enough 1's remaining; return a 2
[2]
>>> m.coins == {2: 1, 3: 1, 5: 1}
True
>>> m.change(8) # cannot use the 2 to make 8, so use 3 & 5
[3, 5]
>>> m.coins == {2: 1, 8: 1}
True
>>> m.change(1) # return the penny passed in (it's the smallest)
[1]
>>> m.change(9) # return the 9 passed in (no change possible)
[9]
>>> m.coins == {2: 1, 8: 1}
True
>>> m.change(10)
[2, 8]
>>> m.coins == {10: 1}
True
>>> m = ChangeMachine(9)
>>> [m.change(k) for k in [2, 2, 3]]
[[1, 1], [1, 1], [1, 1, 1]]
>>> m.coins == {1: 2, 2: 2, 3: 1}
True
>>> m.change(5) # Prefers [1, 1, 3] to [1, 2, 2] (more pennies)
[1, 1, 3]
>>> m.change(7)
[2, 5]
>>> m.coins == {2: 1, 7: 1}
True
"""
def __init__(self, pennies):
self.coins = {1: pennies}
def change(self, coin):
"""Return change for coin, removing the result from self.coins."""
"*** YOUR CODE HERE ***"
```

*Hint*: Call the `make_change`

function in order to compute the result of
`change`

, but update `self.coins`

before returning that result.