# Weekly Challenge #4 – Time for Change – Solutions

#### A test of your arithmetic and list operation skills. A little trickier than normal!

Note: This contains answers to the Time for Change challenge. Have a go at the challenge before coming here!

## Bronze

```shoppingBill = int(input("How much was the shopping? (In pence): "))
cashTendered = int(input("How much cash was tendered? "))

changeToGive = cashTendered - shoppingBill

print("Total change to issue: " + str(changeToGive) + "p")```
```Console.Write("How much was the shopping? (In pence): ");

Console.Write("How much cash was tendered? (In pence): ");

int changeToGive = cashTendered - shoppingBill;

Console.WriteLine("Total change to issue: {0}p", changeToGive);```

## Silver

```currency = [1, 2, 5, 10, 20, 50]

shoppingBill = int(input("How much was the shopping? (In pence): "))
cashTendered = int(input("How much cash was tendered? "))

changeToGive = cashTendered - shoppingBill
pointer = len(currency) - 1

coinsGiven = []

while changeToGive > 0:
if currency[pointer] <= changeToGive:
changeToGive = changeToGive - currency[pointer]
coinsGiven.append(currency[pointer])
else:
pointer = pointer - 1

print(coinsGiven)```
```int[] currency = new int { 1, 2, 5, 10, 20, 50 };

Console.Write("How much was the shopping? (In pence): ");

Console.Write("How much cash was tendered? (In pence): ");

int changeToGive = cashTendered - shoppingBill;
int pointer = currency.Length - 1;
List<int> coinsGiven = new List<int>();
while (changeToGive > 0)
{
if (currency[pointer] <= changeToGive)
{
changeToGive -= currency[pointer];
}
else
{
pointer--;
}
}

foreach(int coin in coinsGiven)
{
Console.Write("{0} ", coin);
}```

## Gold

```currency = [1, 2, 5, 10, 20, 50]
quantity = [20, 4, 5, 3, 1, 3]

shoppingBill = int(input("How much was the shopping? (In pence): "))
cashTendered = int(input("How much cash was tendered? "))

changeToGive = cashTendered - shoppingBill
pointer = len(currency) - 1

coinsGiven = []

while changeToGive > 0 and pointer >= 0:
if currency[pointer] <= changeToGive:
if quantity[pointer] > 0:
changeToGive = changeToGive - currency[pointer]
coinsGiven.append(currency[pointer])
quantity[pointer] = quantity[pointer] - 1
else:
pointer = pointer - 1
else:
pointer = pointer - 1

if changeToGive == 0:
print(coinsGiven)
else:
print("Insufficient change.")
```
```int[] currency = new int { 1, 2, 5, 10, 20, 50 };
int[] quantity = new int { 20, 4, 5, 3, 1, 3 };

Console.Write("How much was the shopping? (In pence): ");

Console.Write("How much cash was tendered? (In pence): ");

int changeToGive = cashTendered - shoppingBill;
int pointer = currency.Length - 1;
List<int> coinsGiven = new List<int>();

while (changeToGive > 0 && pointer >= 0)
{
if (currency[pointer] <= changeToGive)
{
if (quantity[pointer] > 0)
{
changeToGive -= currency[pointer];
quantity[pointer]--;
}
else
{
pointer--;
}
}
else
{
pointer--;
}
}

if(changeToGive == 0)
{
foreach (int coin in coinsGiven)
{
Console.Write("{0} ", coin);
}
}
else
{
Console.WriteLine("Insufficient Change.");
}```