Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BankingSystemLevel3:
- def __init__(self):
- self.accounts = {}
- self.transfers = {}
- self.transfer_counter = 1
- def create_account(self, timestamp, account_id):
- if account_id in self.accounts:
- return "false"
- self.accounts[account_id] = 0
- return "true"
- def deposit(self, timestamp, account_id, amount):
- if account_id not in self.accounts:
- return ""
- self.accounts[account_id] += int(amount)
- return str(self.accounts[account_id])
- def transfer(self, timestamp, source_account_id, target_account_id, amount):
- amount = int(amount)
- timestamp = int(timestamp)
- if source_account_id == target_account_id or \
- source_account_id not in self.accounts or \
- target_account_id not in self.accounts or \
- self.accounts[source_account_id] < amount:
- return ""
- transfer_id = f"transfer{self.transfer_counter}"
- self.transfer_counter += 1
- self.accounts[source_account_id] -= amount
- self.transfers[transfer_id] = {
- 'source': source_account_id,
- 'target': target_account_id,
- 'amount': amount,
- 'timestamp': timestamp,
- 'expired': False
- }
- return transfer_id
- def accept_transfer(self, timestamp, account_id, transfer_id):
- timestamp = int(timestamp)
- if transfer_id not in self.transfers:
- return "false"
- transfer = self.transfers[transfer_id]
- if timestamp - transfer['timestamp'] > 86400000: # Check for transfer expiry
- # Refund only if the transfer wasn't previously marked as expired
- if not transfer['expired']:
- self.accounts[transfer['source']] += transfer['amount']
- transfer['expired'] = True
- return "false"
- # Check if the transfer is already expired or account is incorrect
- if transfer['expired'] or transfer['target'] != account_id:
- return "false"
- # Transfer is successful
- self.accounts[account_id] += transfer['amount']
- transfer['expired'] = True # Mark transfer as expired after acceptance
- return "true"
- def process_queries(self, queries):
- results = []
- for query in queries:
- operation = query[0]
- timestamp = int(query[1])
- params = query[2:]
- if operation == "CREATE_ACCOUNT":
- results.append(self.create_account(timestamp, *params))
- elif operation == "DEPOSIT":
- results.append(self.deposit(timestamp, *params))
- elif operation == "TRANSFER":
- results.append(self.transfer(timestamp, *params))
- elif operation == "ACCEPT_TRANSFER":
- results.append(self.accept_transfer(timestamp, *params))
- return results
- # Example usage:
- # banking_system = BankingSystemLevel3()
- # output = banking_system.process_queries(your_query_list_here)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement