Advertisement
SimeonTs

SUPyF Dictionaries Exercises - 04. Forum Topics

Jun 24th, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.22 KB | None | 0 0
  1. """
  2. Dictionaries - Exercises
  3. Проверка: https://judge.softuni.bg/Contests/Practice/Index/1088#3
  4.  
  5. SUPyF Dictionaries Exercises - 04. Forum Topics
  6.  
  7. Problem:
  8. You have been tasked to store a few forum topics, and filter them by several given tags.
  9. You will be given several input lines, containing data about topics in the following format:
  10. {topic} -> {tag1, tag2, tag3...}
  11. The topic and tags will be strings. They will NOT contain spaces or ‘-’, ‘>’ symbols.
  12. If you receive an existing topic, you must add the new tags to it. There should be NO duplicate tags.
  13. When you receive the command “filter”, you must end the input sequence. On the next line (after “filter”),
  14. you will receive a sequence of tags, separated by a comma and a space. You must print ONLY those topics,
  15. which contain all tags in the given sequence.
  16. The topics must be printed in the following format:
  17. {topic} | {#tag1, #tag2, …, #tagN}
  18. NOTE: The tags have a number sign (‘#’) as a prefix.
  19. EXAMPLES:
  20.    INPUT <<<
  21. HelloWorld -> hello, forum, topic
  22. HelpWithHomework -> homework, forum, topic
  23. filter
  24. forum, topic
  25.    OUTPUT >>>
  26. HelloWorld | #hello, #forum, #topic
  27. HelpWithHomework | #homework, #forum, #topic
  28.    INPUT <<<
  29. First -> this
  30. First -> that
  31. First -> who
  32. Second -> this, what, why
  33. First -> this
  34. Third -> this, third
  35. Third -> that
  36. filter
  37. that, this
  38.    OUTPUT >>>
  39. First | #this, #that, #who
  40. Third | #this, #third, #that
  41. """
  42.  
  43. topics = {}
  44.  
  45. while True:
  46.     command = input()
  47.     if command == "filter":
  48.         break
  49.     a = [word for word in command.split(" -> ")]
  50.     name = a[0]
  51.     items = [word for word in a[1].split(", ")]
  52.     if name not in topics.keys():
  53.         topics[name] = []
  54.         for item in items:
  55.             if item not in topics[name]:
  56.                 topics[name] += [item]
  57.     else:
  58.         for item in items:
  59.             if item not in topics[name]:
  60.                 topics[name].append(item)
  61.  
  62. tags = [tag for tag in input().split(", ")]
  63.  
  64. for topic, items in topics.items():
  65.     if_all = len(tags)
  66.     for item in tags:
  67.         if item in items:
  68.             if_all -= 1
  69.     if if_all == 0:
  70.         print(topic, end=" | ")
  71.         print("#", end="")
  72.         print(", #".join(items))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement