Advertisement
SimeonTs

SUPyF Exam Preparation 1 - 02. Array Manipulator

Aug 16th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.02 KB | None | 0 0
  1. """
  2. Basics OOP Principles
  3. Check your solution: https://judge.softuni.bg/Contests/Practice/Index/967#1
  4.  
  5. SUPyF Exam Preparation 1 - 02. Array Manipulator
  6.  
  7. Problem:
  8. Trifon has finally become a junior developer and has received his first task.
  9. It’s about manipulating an array of integers. He is not quite happy about it, since he hates manipulating arrays.
  10. They are going to pay him a lot of money, though,
  11. and he is willing to give somebody half of it if to help him do his job.
  12. You, on the other hand, love arrays (and money) so you decide to try your luck.
  13. The array may be manipulated by one of the following commands
  14. • exchange {index} – splits the array after the given index, and exchanges the places of the two resulting sub-arrays.
  15. E.g. [1, 2, 3, 4, 5] -> exchange 2 -> result: [4, 5, 1, 2, 3]
  16. o   If the index is outside the boundaries of the array, print “Invalid index”
  17. • max even/odd– returns the INDEX of the max even/odd element -> [1, 4, 8, 2, 3] -> max odd -> print 4
  18. • min even/odd – returns the INDEX of the min even/odd element -> [1, 4, 8, 2, 3] -> min even > print 3
  19. o   If there are two or more equal min/max elements, return the index of the rightmost one
  20. o   If a min/max even/odd element cannot be found, print “No matches”
  21. • first {count} even/odd– returns the first {count} elements -> [1, 8, 2, 3] -> first 2 even -> print [8, 2]
  22. • last {count} even/odd – returns the last {count} elements -> [1, 8, 2, 3] -> last 2 odd -> print [1, 3]
  23. o   If the count is greater than the array length, print “Invalid count”
  24. o   If there are not enough elements to satisfy the count, print as many as you can.
  25. If there are zero even/odd elements, print an empty array “[]”
  26. • end – stop taking input and print the final state of the array
  27. Input
  28. • The input data should be read from the console.
  29. • On the first line, the initial array is received as a line of integers, separated by a single space
  30. • On the next lines, until the command “end” is received, you will receive the array manipulation commands
  31. • The input data will always be valid and in the format described. There is no need to check it explicitly.
  32. Output
  33. • The output should be printed on the console.
  34. • On a separate line, print the output of the corresponding command
  35. • On the last line, print the final array in square brackets with its elements separated by a comma and a space
  36. • See the examples below to get a better understanding of your task
  37. Constraints
  38. • The number of input lines will be in the range [2 … 50].
  39. • The array elements will be integers in the range [0 … 1000].
  40. • The number of elements will be in the range [1 .. 50]
  41. • The split index will be an integer in the range [-231 … 231 – 1]
  42. • first/last count will be an integer in the range [1 … 231 – 1]
  43. • There will not be redundant whitespace anywhere in the input
  44. • Allowed working time for your program: 0.1 seconds. Allowed memory: 16 MB.
  45.  
  46. Examples
  47. Input:
  48. 1 3 5 7 9
  49. exchange 1
  50. max odd
  51. min even
  52. first 2 odd
  53. last 2 even
  54. exchange 3
  55. end
  56. Output:
  57. 2
  58. No matches
  59. [5, 7]
  60. []
  61. [3, 5, 7, 9, 1]
  62.  
  63. Input:
  64. 2
  65. No matches
  66. [5, 7]
  67. []
  68. [3, 5, 7, 9, 1]
  69. Output:
  70. 3
  71. Invalid count
  72. Invalid index
  73. 0
  74. 2
  75. 0
  76. [10, 100, 1000, 1]
  77.  
  78. Input:
  79. 1 10 100 1000
  80. exchange 3
  81. first 2 odd
  82. last 4 odd
  83. end
  84.  
  85. Output:
  86. [1]
  87. [1]
  88. [1, 10, 100, 1000]
  89. """
  90.  
  91. import sys
  92. the_list = [int(item) for item in input().split()]
  93.  
  94. while True:
  95.     command = input().split()
  96.     if command[0] == "end":
  97.         break
  98.  
  99.     elif command[0] == "exchange":
  100.         index_to_exchange = int(command[1])
  101.         if len(the_list) > index_to_exchange >= 0:
  102.             the_list = the_list[index_to_exchange + 1:] + the_list[:index_to_exchange + 1]
  103.         else:
  104.             print("Invalid index")
  105.  
  106.     elif command[0] == "max":
  107.         max_number = -sys.maxsize - 1
  108.         index_of_number = -1
  109.         if command[1] == "even":
  110.             for number in range(len(the_list)):
  111.                 if the_list[number] % 2 == 0 and the_list[number] >= max_number:
  112.                     max_number = the_list[number]
  113.                     index_of_number = number
  114.         elif command[1] == "odd":
  115.             for number in range(len(the_list)):
  116.                 if the_list[number] % 2 != 0 and the_list[number] >= max_number:
  117.                     max_number = the_list[number]
  118.                     index_of_number = number
  119.         if index_of_number == -1:
  120.             print("no matches")
  121.         else:
  122.             print(index_of_number)
  123.  
  124.     elif command[0] == "min":
  125.         min_number = sys.maxsize
  126.         index_of_number = -1
  127.         if command[1] == "even":
  128.             for number in range(len(the_list)):
  129.                 if the_list[number] % 2 == 0 and the_list[number] <= min_number:
  130.                     min_number = the_list[number]
  131.                     index_of_number = number
  132.         elif command[1] == "odd":
  133.             for number in range(len(the_list)):
  134.                 if the_list[number] % 2 != 0 and the_list[number] <= min_number:
  135.                     min_number = the_list[number]
  136.                     index_of_number = number
  137.         if index_of_number == -1:
  138.             print("No matches")
  139.         else:
  140.             print(index_of_number)
  141.  
  142.     elif command[0] == "first":
  143.         count_numbers = int(command[1])
  144.         if count_numbers > len(the_list):
  145.             print("Invalid count")
  146.             continue
  147.         if command[2] == "even":
  148.             print([item for item in the_list if item % 2 == 0][:count_numbers])
  149.         elif command[2] == "odd":
  150.             print([item for item in the_list if item % 2 != 0][:count_numbers])
  151.  
  152.     elif command[0] == "last":
  153.         count_numbers = int(command[1])
  154.         if count_numbers > len(the_list):
  155.             print("Invalid count")
  156.             continue
  157.         if command[2] == "even":
  158.             print([item for item in the_list if item % 2 == 0][-count_numbers:])
  159.         elif command[2] == "odd":
  160.             print([item for item in the_list if item % 2 != 0][-count_numbers:])
  161.  
  162. print(the_list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement