Advertisement
rajeshinternshala

Untitled

Dec 22nd, 2023
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.81 KB | None | 0 0
  1. class BankingSystemLevel3:
  2.     def __init__(self):
  3.         self.accounts = {}
  4.         self.transfers = {}
  5.         self.transfer_counter = 1
  6.         self.transfer_expiry = 86400000  # 24 hours in milliseconds
  7.  
  8.     def create_account(self, timestamp, account_id):
  9.         if account_id in self.accounts:
  10.             return "false"
  11.         self.accounts[account_id] = 0
  12.         return "true"
  13.  
  14.     def deposit(self, timestamp, account_id, amount):
  15.         if account_id not in self.accounts:
  16.             return ""
  17.         self.accounts[account_id] += int(amount)
  18.         return str(self.accounts[account_id])
  19.  
  20.     def pay(self, timestamp, account_id, amount):
  21.         if account_id not in self.accounts or self.accounts[account_id] < int(amount):
  22.             return ""
  23.         self.accounts[account_id] -= int(amount)
  24.         return str(self.accounts[account_id])
  25.  
  26.     def transfer(self, timestamp, source_account_id, target_account_id, amount):
  27.         if source_account_id == target_account_id or \
  28.                 source_account_id not in self.accounts or \
  29.                 target_account_id not in self.accounts or \
  30.                 self.accounts[source_account_id] < int(amount):
  31.             return ""
  32.  
  33.         transfer_id = f"transfer{self.transfer_counter}"
  34.         self.transfer_counter += 1
  35.  
  36.         # Withdraw the amount from source account
  37.         self.accounts[source_account_id] -= int(amount)
  38.  
  39.         # Create a transfer
  40.         self.transfers[transfer_id] = {
  41.             'source': source_account_id,
  42.             'target': target_account_id,
  43.             'amount': int(amount),
  44.             'timestamp': int(timestamp),
  45.             'expired': False
  46.         }
  47.         return transfer_id
  48.  
  49.     def accept_transfer(self, timestamp, account_id, transfer_id):
  50.         if transfer_id not in self.transfers or \
  51.                 self.transfers[transfer_id]['target'] != account_id or \
  52.                 self.transfers[transfer_id]['expired']:
  53.             return "false"
  54.  
  55.         # Check for expiry
  56.         if int(timestamp) - self.transfers[transfer_id]['timestamp'] > self.transfer_expiry:
  57.             self.accounts[self.transfers[transfer_id]['source']] += self.transfers[transfer_id]['amount']
  58.             self.transfers[transfer_id]['expired'] = True
  59.             return "false"
  60.  
  61.         # Complete transfer
  62.         self.accounts[account_id] += self.transfers[transfer_id]['amount']
  63.         del self.transfers[transfer_id]  # Remove transfer as it is completed
  64.         return "true"
  65.  
  66.     def process_queries(self, queries):
  67.         results = []
  68.         for query in queries:
  69.             operation = query[0]
  70.             args = query[1:]
  71.             if operation == "CREATE_ACCOUNT":
  72.                 results.append(self.create_account(*args))
  73.             elif operation == "DEPOSIT":
  74.                 results.append(self.deposit(*args))
  75.             elif operation == "PAY":
  76.                 results.append(self.pay(*args))
  77.             elif operation == "TRANSFER":
  78.                 results.append(self.transfer(*args))
  79.             elif operation == "ACCEPT_TRANSFER":
  80.                 results.append(self.accept_transfer(*args))
  81.         return results
  82.  
  83. # Example usage:
  84. banking_system = BankingSystemLevel3()
  85. queries = [
  86.     ["CREATE_ACCOUNT", "1622534400000", "account1"],
  87.     ["CREATE_ACCOUNT", "1622534401000", "account2"],
  88.     ["DEPOSIT", "1622534402000", "account1", "1000"],
  89.     ["DEPOSIT", "1622534403000", "account2", "1000"],
  90.     ["TRANSFER", "1622534404000", "account1", "account2", "500"],
  91.     ["ACCEPT_TRANSFER", "1622620800000", "account2", "transfer1"],
  92.     ["TRANSFER", "1622534405000", "account1", "account2", "100"],
  93.     ["ACCEPT_TRANSFER", "1622620801000", "account2", "transfer2"]
  94. ]
  95.  
  96. # Process the queries and output the results
  97. output = banking_system.process_queries(queries)
  98. output
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement