Advertisement
Guest User

Untitled

a guest
May 22nd, 2020
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. entry_array = [int(i) for i in input().split(' ')]
  2.  
  3.  
  4. def exchange(array, index):
  5. return array[index + 1:] + array[:index + 1]
  6.  
  7.  
  8. def max_num(array, subreq):
  9. if subreq == 'even':
  10. max_elem = [int(i) for i in array if i % 2 == 0]
  11. if len(max_elem) == 0:
  12. return 'No matches'
  13. else:
  14. max_elem = max(max_elem)
  15. return len(array) - 1 - array[::-1].index(max_elem)
  16. elif subreq == 'odd':
  17. max_elem = [int(i) for i in array if i % 2 != 0]
  18. if len(max_elem) == 0:
  19. return 'No matches'
  20. else:
  21. max_elem = max(max_elem)
  22. return len(array) - 1 - array[::-1].index(max_elem)
  23.  
  24.  
  25. def min_num(array, subreq):
  26. if subreq == 'even':
  27. min_elem = [int(i) for i in array if i % 2 == 0]
  28. if len(min_elem) == 0:
  29. return 'No matches'
  30. else:
  31. min_elem = min(min_elem)
  32. return len(array) - 1 - array[::-1].index(min_elem)
  33. elif subreq == 'odd':
  34. min_elem = [int(i) for i in array if i % 2 != 0]
  35. if len(min_elem) == 0:
  36. return 'No matches'
  37. else:
  38. min_elem = min(min_elem)
  39. return len(array) - 1 - array[::-1].index(min_elem)
  40.  
  41.  
  42. def first_count(array, count, subreq):
  43. if len(array) < count:
  44. return 'Invalid count'
  45. else:
  46. new_arr = []
  47.  
  48. if subreq == "odd":
  49. for i in range(len(array)):
  50. if array[i] % 2 != 0 and count > 0:
  51. new_arr.append(array[i])
  52. count -= 1
  53. elif subreq == "even":
  54. for i in range(len(array)):
  55. if array[i] % 2 == 0 and count > 0:
  56. new_arr.append(array[i])
  57. count -= 1
  58.  
  59. return new_arr
  60.  
  61.  
  62. def last_count(array, count, subreq):
  63. if len(array) < count:
  64. return 'Invalid count'
  65. else:
  66. if subreq == 'even':
  67. all_nums = [i for i in array if i % 2 == 0]
  68. if len(all_nums) == 0:
  69. return []
  70. else:
  71. if len(all_nums) < count:
  72. return all_nums
  73. else:
  74. return all_nums[len(all_nums) - count:]
  75.  
  76. elif subreq == 'odd':
  77. all_nums = [i for i in array if i % 2 != 0]
  78. if len(all_nums) == 0:
  79. return []
  80. else:
  81. if len(all_nums) < count:
  82. return all_nums
  83. else:
  84. return all_nums[len(all_nums) - count:]
  85.  
  86.  
  87. def solution(array, command):
  88. global entry_array
  89. command_split = command.split(' ')
  90. if command_split[0] == 'exchange':
  91. split_index = int(command_split[1])
  92. if split_index < 0 or split_index >= len(array):
  93. print("Invalid index")
  94. else:
  95. entry_array = exchange(entry_array, split_index)
  96. elif command_split[0] == 'max':
  97. print(max_num(array, command_split[1]))
  98. elif command_split[0] == 'min':
  99. print(min_num(array, command_split[1]))
  100. elif command_split[0] == 'first':
  101. print(first_count(array, int(command_split[1]), command_split[2]))
  102. elif command_split[0] == 'last':
  103. print(last_count(array, int(command_split[1]), command_split[2]))
  104.  
  105.  
  106. while True:
  107. command = input()
  108. if command == 'end':
  109. print(entry_array)
  110. break
  111. else:
  112. solution(entry_array, command)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement