sibinasto

Problem 3 - 3.5. Statistics + Message

Apr 3rd, 2021
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. max_capacity = int(input())
  2. users = {}
  3.  
  4. line = input()
  5. while not line == "Statistics":
  6.     tokens = line.split("=")
  7.     command = tokens[0]
  8.     if command == "Add":
  9.         name = tokens[1]
  10.         sent = int(tokens[2])
  11.         received = int(tokens[3])
  12.         if name not in users:
  13.             users[name] = {"sent": sent, "received": received}
  14.     elif command == "Message":
  15.         sender = tokens[1]
  16.         receiver = tokens[2]
  17.         if sender in users and receiver in users:
  18.             users[sender]["sent"] += 1
  19.             if users[sender]["sent"] + users[sender]["received"] == max_capacity:
  20.                 users.pop(sender)
  21.                 print(f"{sender} reached the capacity!")
  22.             users[receiver]["received"] += 1
  23.             if users[receiver]["received"] + users[receiver]["sent"] == max_capacity:
  24.                 users.pop(receiver)
  25.                 print(f"{receiver} reached the capacity!")
  26.     elif command == "Empty":
  27.         name = tokens[1]
  28.         if name == "All":
  29.             users = {}
  30.         elif name in users:
  31.             users.pop(name)
  32.  
  33.     line = input()
  34.  
  35. print(f"Users count: {len(users)}")
  36. for user, data in sorted(users.items(), key=lambda kvp: (-kvp[1]["received"], kvp[0])):
  37.     total = data["received"] + data["sent"]
  38.     print(f"{user} - {total}")
Advertisement
Add Comment
Please, Sign In to add comment