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 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