Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- l = [int(i) for i in input().split(' ')]
- def even_odd(n: int, s: str):
- return n & 1 == (s == 'odd') # Return True if n is described properly by s (odd, even)
- def filter_eo(s: str):
- return [i for i in l if even_odd(i, s)] # return filtered list of only evens or odds
- def valid_i(i: int):
- return 0 <= i < len(l) # Returns true if index is in range of l boundaries
- def exch(i):
- l[:] = l[i:] + l[:i] # Exchange spliced l around i. Need this to change the main list
- def exchange(i: int):
- exch(i + 1) if valid_i(i) else print('Invalid index') # Exchange l around i + 1, or print error msg
- def max_eo(s: str):
- m = filter_eo(s) # Get filtered list
- print((len(l) - l[::-1].index(max(m)) - 1) if m else 'No matches') # I'm not sure : >
- def min_eo(s: str):
- m = filter_eo(s)
- print((len(l) - l[::-1].index(min(m)) - 1) if m else 'No matches') # A magic
- def first(a: list):
- print('Invalid count' if a[0] > len(l) else filter_eo(
- a[1])[:a[0]]) # Just get filtered list up to index, or err-msg
- def last(a: list):
- print('Invalid count' if a[0] > len(l) else filter_eo(a[1])[-a[0]:]) # Get filtered list backwards, or error msg
- commands = {'exchange': exchange, 'max': max_eo, 'min': min_eo, 'first': first, 'last': last}
- arguments = {'exchange': lambda x: int(x[0]), 'max': lambda x: x[0], 'min': lambda x: x[0],
- 'first': lambda x: [int(x[0]), x[1]], 'last': lambda x: [int(x[0]), x[1]]}
- comm = input()
- while comm != 'end':
- args = comm.split(' ')
- op = args.pop(0)
- commands[op](arguments[op](args))
- comm = input()
- print(l)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement