W1174-12 Making Change

From Coder Merlin
(Redirected from W1174 Making Change)
Monnaie de Bactriane, Eucratide I, pile

Prerequisites[edit]

Introduction[edit]

When paying in cash for a product at a store, it is very unlikely you have the exact amount of money in bills and coins needed to pay. Usually you pay some amount over, and have the result of the (paid - total) returned back to us as change.

When giving change you always want to give the largest denomination of bills and coins back to the customer so you do not burden them with extra smaller bills and coins.

Denominations[edit]

For making change in this exercise, the available denominations are limited to:

    Bills
    $100 bill
    $50 bill
    $20 bill
    $10 bill
    $5 bill
    $2 bill
    $1 bill
    Coins
    $1 (one dollar coin)
    $0.50 (half-dollar coin)
    $0.25 (quarter)
    $0.10 (dime)
    $0.05 (nickel)
    $0.01 (penny)


Calculating Change[edit]

When trying to think of an algorithm, approach the problem as you would with money in front of you. Think about how you can check if your bill will fit in the amount of many specified. Usually you follow some steps similar to these.

    Check the next largest bill.
    Does it fit in the total?
    If yes, how many times?
    Store the amount of times.
    Subtract the bills you just found from the total so you can try the next size.


HintIcon.png
Helpful Hint

You will be doing these same steps to check every bill and coin value. Using a Function would be helpful since you are repeating code over and over again.

String to Double[edit]

All of our money values are given to us as Strings formatted with a dollar sign.

"$123.45"

In order to calculate bills and coins that can fit into this amount, we need to Cast our amount from a String to a Double.

Try:

Double("$123.45")!

So what happened? Since we have the dollar sign in our string, we cannot convert our String into a Double. We need a way to work around this.

We want to either build an algorithm that can go through each digit to check if it is a number, and built an integer that we can use, or use a method to remove the dollar sign from the string.

String to Integer[edit]

If we remember back, Strings are a collection type. This means that we can use a For Loop to iterate over it, getting each individual character. Once we get an individual character, we can create a conditional to check whether or not it is a number or not. If it is a number we can save that as a digit. By using what we learned about number systems we can build each digit from each number that we read. Doing this we can create a number from a string as a new variable.

let str = "5678"
for c in str {
    print(c)
}
   5
   6
   7
   8

Key Concepts[edit]

KeyConceptsIcon.png
Key Concepts
  • Use a function to reuse code, to keep yourself from rewriting your algorithm over and over
  • Using For Loops to convert a String to an Integer

Exercises[edit]

ExercisesIcon.png
Exercises
  • Complete Merlin Mission Manager Mission M1174-10
    • C101 Denominations
    • C102 Change