Skip to content

Instantly share code, notes, and snippets.

@toshvelaga
Created July 29, 2018 20:41
Show Gist options
  • Save toshvelaga/b2fded2bcd74e572f15bb3d984596d12 to your computer and use it in GitHub Desktop.
Save toshvelaga/b2fded2bcd74e572f15bb3d984596d12 to your computer and use it in GitHub Desktop.
CS50 Greedy in python
from cs50 import cs50
while True:
change = cs50.get_float("How much change do you need: ")
if change < 100 and change > 0:
break
numQ, numD, numN, numP = [0,0,0,0]
while change >= 25:
change -= 25
numQ += 1
while change >= 10:
change -= 10
numD += 1
while change >= 5:
change -= 5
numN += 1
while change >= 1:
change -= 1
numP += 1
numTotal = numQ + numD + numN + numP
print("You need", numTotal, "coins in total:")
print(numQ, "Quarters,", numD, "Dimes,", numN, "Nickels,", numP, "Pennies.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment