Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BankingSystem:
- def __init__(self):
- # Initialize a dictionary to store account balance
- self.accounts = {}
- # Initialize a dictionary to store the total transaction value per account
- self.transaction_totals = {}
- def create_account(self, timestamp, account_id):
- # Check if account already exists
- if account_id in self.accounts:
- return "false"
- else:
- # Create account with a balance of 0 and set its transaction total to 0
- self.accounts[account_id] = 0
- self.transaction_totals[account_id] = 0
- return "true"
- def deposit(self, timestamp, account_id, amount):
- # Check if account exists
- if account_id not in self.accounts:
- return ""
- else:
- # Add amount to account balance and transaction total
- self.accounts[account_id] += amount
- self.transaction_totals[account_id] += amount
- return str(self.accounts[account_id])
- def pay(self, timestamp, account_id, amount):
- # Check if account exists and has enough balance
- if account_id not in self.accounts or self.accounts[account_id] < amount:
- return ""
- else:
- # Subtract amount from account balance and add to transaction total
- self.accounts[account_id] -= amount
- self.transaction_totals[account_id] += amount
- return str(self.accounts[account_id])
- def top_activity(self, n):
- # Sort the accounts by the total value of transactions and account_id
- sorted_accounts = sorted(self.transaction_totals.items(), key=lambda item: (-item[1], item[0]))
- # Take the top n accounts or all accounts if there are less than n
- top_accounts = sorted_accounts[:n]
- # Format the output
- return ", ".join([f"{account}({value})" for account, value in top_accounts])
- def process_queries(self, queries):
- results = []
- for query in queries:
- operation, *params = query
- if operation == "CREATE_ACCOUNT":
- results.append(self.create_account(*params))
- elif operation == "DEPOSIT":
- results.append(self.deposit(*params))
- elif operation == "PAY":
- results.append(self.pay(*params))
- elif operation == "TOP_ACTIVITY":
- timestamp, n = params
- results.append(self.top_activity(int(n)))
- return results
- # Example usage
- banking_system = BankingSystem()
- queries = [
- ["CREATE_ACCOUNT", "1", "account1"],
- ["CREATE_ACCOUNT", "2", "account2"],
- ["CREATE_ACCOUNT", "3", "account3"],
- ["DEPOSIT", "4", "account1", 2000],
- ["DEPOSIT", "5", "account2", 3000],
- ["DEPOSIT", "6", "account3", 4000],
- ["TOP_ACTIVITY", "7", 3],
- ["PAY", "8", "account1", 1500],
- ["DEPOSIT", "9", "account2", 250],
- ["DEPOSIT", "10", "account3", 250],
- ["TOP_ACTIVITY", "11", 3]
- ]
- output = banking_system.process_queries(queries)
- output
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement