Coursework 2
(f) Determine Payment from a "Pocket" of Available Coins
An android is being programmed to go shopping.
It will need to deal with coins and payments in a similar way to humans.
Define a function pay_with_coins_from_pocket( amount, pocket ), where amount
is a floating point number with up to two decimal places, and
pocket is a list of 8 integers representing the numbers of coins the android
has in its pocket, given in the following order (same as for part (e)):
- Number of £2 coins,
- Number of £1 coins,
- Number of 50p coins,
- Number of 20p coins,
- Number of 10p coins,
- Number of 5p coins,
- Number of 2p coins,
- Number of 1p coins.
The value returned by pay_with_coins_from_pocket( amount, pocket
) is given by the following specification:
- If the total value of the coins represented by
pocket is
less than amount return the boolean value
False.
- In all other cases return a list of 9 integers that satisfies the
following conditions:
- The first 8 numbers of the returned list represent coins that
should be paid, using the same ordering as the input list,
pocket.
- The last number of the returned list gives the amount of money that
the android should get back as change. So, if the android can pay
the exact money, this will be
0; but if the android
has enough money in pocket, but does not have the right
coins to pay the exact amount, this will be the amount of change that
will be returned. The change should be given as a float, with the
whole part being the number of pounds and the part after the decimal
being the number of pennies. (If the change is a whole number of pounds,
you can represent it either as a float or an integer.)
- The total of the coins paid must be the exact amount, if it is possible to pay
that.
- If it is not possible to pay the exact amount, the amount paid must
be the lowest amount that covers the cost. (In other words, the amount of
change returned should be as low as possible.)
- If there are several ways that the android can pay the amount,
the payment made should be one that uses the most coins possible.
(Even androids don't like carrying a lot of small change.)
- If there are several ways to pay satisfying all previous conditions,
the android should make a payment using the fewest different types
of coin. (Will this condition ever make a difference?)
| Examples |
| INPUT 1 | INPUT 2 | OUPUT | Coins to be paid |
| 0.50 | [0,0,0,0,4,1,1,1] | False | Cannot pay (only have 48p) |
| 1.62 | [2,1,0,0,0,0,0,0] | [1,0,0,0,0,0,0,0,0.38] | 1×£2, Change 38p |
| 3.75 | [2,3,4,0,0,0,0,0] | [0,2,4,0,0,0,0,0,0.25] | 2×£1, 4×50p, Change 25p |
| 0.05 | [0,0,0,0,0,5,3,6] | [0,0,0,0,0,0,0,5,0] | 5×1p, Change 0 |
| 0.05 | [0,0,0,0,0,1,3,0] | [0,0,0,0,0,1,0,0,0] | 1×5p, Change 0 |
| 0.08 | [0,0,0,0,0,1,4,3] | [0,0,0,0,0,0,3,2,0] | 3×2p, 2×1p, Change 0 |
Notes:
- This is quite a difficult programming problem. Don't worry if you cannot
write a program that fulfills all the requirements. You can get partial marks
if your program only works for the simpler cases, and you can still get a high
overall mark for this assignment if you do not do this part.
- A student has pointed out to me that, even with the given constraints on the
preferred payment, there are some cases where several options are
possible. This was not my intention. It is because I accidentally removed a further
constraint thinking it would not be necessary. You do not need to worry about
this problem since I will only be marking based on cases where there is
a unique solution. For cases where there are multiple solutions, you could
return
a list of them, or any one of them, or any other value; it will not affect
the marking.