AlexG2230954

ДЗ. Задание 12

May 22nd, 2022 (edited)
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. from collections import defaultdict
  2.  
  3.  
  4. clients = defaultdict(lambda: defaultdict(int))
  5. stocks = defaultdict(lambda: 1)
  6.  
  7. def buy_action(client, company, value):
  8. clients[client][company] += value
  9.  
  10.  
  11. def sell_action(client, company, value):
  12. amount = clients[client][company]
  13. clients[client][company] -= min(value, amount)
  14.  
  15.  
  16. def balance_check(client):
  17. return sum(stocks[company] * amount for company, amount in clients[client].items())
  18.  
  19.  
  20. def price_raise(company, percents):
  21. stocks[company] *= (1 + percents / 100)
  22.  
  23.  
  24. def price_fall(company, percents):
  25. stocks[company] *= (1 - percents / 100)
  26.  
  27.  
  28. handlers = {
  29. "BUY" : lambda args: buy_action(args[0], args[1], int(args[2])),
  30. "SELL" : lambda args : sell_action(args[0], args[1], int(args[2])),
  31. "PRICE_RAISE" : lambda args : price_raise(args[0], float(args[1])),
  32. "PRICE_FALL" : lambda args : price_fall(args[0], float(args[1])),
  33. "BALANCE" : lambda args: print(round(balance_check(args[0])))
  34. }
  35.  
  36. n = int(input())
  37.  
  38. for _ in range(n):
  39. cmd, *args = input().split()
  40. handlers[cmd](args)
  41.  
Add Comment
Please, Sign In to add comment