DEV Community

loading...

Day 57 Of 100DaysOfCode: Money Change Problem

iamdurga profile image Durga Pokharel Updated on ・2 min read

Today is my day 57th of #100dayofcode and #python. Today also continued to learned more properties of SQL including INNER JOIN,LEFT JOIN, RIGHT JOIN, FULL JOIN, IS LIKE, NOT LIKE, IS NULL, IS NOT NULL etc from Datacamp

Also tried to complete some assignment in coursera regarding to the topic algorithmic toolbox. I tired to solve money change problems, Fractional Knapsack problems, car fueling problem.

Python code

We are going to change Rs.28. In the shop shopkeeper has only Rs.1, Rs.5, Rs.10 as a change. So, possible change for 28 are 10,10,5,1,1,1. For this I tried to write following code.

number = 28
valid_changes = [1, 5, 10]
rem = number
changes=[]

while rem!=0:
#     print(rem)
    if rem%valid_changes[2]==0:
        changes.extend([valid_changes[2]])
        rem -= valid_changes[2]
    elif rem%valid_changes[1]==0:
        changes.extend([valid_changes[1]])
        rem -= valid_changes[1]
    else:
        changes.extend([1])
        rem-=1
print(len(changes))
Enter fullscreen mode Exit fullscreen mode

When above code is run we get,

28
27
26
25
20
10
[1, 1, 1, 5, 10, 10]
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

pic
Editor guide