Skip to content

Instantly share code, notes, and snippets.

@ssimono
Last active April 25, 2023 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssimono/1c0ee4adea6984419c3a52eb9c2ef395 to your computer and use it in GitHub Desktop.
Save ssimono/1c0ee4adea6984419c3a52eb9c2ef395 to your computer and use it in GitHub Desktop.
Diego's Puzzle
"""
Problem:
An icecream vendor starts her shift with an empty box of change. She sells icecreams for 5€ per unit.
Customers show up one by one and pay with bills of either 5€, 10€, 20€ or 50€.
Depending on the order of customers and the bills they give, she might or might not become unable to process a transaction at some point.
Task: write a function icecream that takes an array of integers representing the suite of bills that each customer gives.
The function must return a boolean that indicates whether the seller can fullfil all customers purchases.
"""
import unittest
ICECREAM_PRICE = 5
def icecream(*bills):
change = {}
for cash_in in bills:
change.update({cash_in: change.get(cash_in, 0) + 1})
cash_back = cash_in - ICECREAM_PRICE
for bill in (50, 20, 10, 5):
nb_bills = min(change.get(bill, 0), cash_back // bill)
if nb_bills == 0:
continue
change.update({bill: change.get(bill) - nb_bills})
cash_back -= nb_bills * bill
if cash_back > 0:
return False
return True
class TestIcecream(unittest.TestCase):
def test(self):
self.assertTrue(icecream(5))
self.assertTrue(icecream(5, 10))
self.assertTrue(icecream(5, 5, 5, 10, 10, 20))
self.assertTrue(icecream(5, 10, 5, 10, 5, 20, 5, 10, 5, 10, 5, 20, 5, 50))
self.assertFalse(icecream(10))
self.assertFalse(icecream(5, 20))
self.assertFalse(icecream(5, 10, 10))
if __name__ == "__main__":
""" Usage: python3 puzzle.py """
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment