SimeonTs

SUPyF2 P.-Mid-Exam/2 November 2019/1. - Weaponsmith

Nov 5th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.06 KB | None | 0 0
  1. """
  2. Programming Fundamentals Mid Exam - 2 November 2019 Group 1
  3. Check your code: https://judge.softuni.bg/Contests/1859/Programming-Fundamentals-Mid-Exam-2-November-2019-Group-1
  4.  
  5. SUPyF2 P.-Mid-Exam/2 November 2019/1. - Weaponsmith
  6.  
  7. Problem:
  8. You are a legendary weaponsmith.
  9. Heroes from all over the world come to you for the greatest weapons so they can rid the world of the greatest threats.
  10. In order for you to craft a weapon you need to assemble its particles.
  11. You will receive a line with string particles, separated by "|", representing parts of the name of a weapon.
  12. The particles will be in mixed order and you can align them through the commands,
  13. which you will receive on the next lines, until you receive the "Done" command.
  14.  
  15. They will come the form of strings, separated by space. There are five supported commands:
  16. • "Move Left {index}":
  17. • Moves the value at {index} position to the Left, if the index exist and the move is possible.
  18. • If movement is not possible, do nothing.
  19. • "Move Right {index}":
  20. • Moves the value at {index} position to the Right, if the index exists and the move is possible.
  21. • If movement is not possible, do nothing.
  22. • "Check Even":
  23. • Print the elements at even index positions, separated by a single space.
  24. • "Check Odd":
  25. • Print the elements at odd index positions, separated by a single space.
  26. After the "Done" command, the weapon name is considered correct and you should print the particles in their current
  27.    order joined together in the following format: "You crafted {WeaponName}!"
  28.  
  29. Input:
  30. • On the first line, you will receive parts of the given weapon name in a mixed order separated by "|".
  31. • On the next lines, until the "Done" command, you will receive commands in the format described above.
  32.  
  33. Output;
  34. • Print the needed output upon the "Done" command as a string.
  35. • Output should be in the format "You crafted {Weapon name}!"
  36.  
  37. Examples:
  38.  
  39. Input:
  40. ha|Do|mm|om|er
  41. Move Right 0
  42. Move Left 3
  43. Check Odd
  44. Move Left 2
  45. Move Left 10
  46. Move Left 0
  47. Done
  48.  
  49. Output:
  50. ha mm
  51. You crafted Doomhammer!
  52.  
  53. Comments:
  54. First, we receive the "Move Right 0" command, so we move "ha" one position to the right and we get "Do ha mm om as er".
  55. Then we receive "Move Left 3" -> "Do ha om mm er"
  56. Checking the current state of the array on odd index positons.
  57. "Move Left 2" -> "Do om ha mm er"
  58. Invalid index, so we do nothing
  59. We can't move the value at zero position to the left, so we do nothing
  60. Lastly, we have to print the collection as one word, so our output is: "You crafted {Weapon name}!"
  61.  
  62. Input:
  63. ri|As|er|hb|ng
  64. Move Left 1
  65. Move Right 2
  66. Move Right 3
  67. Move Left 2
  68. Done
  69.  
  70. Output:
  71. You crafted Ashbringer!
  72. """
  73. particles = input().split("|")
  74.  
  75. while True:
  76.     command = input().split()
  77.     if command[0] == "Done":
  78.         print(f"You crafted {''.join(particles)}!")
  79.         break
  80.  
  81.     elif command[0] == "Move" and command[1] == "Left":
  82.         old_index_left = int(command[2])
  83.         if 0 < old_index_left < len(particles):
  84.             element_to_move_left = particles[old_index_left]
  85.             new_index_left = int(command[2]) - 1
  86.             particles.pop(old_index_left)
  87.             particles.insert(new_index_left, element_to_move_left)
  88.  
  89.     elif command[0] == "Move" and command[1] == "Right":
  90.         old_index_right = int(command[2])
  91.         if 0 <= old_index_right < len(particles) - 1:
  92.             element_to_move_right = particles[old_index_right]
  93.             new_index_right = int(command[2]) + 1
  94.             particles.pop(old_index_right)
  95.             particles.insert(new_index_right, element_to_move_right)
  96.  
  97.     elif command[0] == "Check" and command[1] == "Even":
  98.         even_elements = []
  99.         for index in range(len(particles)):
  100.             if index % 2 == 0:
  101.                 even_elements += [particles[index]]
  102.         print(*even_elements)
  103.  
  104.     elif command[0] == "Check" and command[1] == "Odd":
  105.         odd_elements = []
  106.         for index in range(len(particles)):
  107.             if index % 2 != 0:
  108.                 odd_elements += [particles[index]]
  109.         print(*odd_elements)
Add Comment
Please, Sign In to add comment