Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MONEY CHANGE
#1
amount=int(input("Please enter amount in LBP: "))
bills = [100,50,20,10,5,1]
billsReturned = []

def change(amount):
    for i in bills:
        while amount >=i:
            billsReturned.append(i)
            amount -= i
    print(billsReturned)

change(amount)
This is the code where you insert an amount of money and you want to have its change in the least possible bills available, this code outputs:

Output:
Please enter an amount in LBP: 270 [100,100,50,20]
It is working, but I want it to be displayed as the following:
Output:
2 100 LBP 1 50 LBP 1 20 LBP
I was thinking of a dictionary and print the key-value pairs, but I couldn't think of a way of creating the dictionary, please help?
Reply
#2
If you are allowed to use other modules, collections.Counter will create the dictionary you want for this.
>>> from collections import Counter
>>> bills = [100, 100, 50, 20]
>>> counts = Counter(bills)
>>> counts
Counter({100: 2, 20: 1, 50: 1})
>>> for bill,n in counts.items():
...     print("{} : {:>3} bill(s)".format(n, bill))
...
1 :  20 bill(s)
1 :  50 bill(s)
2 : 100 bill(s)
>>>
If not it isn't much harder without.

Also note, for the record, this greedy approach will not necessarily work for certain sets of denominations.
For instance if you need to make change for 30 with the given bills {25, 15, 1} the greedy approach requires 6 bills whereas the correct minimum requires 2.  Probably not something you need to deal with but it is worth being aware of.  DP would be needed if you were expected to handle cases like this.
Reply
#3
Loop through bills again. For each bill, use the count method of the billsReturned list to get how many are used. If the count is non-zero, print it and the denomination.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
(Oct-20-2017, 03:29 PM)ichabod801 Wrote: Loop through bills again. For each bill, use the count method of the billsReturned list to get how many are used. If the count is non-zero, print it and the denomination.

Not that it will make any practical difference here, but I hate teaching people quadratic time methods when linear time methods exist:  
https://python-forum.io/Thread-Efficienc...17#pid8517
Reply
#5
My apologies for being such an idiot.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
Mekire,

Read through your link,

I'm thinking that the differences in time between OS's could be that the cmos timer's frequency is dependent on a crystal which
oscillates at 32768 Hertz, this is a perfect 2^15 value. This 32.768kHz divides perfectly into 1hz using off the shelf circuitry (counters), to get
1 second interval at 1Hz. (The resolution of the crystal). To get the smaller increments, there will be fractions of seconds with remainders. The difference
is probably related to how these fractions are accounted for.

The choice of this crystal frequency is directly related to power consumption (battery, that has to last many years).

Then again I might be totally off the wall!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python code for counting money at multiple stores duddey 9 6,184 Oct-16-2020, 01:17 PM
Last Post: bill_z
  Money conversion - problems with lists and .format function fatherted99 1 1,826 Mar-12-2020, 06:29 PM
Last Post: ndc85430
  How many money in 30 days. miguelramos122 4 5,760 Dec-16-2017, 12:48 PM
Last Post: squenson

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020