Advertisement
Kaloyankerr

Array manipulator

Jun 13th, 2020
1,065
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.61 KB | None | 0 0
  1. import sys
  2.  
  3.  
  4. def exchange(numbers, i):
  5.     first_part = numbers[:i + 1]
  6.     second_part = numbers[i + 1:]
  7.     return second_part + first_part
  8.  
  9.  
  10. def max_even_index(numbers):
  11.     max_number_even = -sys.maxsize
  12.     max_number_even_index = -1
  13.     for i in range(len(numbers)):
  14.         if numbers[i] % 2 == 0 and numbers[i] >= max_number_even:
  15.             max_number_even = numbers[i]
  16.             max_number_even_index = i
  17.  
  18.     if max_number_even_index == -1:
  19.         return 'No matches'
  20.     else:
  21.         return max_number_even_index
  22.  
  23.  
  24. def max_odd_index(numbers):
  25.     max_number_odd = -sys.maxsize
  26.     max_number_odd_index = -1
  27.     for i in range(len(numbers)):
  28.         if numbers[i] % 2 != 0 and numbers[i] >= max_number_odd:
  29.             max_number_odd = numbers[i]
  30.             max_number_odd_index = i
  31.  
  32.     if max_number_odd_index == -1:
  33.         return 'No matches'
  34.     else:
  35.         return max_number_odd_index
  36.  
  37.  
  38. def min_even_index(numbers):
  39.     min_number_even = sys.maxsize
  40.     max_number_even_index = -1
  41.     for i in range(len(numbers)):
  42.         if numbers[i] % 2 == 0 and numbers[i] <= min_number_even:
  43.             min_number_even = numbers[i]
  44.             max_number_even_index = i
  45.  
  46.     if max_number_even_index == -1:
  47.         return 'No matches'
  48.     else:
  49.         return max_number_even_index
  50.  
  51.  
  52. def min_odd_index(numbers):
  53.     min_number_odd = sys.maxsize
  54.     max_number_odd_index = -1
  55.     for i in range(len(numbers)):
  56.         if numbers[i] % 2 != 0 and numbers[i] <= min_number_odd:
  57.             min_number_odd = numbers[i]
  58.             max_number_odd_index = i
  59.  
  60.     if max_number_odd_index == -1:
  61.         return 'No matches'
  62.     else:
  63.         return max_number_odd_index
  64.  
  65.  
  66. def first_even(numbers, counter):
  67.     first_list_even = []
  68.     count = 0
  69.     for number in numbers:
  70.         if count == counter:
  71.             break
  72.         if number % 2 == 0:
  73.             first_list_even.append(number)
  74.             count += 1
  75.  
  76.     return first_list_even
  77.  
  78.  
  79. def first_odd(numbers, counter):
  80.     first_list_odd = []
  81.     count = 0
  82.     for number in numbers:
  83.         if count == counter:
  84.             break
  85.         if number % 2 != 0:
  86.             first_list_odd.append(number)
  87.             count += 1
  88.  
  89.     return first_list_odd
  90.  
  91.  
  92. def last_even(numbers, counter):
  93.     last_list_even = []
  94.     count = 0
  95.     for i in range(len(numbers) - 1, -1, -1):
  96.  
  97.         if count == counter:
  98.             break
  99.  
  100.         if numbers[i] % 2 == 0:
  101.             last_list_even.append(numbers[i])
  102.             count += 1
  103.  
  104.     return last_list_even
  105.  
  106.  
  107. def last_odd(numbers, counter):
  108.     last_list_odd = []
  109.     count = 0
  110.     for i in range(len(numbers) - 1, -1, -1):
  111.  
  112.         if count == counter:
  113.             break
  114.  
  115.         if numbers[i] % 2 != 0:
  116.             last_list_odd.append(numbers[i])
  117.             count += 1
  118.  
  119.     return last_list_odd
  120.  
  121.  
  122. numbers_list = input().split(' ')
  123. numbers_list = [int(x) for x in numbers_list]
  124.  
  125. command_input = input().split(' ')
  126. while command_input[0] != 'end':
  127.     command = command_input[0]
  128.  
  129.     if command == 'exchange':
  130.         index = int(command_input[1])
  131.         if len(numbers_list) >= index >= 0:
  132.             numbers_list = exchange(numbers_list, index)
  133.         else:
  134.             print('Invalid index')
  135.     elif command == 'max':
  136.         criteria = command_input[1]
  137.         if criteria == 'even':
  138.             res = max_even_index(numbers_list)
  139.             print(res)
  140.         elif criteria == 'odd':
  141.             res = max_odd_index(numbers_list)
  142.             print(res)
  143.     elif command == 'min':
  144.         criteria = command_input[1]
  145.         if criteria == 'even':
  146.             res = min_even_index(numbers_list)
  147.             print(res)
  148.         elif criteria == 'odd':
  149.             res = max_odd_index(numbers_list)
  150.             print(res)
  151.     elif command == 'first':
  152.         criteria = command_input[2]
  153.         index = int(command_input[1])
  154.         if len(numbers_list) >= index >= -1:
  155.             if criteria == 'even':
  156.                 print(first_even(numbers_list, index))
  157.             elif criteria == 'odd':
  158.                 print(first_odd(numbers_list, index))
  159.         else:
  160.             print('Invalid count')
  161.     elif command == 'last':
  162.         criteria = command_input[2]
  163.         index = int(command_input[1])
  164.         if len(numbers_list) >= index >= -1:
  165.             if criteria == 'even':
  166.                 print(last_even(numbers_list, index))
  167.             elif criteria == 'odd':
  168.                 print(last_odd(numbers_list, index))
  169.         else:
  170.             print('Invalid count')
  171.  
  172.     command_input = input().split(' ')
  173.  
  174. print(numbers_list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement