Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- def calculation(calculation, list_of_numbers): # Calculation while command is not "end"
- type_of_number = command[1]
- if command[0] == "exchange":
- exchange(command, list_of_numbers)
- elif command[0] == "max":
- print(max(command, list_of_numbers))
- elif command[0] == "min":
- print(min(command, list_of_numbers))
- elif command[0] == "first":
- print(first(command, list_of_numbers))
- elif command[0] == "last":
- print(last(command, list_of_numbers))
- def exchange(command, list_of_numbers): # Exchanging places of the elements
- type_of_number = int(command[1])
- if type_of_number > (len(list_of_numbers) - 1) or type_of_number < 0:
- print("Invalid index")
- else:
- for i in range(len(list_of_numbers) - 1, type_of_number, -1):
- list_of_numbers.insert(0, list_of_numbers.pop(-1))
- return list_of_numbers
- def max(command, list_of_numbers): # Finding max even/odd number in the list
- max_odd_element = -sys.maxsize
- max_even_element = -sys.maxsize
- found_max_even_at_index = 0
- found_max_odd_at_index = 0
- max_even_IsFound = False
- max_odd_IsFound = False
- noMatches = "No matches"
- type_of_number = command[1]
- for i in range(len(list_of_numbers) - 1, -1, -1):
- if list_of_numbers[i] % 2 != 0:
- if max_odd_element < list_of_numbers[i]:
- max_odd_element = list_of_numbers[i]
- found_max_odd_at_index = i
- max_odd_IsFound = True
- else:
- if max_even_element < list_of_numbers[i]:
- max_even_element = list_of_numbers[i]
- found_max_even_at_index = i
- max_even_IsFound = True
- if type_of_number == "even":
- if max_even_IsFound:
- return found_max_even_at_index
- else:
- return noMatches
- elif type_of_number == "odd":
- if max_odd_IsFound:
- return found_max_odd_at_index
- else:
- return noMatches
- def min(command, list_of_numbers): # Finding min even/odd number in the list
- min_odd_element = sys.maxsize
- min_even_element = sys.maxsize
- found_min_even_at_index = 0
- found_min_odd_at_index = 0
- min_even_IsFound = False
- min_odd_IsFound = False
- noMatches = "No matches"
- type_of_number = command[1]
- for i in range(len(list_of_numbers) - 1, -1, -1):
- if list_of_numbers[i] % 2 != 0:
- if min_odd_element > list_of_numbers[i]:
- min_odd_element = list_of_numbers[i]
- found_min_odd_at_index = i
- min_odd_IsFound = True
- else:
- if min_even_element > list_of_numbers[i]:
- min_even_element = list_of_numbers[i]
- found_min_even_at_index = i
- min_even_IsFound = True
- if type_of_number == "even":
- if min_even_IsFound:
- return found_min_even_at_index
- else:
- return noMatches
- elif type_of_number == "odd":
- if min_odd_IsFound:
- return found_min_odd_at_index
- else:
- return noMatches
- def first(command, list_of_numbers): # Finding first even/odd numbers in the list
- invalidCount = "Invalid count"
- invalidCountFounded = False
- first_list = []
- last_list = []
- counter_for_first_and_last_list = 0
- if int(command[1]) > len(list_of_numbers) or int(command[1]) < 0:
- invalidCountFounded = True
- return invalidCount
- else:
- if command[2] == "even":
- for i in range(0, len(list_of_numbers)):
- if counter_for_first_and_last_list == int(command[1]):
- break
- if list_of_numbers[i] % 2 == 0:
- first_list.append(list_of_numbers[i])
- counter_for_first_and_last_list += 1
- elif command[2] == "odd":
- for i in range(0, len(list_of_numbers)):
- if counter_for_first_and_last_list == int(command[1]):
- break
- if list_of_numbers[i] % 2 != 0:
- first_list.append(list_of_numbers[i])
- counter_for_first_and_last_list += 1
- if not invalidCountFounded:
- return first_list
- def last(command, list_of_numbers): # Finding last even/odd numbers in the list
- invalidCount = "Invalid count"
- invalidCountFounded = False
- first_list = []
- last_list = []
- counter_for_first_and_last_list = 0
- if int(command[1]) > len(list_of_numbers) or int(command[1]) < 0:
- invalidCountFounded = True
- return invalidCount
- else:
- if command[2] == "even":
- for i in range(len(list_of_numbers) - 1, -1, -1):
- if counter_for_first_and_last_list == int(command[1]):
- break
- if list_of_numbers[i] % 2 == 0:
- last_list.append(list_of_numbers[i])
- counter_for_first_and_last_list += 1
- elif command[2] == "odd":
- for i in range(len(list_of_numbers) - 1, -1, -1):
- if counter_for_first_and_last_list == int(command[1]):
- break
- if list_of_numbers[i] % 2 != 0:
- last_list.append(list_of_numbers[i])
- counter_for_first_and_last_list += 1
- if not invalidCountFounded:
- return last_list
- list_of_numbers = input().split(" ")
- list_of_numbers = [int(i) for i in list_of_numbers]
- command = input().split(" ")
- while command[0] != "end":
- calculation(command, list_of_numbers)
- command = input().split(" ")
- print(list_of_numbers)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement