Space
Open
From Fieldwork
Scales
Archive
Just index offsetting.
class Bank:
def __init__(self, balance: List[int]):
# Store the balance list.
# We keep the length to avoid calling len() repeatedly in checks.
self.balance = balance
self.n = len(balance)
def transfer(self, account1: int, account2: int, money: int) -> bool:
# Check if both accounts exist (1 <= account <= n)
if not (1 <= account1 <= self.n and 1 <= account2 <= self.n):
return False
# Check if sender has enough money
# Note: account numbers are 1-indexed, list is 0-indexed
if self.balance[account1 - 1] < money:
return False
# Perform transfer
self.balance[account1 - 1] -= money
self.balance[account2 - 1] += money
return True
def deposit(self, account: int, money: int) -> bool:
# Check if account exists
if not (1 <= account <= self.n):
return False
# Perform deposit
self.balance[account - 1] += money
return True
def withdraw(self, account: int, money: int) -> bool:
# Check if account exists
if not (1 <= account <= self.n):
return False
# Check if account has enough money
if self.balance[account - 1] < money:
return False
# Perform withdrawal
self.balance[account - 1] -= money
return TruePractice bench
A private scratchpad for this reading. Nothing is sent or scored.
What is still unclear, or what would change the explanation?
Saved on this device · one draft per mode