SimeonTs

SUPyF2 Text-Pr.-More-Ex. - 03. Treasure Finder

Oct 27th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.27 KB | None | 0 0
  1. """
  2. Text Processing - More Exercises
  3. Check your code: https://judge.softuni.bg/Contests/Practice/Index/1741#2
  4.  
  5. SUPyF2 Text-Pr.-More-Ex. - 03. Treasure Finder
  6.  
  7. Problem:
  8. Write a program that decrypts a message by a given key and gathers information about hidden treasure
  9. type and its coordinates. On the first line you will receive a key (sequence of numbers).
  10. On the next few lines until you receive "find" you will get lines of strings.
  11. You have to loop through every string and decrease the ascii code of each character with a corresponding
  12. number of the key sequence. The way you choose a key number from the sequence is just looping through it.
  13. If the length of the key sequence is less than the string sequence, you start looping from the beginning of the key.
  14. For more clarification see the example below.
  15. After decrypting the message you will get a type of treasure and its coordinates.
  16. The type will be between the symbol '&' and the coordinates will be between the symbols '<' and '>'.
  17. For each line print the type and the coordinates in format "Found {type} at {coordinates}".
  18.  
  19. Example:
  20. Input:
  21. 1 2 1 3
  22. ikegfp'jpne)bv=41P83X@
  23. ujfufKt)Tkmyft'duEprsfjqbvfv=53V55XA
  24. find
  25.  
  26. Output:
  27. Found gold at 10N70W
  28. Found Silver at 32S43W
  29.  
  30. Comment:
  31. We start looping through the first string and the key.
  32. When we reach the end of the key we start looping from the beginning of the key,
  33. but we continue looping through the string. (until the string is over)
  34. The first message is: "hidden&gold&at<10N70W>" so we print we found gold at the given coordinates
  35. We do the same for the second string
  36. "thereIs&Silver&atCoordinates<32S43W>"(starting from the beginning of the key and the beginning of the string)
  37. """
  38. keys = [int(key) for key in input().split()]
  39. key = 0
  40. end = len(keys)
  41.  
  42. while True:
  43.     command = input()
  44.     if command == "find":
  45.         break
  46.     a = [letter for letter in command]
  47.     for i in range(len(a)):
  48.         if key == end:
  49.             key = 0
  50.         a[i] = chr(ord(a[i]) - keys[key])
  51.         key += 1
  52.     start_treasure = a.index("&") + 1
  53.     end_treasure = a.index("&", start_treasure)
  54.     treasure = ''.join(a[start_treasure:end_treasure])
  55.     coordinates = ''.join(a[a.index("<") + 1:a.index(">")])
  56.     print(f"Found {treasure} at {coordinates}")
  57.     key = 0
Add Comment
Please, Sign In to add comment