SimeonTs

SUPyF2 P.-Mid-Exam/10 March 2019/2 - 03. The Final Quest

Oct 29th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.51 KB | None | 0 0
  1. """
  2. Technology Fundamentals Mid Exam - 10 March 2019 Group 2
  3. Check your code: https://judge.softuni.bg/Contests/Practice/Index/1555#2
  4.  
  5. SUPyF2 P.-Mid-Exam/10 March 2019/2 - 03. The Final Quest
  6.  
  7. Problem:
  8. After walking through fire, the group has reached the final step of the quest.
  9. They have received a list with instructions that will help them resolve the last riddle that will lead them to the truth
  10. about the Hunting Games.
  11. Create a program that follows given instructions.
  12. You will receive a collection of words on a single line, split by a single space.
  13. They are not what they are supposed to be, so you have to follow the instructions in order to find the real message.
  14. You will be receiving commands. Here are the possible ones:
  15. -   Delete {index} – removes the word after the given index if it is valid.
  16. -   Swap {word1} {word2} – find the given words in the collections if they exist and swap their places.
  17. -   Put {word} {index} – add a word at the previous place {index} before the
  18. given one, if it is valid. Note: putting at the last index simply appends the word to the end of the list.
  19. -   Sort – you must sort the words in descending order.
  20. -   Replace {word1} {word2} – find the second word {word2} in the collection
  21. (if it exists) and replace it with the first word – {word1}.
  22. Follow them until you receive the "Stop" command. After you have successfully followed the instructions,
  23. you must print the words on a single line, split by a space.
  24. Input / Constraints
  25. • On the 1st line, you are going to receive the collection of words, split by a single space – strings
  26. • On the next lines, you are going to receive commands, until you receive the "Stop" command
  27. Output
  28. • Print the words you have gathered on a single line, split by a single space
  29.  
  30. Examples:
  31. Input:
  32. Congratulations! You last also through the have challenge!
  33. Swap have last
  34. Replace made have
  35. Delete 2
  36. Put it 4
  37. Stop
  38.  
  39. Output:
  40. Congratulations! You made it through the last challenge!
  41.  
  42. Comments:
  43. First, we receive the command “Swap”, so we change the positions of the words have and last.
  44. The text at this point should look like this:
  45. Congratulations! You have also through the last challenge!
  46. After that, we receive “Replace” and we have to replace the second word – “have” with the first – “made”.
  47. Afterwards we have to delete the word, which is after the second index.
  48. And finally, we have to put a word on the previous position before 4.
  49.  
  50. Input:
  51. This the my quest! final
  52. Put is 2
  53. Swap final quest!
  54. Delete 2
  55. Stop
  56.  
  57. Output:
  58. This is the final quest!
  59. """
  60. words = input().split()
  61.  
  62. while True:
  63.     command = input().split()
  64.  
  65.     if command[0] == "Stop":
  66.         print(' '.join(words))
  67.         break
  68.  
  69.     elif command[0] == "Delete":
  70.         index = int(command[1]) + 1
  71.         if 0 <= index < len(words):
  72.             words.pop(index)
  73.  
  74.     elif command[0] == "Swap":
  75.         word_1, word_2 = command[1], command[2]
  76.         if word_1 in words and word_2 in words:
  77.             index_1 = words.index(word_1)
  78.             index_2 = words.index(word_2)
  79.             words[index_1] = word_2
  80.             words[index_2] = word_1
  81.  
  82.     elif command[0] == "Put":
  83.         word, index = command[1], int(command[2]) - 1
  84.         if 0 <= index <= len(words):
  85.             words.insert(index, word)
  86.  
  87.     elif command[0] == "Sort":
  88.         words.sort(reverse=True)
  89.  
  90.     elif command[0] == "Replace":
  91.         word_1, word_2 = command[1], command[2]
  92.         if word_2 in words:
  93.             words[words.index(word_2)] = word_1
Add Comment
Please, Sign In to add comment