Advertisement
exDotaPro

followers

Jul 26th, 2020 (edited)
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.02 KB | None | 0 0
  1. def valid_user(user, collection):
  2.     if user not in collection:
  3.         collection[user] = [0, 0]
  4.     return collection
  5.  
  6.  
  7. def sort_dict(my_dict):
  8.     my_dict = {k: v for k, v in sorted(my_dict.items(), key=lambda x: (-(x[1][0]), x[0]))}
  9.     return my_dict
  10.  
  11.  
  12. followers = dict()
  13.  
  14. while True:
  15.     line = input().split(': ')
  16.     command = line[0]
  17.  
  18.     if command == 'Log out':
  19.         break
  20.  
  21.     username = line[1]
  22.     if command == 'New follower':
  23.         valid_user(username, followers)
  24.     elif command == 'Like':
  25.         valid_user(username, followers)
  26.         followers[username][0] += int(line[2])
  27.     elif command == 'Comment':
  28.         valid_user(username, followers)
  29.         followers[username][1] += 1
  30.     elif command == 'Blocked':
  31.         if username not in followers:
  32.             print(f'{username} doesn\'t exist.')
  33.         else:
  34.             del followers[username]
  35.  
  36. print(f'{len(followers)} followers')
  37. for username, likes in sort_dict(followers).items():
  38.     print(f'{username}: {sum(likes)}')
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement