Advertisement
GeorgiLukanov87

list_manipulator_2

Aug 10th, 2022 (edited)
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.54 KB | None | 0 0
  1. import sys
  2.  
  3.  
  4. def is_valid_index(collection: list, index: int):
  5.     if 0 <= index < len(collection):
  6.         return True
  7.  
  8.     return False
  9.  
  10.  
  11. def exchange_list_at_index(collection: list, index: int):
  12.     exchanged_list = []
  13.  
  14.     if not is_valid_index(collection, index):
  15.         print('Invalid index')
  16.         exchanged_list = collection
  17.     else:
  18.         left_sub_list = collection[:index + 1]
  19.         right_sub_list = collection[index + 1:]
  20.  
  21.         for el in right_sub_list:
  22.             exchanged_list.append(el)
  23.  
  24.         for el in left_sub_list:
  25.             exchanged_list.append(el)
  26.  
  27.     return exchanged_list
  28.  
  29.  
  30. def max_num_by_criteria(collection: list, custom_filter):
  31.     # Returns the index of max even/odd element by given filter function.
  32.     # It returns -1 if there is no match
  33.     max_element = float('-inf')
  34.     max_element_index = -1
  35.  
  36.     for i in range(len(collection)):
  37.         num = collection[i]
  38.  
  39.         if custom_filter(num) and num >= max_element:
  40.             max_element = num
  41.             max_element_index = i
  42.  
  43.     if max_element_index == -1:
  44.         # No matches found
  45.         print('No matches')
  46.  
  47.     return max_element_index
  48.  
  49.  
  50. def min_num_by_criteria(collection: list, custom_filter):
  51.     min_element = sys.maxsize
  52.     min_element_index = -1
  53.  
  54.     for i in range(len(collection)):
  55.         num = collection[i]
  56.  
  57.         if custom_filter(num) and num <= min_element:
  58.             min_element = num
  59.             min_element_index = i
  60.  
  61.     if min_element_index == -1:
  62.         print('No matches')
  63.  
  64.     return min_element_index
  65.  
  66.  
  67. def first_count_elements_by_criteria(collection: list, count: int, custom_filter):
  68.     if count > len(collection):
  69.         print('Invalid count')
  70.     else:
  71.         matching_elements = []
  72.  
  73.         for num in collection:
  74.             if custom_filter(num) and len(matching_elements) < count:
  75.                 matching_elements.append(num)
  76.  
  77.         print(matching_elements)
  78.  
  79.  
  80. def last_count_elements_by_criteria(collection: list, count: int, custom_filter):
  81.     if count > len(collection):
  82.         print('Invalid count')
  83.     else:
  84.         matching_elements = []
  85.  
  86.         for i in range(len(collection) - 1, -1, -1):
  87.             num = collection[i]
  88.  
  89.             if custom_filter(num) and len(matching_elements) < count:
  90.                 matching_elements.append(num)
  91.  
  92.         print(matching_elements[::-1])
  93.  
  94.  
  95. def parse_collection_to_int(collection: list):
  96.     parsed_collection = []
  97.  
  98.     for el in collection:
  99.         parsed_collection.append(int(el))
  100.  
  101.     return parsed_collection
  102.  
  103.  
  104. def stringify_collection(collection: list, delimiter: str):
  105.     parsed_collection = []
  106.  
  107.     for num in collection:
  108.         parsed_collection.append(str(num))
  109.  
  110.     return delimiter.join(parsed_collection)
  111.  
  112.  
  113. elements = input().split()
  114. numbers = parse_collection_to_int(elements)
  115.  
  116. command = input()
  117. while command != "end":
  118.     cmd_args = command.split()
  119.     cmd_type = cmd_args[0]
  120.  
  121.     if cmd_type == "exchange":
  122.         index = int(cmd_args[1])
  123.  
  124.         numbers = exchange_list_at_index(numbers, index)
  125.     elif cmd_type == "max":
  126.         cmd_filter = cmd_args[1]
  127.  
  128.         max_index = -1
  129.  
  130.         if cmd_filter == "even":
  131.             max_index = max_num_by_criteria(numbers, lambda n: n % 2 == 0)
  132.         elif cmd_filter == "odd":
  133.             max_index = max_num_by_criteria(numbers, lambda n: n % 2 != 0)
  134.  
  135.         if max_index != -1:
  136.             print(max_index)
  137.     elif cmd_type == "min":
  138.         cmd_filter = cmd_args[1]
  139.  
  140.         min_index = -1
  141.  
  142.         if cmd_filter == "even":
  143.             min_index = min_num_by_criteria(numbers, lambda n: n % 2 == 0)
  144.         elif cmd_filter == "odd":
  145.             min_index = min_num_by_criteria(numbers, lambda n: n % 2 != 0)
  146.  
  147.         if min_index != -1:
  148.             print(min_index)
  149.     elif cmd_type == "first":
  150.         count = int(cmd_args[1])
  151.         cmd_filter = cmd_args[2]
  152.  
  153.         if cmd_filter == "even":
  154.             first_count_elements_by_criteria(numbers, count, lambda n: n % 2 == 0)
  155.         elif cmd_filter == "odd":
  156.             first_count_elements_by_criteria(numbers, count, lambda n: n % 2 != 0)
  157.     elif cmd_type == "last":
  158.         count = int(cmd_args[1])
  159.         cmd_filter = cmd_args[2]
  160.  
  161.         if cmd_filter == "even":
  162.             last_count_elements_by_criteria(numbers, count, lambda n: n % 2 == 0)
  163.         elif cmd_filter == "odd":
  164.             last_count_elements_by_criteria(numbers, count, lambda n: n % 2 != 0)
  165.  
  166.     command = input()
  167.  
  168. print(f'[{stringify_collection(numbers, ", ")}]')
  169.  
  170.  
  171.  
  172.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement