kostovhg

Untitled

Jun 23rd, 2021
684
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. l = [int(i) for i in input().split(' ')]
  2.  
  3.  
  4. def even_odd(n: int, s: str):
  5.     return n & 1 == (s == 'odd')  # Return True if n is described properly by s (odd, even)
  6.  
  7.  
  8. def filter_eo(s: str):
  9.     return [i for i in l if even_odd(i, s)]  # return filtered list of only evens or odds
  10.  
  11.  
  12. def valid_i(i: int):
  13.     return 0 <= i < len(l)  # Returns true if index is in range of l boundaries
  14.  
  15.  
  16. def exch(i):
  17.     l[:] = l[i:] + l[:i]  # Exchange spliced l around i. Need this to change the main list
  18.  
  19.  
  20. def exchange(i: int):
  21.     exch(i + 1) if valid_i(i) else print('Invalid index')  # Exchange l around i + 1, or print error msg
  22.  
  23.  
  24. def max_eo(s: str):
  25.     m = filter_eo(s)  # Get filtered list
  26.     print((len(l) - l[::-1].index(max(m)) - 1) if m else 'No matches')  # I'm not sure : >
  27.  
  28.  
  29. def min_eo(s: str):
  30.     m = filter_eo(s)
  31.     print((len(l) - l[::-1].index(min(m)) - 1) if m else 'No matches')  # A magic
  32.  
  33.  
  34. def first(a: list):
  35.     print('Invalid count' if a[0] > len(l) else filter_eo(
  36.         a[1])[:a[0]])  # Just get filtered list up to index, or err-msg
  37.  
  38.  
  39. def last(a: list):
  40.     print('Invalid count' if a[0] > len(l) else filter_eo(a[1])[-a[0]:])  # Get filtered list backwards, or error msg
  41.  
  42.  
  43. commands = {'exchange': exchange, 'max': max_eo, 'min': min_eo, 'first': first, 'last': last}
  44. arguments = {'exchange': lambda x: int(x[0]), 'max': lambda x: x[0], 'min': lambda x: x[0],
  45.              'first': lambda x: [int(x[0]), x[1]], 'last': lambda x: [int(x[0]), x[1]]}
  46. comm = input()
  47. while comm != 'end':
  48.     args = comm.split(' ')
  49.     op = args.pop(0)
  50.     commands[op](arguments[op](args))
  51.     comm = input()
  52.  
  53. print(l)
  54.  
RAW Paste Data