Advertisement
rajeshinternshala

Untitled

Dec 22nd, 2023
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.06 KB | None | 0 0
  1. class BankingSystem:
  2.     def __init__(self):
  3.         # Initialize a dictionary to store account balance
  4.         self.accounts = {}
  5.         # Initialize a dictionary to store the total transaction value per account
  6.         self.transaction_totals = {}
  7.  
  8.     def create_account(self, timestamp, account_id):
  9.         # Check if account already exists
  10.         if account_id in self.accounts:
  11.             return "false"
  12.         else:
  13.             # Create account with a balance of 0 and set its transaction total to 0
  14.             self.accounts[account_id] = 0
  15.             self.transaction_totals[account_id] = 0
  16.             return "true"
  17.  
  18.     def deposit(self, timestamp, account_id, amount):
  19.         # Check if account exists
  20.         if account_id not in self.accounts:
  21.             return ""
  22.         else:
  23.             # Add amount to account balance and transaction total
  24.             self.accounts[account_id] += amount
  25.             self.transaction_totals[account_id] += amount
  26.             return str(self.accounts[account_id])
  27.  
  28.     def pay(self, timestamp, account_id, amount):
  29.         # Check if account exists and has enough balance
  30.         if account_id not in self.accounts or self.accounts[account_id] < amount:
  31.             return ""
  32.         else:
  33.             # Subtract amount from account balance and add to transaction total
  34.             self.accounts[account_id] -= amount
  35.             self.transaction_totals[account_id] += amount
  36.             return str(self.accounts[account_id])
  37.  
  38.     def top_activity(self, n):
  39.         # Sort the accounts by the total value of transactions and account_id
  40.         sorted_accounts = sorted(self.transaction_totals.items(), key=lambda item: (-item[1], item[0]))
  41.         # Take the top n accounts or all accounts if there are less than n
  42.         top_accounts = sorted_accounts[:n]
  43.         # Format the output
  44.         return ", ".join([f"{account}({value})" for account, value in top_accounts])
  45.  
  46.     def process_queries(self, queries):
  47.         results = []
  48.         for query in queries:
  49.             operation, *params = query
  50.             if operation == "CREATE_ACCOUNT":
  51.                 results.append(self.create_account(*params))
  52.             elif operation == "DEPOSIT":
  53.                 results.append(self.deposit(*params))
  54.             elif operation == "PAY":
  55.                 results.append(self.pay(*params))
  56.             elif operation == "TOP_ACTIVITY":
  57.                 timestamp, n = params
  58.                 results.append(self.top_activity(int(n)))
  59.         return results
  60.  
  61. # Example usage
  62. banking_system = BankingSystem()
  63. queries = [
  64.     ["CREATE_ACCOUNT", "1", "account1"],
  65.     ["CREATE_ACCOUNT", "2", "account2"],
  66.     ["CREATE_ACCOUNT", "3", "account3"],
  67.     ["DEPOSIT", "4", "account1", 2000],
  68.     ["DEPOSIT", "5", "account2", 3000],
  69.     ["DEPOSIT", "6", "account3", 4000],
  70.     ["TOP_ACTIVITY", "7", 3],
  71.     ["PAY", "8", "account1", 1500],
  72.     ["DEPOSIT", "9", "account2", 250],
  73.     ["DEPOSIT", "10", "account3", 250],
  74.     ["TOP_ACTIVITY", "11", 3]
  75. ]
  76. output = banking_system.process_queries(queries)
  77. output
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement