SimeonTs

SUPyF Exam 10.03.2019 - 02. Listmon says

Aug 13th, 2019
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.19 KB | None | 0 0
  1. """
  2. Basics OOP Principles
  3. Check your solution: https://judge.softuni.bg/Contests/Practice/Index/1511#1
  4.  
  5. SUPyF Exam 10.03.2019 - 02. Listmon says
  6.  
  7. Problem:
  8. Input / Constraints
  9. We are at the Listmon's playground. The collection defines its own rules and if we want to beat It in its own game,
  10. we must play by the rules .A game is pretty similar to 'Simon says'.
  11. So, It will tell us what to do and we should read very carefully what It wants from us.
  12. At the beginning of the game Listmon will give us an input with different elements separated by space (one or several).
  13. The elements will be numbers.
  14. After that It will start giving us commands in format:
  15. • set
  16. • filter {command} (where command will be either odd or even)
  17. • multiply {number}
  18. • divide {number}
  19. • slice {indexN} {indexM}
  20. • sort
  21. • reverse
  22.  
  23. *If you receive command 'set' you should check if the list is not already with unique elements, if this is so -
  24. print:
  25. • "It is a set"
  26. If it isn't, make it one and print it as a list.
  27. Keep the order of the elements exactly the same as Listmon gave it to you, just remove the non-unique elements.
  28. *If you receive ‘filter’ you should print only either odds elements, or even, depending on the command next to filter.
  29. If there are no elements in the list after filter command do not print the list.
  30. *If you receive 'multiply', multiply every element of the list by the given number.
  31. *If you receive ‘divide’ you must divide every element by the given number and print the list. If the number is 0, print
  32. • 'ZeroDivisionError caught'  And do not print the list.
  33. *If you receive ‘slice’ {indexN} {indexM} you should print the elements from n to m including without actually
  34. changing the list! Keep in mind that Listmon is tricky and can give you indexes which are not part of the list.
  35. In this case you should print:
  36. • 'IndexError caught '.
  37. And do not print the list.
  38. Index N always will be smaller number than M if the two indexes are valid
  39. *If you receive sort - print the elements in ascending order.
  40. *If you receive reverse – print all elements in reversed order.
  41.  
  42. ALWAYS keep in mind that the Listmon’s list never should be changed, otherwise we are going to lose the game.
  43.  
  44. NOTE:
  45. After each command you should print the result ONLY if there are elements in the list.
  46. If the list is empty do not print it!
  47. Output
  48. When you recieve a commant which says 'exhausted', you should print the count of roundes played in format:
  49. • "I beat It for {count} rounds!"
  50. where count is the number of commands you have recieved during the game.
  51.  
  52. Examples:
  53.  
  54. Input:
  55. 1 3 2 4 5
  56. set
  57. slice 1 5
  58. sort
  59. reverse
  60. exhausted
  61.  
  62. Output:
  63. It is a set
  64. IndexError caught
  65. [1, 2, 3, 4, 5]
  66. [5, 4, 2, 3, 1]
  67. I beat It for 4 rounds!
  68.  
  69. Input:
  70. 1 15 3 279 12
  71. filter odd
  72. multiply 7
  73. divide 0
  74. reverse
  75. exhausted
  76.  
  77. Output:
  78. [1, 15, 3, 279]
  79. [7, 105, 21, 1953, 84]
  80. ZeroDivisionError caught
  81. [12, 279, 3, 15, 1]
  82. I beat It for 4 rounds!
  83. """
  84.  
  85. nums = [int(item) for item in input().split(" ") if item != ""]
  86. count_rounds = 0
  87. while True:
  88.     command = input()
  89.     if command == "exhausted":
  90.         break
  91.  
  92.     elif command == "set":
  93.         count_rounds += 1
  94.         unique = len(set(nums)) == len(nums)
  95.         if unique:
  96.             print("It is a set")
  97.         else:
  98.             unique_leftovers = []
  99.             for num in nums:
  100.                 if num not in unique_leftovers:
  101.                     unique_leftovers += [int(num)]
  102.             print(unique_leftovers)
  103.  
  104.     elif "filter" in command:
  105.         count_rounds += 1
  106.         if "even" in command:
  107.             even = [item for item in nums if item % 2 == 0]
  108.             if even:
  109.                 print(even)
  110.         elif "odd" in command:
  111.             odd = [item for item in nums if item % 2 != 0]
  112.             if odd:
  113.                 print(odd)
  114.  
  115.     elif "multiply" in command:
  116.         count_rounds += 1
  117.         split_the_command = [item for item in command.split(" ")]
  118.         multiply_by = int(split_the_command[1])
  119.         multiplied_nums = [int(num) * multiply_by for num in nums]
  120.         print(multiplied_nums)
  121.  
  122.     elif "divide" in command:
  123.         count_rounds += 1
  124.         split_the_command = [item for item in command.split(" ")]
  125.         divide_by = int(split_the_command[1])
  126.         if divide_by == 0:
  127.             print("ZeroDivisionError caught")
  128.         else:
  129.             divided_nums = [int(num) / divide_by for num in nums]
  130.             print(divided_nums)
  131.  
  132.     elif "slice" in command:
  133.         count_rounds += 1
  134.         split_the_command = [item for item in command.split(" ")]
  135.         start_index = int(split_the_command[1])
  136.         end_index = int(split_the_command[2])
  137.         if start_index < 0 or start_index > len(nums) - 1:
  138.             print("IndexError caught")
  139.         elif end_index > len(nums) - 1 or end_index < 0:
  140.             print("IndexError caught")
  141.         else:
  142.             print(nums[start_index:end_index + 1])
  143.  
  144.     elif command == "sort":
  145.         count_rounds += 1
  146.         print(sorted(nums))
  147.  
  148.     elif command == "reverse":
  149.         count_rounds += 1
  150.         reversed_list = [int(item) for item in reversed(nums)]
  151.         print(reversed_list)
  152.  
  153. print(f"I beat It for {count_rounds} rounds!")
Add Comment
Please, Sign In to add comment