Advertisement
SimeonTs

SUPyF Dictionaries Exercises - 05. Social Media Posts

Jun 24th, 2019
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.67 KB | None | 0 0
  1. """
  2. Dictionaries - Exercises
  3. Проверка: https://judge.softuni.bg/Contests/Practice/Index/1088#4
  4.  
  5. SUPyF Dictionaries Exercises - 05. Social Media Posts
  6.  
  7. Problem:
  8. You have been tasked to create a Console Social Media Database.
  9. You will receive several input lines in one of the following formats:
  10. - post {postName}
  11. - like {postName}
  12. - dislike {postName}
  13. - comment {postName} {commenterName} {content}
  14. If you receive the post command, you must create a post with the given name.
  15. If you receive the like command you must add a like to the given post.
  16. If you receive the dislike command you must add a dislike to the given post.
  17. If you receive the comment command, you must add a comment to the given post.
  18. The comment’s writer must be the given commentator name, and the content of the comment must be the given content.
  19. By default, the posts have 0 likes, 0 dislikes and 0 comments when created.
  20. When you receive the command “drop the media”, you must end the input sequence, and you must print every post with its
  21. likes, dislikes and comments in the following format:
  22. Post: {postName} | Likes: {likes} | Dislikes {dislikes}
  23. Comments:
  24. *  {commentator1}: {comment1}
  25. *  {commentator2}: {comment2}
  26. . . .
  27. If a certain post does not have any comments, you should just print “None”.
  28. The comments have a prefix of a single asterisk (‘*’) and 2 spaces.
  29. There is NO space between the commentator’s name and the colon.
  30. Constraints
  31. - The post name will be a string of letters and digits.
  32. - The commentator’s name will be a string of letters.
  33. - The comment’s CONTENT, may contain ANY ASCII character.
  34. - There will be NO invalid input data.
  35. EXAMPLES:
  36.    INPUT <<<
  37. post FirstPost
  38. like FirstPost
  39. like FirstPost
  40. dislike FirstPost
  41. post SecondPost
  42. comment FirstPost Isacc Cool
  43. comment SecondPost Isacc Lol
  44. like SecondPost
  45. drop the media
  46.    OUTPUT >>>
  47. Post: FirstPost | Likes: 2 | Dislikes: 1
  48. Comments:
  49. *  Isacc: Cool
  50. Post: SecondPost | Likes: 1 | Dislikes: 0
  51. Comments:
  52. *  Isacc: Lol
  53.  
  54.    INPUT <<<
  55. post SomePost
  56. like SomePost
  57. like SomePost
  58. dislike SomePost
  59. post OtherPost
  60. comment OtherPost Isacc Naaais
  61. comment OtherPost Peter GoodPost
  62. comment OtherPost John Meh...
  63. drop the media
  64.    OUTPUT >>>
  65. Post: SomePost | Likes: 2 | Dislikes: 1
  66. Comments:
  67. None
  68. Post: OtherPost | Likes: 0 | Dislikes: 0
  69. Comments:
  70. *  Isacc: Naaais
  71. *  Peter: GoodPost
  72. *  John: Meh...
  73. """
  74. social_media = {}
  75.  
  76. while True:
  77.     command = input()
  78.     if command == "drop the media":
  79.         break
  80.     a = [word for word in command.split(" ")]
  81.     if a[0] == "post":
  82.         social_media[a[1]] = {}
  83.         social_media[a[1]]["Likes"] = 0
  84.         social_media[a[1]]["Dislikes"] = 0
  85.         social_media[a[1]]["Comments"] = {}
  86.     elif a[0] == "like":
  87.         social_media[a[1]]["Likes"] += 1
  88.     elif a[0] == "dislike":
  89.         social_media[a[1]]["Dislikes"] += 1
  90.     elif a[0] == "comment":
  91.         if a[1] in social_media:
  92.             social_media[a[1]]["Comments"][a[2]] = a[3:]
  93.         elif a[1] not in social_media:
  94.             social_media[a[1]]["Comments"][a[2]] += a[3:]
  95.  
  96. for post, key, in social_media.items():
  97.     print(f"Post: {post} | ", end="")
  98.  
  99.     for value in key:
  100.         if value == "Likes":
  101.             print(f"{value}: {key[value]} | ", end="")
  102.         elif value == "Dislikes":
  103.             print(f"{value}: {key[value]}")
  104.         elif value == "Comments":
  105.             if len(key[value]) == 0:
  106.                 print("Comments:")
  107.                 print("None")
  108.             else:
  109.                 print("Comments:")
  110.                 for val in key[value]:
  111.                     print(f"*  {val}: ", end="")
  112.                     print(" ".join(key[value][val]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement