Advertisement
Spocoman

06. List Manipulator

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