Sobes.tech
Senior

class ATM(SKD): def __init__(self, atm_api: "SKD"): self.atm_api: SKD = atm_api self.bills_count: dict[int:int] = {bill: self.atm_api.count_banknotes(bill) for bill in BILLS} def withdraw(self, amount: int) -> bool: bills_plan: dict[int: int] = {} remaining = amount for bill in BILLS: available = self.bills_count[bill] take = min(remaining // bill, available) if take > 0: bills_plan[bill] = take remaining -= take * bill if remaining != 0: return False bills_count_const = {50: 0, 100: 2, 500: 0, 1000: 0, ...}, expected = True @pytest.mark.parametrize( "bills_count_const,expected", [ ({5000: 0, 1000: 0, 500: 0, 100: 0, 50: 0}, False), ({5000: 100, 1000: 100, 500: 100, 100: 100, 50: 100}, True), ({5000: 1, 1000: 0, 500: 0, 100: 2, 50: 0}, True) ] ) def test_false(bills_count_const, expected): atm = ATM(FakeSDK()) atm.bills_count = bills_count_const amount = 1000 res = atm.withdraw(amount) assert res == expected

sobes.tech AI

Answer from AI

The provided code implements an ATM class that handles cash withdrawal using available banknotes. The withdraw method attempts to create a plan to dispense the amount, starting with the largest banknotes and decreasing the remaining amount accordingly.

Tests using pytest verify the correctness of the withdraw method under different banknote availability states (bills_count_const) and expected outcomes (True/False).

Key points:

  • The withdraw method returns False if it is impossible to dispense the exact amount with the available banknotes.
  • Tests are parameterized, allowing testing of different scenarios without code duplication.

An example of parameterization in pytest:

import pytest

@pytest.mark.parametrize(
    "bills_count_const,expected",
    [
        ({5000: 0, 1000: 0, 500: 0, 100: 0, 50: 0}, False),
        ({5000: 100, 1000: 100, 500: 100, 100: 100, 50: 100}, True),
        ({5000: 1, 1000: 0, 500: 0, 100: 2, 50: 0}, True)
    ]
)
def test_withdraw(bills_count_const, expected):
    atm = ATM(FakeSDK())
    atm.bills_count = bills_count_const
    amount = 1000
    assert atm.withdraw(amount) == expected

This approach makes it easy to extend tests and maintain code quality.