Advertisement
stanislavvvasilev

Array Manioulator

Feb 5th, 2021
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.76 KB | None | 0 0
  1. def exchange_func(array, exch_index):
  2.     if exch_index >= len(array):
  3.         print("Invalid index")
  4.     else:
  5.         first_slice = array[:exch_index + 1]
  6.         second_slice = array[exch_index + 1:]
  7.         array = second_slice + first_slice
  8.     return array
  9.  
  10.  
  11. def max_func(array, odd_or_even):
  12.     even_elements = []
  13.     odd_elements = []
  14.     for element in array:
  15.         if element % 2 == 0:
  16.             even_elements.append(element)
  17.         else:
  18.             odd_elements.append(element)
  19.     if odd_or_even == "odd":
  20.         if len(odd_elements) < 1:
  21.             print("No matches")
  22.         else:
  23.             max_odd = max(odd_elements)
  24.             for i in range(len(array) - 1, -1, -1):
  25.                 if array[i] == max_odd:
  26.                     print(i)
  27.                     break
  28.     elif odd_or_even == "even":
  29.         if len(even_elements) < 1:
  30.             print("No matches")
  31.         else:
  32.             max_even = max(even_elements)
  33.             for i in range(len(array) - 1, -1, -1):
  34.                 if array[i] == max_even:
  35.                     print(i)
  36.                     break
  37.  
  38.  
  39. def min_func(array, odd_or_even):
  40.     even_elements = []
  41.     odd_elements = []
  42.     for element in array:
  43.         if element % 2 == 0:
  44.             even_elements.append(element)
  45.         else:
  46.             odd_elements.append(element)
  47.     if odd_or_even == "odd":
  48.         if len(odd_elements) < 1:
  49.             print("No matches")
  50.         else:
  51.             min_odd = min(odd_elements)
  52.             for i in range(len(array) - 1, -1, -1):
  53.                 if array[i] == min_odd:
  54.                     print(i)
  55.                     break
  56.     elif odd_or_even == "even":
  57.         if len(even_elements) < 1:
  58.             print("No matches")
  59.         else:
  60.             min_even = min(even_elements)
  61.             for i in range(len(array) - 1, -1, -1):
  62.                 if array[i] == min_even:
  63.                     print(i)
  64.                     break
  65.  
  66.  
  67. def first_func(array, command_count, odd_or_even):
  68.     if command_count > len(array) or command_count < 0:
  69.         print("Invalid count")
  70.     else:
  71.         if odd_or_even == "even":
  72.             first_even = []
  73.             for element in array:
  74.                 if element % 2 == 0:
  75.                     first_even.append(element)
  76.                     if len(first_even) == command_count:
  77.                         break
  78.             print(first_even)
  79.         elif odd_or_even == "odd":
  80.             first_odd = []
  81.             for element in array:
  82.                 if element % 2 != 0:
  83.                     first_odd.append(element)
  84.                     if len(first_odd) == command_count:
  85.                         break
  86.             print(first_odd)
  87.  
  88.  
  89. def last_func(array, command_count, odd_or_even):
  90.     if command_count > len(array) or command_count < 0:
  91.         print("Invalid count")
  92.     else:
  93.         if odd_or_even == "even":
  94.             all_even = []
  95.             last_even = []
  96.             for element in array:
  97.                 if element % 2 == 0:
  98.                     all_even.append(element)
  99.             if len(all_even) > command_count:
  100.                 count = command_count
  101.                 for i in range(1, command_count + 1):
  102.                     last_even.append(all_even[-count])
  103.                     count -= 1
  104.             else:
  105.                 last_even = all_even
  106.             print(last_even)
  107.         elif odd_or_even == "odd":
  108.             all_odd = []
  109.             last_odd = []
  110.             for element in array:
  111.                 if element % 2 != 0:
  112.                     all_odd.append(element)
  113.             if len(all_odd) > command_count:
  114.                 count = command_count
  115.                 for i in range(1, command_count + 1):
  116.                     last_odd.append(all_odd[-count])
  117.                     count -= 1
  118.             else:
  119.                 last_odd = all_odd
  120.             print(last_odd)
  121.  
  122.  
  123. array = [int(num) for num in input().split(" ")]
  124. command = input()
  125.  
  126. while command != "end":
  127.     command_list = command.split(" ")
  128.     command_action = command_list[0]
  129.     if command_action == "exchange":
  130.         exch_index = int(command_list[1])
  131.         array = exchange_func(array, exch_index)
  132.     elif command_action == "max":
  133.         odd_or_even = command_list[1]
  134.         max_func(array, odd_or_even)
  135.     elif command_action == "min":
  136.         odd_or_even = command_list[1]
  137.         min_func(array, odd_or_even)
  138.     elif command_action == "first":
  139.         command_count = int(command_list[1])
  140.         odd_or_even = command_list[2]
  141.         first_func(array, command_count, odd_or_even)
  142.     elif command_action == "last":
  143.         command_count = int(command_list[1])
  144.         odd_or_even = command_list[2]
  145.         last_func(array, command_count, odd_or_even)
  146.     command = input()
  147.  
  148. print(array)
  149.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement