diff options
author | Matthew Strapp <msattr@gmail.com> | 2019-09-16 12:53:26 -0500 |
---|---|---|
committer | Matthew Strapp <msattr@gmail.com> | 2019-09-16 12:53:26 -0500 |
commit | 45593e057d27b8d783390ffa758b168ea136b5b9 (patch) | |
tree | 6bb7bf1cbe48fe0fbcc9d448b9c06c65ec3c5dec /csci1913 | |
parent | Finish lab (diff) | |
parent | E (diff) | |
download | homework-45593e057d27b8d783390ffa758b168ea136b5b9.tar homework-45593e057d27b8d783390ffa758b168ea136b5b9.tar.gz homework-45593e057d27b8d783390ffa758b168ea136b5b9.tar.bz2 homework-45593e057d27b8d783390ffa758b168ea136b5b9.tar.lz homework-45593e057d27b8d783390ffa758b168ea136b5b9.tar.xz homework-45593e057d27b8d783390ffa758b168ea136b5b9.tar.zst homework-45593e057d27b8d783390ffa758b168ea136b5b9.zip |
Merge branch 'master' of github.com:RosstheRoss/TestingFun
Diffstat (limited to 'csci1913')
-rw-r--r-- | csci1913/lab1/lab1_strap012.py | 35 |
1 files changed, 16 insertions, 19 deletions
diff --git a/csci1913/lab1/lab1_strap012.py b/csci1913/lab1/lab1_strap012.py index e04cf82..48e2ffe 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,48 +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 52 else: return None -def solving(v,q): - if left(q)==v: - return q - elif type(left(left(q))) is not tuple: - if op(left(q))=='+': - return solvingAdd(v, q) - elif op(left(q))=='-': - return solvingSubtract(v, q) - elif op(left(q))=='*': - return solvingMultiply(v, q) - elif op(left(q))=='/': - return solvingDivide(v, q) - else: - solving(v,left(q)) - +#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: + raise ValueError + return solving(v,newQ) # # TESTS. Test the equation solver for CSci 1913 Lab 1. |