Senior
# ATM that is loaded with cash cassettes containing banknotes, with our app onboard # should be able to dispense banknotes for a specified amount or respond with a refusal # When dispensing banknotes, the amount is deducted from the ATM's balance. # Allowed denominations: 50R, 100R, 500R, 1000R, 5000R. class ATM: pass # place for code # ATM device: # - money is stored in cassettes inside the ATM, which are loaded and reloaded by a courier; # - each cassette contains banknotes of its denomination; # - the ATM can count the remaining banknotes in the cassettes, but this operation takes a considerable amount of time - it should be called as rarely as possible. # API for interacting with the ATM hardware. # The SDK interface can be modified/extended by mutual agreement if necessary. class SDK(ABC): @abstractmethod def count_banknotes(self, banknote: int) -> int: pass @abstractmethod def move_banknote_to_dispenser(self, banknote: int, count: int) -> None: pass @abstractmethod def open_dispenser(self) -> None: pass
sobes.tech AI
Answer from AI
python from abc import ABC, abstractmethod
class ATM: def init(self, sdk: SDK): self.sdk = sdk self.banknotes_cache = {}
def _update_cache(self):
# Update the cache of banknotes count for each denomination
for banknote in [50, 100, 500, 1000, 5000]:
self.banknotes_cache[banknote] = self.sdk.count_banknotes(banknote)
def _can_dispense(self, amount: int) -> bool:
# Check if the amount can be dispensed with the current cache
# Using a greedy algorithm by descending denominations
remaining = amount
temp_cache = self.banknotes_cache.copy()
for banknote in sorted(temp_cache.keys(), reverse=True):
needed = remaining // banknote
available = temp_cache[banknote]
used = min(needed, available)
remaining -= used * banknote
temp_cache[banknote] -= used
if remaining == 0:
return True
return False
def dispense(self, amount: int) -> bool:
# Update cache before dispensing
self._update_cache()
if amount <= 0 or amount % 50 != 0:
return False # Invalid amount
if not self._can_dispense(amount):
return False # Cannot dispense the amount
remaining = amount
for banknote in sorted(self.banknotes_cache.keys(), reverse=True):
needed = remaining // banknote
available = self.banknotes_cache[banknote]
used = min(needed, available)
if used > 0:
self.sdk.move_banknote_to_dispenser(banknote, used)
self.banknotes_cache[banknote] -= used
remaining -= used * banknote
if remaining == 0:
break
self.sdk.open_dispenser()
return True
**Explanation:**
- The `ATM` class takes an `SDK` object for hardware interaction.
- A cache is used to optimize calls to `count_banknotes`, which is updated before dispensing.
- The `dispense` method checks if the amount can be dispensed using a greedy algorithm by denominations.
- If dispensing is possible, banknotes are moved to the dispenser, which is then opened.
- If the amount is not a multiple of 50 or cannot be dispensed, it returns false.