How to create function that returns 'change in highest denominations'

Amatuer Ayers

I am trying to make a programme that will return the correct change, in the highest denominations possible

i.e $73 will return 1 x $50, 1 x $20, 1 x $2 and 1 $1

(I am using whole values of $100, $50, $20, $10, $5, $2, $1)

I have a long code that works...it is

hundred = x // 100
hundred_remainder = x % 100

fifty = hundred_remainder // 50
fifty_remainder = hundred_remainder % 50

twenty = fifty_remainder // 20 

etc....then

if hundred > 0:
    print (str(hundred) + " x $100")
if fifty> 0:
    print (str(fifty) + " x $50")

etc....which works fine, but I know there must be a way of writing a function that has a loop to work it out with less typing. Something like X = $345, then it gets 3 x $100, subtracts that from the total and updates X with the remainder, then repeats the process going through each denomination, until complete. I'm just a bit unsure how to figure it out, any guidance will be greatly appreciated!

Hypaethral

I think a cool model for this problem is defining the values that you consider "legal" up-front, and then iterating over those from the top to the bottom to reach your counts. Consider the following loop:

    #where x is the value from which you're making change 
    legal = [100,50,20,10,5,2,1,.50,.25,.10,.05,.01]
    for dolAmount in legal:
        count = x//dolAmount
        if count > 0:
            print int(count),\
                  dolAmount,\
                  " coin(s)" if dolAmount<1 else " dollar bill(s)"
        x-=count*dolAmount

    #  prints lines like this for x=103.58:
    #  
    #  1 100 dollar bill(s)
    #  3 1 dollars bill(s) 
    #  1 0.5 coin(s)
    #  1 0.05 coim(s)
    #  3 0.01 coin(s)
    #  ehhh actually it will probably say 2 0.01 coin(s) because floating points....

The formatting needs work (what the heck is a 0.5 coin?), but this is a neat way of saving yourself a lot of code-writing, and the pattern is applicable to other programming endeavors. In general, when you find yourself creating a lot of variables/constants by hand, it's probably time to start thinking about a list (or similar collection)!

Check out http://www.codeskulptor.org/#user39_ur6ybhs9HAmknOL.py for a learning-example of what this would look like in practice (used within a function!) Happy coding!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Making change with minimum denominations with memoization

How to create function that returns nothing

How do we create a recursive function to find the highest digit of a number?

How to change a function which returns local variable

Function that returns three highest value in row in R

Interview: Making change for n cents (arbitrary denominations)

How create a javascript function that returns a html

How to create a function that returns a new stack?

How to create an enum function that returns a tuple of Ints?

How to create a function that returns a value if id is in an array?

How to get exchange by coins with two denominations?

How do a run a search with returns by the highest average

create a function that returns an object

Recursion function to find highest value returns 0s

A function that returns the highest average from multiple lists of numbers

How to create a function that returns the number of nodes in a tree on a given level

How can I create a "forEach" that returns an object that is a receiver to the consuming function?

Excel/VBA - How to create a function which returns a list

How to Create a function that returns the total number of boomerangs in an array

How to create SWIG typemap for function that takes and returns 2 tables

Postgres: how to create overloaded function that returns setof type

how to create function in postgres that returns rowcounts between tables given as parameters

How to create function that returns all combinations of this list in Python?

How to create a function with zipWithIndex that returns an Int from a List[Int] in scala

How to create public calculation function that returns calculated value?

How to create a function that takes a character and string and returns the index of this character in the string

Trying to understand how to create an async function in rust that returns a MySqlPool object

How to create a function that returns an index of the first repeated value in an array?

How can I create a function that returns Product of Digit Of Sum of a number