Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Get the filepath from the command line
- import sys
- F1= sys.argv[1]
- F2= sys.argv[2]
- # Your code goes here
- def splitList(filepath):
- file = open(filepath,'r')
- contents = file.read()
- file.close()
- output = []
- S = contents.split('\n')
- for X in range(len(S)-1):
- output.append(S[X].split('|'))
- return output
- def executeTransaction(accounts, commands):
- for commandIndex in range(len(commands)):
- for accountIndex in range(len(accounts)):
- if commands[commandIndex][2] == accounts[accountIndex][0] and commands[commandIndex][3] == accounts[accountIndex][2]:
- account = accounts[accountIndex][0]
- balance = int(accounts[accountIndex][2])
- command = commands[commandIndex][0]
- amount = int(commands[commandIndex][1])
- if command == 'add':
- balance += amount
- else:
- if balance - amount >= 0:
- balance -= amount
- accounts[account][2] = str(balance)
- return accounts
- def returnToStr(listToConvert):
- output = []
- for X in range(len(listToConvert)):
- outputTMP = ('|').join(listToConvert[X])
- output.append(outputTMP)
- output = ('\n').join(output)
- return output
- AccountList = splitList(F1)
- CommandList = splitList(F2)
- updatedAccountList = executeTransaction(AccountList, CommandList)
- output = returnToStr(updatedAccountList)
- file = open(F1, 'w')
- file.write(output)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement