class Stack: """An implementation of the typical stack data structure""" def __init__(self, *args): self.storage = Vector() if len(args) != 0: for item in args: self.storage.append(item) def clear(self): """Clears the entire contents of the stack""" self.storage = Vector() def is_emtpy(self): return self.is_emtpy @property def is_empty(self): if len(self.storage) == 0: return True else: return False def push(self, item): """Adds an item to the top of the stack""" self.storage.append(item) def pop(self): """Removes an item from the top of the stack""" self.storage.erase_rear() def top(self): """Returns the item at the top of the stack""" return self.storage.back()
class Stack: """An implementation of the typical stack data structure""" def __init__(self, *args): self.storage = Vector() if len(args) != 0: for item in args: self.storage.append(item) def clear(self): """Clears the entire contents of the stack""" self.storage = Vector() def is_emtpy(self): return self.is_emtpy @property def is_empty(self): if len(self.storage) == 0: return True else: return False def push(self, item): """Adds an item to the top of the stack""" self.storage.append(item) def pop(self): """Removes an item from the top of the stack""" self.storage.erase_rear() def top(self): """Returns the item at the top of the stack""" return self.storage.back()
def main(argv): input_file = "" output_file = "" # First we attempt to get the options from the command line try: options, args = getopt.getopt(argv, "i:o:h") except getopt.GetoptError: print( """Call via `python poly_calc.py -i <input_file> -o <output_file>`\n Use `python poly_calc.py -h` for help""") # exit status 2 for command line error sys.exit(2) # Now we check what options were entered and act appropriately for option, argument in options: if option == "-h": print("Call the script using:") print(" python poly_calc.py -i <input_file> -o <output_file>") print("The following flags are used:") print(" -i must be by the input file name") print(" -o can be followed by the output file name if desired") if option == "-i": input_file = argument if option == "-o": output_file = argument # Ensure that there is input data if input_file == "": print("An input file must be specified") else: data = Vector() # Holds all of the lines out of the input file stack = Stack() # Used to store the Polynomials and perform operations # Read all of the lines out of the file with open(input_file) as f: for line in f: data.append(line.strip()) for line in data: if line == "+": p1 = stack.top() stack.pop() p2 = stack.top() stack.pop() stack.push(p1 + p2) elif line == "-": p1 = stack.top() stack.pop() p2 = stack.top() stack.pop() stack.push(p1 - p2) elif line == "*": p1 = stack.top() stack.pop() p2 = stack.top() stack.pop() stack.push(p1 * p2) else: stack.push(Polynomial(line)) if output_file != "": with open(output_file, 'w+') as f: print(stack.top(), file=f) else: print(stack.top())
def main(argv): input_file = "" output_file = "" # First we attempt to get the options from the command line try: options, args = getopt.getopt(argv, "i:o:h") except getopt.GetoptError: print("""Call via `python poly_calc.py -i <input_file> -o <output_file>`\n Use `python poly_calc.py -h` for help""") # exit status 2 for command line error sys.exit(2) # Now we check what options were entered and act appropriately for option, argument in options: if option == "-h": print("Call the script using:") print(" python poly_calc.py -i <input_file> -o <output_file>") print("The following flags are used:") print(" -i must be by the input file name") print(" -o can be followed by the output file name if desired") if option == "-i": input_file = argument if option == "-o": output_file = argument # Ensure that there is input data if input_file == "": print("An input file must be specified") else: data = Vector() # Holds all of the lines out of the input file stack = Stack() # Used to store the Polynomials and perform operations # Read all of the lines out of the file with open(input_file) as f: for line in f: data.append(line.strip()) for line in data: if line == "+": p1 = stack.top() stack.pop() p2 = stack.top() stack.pop() stack.push(p1 + p2) elif line == "-": p1 = stack.top() stack.pop() p2 = stack.top() stack.pop() stack.push(p1 - p2) elif line == "*": p1 = stack.top() stack.pop() p2 = stack.top() stack.pop() stack.push(p1 * p2) else: stack.push(Polynomial(line)) if output_file != "": with open(output_file, 'w+') as f: print(stack.top(), file=f) else: print(stack.top())
def __add__(self, other): """Overloaded addition operator""" temp = Vector() for term in self.terms: temp.append(term) for term in other.terms: temp.append(term) new_poly = Polynomial() new_poly.terms = temp new_poly.combine_like_terms() new_poly.sort_terms() return new_poly
def __add__(self, other): """Overloaded addition operator""" temp = Vector() for term in self.terms: temp.append(term) for term in other.terms: temp.append(term) new_poly = Polynomial() new_poly.terms = temp new_poly.combine_like_terms() new_poly.sort_terms() return new_poly
def sort_terms(self): """Sorts the terms of the polynomial in O(n^2) time""" temp = Vector() for i in range(len(self.terms)): max_exp = self.terms[0].exponent max_exp_index = 0 for index, term in enumerate(self.terms): if term.exponent > max_exp: max_exp = term.exponent max_exp_index = index temp.append(self.terms[max_exp_index]) self.terms.erase(max_exp_index) self.terms = temp
def __mul__(self, other): """Overloaded multiplication operator""" temp = Vector() for term in self.terms: for item in other.terms: new_coef = term.coef * item.coef new_exp = term.exponent + item.exponent temp.append(Term(new_coef, new_exp)) new_poly = Polynomial() new_poly.terms = temp new_poly.combine_like_terms() new_poly.sort_terms() return new_poly
def sort_terms(self): """Sorts the terms of the polynomial in O(n^2) time""" temp = Vector() for i in range(len(self.terms)): max_exp = self.terms[0].exponent max_exp_index = 0 for index, term in enumerate(self.terms): if term.exponent > max_exp: max_exp = term.exponent max_exp_index = index temp.append(self.terms[max_exp_index]) self.terms.erase(max_exp_index) self.terms = temp
def __mul__(self, other): """Overloaded multiplication operator""" temp = Vector() for term in self.terms: for item in other.terms: new_coef = term.coef * item.coef new_exp = term.exponent + item.exponent temp.append(Term(new_coef, new_exp)) new_poly = Polynomial() new_poly.terms = temp new_poly.combine_like_terms() new_poly.sort_terms() return new_poly
def combine_like_terms(self): max_exponent = 0 temp = Vector() for term in self.terms: max_exponent = max(max_exponent, term.exponent) for exponent in range(max_exponent + 1): new_coef = 0 for term in self.terms: if term.exponent == exponent: new_coef += term.coef if (new_coef != 0): temp.append(Term(new_coef, exponent)) self.terms = temp self.sort_terms
def combine_like_terms(self): max_exponent = 0 temp = Vector() for term in self.terms: max_exponent = max(max_exponent, term.exponent) for exponent in range(max_exponent + 1): new_coef = 0 for term in self.terms: if term.exponent == exponent: new_coef += term.coef if (new_coef != 0): temp.append(Term(new_coef, exponent)) self.terms = temp self.sort_terms