From 20fb15d4fefddd12dfc5be30c7175c2bf606a7b8 Mon Sep 17 00:00:00 2001 From: RossTheRoss Date: Fri, 13 Sep 2019 11:45:06 -0500 Subject: Change something not broken --- csci1913/lab1/lab1_strap012.py | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) (limited to 'csci1913/lab1') diff --git a/csci1913/lab1/lab1_strap012.py b/csci1913/lab1/lab1_strap012.py index 771734d..d69ddc6 100644 --- a/csci1913/lab1/lab1_strap012.py +++ b/csci1913/lab1/lab1_strap012.py @@ -6,12 +6,13 @@ def right(exp): return exp[2] - def isInside(var, e): if type(e) is tuple: return isInside(var, left(e)) or isInside(var, right(e)) elif type(e) is str: return var==e + else: + return -1 def solve(v, e): if isInside(v, left(e)): @@ -19,49 +20,44 @@ def solve(v, e): elif isInside(v,right(e)): newE=(right(e),op(e),left(e)) return solving(v,newE) + #Solving defined on line 53 else: return None -def solving(v,q): - if left(q)==v: - return q - else: - if op(left(q))=='+': - newQ = solvingAdd(v, q) - elif op(left(q))=='-': - newQ = solvingSubtract(v, q) - elif op(left(q))=='*': - newQ = solvingMultiply(v, q) - elif op(left(q))=='/': - newQ = solvingDivide(v, q) - else: - return 6 - return solving(v,newQ) - +#Four major solving means def solvingAdd(v,q): if isInside(v, left(left(q))): return left(left(q)), '=', (right(q), '-', right(left(q))) else: return right(left(q)), '=', (right(q), '-', left(left(q))) - def solvingSubtract(v,q): if isInside(v, left(left(q))): return left(left(q)), '=', (right(q), '+', right(left(q))) else: return right(left(q)), '=', (left(left(q)), '-', right(q)) - def solvingMultiply(v,q): if isInside(v, left(left(q))): return left(left(q)), '=', (right(q), '/', right(left(q))) else: return right(left(q)), '=', (right(q), '/', left(left(q))) - def solvingDivide(v,q): if isInside(v, left(left(q))): return left(left(q)), '=', (right(q), '*', right(left(q))) else: return right(left(q)), '=', (left(left(q)), '/', right(q)) +#Dict based off of lecture 13th Sept. 2019 +dispatcher={'+':solvingAdd,'-':solvingSubtract,'*':solvingMultiply,'/':solvingDivide} + +def solving(v,q): + if left(q) is v: + return q + else: + if op(left(q)) in dispatcher: + newQ= dispatcher[op(left(q))](v,q) + else: + return -2 + return solving(v,newQ) # # TESTS. Test the equation solver for CSci 1913 Lab 1. -- cgit v1.2.3