Advertisement
simeonshopov

Array Manipulator

Jan 26th, 2020
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.64 KB | None | 0 0
  1. #!/usr/local/bin/python3.7
  2. # -*- coding: utf-8 -*import
  3.  
  4.  
  5. def check_even(c: list):
  6.     evens = []
  7.     for i in c:
  8.         if i % 2 == 0:
  9.             evens.append(i)
  10.     return evens
  11.  
  12.  
  13. def check_odd(d: list):
  14.     odds = []
  15.     for i in d:
  16.         if i % 2 != 0:
  17.             odds.append(i)
  18.     return odds
  19.  
  20.  
  21. def duplicate_val(f: list, g: int):
  22.     seen = set()
  23.     res = []
  24.     for i, j in enumerate(f):
  25.         if j == g and j not in seen:
  26.             seen.add(j)
  27.         else:
  28.             if j == g:
  29.                 res.append(i)
  30.     return max(res)
  31.  
  32.  
  33. def array_manipulator(a: str, b: str):
  34.     lst = [int(x) for x in a.split(' ')]
  35.     while b != 'end':
  36.         b = b.split(' ')
  37.         if 'exchange' in b:
  38.             idx = int(b[1])
  39.             if len(lst) - 1 < idx or idx < 0:
  40.                 print('Invalid index')
  41.             else:
  42.                 sub_lst_1 = lst[:idx + 1]
  43.                 sub_lst_2 = lst[idx + 1:]
  44.                 lst = sub_lst_2 + sub_lst_1
  45.         elif 'max' in b:
  46.             if 'even' in b:
  47.                 nums = check_even(lst)
  48.                 if nums:
  49.                     max_min = max(nums)
  50.                     if nums.count(max_min) < 2:
  51.                         idx = lst.index(max_min)
  52.                         print(idx)
  53.                     else:
  54.                         print(duplicate_val(lst, max_min))
  55.                 else:
  56.                     print('No matches')
  57.             elif 'odd' in b:
  58.                 nums = check_odd(lst)
  59.                 if nums:
  60.                     max_min = max(nums)
  61.                     if nums.count(max_min) < 2:
  62.                         idx = lst.index(max_min)
  63.                         print(idx)
  64.                     else:
  65.                         print(duplicate_val(lst, max_min))
  66.                 else:
  67.                     print('No matches')
  68.         elif 'min' in b:
  69.             if 'even' in b:
  70.                 nums = check_even(lst)
  71.                 if nums:
  72.                     max_min = min(nums)
  73.                     if nums.count(max_min) < 2:
  74.                         idx = lst.index(max_min)
  75.                         print(idx)
  76.                     else:
  77.                         print(duplicate_val(lst, max_min))
  78.                 else:
  79.                     print('No matches')
  80.             elif 'odd' in b:
  81.                 nums = check_odd(lst)
  82.                 if nums:
  83.                     max_min = min(nums)
  84.                     if nums.count(max_min) < 2:
  85.                         idx = lst.index(max_min)
  86.                         print(idx)
  87.                     else:
  88.                         print(duplicate_val(lst, max_min))
  89.                 else:
  90.                     print('No matches')
  91.         elif 'first' in b:
  92.             count = int(b[1])
  93.             if count > len(lst):
  94.                 print('Invalid count')
  95.             else:
  96.                 if 'even' in b:
  97.                     nums = check_even(lst)
  98.                     if not nums:
  99.                         print(f'{nums}')
  100.                     else:
  101.                         if len(nums) >= count:
  102.                             nums_found = nums[:count]
  103.                             print(nums_found)
  104.                         else:
  105.                             print(nums)
  106.                 elif 'odd' in b:
  107.                     nums = check_odd(lst)
  108.                     if not nums:
  109.                         print(f'{nums}')
  110.                     else:
  111.                         if len(nums) >= count:
  112.                             nums_found = nums[:count]
  113.                             print(nums_found)
  114.                         else:
  115.                             print(nums)
  116.         elif 'last' in b:
  117.             count = int(b[1])
  118.             if count > len(lst):
  119.                 print('Invalid count')
  120.             else:
  121.                 if 'even' in b:
  122.                     nums = check_even(lst)
  123.                     if not nums:
  124.                         print(f'{nums}')
  125.                     else:
  126.                         if len(nums) >= count:
  127.                             nums_found = nums[-count:]
  128.                             print(nums_found)
  129.                         else:
  130.                             print(nums)
  131.                 elif 'odd' in b:
  132.                     nums = check_odd(lst)
  133.                     if not nums:
  134.                         print(f'{nums}')
  135.                     else:
  136.                         if len(nums) >= count:
  137.                             nums_found = nums[-count:]
  138.                             print(nums_found)
  139.                         else:
  140.                             print(nums)
  141.         b = input()
  142.     else:
  143.         print(lst)
  144.  
  145.  
  146. array_manipulator(input(), input())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement