Advertisement
Osiris1002

List Manipulator

Apr 17th, 2024
775
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 KB | None | 0 0
  1. numbers = [int(i) for i in input().split()]
  2. command = input().split()
  3.  
  4. while command[0] != "end":
  5.     even = [i for i in numbers if i % 2 == 0]
  6.     odd = [i for i in numbers if i % 2 != 0]
  7.  
  8.     if command[0] == "exchange":
  9.         if 0 <= int(command[1]) < len(numbers):
  10.             numbers = numbers[int(command[1]) + 1:] + numbers[:int(command[1]) + 1]
  11.         else:
  12.             print(f'Invalid index')
  13.  
  14.     elif command[0] == "max":
  15.         if command[1] == "even" and even:
  16.             print((len(numbers) - numbers[::-1].index(max(even)) - 1))
  17.         elif command[1] == "odd" and odd:
  18.             print((len(numbers) - numbers[::-1].index(max(odd)) - 1))
  19.         else:
  20.             print('No matches')
  21.  
  22.     elif command[0] == "min":
  23.         if command[1] == "even" and even:
  24.             print((len(numbers) - numbers[::-1].index(min(even)) - 1))
  25.         elif command[1] == "odd" and odd:
  26.             print((len(numbers) - numbers[::-1].index(min(odd)) - 1))
  27.         else:
  28.             print('No matches')
  29.  
  30.     elif command[0] == "first":
  31.         if 0 < int(command[1]) <= len(numbers):
  32.             if command[2] == "even":
  33.                 print(even[0:int(command[1])])
  34.             else:
  35.                 print(odd[0:int(command[1])])
  36.         else:
  37.             print(f"Invalid count")
  38.  
  39.     elif command[0] == "last":
  40.         if 0 < int(command[1]) <= len(numbers):
  41.             if command[2] == "even":
  42.                 print(even[-int(command[1]):])
  43.             else:
  44.                 print(odd[-int(command[1]):])
  45.         else:
  46.             print(f"Invalid count")
  47.     command = input().split()
  48.  
  49. print(numbers)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement