Answer to Question #218874 in Python for Ravichandrasekhar

Question #218874

Write a program to find the minimum number of notes required for the amount M. Available note denominations are 500, 50, 10, 1.

Given

M = 1543, it can be written as1543 = 3*(500) + 3*(50) + 0*(10) + 1*(3)Then the output should be

500: 3 50: 0 10: 4 1: 3


sample input=1543 output is 500: 3 50: 0 10: 4 1: 3


sample input is=1259 and output is 500: 2 50: 5 10: 0 1: 9




1
Expert's answer
2021-07-22T07:08:04-0400
def change(amount):
    nominals = [500, 50, 10, 1]
    d  = {}
    for nominal in nominals:
        n = 0
        while amount >= nominal:
            amount -= nominal
            n += 1
        d[nominal] = n
    return d        

def print_change(d):
    for nominal in sorted(d, reverse=True)                    :
        print(f'{nominal}: {d[nominal]}', end='  ' )
    print()
    
def main():
    n = int(input())        
    d = change(n)
    print_change(d)
    
if __name__ == '__main__':
    main()
           

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS