Advertisement
SimeonTs

SUPyF Dictionaries Exercises - 01. Key-Key Value-Value

Jun 24th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.12 KB | None | 0 0
  1. """
  2. Dictionaries - Exercises
  3. Проверка: https://judge.softuni.bg/Contests/Practice/Index/1088#0
  4.  
  5. SUPyF Dictionaries Exercises - 01. Key-Key Value-Value
  6.  
  7. Problem:
  8. Write a program, which searches for a key and value inside of several key-value pairs.
  9. Input
  10. - On the first line, you will receive a key.
  11. - On the second line, you will receive a value.
  12. - On the third line, you will receive N.
  13. - On the next N lines, you will receive strings in the following format:
  14. “key => {value 1};{value 2};…{value X}”
  15. After you receive N key -> values pairs, your task is to go through them and print only the keys,
  16. which contain the key and the values, which contain the value. Print them in the following format:
  17. {key}:
  18. -{value1}
  19. -{value2}
  20. -{valueN}
  21.  
  22. Examples:
  23.  
  24. INPUT:                              OUTPUT:
  25. bug                                 debug:
  26. X                                   -XUL
  27. 3                                   -XC
  28. invalidkey => testval;x;y           buggy:
  29. debug => XUL;ccx;XC                 -testX
  30. buggy => testX;testY;XtestZ         -XtestZ
  31.  
  32. INPUT:
  33. key
  34. valu
  35. 2
  36. xkeyc => value;value;valio
  37. keyhole => valuable;x;values
  38.  
  39. INPUT:                              OUTPUT:
  40. key                                 xkeyc:
  41. valu                                -value
  42. 2                                   -value
  43. xkeyc => value;value;valio          keyhole:
  44. keyhole => valuable;x;values        -valuable
  45.                                    -values
  46. """
  47.  
  48. key = input()
  49. value = input()
  50. n = int(input())
  51.  
  52. dictionary = {}
  53.  
  54. for i in range(n):
  55.     a = [item for item in input().split(" => ")]
  56.     key_check = a[0]
  57.     value_check = [item for item in a[1].split(";")]
  58.  
  59.     if key in key_check:
  60.         dictionary[key_check] = []
  61.         for item in value_check:
  62.             if value in item:
  63.                 if len(dictionary[key_check]) == 0:
  64.                     dictionary[key_check] = [item]
  65.                 elif len(dictionary[key_check]) != 0:
  66.                     dictionary[key_check] += [item]
  67.  
  68. for key, value in dictionary.items():
  69.     print(f"{key}:")
  70.     for item in value:
  71.         print(f"-{item}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement