Commit 6180fd72 by mrk022

### worked on tutor.py

parent 8ecfff01
 ... @@ -17,8 +17,11 @@ class TimesNode: ... @@ -17,8 +17,11 @@ class TimesNode: return eval(evalStr) return eval(evalStr) else: else: return self.left.eval() * self.right.eval()''' return self.left.eval() * self.right.eval()''' #if isinstance(self.left, VariableNode): #if isinstance(self.left, VariableNode) and isinstance(self.right,VariableNode): # return self.left.getMultiplier() * self.left.getMultiplier() # return self.left.eval()*self.right.eval() #elif isinstance(self.left,VariableNode) or isinstance(self.right,VariableNode): # print("can't combine variables of differing types") #need some sort of combined node here i think #else: #else: return self.left.eval() * self.right.eval() return self.left.eval() * self.right.eval() ... @@ -106,6 +109,7 @@ OPERATORS = set(['+', '-', '*', '/', '(', ')']) ... @@ -106,6 +109,7 @@ OPERATORS = set(['+', '-', '*', '/', '(', ')']) PRIORITY = {'+':1, '-':1, '*':2, '/':2} PRIORITY = {'+':1, '-':1, '*':2, '/':2} def infix_to_prefix(formula): def infix_to_prefix(formula): # this method was taken from open source https://github.com/lilianweng/LeetcodePython/blob/master/expression.py op_stack = [] op_stack = [] exp_stack = [] exp_stack = [] for ch in formula.split(): for ch in formula.split(): ... @@ -139,14 +143,14 @@ def infix_to_prefix(formula): ... @@ -139,14 +143,14 @@ def infix_to_prefix(formula): def eval_tree(expression): def eval_tree(expression): x = NumNode(5) '''x = NumNode(5) y = NumNode(4) y = NumNode(4) p = PlusNode(x,y) p = PlusNode(x,y) t = TimesNode(p, NumNode(6)) t = TimesNode(p, NumNode(6)) root = PlusNode(t, NumNode(3)) root = PlusNode(t, NumNode(3)) print(root.eval()) print(root.eval()) print(root.inorder()) print(root.inorder())''' #x = input("Please enter a prefix expression: ") #x = input("Please enter a prefix expression: ") x = infix_to_prefix(expression) x = infix_to_prefix(expression) ... @@ -159,9 +163,10 @@ def eval_tree(expression): ... @@ -159,9 +163,10 @@ def eval_tree(expression): root = E(q) root = E(q) result = root.eval() print(root.eval()) print(root.eval()) print(root.inorder()) print(root.inorder()) return result #print(infix_to_prefix("3 + 4 - 6")) #print(infix_to_prefix("3 + 4 - 6")) def split_equation(equation): def split_equation(equation): ... @@ -169,9 +174,8 @@ def split_equation(equation): ... @@ -169,9 +174,8 @@ def split_equation(equation): return expressions return expressions def main(): def main(): exp = split_equation("4x * 3x") exp = split_equation("4x * 3x + 2") x = eval_tree(exp[0]) eval_tree(exp[0]) if __name__ == "__main__": if __name__ == "__main__": ... ...