Commit 8ecfff01 authored by mrk022's avatar mrk022
Browse files

updated tree.py to handle variable nodes

parent 1e7de1fe
...@@ -20,8 +20,8 @@ class Queue: ...@@ -20,8 +20,8 @@ class Queue:
# When queue is half full, compress it. This # When queue is half full, compress it. This
# achieves an amortized complexity of O(1) while # achieves an amortized complexity of O(1) while
# not letting the list continue to grow unchecked. # not letting the list continue to grow unchecked.
if self.frontIdx * 2 > len(self.items): #if self.frontIdx * 2 > len(self.items):
self.__compress() # self.__compress()
item = self.items[self.frontIdx] item = self.items[self.frontIdx]
self.frontIdx += 1 self.frontIdx += 1
......
No preview for this file type
# base tree code from http://knuth.luther.edu/~leekent/CS2Plus/chap6/chap6.html # base tree code from http://knuth.luther.edu/~leekent/CS2Plus/chap6/chap6.html
# changed this code to allow for variable functions, and change infix expressions to postfix # changed this code to allow for variable functions, and change infix expressions to postfix
# added variable node class to consider when both nodes have variables in them
import queue import queue
...@@ -9,6 +10,16 @@ class TimesNode: ...@@ -9,6 +10,16 @@ class TimesNode:
self.right = right self.right = right
def eval(self): def eval(self):
'''if check_if_variables(str(self.left.eval()), str(self.right.eval())) == "both_vars":
v1 = str(self.left.eval()).split("x")[0]
v2 = str(self.right.eval()).split("x")[0]
evalStr = v1 + "*" + v2
return eval(evalStr)
else:
return self.left.eval() * self.right.eval()'''
#if isinstance(self.left, VariableNode):
# return self.left.getMultiplier() * self.left.getMultiplier()
#else:
return self.left.eval() * self.right.eval() return self.left.eval() * self.right.eval()
def inorder(self): def inorder(self):
...@@ -47,6 +58,17 @@ class NumNode: ...@@ -47,6 +58,17 @@ class NumNode:
def inorder(self): def inorder(self):
return str(self.num) return str(self.num)
class VariableNode:
def __init__(self, value):
self.value = value
def eval(self):
# get value before x
return float(self.value.split("x")[0])
def inorder(self):
return str(self.eval())
def check_if_variables(x, y): def check_if_variables(x, y):
if 'x' in x and 'x' in y: if 'x' in x and 'x' in y:
return "both_vars" return "both_vars"
...@@ -60,7 +82,6 @@ def E(q): ...@@ -60,7 +82,6 @@ def E(q):
raise ValueError("Invalid Prefix Expression") raise ValueError("Invalid Prefix Expression")
token = q.dequeue() token = q.dequeue()
if token == "+": if token == "+":
return PlusNode(E(q),E(q)) return PlusNode(E(q),E(q))
...@@ -70,11 +91,15 @@ def E(q): ...@@ -70,11 +91,15 @@ def E(q):
if token == "-": if token == "-":
return MinusNode(E(q), E(q)) return MinusNode(E(q), E(q))
try: #try:
if "x" in token:
output = VariableNode(token)
# make a different class for variable node
else:
output = NumNode(float(token)) output = NumNode(float(token))
except: #except:
print("Not a valid operation, need variables of like terms") # print("Not a valid operation, need variables of like terms")
output = NumNode(float(0)) # output = NumNode(float(0))
return output return output
OPERATORS = set(['+', '-', '*', '/', '(', ')']) OPERATORS = set(['+', '-', '*', '/', '(', ')'])
...@@ -144,7 +169,8 @@ def split_equation(equation): ...@@ -144,7 +169,8 @@ def split_equation(equation):
return expressions return expressions
def main(): def main():
exp = split_equation("3 * 4 - 6=0") exp = split_equation("4x * 3x")
eval_tree(exp[0]) eval_tree(exp[0])
......
...@@ -101,7 +101,6 @@ class IntelligentTutor(ACTR): ...@@ -101,7 +101,6 @@ class IntelligentTutor(ACTR):
goal = Buffer() goal = Buffer()
user = UserState("4x+7=5x+2") user = UserState("4x+7=5x+2")
def init(): def init():
user.get_input() user.get_input()
goal.set(user.state) goal.set(user.state)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment