viligen

list_manipulator

Oct 6th, 2021 (edited)
851
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.41 KB | None | 0 0
  1. list_numbers_ = input().split()
  2. list_numbers = list(map(int, list_numbers_))
  3.  
  4. evens = []
  5. odds = []
  6. current_list = []
  7. while True:
  8.     command = input()
  9.     if command == "end":
  10.         break
  11.     command_split = command.split()
  12.  
  13.     if command_split[0] == "exchange" and 0 <= int(command_split[1]) < len(list_numbers):
  14.         index = int(command_split[1])
  15.         current_list = list_numbers[index+1:] + list_numbers[:index+1]
  16.  
  17.         list_numbers = current_list[:]
  18.     elif command_split[0] == "exchange":
  19.         print("Invalid index")
  20.     elif command_split[0] == "max" and command_split[1] == "even":
  21.         for i in range(len(list_numbers) - 1, -1, -1):
  22.             if int(list_numbers[i]) % 2 == 0:
  23.                 evens.append(list_numbers[i])
  24.         if len(evens) > 0:
  25.             list_index = [i for i, number in enumerate(list_numbers) if number == max(evens)]
  26.             index_even = max(list_index)
  27.             print(index_even)
  28.             evens = []
  29.         else:
  30.             print("No matches")
  31.     elif command_split[0] == "max" and command_split[1] == "odd":
  32.         for i in range(len(list_numbers) - 1, -1, -1):
  33.             if int(list_numbers[i]) % 2 != 0:
  34.                 odds.append(list_numbers[i])
  35.         if len(odds) > 0:
  36.             list_index = [i for i, number in enumerate(list_numbers) if number == max(odds)]
  37.             index_odds = max(list_index)
  38.             print(index_odds)
  39.             odds = []
  40.         else:
  41.             print("No matches")
  42.     elif command_split[0] == "min" and command_split[1] == "even":
  43.         for i in range(len(list_numbers) - 1, -1, -1):
  44.             if int(list_numbers[i]) % 2 == 0:
  45.                 evens.append(list_numbers[i])
  46.         if len(evens) > 0:
  47.             list_index = [i for i, number in enumerate(list_numbers) if number == min(evens)]
  48.             index_even = max(list_index)
  49.  
  50.             print(index_even)
  51.             evens = []
  52.         else:
  53.             print("No matches")
  54.     elif command_split[0] == "min" and command_split[1] == "odd":
  55.         for i in range(len(list_numbers) - 1, -1, -1):
  56.             if int(list_numbers[i]) % 2 != 0:
  57.                 odds.append(list_numbers[i])
  58.         if len(odds) > 0:
  59.             list_index = [i for i, number in enumerate(list_numbers) if number == min(odds)]
  60.             index_odds = max(list_index)
  61.  
  62.             print(index_odds)
  63.             odds = []
  64.         else:
  65.             print("No matches")
  66.     elif command_split[0] == "first" and command_split[2] == "even":
  67.         if int(command_split[1]) > len(list_numbers):
  68.             print("Invalid count")
  69.         else:
  70.             for i in range(len(list_numbers)):
  71.                 if int(list_numbers[i]) % 2 == 0:
  72.                     evens.append(list_numbers[i])
  73.                 if len(evens) == int(command_split[1]):
  74.                     break
  75.             print([int(digit) for digit in evens])
  76.             evens = []
  77.  
  78.     elif command_split[0] == "first" and command_split[2] == "odd":
  79.         if int(command_split[1]) > len(list_numbers):
  80.             print("Invalid count")
  81.         else:
  82.             for i in range(len(list_numbers)):
  83.                 if int(list_numbers[i]) % 2 != 0:
  84.                     odds.append(list_numbers[i])
  85.                 if len(odds) == int(command_split[1]):
  86.                     break
  87.             print([int(digit) for digit in odds])
  88.             odds = []
  89.  
  90.     elif command_split[0] == "last" and command_split[2] == "even":
  91.         if int(command_split[1]) > len(list_numbers):
  92.             print("Invalid count")
  93.         else:
  94.             for i in range(len(list_numbers) - 1, -1, -1):
  95.                 if int(list_numbers[i]) % 2 == 0:
  96.                     evens.append(list_numbers[i])
  97.                 if len(evens) == int(command_split[1]):
  98.                     break
  99.             print([int(digit) for digit in reversed(evens)])
  100.             evens = []
  101.  
  102.     elif command_split[0] == "last" and command_split[2] == "odd":
  103.         if int(command_split[1]) > len(list_numbers):
  104.             print("Invalid count")
  105.         else:
  106.             for i in range(len(list_numbers)-1, -1, -1):
  107.                 if int(list_numbers[i]) % 2 != 0:
  108.                     odds.append(list_numbers[i])
  109.                 if len(odds) == int(command_split[1]):
  110.                     break
  111.             print([int(digit) for digit in reversed(odds)])
  112.             odds = []
  113.  
  114. print([int(n) for n in list_numbers])
  115.  
Add Comment
Please, Sign In to add comment