Skip to content

Instantly share code, notes, and snippets.

@anirudhpillai
Last active April 21, 2016 10:49
Show Gist options
  • Save anirudhpillai/3ea564ded87c1fe32ea9eab56813979e to your computer and use it in GitHub Desktop.
Save anirudhpillai/3ea564ded87c1fe32ea9eab56813979e to your computer and use it in GitHub Desktop.
import ast
import sys
class Vending_Machine:
def __init__(self, products, change):
self.products = products
self.change = change
def reset_products(self):
print("Let's reload the products")
products = ast.literal_eval(input("Enter a dictionary with the product name as key and its price as the value. eg {\"Coke\": 0.5, \"iPhone\":600}\n"))
self.products = products
print("The products have been reset, you may now continue using the Vending Machine")
def reset_change(self):
print("Let's now reload the change")
change = ast.literal_eval(input("Enter a dictionary with the coin denomination as key and its quantity as the value. eg {\"0.01\": 5, \"2\":20}\n"))
self.change = change
print("The change has been reset, you may now continue using the Vending Machine")
def buy_product(self, productName):
if productName in self.products:
amount = input("How much are you paying for it?\n")
change = round((float(amount) - self.products[productName])*100)
if change < 0:
print("Insufficient Funds")
return
elif change == 0:
print("Here's your %s, thanks for providing the exact amount!" % productName)
else:
original_vals = [i for i in self.change.keys()]
vals = [round(float(i)*100) for i in self.change.keys()] #converting all denomination to pennies
amts = [i for i in self.change.values()] #the amount of each denomination
best = [[0 for i in vals]] #memo for dynamic programming
bestScoreList = [0]
for i in range(1, change+1):
best.append([0]*len(vals))
bestScore = i+1
for c in range(len(vals)):
if vals[c] > i or best[i - vals[c]][c] >= amts[c]:
continue
bestIndex = i - vals[c]
score = 0
if bestScoreList[bestIndex] != 0 or bestIndex == 0:
score = bestScoreList[bestIndex] + 1
if score < bestScore:
bestScore = score
best[i] = list(best[bestIndex])
best[i][c] += 1
bestScoreList.append(bestScore)
#best[change] will be in a form like [0, 3, 2] which means 3 unit of the denomination at vals[1] and 2 units of denomination at vals[2]
#here we check if Vending Machine can't give that change (the denominations in best[change] will not add up to the change)
sum = 0
for ind, k in enumerate(best[change]):
change_key = vals[ind]
sum += change_key * k
if sum < change:
print("Sorry, I don't have change for that. Please try buying with a different amount")
return
returned_change = dict()
#here we convert the best[change] into a dictionary showing the amount of each denomination
#simultaneously we are also removing the amount of each denomination from the the change contained in the Vending Machine
for ind, k in enumerate(best[change]):
if k != 0:
change_key = original_vals[ind]
returned_change[change_key] = k
frequency = self.change[change_key]
frequency -= k
if frequency == 0:
self.change.pop(change_key, None)
else:
self.change[change_key] = frequency
print("Here's your change")
print(returned_change)
self.products.pop(productName, None)
else:
print("Invalid product name")
def get_products(self):
for product, price in self.products.items():
print("%s %s" % (product, price))
def executeCommand(vm, command, parameter):
if command == "help":
print("Commands Manual")
print("<...> is just a placeholder")
print("reset_products - to reset the products in the vending machine")
print("reset_change - to reset the change in the vending machine")
print("buy <item name> - to buy the specified item")
print("see - to view all the items you can buy")
print("quit - to finish the game")
elif command == "see":
vm.get_products()
elif command == "reset_products":
vm.reset_products()
elif command == "reset_change":
vm.reset_change()
elif command == "buy":
vm.buy_product(parameter)
elif command == "quit":
sys.exit("Thanks for playing! Hope you enjoyed the game")
else:
print("Invalid command, type help for list of commands")
def main():
print("Vending Machine 1.0")
print("Let's load the products")
products = ast.literal_eval(input("Enter a dictionary with the product name as key and its price as the value. eg {\"Coke\": 0.5, \"Pen\":6}\n"))
print("Let's now set the change")
change = ast.literal_eval(input("Enter a dictionary with the coin denomination (to two decimal place) as key and its quantity as the value. eg {\"0.01\": 5, \"2.00\":20}\n"))
vm = Vending_Machine(products, change)
print("Great, now you can interact with the vending machine")
print("Try typing \'help\' to see a list of commands")
while(True):
user_input = input()
input_list = user_input.split(" ")
command = input_list[0]
parameter = "no parameter"
if len(input_list) > 1:
parameter = " ".join(input_list[1:])
print()
executeCommand(vm, command, parameter)
print()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment