Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- def read_number_list():
- input_arr = input().split()
- nums = []
- for i in input_arr:
- nums.append(int(i))
- return nums
- def exchange(nums, index):
- new_list = []
- for i in range(index+1, len(nums)):
- new_list.append(nums[i])
- for j in range(index+1):
- new_list.append(nums[j])
- nums = new_list
- return nums
- def min_number_index(nums, num_type):
- min_number = sys.maxsize
- min_num_index = -1
- if num_type == "even":
- for i in range(len(nums)):
- num = nums[i]
- if num % 2 == 0 and num <= min_number:
- min_number = num
- min_num_index = i
- elif num_type == "odd":
- for i in range(len(nums)):
- num = nums[i]
- if num % 2 != 0 and num <= min_number:
- min_number = num
- min_num_index = i
- return min_num_index
- def max_number_index(nums, num_type):
- max_number = -sys.maxsize
- max_num_index = -1
- if num_type == "even":
- for i in range(len(nums)):
- num = nums[i]
- if num % 2 == 0 and num >= max_number:
- max_number = num
- max_num_index = i
- elif num_type == "odd":
- for i in range(len(nums)):
- num = nums[i]
- if num % 2 != 0 and num >= max_number:
- max_number = num
- max_num_index = i
- return max_num_index
- def first(nums, count, num_type):
- even_list = []
- odd_list = []
- result_even = []
- result_odd = []
- if num_type == "even":
- for i in nums:
- if i % 2 == 0:
- even_list.append(i)
- if count > len(even_list):
- result_even = even_list
- print(result_even)
- else:
- for j in range(count):
- result_even.append(even_list[j])
- print(result_even)
- elif num_type == "odd":
- for g in nums:
- if g % 2 != 0:
- odd_list.append(g)
- if count > len(odd_list):
- result_odd = odd_list
- print(result_odd)
- else:
- for k in range(count):
- result_odd.append(odd_list[k])
- print(result_odd)
- def last(nums, count, num_type):
- even_list = []
- odd_list = []
- result_even = []
- result_odd = []
- if num_type == "even":
- for i in nums:
- if i % 2 == 0:
- even_list.append(i)
- if count > len(even_list):
- result_even = even_list
- print(result_even)
- else:
- for j in range(len(even_list)-1, len(even_list)-count-1, -1):
- result_even.append(even_list[j])
- print(result_even)
- elif num_type == "odd":
- for g in nums:
- if g % 2 != 0:
- odd_list.append(g)
- if count > len(odd_list):
- result_odd = odd_list
- print(result_odd)
- else:
- for k in range(len(odd_list) - 1, len(odd_list)-count-1, -1):
- result_odd.append(odd_list[k])
- print(result_odd)
- def solve():
- nums = read_number_list()
- command_input = input()
- while command_input != "end":
- arg = command_input.split()
- command = arg[0]
- if command == "exchange":
- index = int(arg[1])
- if index > len(nums) or index < 0:
- print("Invalid index")
- command_input = input()
- continue
- nums = exchange(nums, index)
- elif command == "max":
- num_type = arg[1]
- max_num_index = max_number_index(nums, num_type)
- if max_num_index >=0:
- print(max_num_index)
- command_input = input()
- continue
- else:
- print("No matches")
- command_input = input()
- continue
- elif command == "min":
- num_type = arg[1]
- min_num_index = min_number_index(nums, num_type)
- if min_num_index >= 0:
- print(min_num_index)
- command_input = input()
- continue
- else:
- print("No matches")
- command_input = input()
- continue
- elif command == "first":
- count = int(arg[1])
- num_type = arg[2]
- if count > int(len(nums)):
- print("Invalid count")
- command_input = input()
- continue
- else:
- first(nums, count, num_type)
- elif command == "last":
- count = int(arg[1])
- num_type = arg[2]
- if count > int(len(nums)):
- print("Invalid count")
- command_input = input()
- continue
- else:
- last(nums, count, num_type)
- command_input = input()
- print(nums)
- solve()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement