SimeonTs

SUPyF2 Functions-Exercise - 10. Array Manipulator

Oct 8th, 2019
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.46 KB | None | 0 0
  1. """
  2. Functions - Exercise
  3. Check your code: https://judge.softuni.bg/Contests/Practice/Index/1728#9
  4.  
  5. SUPyF2 Functions-Exercise - 10. Array Manipulator (not included in final score)
  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. import sys
  91. the_list = []
  92.  
  93.  
  94. def exchange(command):
  95.     global the_list
  96.     index_to_exchange = int(command[1])
  97.     if len(the_list) > index_to_exchange >= 0:
  98.         the_list = the_list[index_to_exchange + 1:] + the_list[:index_to_exchange + 1]
  99.     else:
  100.         print("Invalid index")
  101.  
  102.  
  103. def max_num(command):
  104.     global the_list
  105.     max_number = -sys.maxsize - 1
  106.     index_of_number = -1
  107.     if command[1] == "even":
  108.         for number in range(len(the_list)):
  109.             if the_list[number] % 2 == 0 and the_list[number] >= max_number:
  110.                 max_number = the_list[number]
  111.                 index_of_number = number
  112.     elif command[1] == "odd":
  113.         for number in range(len(the_list)):
  114.             if the_list[number] % 2 != 0 and the_list[number] >= max_number:
  115.                 max_number = the_list[number]
  116.                 index_of_number = number
  117.     if index_of_number == -1:
  118.         print("no matches")
  119.     else:
  120.         print(index_of_number)
  121.  
  122.  
  123. def min_num(command):
  124.     global the_list
  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.  
  143. def first(command):
  144.     global the_list
  145.     its_ok = True
  146.     count_numbers = int(command[1])
  147.     if count_numbers > len(the_list):
  148.         print("Invalid count")
  149.         its_ok = False
  150.     if command[2] == "even" and its_ok:
  151.         print([item for item in the_list if item % 2 == 0][:count_numbers])
  152.     elif command[2] == "odd" and its_ok:
  153.         print([item for item in the_list if item % 2 != 0][:count_numbers])
  154.  
  155.  
  156. def last(command):
  157.     global the_list
  158.     its_ok = True
  159.     count_numbers = int(command[1])
  160.     if count_numbers > len(the_list):
  161.         print("Invalid count")
  162.         its_ok = False
  163.     if command[2] == "even" and its_ok:
  164.         print([item for item in the_list if item % 2 == 0][-count_numbers:])
  165.     elif command[2] == "odd" and its_ok:
  166.         print([item for item in the_list if item % 2 != 0][-count_numbers:])
  167.  
  168.  
  169. def array_manipulator():
  170.     global the_list
  171.     the_list = [int(digit) for digit in input().split()]
  172.     while True:
  173.         command_ = input().split()
  174.         if command_[0] == "end":
  175.             break
  176.         if command_[0] == "exchange":
  177.             exchange(command_)
  178.         elif command_[0] == "max":
  179.             max_num(command_)
  180.         elif command_[0] == "min":
  181.             min_num(command_)
  182.         elif command_[0] == "first":
  183.             first(command_)
  184.         elif command_[0] == "last":
  185.             last(command_)
  186.     print(the_list)
  187.  
  188.  
  189. if __name__ == '__main__':
  190.     array_manipulator()
Add Comment
Please, Sign In to add comment