Skip to content

Instantly share code, notes, and snippets.

@malkoG
Created March 25, 2019 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malkoG/c9a502a02f6af7f9f463f1f574db7c6a to your computer and use it in GitHub Desktop.
Save malkoG/c9a502a02f6af7f9f463f1f574db7c6a to your computer and use it in GitHub Desktop.
noj.am/6642
def translate(dotted_cash):
over_dot, under_dot = map(int, dotted_cash.split("."))
return over_dot * 100 + under_dot
output = []
while True:
n=int(input())
bank = dict()
if n == 0:
output.append("goodbye")
break
for i in range(n):
account, cash = input().split()
bank[account] = translate(cash)
while True:
command=input().split()
if command[0] == "end":
output.append("end")
output.append("")
input()
break
if command[0] == "create":
_, account = command
try:
if bank[account] >= 0:
output.append("create: already exists")
except KeyError:
bank[account] = 0
output.append("create: ok")
elif command[0] == "deposit":
_, account, cash = command
try:
bank[account] += translate(cash)
output.append("deposit {}: ok".format(cash))
except KeyError:
output.append("deposit {}: no such account".format(cash))
elif command[0] == "withdraw":
_, account, cash = command
try:
translated_cash = translate(cash)
if bank[account] < translated_cash:
output.append("withdraw {}: insufficient funds".format(cash))
else:
bank[account] -= translated_cash
output.append("withdraw {}: ok".format(cash))
except KeyError:
output.append("withdraw {}: no such account".format(cash))
else:
_, source, target, cash = command
translated_cash = translate(cash)
try:
_ = bank[source], bank[target]
if source == target:
output.append("transfer {}: same account".format(cash))
elif bank[source] < translated_cash:
output.append("transfer {}: insufficient funds".format(cash))
else:
bank[source] -= translated_cash
bank[target] += translated_cash
if source[-1] != target[-1]:
output.append("transfer {}: interbank".format(cash))
else:
output.append("transfer {}: ok".format(cash))
except KeyError:
output.append("transfer {}: no such account".format(cash))
print("\n".join(output))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment