def display_Menu(self): """ Displays the menu, waits for and processes commands """ menu_split = [' '] self._print_sequence(self.Command_Handler.menu_dictionary['display menu'](None), "") while(menu_split[0] != 'quit'): menu_code = input("Input your command: ") menu_split = menu_code.split(sep=" ") try: sequence = self.Command_Handler.process_command(menu_split) if(isinstance(sequence, int)): print(sequence) else: try: Validator.validate_int(get_index(sequence, 0), -10000, 10000) end_character = " " except IndexError: pass except ValueError: end_character = "" self._print_sequence(sequence, end_character) except TypeError as e: if(get_index(e.args[0].split(sep = " "), 1) == 'takes'): print('Invalid command') else: print(e.args[0]) except ValueError as e: print (e.args[0]) except IndexError as e: print (e.args[0])
def delete_from_index(self, index = -1, noCopy = False): """ Deletes the number from a given index Input: index - The index to delete from noCopy - A flag used to stop the saving of the sequence - Set to true when the method is called by another member method Returns: The modified sequence Raises IndexError if the given index is invalid """ Validator.validate_index(self.sequence_store.sequence, index) self.sequence_store.sequence.pop(index) if(not noCopy): self._save_sequence() return self.sequence_store.sequence
def insert_at_index(self, n, index = None, noCopy = False): """ Inserts an element at a given index. If the index is left on it's default value(-1), then the element is simply appended Returns: - The original sequence, if the indexes fail validation - The modified sequence Raises IndexError if the given index is invalid """ if(index == None or index == len(self.sequence_store.sequence)): index = len(self.sequence_store.sequence) # We consider this value implicitly valid else: Validator.validate_index(self.sequence_store.sequence, index) self.sequence_store.sequence.insert(index, n) if(not noCopy): self._save_sequence() return self.sequence_store.sequence
def gcd_subsequence(self, index_start = 0, index_end = -1): """ Prints the gcd of all the numbers in the given subsequence Input: index_start - The starting index index_end - The ending index Returns: The gcd of all elements between the given indexes Raises IndexError if any of the indexes are invalid """ result = 0 Validator.validate_index(self.sequence_store.sequence, index_start, index_end) for i in range(index_start, index_end): result = gcd(self.sequence_store.sequence[i], self.sequence_store.sequence[i+1]) return result
def display_prime(self, index_start = -1, index_end = -1): """ Prints a list containing all prime numbers in the given subsequence Input: index_start - The starting index index_end - The ending index Returns: A list of all prime numbers between the given indexes Raises IndexError if any of the indexes are invalid """ result_list = [] Validator.validate_index(self.sequence_store.sequence, index_start, index_end) for i in range(index_start, index_end + 1): if(isPrime(self.sequence_store.sequence[i])): result_list.append(self.sequence_store.sequence[i]) return result_list
def get_keywords(self, menu_split): """ Gets all the keystrings of a given command Example: - "replace subsequence 1 3 5 with 4 5" will return ["replace subsequence", "with"] - "delete index 1" will return ["delete index"] Input: -menu_split - The command string Returns: An array containing all identified keystrings """ key_list = [] key_string = '' for i in range(0, len(menu_split)): try: num = Validator.validate_int(get_index(menu_split,i), -10000, 10000) if(key_string != ''): key_string = key_string[:len(key_string) - 1] key_list.append(key_string) key_string = '' except ValueError as e: if(get_index(e.args, 0) == "Number is invalid"): key_string += get_index(menu_split,i) + ' ' if(key_string != ''): key_string = key_string[:len(key_string) - 1] key_list.append(key_string) return key_list
def max_subsequence(self, index_start = 0, index_end = -1): """ Prints the greatest element of all the numbers in the given subsequence Input: index_start - The starting index index_end - The ending index Returns: The greatest element of all elements between the given indexes Raises IndexError if any of the indexes are invalid """ Validator.validate_index(self.sequence_store.sequence, index_start, index_end) result = self.sequence_store.sequence[index_start] for i in range(index_start + 1, index_end + 1): if(self.sequence_store.sequence[i] > result): result = self.sequence_store.sequence[i] return result
def delete_subsequence(self, index_start, index_end, noCopy = False): """ Removes the subsequence contained within the given indexes Input: index_start - The starting index index_end - The end index noCopy - A flag used to stop the saving of the sequence - Set to true when the method is called by another member method Returns: The modified sequence Raises IndexError if any of the indexes are invalid """ Validator.validate_index(self.sequence_store.sequence, index_start, index_end) i = index_end - index_start + 1 while(i > 0): sequence = self.delete_from_index(index_start, True) i = i - 1 if(not noCopy): self._save_sequence() return sequence
def get_numbers(self, menu_split, startIndex): """ Gets the first array of numbers encountered after a given index Input: -menu_split - The command string, split into an array of strings by the ' ' character -sequence - The base sequences Returns: The first array of numbers encountered """ num_array = [] for i in range(startIndex, len(menu_split)): try: num = Validator.validate_int(menu_split[i], -10000, 10000) except ValueError: break num_array.append(num) return num_array