class CashMachine:
def __init__(self):
self.coins = {0.20: 0, 0.50: 0, 1: 0, 2: 0}
self.banknotes = {5: 0, 10: 0, 20: 0}
def load_coins(self, number_of_coins, coin_type):
if coin_type in self.coins:
self.coins[coin_type] += number_of_coins
def exchange(self, amount):
coins_to_use = []
banknotes_to_use = []
remaining_amount = amount
for coin_value in sorted(self.coins.keys(), reverse=True):
coin_count = self.coins[coin_value]
while coin_count > 0 and remaining_amount >= coin_value:
coins_to_use.append(coin_value)
remaining_amount -= coin_value
coin_count -= 1
for banknote_value in sorted(self.banknotes.keys(), reverse=True):
banknote_count = self.banknotes[banknote_value]
while banknote_count > 0 and remaining_amount >= banknote_value:
banknotes_to_use.append(banknote_value)
remaining_amount -= banknote_value
banknote_count -= 1
if remaining_amount == 0:
for coin in coins_to_use:
self.coins[coin] -= 1
for banknote in banknotes_to_use:
self.banknotes[banknote] -= 1
return coins_to_use, banknotes_to_use
else:
return [], []
def print_machine_status(self):
coin_counts = [f"{count} {coin}£" for coin, count in self.coins.items() if count > 0]
banknote_counts = [f"{count} {banknote}£" for banknote, count in self.banknotes.items() if count > 0]
print(f"= {', '.join(coin_counts)} {', '.join(banknote_counts)}")
def process_commands(input_file):
cash_machine = CashMachine()
with open(input_file, 'r') as file:
for line in file:
line = line.strip()
if line.startswith("LOAD"):
print("> " + line)
_, number_of_items, item_type = line.split()
number_of_items = int(number_of_items)
item_type = float(item_type)
cash_machine.load_coins(number_of_items, item_type)
cash_machine.print_machine_status()
if line.startswith("EXCHANGE"):
print("> " + line)
_, amount = line.split()
amount = float(amount)
coins_used, banknotes_used = cash_machine.exchange(amount)
if coins_used or banknotes_used:
coin_counts = [f"{coins_used.count(coin)} {coin}£" for coin in set(coins_used)]
banknote_counts = [f"{banknotes_used.count(banknote)} {banknote}£" for banknote in set(banknotes_used)]
print("< " + " ,".join(coin_counts + banknote_counts))
else:
print("< CANNOT EXCHANGE")
cash_machine.print_machine_status()
if __name__ == '__main__':
CashMachine.process_commands("input.txt")
As the code works well ,the main logic is to load 0.20£, 0.50£, 1£, 2£ and convert the banknote of the given amount 5£, 10£, 20£ for an equivalent coin.
> LOAD 20 1
= ['20 1£']
> LOAD 10 2
= ['20 1£', '10 2£']
> LOAD 40 0.50
= ['40 0.5£', '20 1£', '10 2£']
> EXCHANGE 5
< 1 1£, 2 2£
= ['40 0.5£', '19 1£', '8 2£']
> EXCHANGE 20
< 4 1£, 8 2£
= ['40 0.5£', '15 1£']
> EXCHANGE 20
< 10 0.5£, 15 1£
= ['30 0.5£']
works well but when i try with 0.20 values,i am not getting relavant output
> LOAD 20 1
= ['20 1£']
> LOAD 10 2
= ['20 1£', '10 2£']
> LOAD 40 0.20
= ['40 0.2£', '20 1£', '10 2£']
> EXCHANGE 5
< CANNOT EXCHANGE
= ['40 0.2£', '20 1£', '10 2£']
> EXCHANGE 20
< CANNOT EXCHANGE
= ['40 0.2£', '20 1£', '10 2£']
> EXCHANGE 20
< CANNOT EXCHANGE
= ['40 0.2£', '20 1£', '10 2£']
What logic i am missing or what error i am doing,cant understand ,please help me out to understand the logic
And my input.txt structure of
`> LOAD 20 1
> LOAD 10 2
> LOAD 40 0.20
> EXCHANGE 5
> EXCHANGE 20
> EXCHANGE 20`