Advertisement
SimeonTs

SUPyF Dictionaries Exercises - 03. Dict-Ref-Advanced

Jun 24th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.00 KB | None | 0 0
  1. """
  2. Dictionaries - Exercises
  3. Проверка: https://judge.softuni.bg/Contests/Practice/Index/1088#2
  4.  
  5. SUPyF Dictionaries Exercises - 03. Dict-Ref-Advanced
  6.  
  7. Problem:
  8. Remember the Dict-Ref Problem from the previous exercise? Well this one is an Advanced Version.
  9. You will begin receiving input lines containing information in one of the following formats:
  10. - {key} -> {value 1, value 2, …, value n}
  11. - {key} -> {otherKey}
  12. The keys will always be strings, and the values will always be integers, separated by a comma and a space.
  13. If you are given a key and values, you must store the values to the given key. If the key already exists,
  14. you must add the given values to the old ones.
  15. If you are given a key and another key, you must copy the values of the other key to the first one.
  16. If the other key does not exist, this input line must be IGNORED.
  17. When you receive the command “end”, you must stop reading input lines, and you must print all keys with their values,
  18. in the following format:
  19. - {key} === {value1, value2, value3. . .}
  20.  
  21. Examples:
  22.    INPUT <<<
  23. Isacc -> 5, 4, 3
  24. Peter -> 6, 3, 3
  25. Derek -> 2, 2, 2
  26. end
  27.    OUTPUT >>>
  28. Isacc === 5, 4, 3
  29. Peter === 6, 3, 3
  30. Derek === 2, 2, 2
  31.  
  32.    INPUT <<<
  33. Donald -> 2, 2, 2
  34. Isacc -> 1
  35. George -> John
  36. John -> Isacc
  37. end
  38.    OUTPUT >>>
  39. Donald === 2, 2, 2
  40. Isacc === 1
  41. John === 1
  42. """
  43. di = {}
  44.  
  45. while True:
  46.     input_line = input()
  47.     if input_line == "end":
  48.         break
  49.     d = [item for item in input_line.split(" -> ")]
  50.     if (not d[1].isalpha()) and (d[0] not in di):
  51.         di[d[0]] = [int(item) for item in d[1].split(", ")]
  52.     elif (not d[1].isalpha()) and (d[0] in di):
  53.         values = [int(item) for item in d[1].split(", ")]
  54.         di[d[0]] += [int(item) for item in d[1].split(", ")]
  55.     elif d[1].isalpha() and (d[1] not in di):
  56.         continue
  57.     elif d[1].isalpha() and d[1] in di:
  58.         di[d[0]] = [int(item) for item in di[d[1]]]
  59.  
  60. for k, v in di.items():
  61.     print(f"{k} === ", end="")
  62.     print(", ".join(str(o) for o in v))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement