Advertisement
JOHNYTHEWINNER

Array Manipulations

Jun 15th, 2020
1,172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.66 KB | None | 0 0
  1. import sys
  2.  
  3.  
  4. def calculation(calculation, list_of_numbers):  # Calculation while command is not "end"
  5.     type_of_number = command[1]
  6.     if command[0] == "exchange":
  7.         exchange(command, list_of_numbers)
  8.     elif command[0] == "max":
  9.         print(max(command, list_of_numbers))
  10.     elif command[0] == "min":
  11.         print(min(command, list_of_numbers))
  12.     elif command[0] == "first":
  13.         print(first(command, list_of_numbers))
  14.     elif command[0] == "last":
  15.         print(last(command, list_of_numbers))
  16.  
  17.  
  18. def exchange(command, list_of_numbers):  # Exchanging places of the elements
  19.     type_of_number = int(command[1])
  20.  
  21.     if type_of_number > (len(list_of_numbers) - 1) or type_of_number < 0:
  22.         print("Invalid index")
  23.     else:
  24.         for i in range(len(list_of_numbers) - 1, type_of_number, -1):
  25.             list_of_numbers.insert(0, list_of_numbers.pop(-1))
  26.     return list_of_numbers
  27.  
  28.  
  29. def max(command, list_of_numbers):  # Finding max even/odd number in the list
  30.     max_odd_element = -sys.maxsize
  31.     max_even_element = -sys.maxsize
  32.     found_max_even_at_index = 0
  33.     found_max_odd_at_index = 0
  34.     max_even_IsFound = False
  35.     max_odd_IsFound = False
  36.     noMatches = "No matches"
  37.     type_of_number = command[1]
  38.  
  39.     for i in range(len(list_of_numbers) - 1, -1, -1):
  40.         if list_of_numbers[i] % 2 != 0:
  41.             if max_odd_element < list_of_numbers[i]:
  42.                 max_odd_element = list_of_numbers[i]
  43.                 found_max_odd_at_index = i
  44.                 max_odd_IsFound = True
  45.         else:
  46.             if max_even_element < list_of_numbers[i]:
  47.                 max_even_element = list_of_numbers[i]
  48.                 found_max_even_at_index = i
  49.                 max_even_IsFound = True
  50.     if type_of_number == "even":
  51.         if max_even_IsFound:
  52.             return found_max_even_at_index
  53.         else:
  54.             return noMatches
  55.     elif type_of_number == "odd":
  56.         if max_odd_IsFound:
  57.             return found_max_odd_at_index
  58.         else:
  59.             return noMatches
  60.  
  61.  
  62. def min(command, list_of_numbers):  # Finding min even/odd number in the list
  63.     min_odd_element = sys.maxsize
  64.     min_even_element = sys.maxsize
  65.     found_min_even_at_index = 0
  66.     found_min_odd_at_index = 0
  67.     min_even_IsFound = False
  68.     min_odd_IsFound = False
  69.     noMatches = "No matches"
  70.     type_of_number = command[1]
  71.  
  72.     for i in range(len(list_of_numbers) - 1, -1, -1):
  73.         if list_of_numbers[i] % 2 != 0:
  74.             if min_odd_element > list_of_numbers[i]:
  75.                 min_odd_element = list_of_numbers[i]
  76.                 found_min_odd_at_index = i
  77.                 min_odd_IsFound = True
  78.         else:
  79.             if min_even_element > list_of_numbers[i]:
  80.                 min_even_element = list_of_numbers[i]
  81.                 found_min_even_at_index = i
  82.                 min_even_IsFound = True
  83.     if type_of_number == "even":
  84.         if min_even_IsFound:
  85.             return found_min_even_at_index
  86.         else:
  87.             return noMatches
  88.     elif type_of_number == "odd":
  89.         if min_odd_IsFound:
  90.             return found_min_odd_at_index
  91.         else:
  92.             return noMatches
  93.  
  94.  
  95. def first(command, list_of_numbers):  # Finding first even/odd numbers in the list
  96.     invalidCount = "Invalid count"
  97.     invalidCountFounded = False
  98.     first_list = []
  99.     last_list = []
  100.     counter_for_first_and_last_list = 0
  101.  
  102.     if int(command[1]) > len(list_of_numbers) or int(command[1]) < 0:
  103.         invalidCountFounded = True
  104.         return invalidCount
  105.     else:
  106.         if command[2] == "even":
  107.             for i in range(0, len(list_of_numbers)):
  108.                 if counter_for_first_and_last_list == int(command[1]):
  109.                     break
  110.                 if list_of_numbers[i] % 2 == 0:
  111.                     first_list.append(list_of_numbers[i])
  112.                     counter_for_first_and_last_list += 1
  113.         elif command[2] == "odd":
  114.             for i in range(0, len(list_of_numbers)):
  115.                 if counter_for_first_and_last_list == int(command[1]):
  116.                     break
  117.                 if list_of_numbers[i] % 2 != 0:
  118.                     first_list.append(list_of_numbers[i])
  119.                     counter_for_first_and_last_list += 1
  120.         if not invalidCountFounded:
  121.             return first_list
  122.  
  123.  
  124. def last(command, list_of_numbers):  # Finding last even/odd numbers in the list
  125.     invalidCount = "Invalid count"
  126.     invalidCountFounded = False
  127.     first_list = []
  128.     last_list = []
  129.     counter_for_first_and_last_list = 0
  130.     if int(command[1]) > len(list_of_numbers) or int(command[1]) < 0:
  131.         invalidCountFounded = True
  132.         return invalidCount
  133.     else:
  134.         if command[2] == "even":
  135.             for i in range(len(list_of_numbers) - 1, -1, -1):
  136.                 if counter_for_first_and_last_list == int(command[1]):
  137.                     break
  138.                 if list_of_numbers[i] % 2 == 0:
  139.                     last_list.append(list_of_numbers[i])
  140.                     counter_for_first_and_last_list += 1
  141.         elif command[2] == "odd":
  142.             for i in range(len(list_of_numbers) - 1, -1, -1):
  143.                 if counter_for_first_and_last_list == int(command[1]):
  144.                     break
  145.                 if list_of_numbers[i] % 2 != 0:
  146.                     last_list.append(list_of_numbers[i])
  147.                     counter_for_first_and_last_list += 1
  148.         if not invalidCountFounded:
  149.             return last_list
  150.  
  151.  
  152. list_of_numbers = input().split(" ")
  153. list_of_numbers = [int(i) for i in list_of_numbers]
  154. command = input().split(" ")
  155. while command[0] != "end":
  156.     calculation(command, list_of_numbers)
  157.     command = input().split(" ")
  158. print(list_of_numbers)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement