Advertisement
Guest User

Untitled

a guest
May 17th, 2020
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. def exchange(array, index):
  2. current_list1 = []
  3. current_list2 = []
  4. current_list = []
  5.  
  6. if int(index) < 0 or int(index) > (len(array) - 1):
  7. print("Invalid index")
  8. return array
  9. elif int(index) < (len(array) - 1):
  10. current_list1 = array[int(index) + 1:]
  11. current_list2 = array[:int(index) + 1]
  12. current_list = current_list1 + current_list2
  13. return current_list
  14. elif int(index) == (len(array) - 1):
  15. current_list = array
  16. return current_list
  17.  
  18.  
  19. def max_min_even_odd(array, number_type, type_):
  20. list_elements = []
  21. max_number = 0
  22. min_number = 1001
  23. is_valid = False
  24. for index, ele in enumerate(array, 0):
  25. if number_type == 'even':
  26. if type_ == "max":
  27. if int(ele) % 2 == 0 and max_number <= int(ele):
  28. max_number = int(ele)
  29. list_elements.append(int(index))
  30. is_valid = True
  31. elif type_ == "min":
  32. if int(ele) % 2 == 0 and min_number >= int(ele):
  33. min_number = int(ele)
  34. list_elements.append(int(index))
  35. is_valid = True
  36. elif number_type == 'odd':
  37. if type_ == "max":
  38. if int(ele) % 2 != 0 and max_number <= int(ele):
  39. max_number = int(ele)
  40. list_elements.append(int(index))
  41. is_valid = True
  42. elif type_ == "min":
  43. if int(ele) % 2 != 0 and min_number >= int(ele):
  44. min_number = int(ele)
  45. list_elements.append(int(index))
  46. is_valid = True
  47.  
  48. if not is_valid:
  49. return print('No matches')
  50. elif type_ == "max":
  51. max_ = max(list_elements)
  52. return print(max_)
  53. elif type_ == "min":
  54. min_ = max(list_elements)
  55. return print(min_)
  56.  
  57.  
  58. def first_last_even_odd(array, type_, count, num_type):
  59. element_list = []
  60. len_arr = len(array)
  61. if int(count) > len(array):
  62. return print('Invalid count')
  63.  
  64. for ele in array:
  65. if num_type == 'even':
  66. if type_ == "first":
  67. if int(ele) % 2 == 0 and len(element_list) <= int(count) - 1:
  68. element_list.append(int(ele))
  69. elif type_ == "last":
  70. if int(ele) % 2 == 0:
  71. element_list.append(int(ele))
  72. elif num_type == 'odd':
  73. if type_ == "first":
  74. if int(ele) % 2 != 0 and len(element_list) <= int(count) - 1:
  75. element_list.append(int(ele))
  76. elif type_ == "last":
  77. if int(ele) % 2 != 0:
  78. element_list.append(int(ele))
  79.  
  80. if type_ == 'last' and len(element_list) >= int(count):
  81. last_list = element_list[len(element_list) - int(count):]
  82. return print(last_list)
  83. else:
  84. return print(element_list)
  85.  
  86.  
  87. input_array = input().split()
  88. command = input()
  89. current_list = []
  90. while command != 'end':
  91. current_command = command.split()
  92. if current_command[0] == 'exchange':
  93. current_list = exchange(input_array, current_command[1])
  94. input_array = current_list
  95. elif current_command[0] in ['max', 'min']:
  96. max_min_even_odd(input_array, current_command[1], current_command[0])
  97. elif current_command[0] in ['first', 'last']:
  98. first_last_even_odd(input_array, current_command[0], current_command[1], current_command[2])
  99.  
  100. command = input()
  101.  
  102. final_list = [int(i) for i in current_list]
  103. print(final_list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement