Advertisement
Guest User

Untitled

a guest
Feb 13th, 2020
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. import sys
  2.  
  3.  
  4. def read_number_list():
  5. input_arr = input().split()
  6. nums = []
  7.  
  8. for i in input_arr:
  9. nums.append(int(i))
  10. return nums
  11.  
  12.  
  13. def exchange(nums, index):
  14. new_list = []
  15. for i in range(index+1, len(nums)):
  16. new_list.append(nums[i])
  17. for j in range(index+1):
  18. new_list.append(nums[j])
  19.  
  20. nums = new_list
  21. return nums
  22.  
  23.  
  24. def min_number_index(nums, num_type):
  25. min_number = sys.maxsize
  26. min_num_index = -1
  27.  
  28. if num_type == "even":
  29. for i in range(len(nums)):
  30. num = nums[i]
  31. if num % 2 == 0 and num <= min_number:
  32. min_number = num
  33. min_num_index = i
  34. elif num_type == "odd":
  35. for i in range(len(nums)):
  36. num = nums[i]
  37. if num % 2 != 0 and num <= min_number:
  38. min_number = num
  39. min_num_index = i
  40. return min_num_index
  41.  
  42.  
  43. def max_number_index(nums, num_type):
  44. max_number = -sys.maxsize
  45. max_num_index = -1
  46. if num_type == "even":
  47. for i in range(len(nums)):
  48. num = nums[i]
  49. if num % 2 == 0 and num >= max_number:
  50. max_number = num
  51. max_num_index = i
  52. elif num_type == "odd":
  53. for i in range(len(nums)):
  54. num = nums[i]
  55. if num % 2 != 0 and num >= max_number:
  56. max_number = num
  57. max_num_index = i
  58. return max_num_index
  59.  
  60.  
  61. def first(nums, count, num_type):
  62. even_list = []
  63. odd_list = []
  64. result_even = []
  65. result_odd = []
  66.  
  67. if num_type == "even":
  68. for i in nums:
  69. if i % 2 == 0:
  70. even_list.append(i)
  71. if count > len(even_list):
  72. result_even = even_list
  73. print(result_even)
  74. else:
  75. for j in range(count):
  76. result_even.append(even_list[j])
  77. print(result_even)
  78.  
  79. elif num_type == "odd":
  80. for g in nums:
  81. if g % 2 != 0:
  82. odd_list.append(g)
  83. if count > len(odd_list):
  84. result_odd = odd_list
  85. print(result_odd)
  86. else:
  87. for k in range(count):
  88. result_odd.append(odd_list[k])
  89. print(result_odd)
  90.  
  91.  
  92. def last(nums, count, num_type):
  93. even_list = []
  94. odd_list = []
  95. result_even = []
  96. result_odd = []
  97. if num_type == "even":
  98. for i in nums:
  99. if i % 2 == 0:
  100. even_list.append(i)
  101. if count > len(even_list):
  102. result_even = even_list
  103. print(result_even)
  104. else:
  105. for j in range(len(even_list)-1, len(even_list)-count-1, -1):
  106. result_even.append(even_list[j])
  107. print(result_even)
  108.  
  109. elif num_type == "odd":
  110. for g in nums:
  111. if g % 2 != 0:
  112. odd_list.append(g)
  113. if count > len(odd_list):
  114. result_odd = odd_list
  115. print(result_odd)
  116. else:
  117. for k in range(len(odd_list) - 1, len(odd_list)-count-1, -1):
  118. result_odd.append(odd_list[k])
  119. print(result_odd)
  120.  
  121.  
  122. def solve():
  123. nums = read_number_list()
  124. command_input = input()
  125. while command_input != "end":
  126. arg = command_input.split()
  127. command = arg[0]
  128. if command == "exchange":
  129. index = int(arg[1])
  130.  
  131. if index > len(nums) or index < 0:
  132. print("Invalid index")
  133. command_input = input()
  134. continue
  135. nums = exchange(nums, index)
  136.  
  137. elif command == "max":
  138. num_type = arg[1]
  139. max_num_index = max_number_index(nums, num_type)
  140. if max_num_index >=0:
  141. print(max_num_index)
  142. command_input = input()
  143. continue
  144.  
  145. else:
  146. print("No matches")
  147. command_input = input()
  148. continue
  149.  
  150. elif command == "min":
  151. num_type = arg[1]
  152. min_num_index = min_number_index(nums, num_type)
  153. if min_num_index >= 0:
  154. print(min_num_index)
  155. command_input = input()
  156. continue
  157. else:
  158. print("No matches")
  159. command_input = input()
  160. continue
  161. elif command == "first":
  162. count = int(arg[1])
  163. num_type = arg[2]
  164. if count > int(len(nums)):
  165. print("Invalid count")
  166. command_input = input()
  167. continue
  168. else:
  169. first(nums, count, num_type)
  170.  
  171. elif command == "last":
  172. count = int(arg[1])
  173. num_type = arg[2]
  174. if count > int(len(nums)):
  175. print("Invalid count")
  176. command_input = input()
  177. continue
  178. else:
  179. last(nums, count, num_type)
  180.  
  181. command_input = input()
  182. print(nums)
  183.  
  184.  
  185. solve()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement