Advertisement
Guest User

Untitled

a guest
Nov 30th, 2024
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.69 KB | None | 0 0
  1. line_numbers = input().split()
  2. command_info = input()
  3. my_integers_list = []
  4. for item in line_numbers:
  5.     if item.isdigit():
  6.         integer_numbers = int(item)
  7.         my_integers_list.append(integer_numbers)
  8. while command_info != "end":
  9.     sub_list_first = []
  10.     sub_list_second = []
  11.     command = command_info.split()[0]
  12.     if command == "exchange":
  13.         index = command_info.split()[1]
  14.         index = int(index)
  15.         if index not in range(len(my_integers_list)):
  16.             print("Invalid index")
  17.         else:
  18.             for item in range(len(my_integers_list)):
  19.                 if item > index:
  20.                     sub_list_second.append(my_integers_list[item])
  21.                 else:
  22.                     sub_list_first.append(my_integers_list[item])
  23.             sub_list_second.extend(sub_list_first)
  24.             my_integers_list = sub_list_second
  25.     elif command == "max":
  26.         kind_number = command_info.split()[1]
  27.         if kind_number == "even":
  28.             even_numbers = [num for num in my_integers_list if num % 2 == 0]
  29.             if not even_numbers:
  30.                 print("No matches")
  31.             else:
  32.                 max_even = max(even_numbers)
  33.                 max_even_index = len(my_integers_list) - my_integers_list[::-1].index(max_even) - 1
  34.                 print(max_even_index)
  35.         elif kind_number == "odd":
  36.             odd_numbers = [num for num in my_integers_list if num % 2 != 0]
  37.             if not odd_numbers:
  38.                 print("No matches")
  39.             else:
  40.                 max_odd = max(odd_numbers)
  41.                 max_odd_index = len(my_integers_list) - my_integers_list[::-1].index(max_odd) - 1
  42.                 print(max_odd_index)
  43.     elif command == "min":
  44.         kind_number = command_info.split()[1]
  45.         if kind_number == "even":
  46.             even_numbers = [num for num in my_integers_list if num % 2 == 0]
  47.             if not even_numbers:
  48.                 print("No matches")
  49.             else:
  50.                 min_even = min(even_numbers)
  51.                 min_even_index = len(my_integers_list) - my_integers_list[::-1].index(min_even) - 1
  52.                 print(min_even_index)
  53.         elif kind_number == "odd":
  54.             odd_numbers = [num for num in my_integers_list if num % 2 != 0]
  55.             if not odd_numbers:
  56.                 print("No matches")
  57.             else:
  58.                 min_odd = min(odd_numbers)
  59.                 min_odd_index = len(my_integers_list) - my_integers_list[::-1].index(min_odd) - 1
  60.                 print(min_odd_index)
  61.     elif command == "first":
  62.         count = command_info.split()[1]
  63.         count = int(count)
  64.         kind_number = command_info.split()[2]
  65.         if kind_number == "even":
  66.             even_numbers = [num for num in my_integers_list if num % 2 == 0]
  67.             if count > len(my_integers_list):
  68.                 print("Invalid count")
  69.             elif not even_numbers:
  70.                 print("[]")
  71.             else:
  72.                 count = min(count, len(even_numbers))
  73.                 first_elements = [even_numbers[i] for i in range(count)]
  74.                 print(first_elements)
  75.         elif kind_number == "odd":
  76.             odd_numbers = [num for num in my_integers_list if num % 2 != 0]
  77.             if count > len(my_integers_list):
  78.                 print("Invalid count")
  79.             elif not odd_numbers:
  80.                 print("[]")
  81.             else:
  82.                 count = min(count, len(odd_numbers))
  83.                 first_elements = [odd_numbers[i] for i in range(count)]
  84.                 print(first_elements)
  85.     elif command == "last":
  86.         count = command_info.split()[1]
  87.         count = int(count)
  88.         kind_number = command_info.split()[2]
  89.         if kind_number == "even":
  90.             even_numbers = [num for num in my_integers_list if num % 2 == 0]
  91.             if count > len(my_integers_list):
  92.                 print("Invalid count")
  93.             elif not even_numbers:
  94.                 print("[]")
  95.             else:
  96.                 count = min(count, len(even_numbers))
  97.                 first_elements = [even_numbers[i] for i in range(len(even_numbers) - count, len(even_numbers))]
  98.                 print(first_elements)
  99.         elif kind_number == "odd":
  100.             odd_numbers = [num for num in my_integers_list if num % 2 != 0]
  101.             if count > len(my_integers_list):
  102.                 print("Invalid count")
  103.             elif not odd_numbers:
  104.                 print("[]")
  105.             else:
  106.                 count = min(count, len(odd_numbers))
  107.                 first_elements = [odd_numbers[i] for i in range(len(odd_numbers) - count, len(odd_numbers))]
  108.                 print(first_elements)
  109.     command_info = input()
  110. print(my_integers_list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement