Advertisement
Guest User

Untitled

a guest
Dec 10th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. # Get the filepath from the command line
  2. import sys
  3. F1= sys.argv[1]
  4. F2= sys.argv[2]
  5.  
  6. # Your code goes here
  7. def splitList(filepath):
  8. file = open(filepath,'r')
  9. contents = file.read()
  10. file.close()
  11. output = []
  12. S = contents.split('\n')
  13. for X in range(len(S)-1):
  14. output.append(S[X].split('|'))
  15. return output
  16.  
  17. def executeTransaction(accounts, commands):
  18. for commandIndex in range(len(commands)):
  19. for accountIndex in range(len(accounts)):
  20. if commands[commandIndex][2] == accounts[accountIndex][0] and commands[commandIndex][3] == accounts[accountIndex][2]:
  21. account = accounts[accountIndex][0]
  22. balance = int(accounts[accountIndex][2])
  23.  
  24. command = commands[commandIndex][0]
  25. amount = int(commands[commandIndex][1])
  26.  
  27. if command == 'add':
  28. balance += amount
  29. else:
  30. if balance - amount >= 0:
  31. balance -= amount
  32.  
  33. accounts[account][2] = str(balance)
  34.  
  35. return accounts
  36.  
  37. def returnToStr(listToConvert):
  38. output = []
  39. for X in range(len(listToConvert)):
  40. outputTMP = ('|').join(listToConvert[X])
  41. output.append(outputTMP)
  42. output = ('\n').join(output)
  43. return output
  44.  
  45. AccountList = splitList(F1)
  46. CommandList = splitList(F2)
  47.  
  48. updatedAccountList = executeTransaction(AccountList, CommandList)
  49.  
  50. output = returnToStr(updatedAccountList)
  51.  
  52. file = open(F1, 'w')
  53. file.write(output)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement