aboutsummaryrefslogtreecommitdiffstats
path: root/csci1913/Python
diff options
context:
space:
mode:
Diffstat (limited to 'csci1913/Python')
-rw-r--r--csci1913/Python/lab1_strap012.py115
-rw-r--r--csci1913/Python/lab2_strap012.py137
2 files changed, 252 insertions, 0 deletions
diff --git a/csci1913/Python/lab1_strap012.py b/csci1913/Python/lab1_strap012.py
new file mode 100644
index 0000000..396fda9
--- /dev/null
+++ b/csci1913/Python/lab1_strap012.py
@@ -0,0 +1,115 @@
+#Matthew Strapp
+#5449340
+#Lab 1
+
+def left(exp):
+ return exp[0]
+def op(exp):
+ return exp[1]
+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)):
+ return solving(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
+
+#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.
+#
+# James Moen
+# 10 Sep 18
+#
+# Every test is followed by a comment which shows what must be printed if your
+# code works correctly. It also shows how many points the test is worth, for a
+# total of 35 possible points.
+#
+
+print(isInside('x', 'x')) # True 1 point
+print(isInside('x', 'y')) # False 1 point
+print(isInside('x', ('x', '+', 'y'))) # True 2 points
+print(isInside('x', ('a', '+', 'b'))) # False 2 points
+print(isInside('+', ('a', '+', 'b'))) # False 2 points
+print(isInside('x', (('m', '*', 'x'), '+', 'b'))) # True 2 points
+
+print(solve('x', (('a', '+', 'x'), '=', 'c')))
+# ('x', '=', ('c', '-', 'a')) 2 points
+
+print(solve('x', (('x', '+', 'b'), '=', 'c')))
+# ('x', '=', ('c', '-', 'b')) 2 points
+
+print(solve('x', (('a', '-', 'x'), '=', 'c')))
+# ('x', '=', ('a', '-', 'c')) 2 points
+
+print(solve('x', (('x', '-', 'b'), '=', 'c')))
+# ('x', '=', ('c', '+', 'b')) 2 points
+
+print(solve('x', (('a', '*', 'x'), '=', 'c')))
+# ('x', '=', ('c', '/', 'a')) 2 points
+
+print(solve('x', (('x', '*', 'b'), '=', 'c')))
+# ('x', '=', ('c', '/', 'b')) 2 points
+
+print(solve('x', (('a', '/', 'x'), '=', 'c')))
+# ('x', '=', ('a', '/', 'c')) 2 points
+
+print(solve('x', (('x', '/', 'b'), '=', 'c')))
+# ('x', '=', ('c', '*', 'b')) 2 points
+
+print(solve('y', ('y', '=', (('m', '*', 'x'), '+', 'b'))))
+# ('y', '=', (('m', '*', 'x'), '+', 'b')) 2 points
+
+print(solve('x', ('y', '=', (('m', '*', 'x'), '+', 'b'))))
+# ('x', '=', (('y', '-', 'b'), '/', 'm')) 2 points
+
+print(solve('a', (('b', '+', 'c'), '=', ('d', '*', (('a', '/', 'e'), '-', 'f')))))
+# ('a', '=', (((('b', '+', 'c'), '/', 'd'), '+', 'f'), '*', 'e')) 5 points \ No newline at end of file
diff --git a/csci1913/Python/lab2_strap012.py b/csci1913/Python/lab2_strap012.py
new file mode 100644
index 0000000..4e154f7
--- /dev/null
+++ b/csci1913/Python/lab2_strap012.py
@@ -0,0 +1,137 @@
+class Zillion:
+ def __init__(self,digits):
+ self.digits = digits
+ self.List = []
+ self.toList()
+
+ def toList(self):
+ badBoolean=False #Kludge
+ if len(self.digits) == 0 :
+ raise RuntimeError #Cannot make a list of nothing
+ for n in range (0,len(self.digits)):
+ if self.digits[n] is not ',':
+ if self.digits[n] is not ' ':
+ try:
+ self.List+=[int(self.digits[n])]
+ badBoolean=True
+ except ValueError: #Non-comma character in string
+ raise RuntimeError
+ if badBoolean is False: #No numbers in string
+ raise RuntimeError
+ else:
+ return self.List
+
+
+ def increment(self):
+ length = len(self.List) - 1
+ newList = self.List
+ newList[len(self.List)-1] += 1
+ if newList[length]>=10: #Loop to increment all 10s to 0s with carry
+ for n in range (0,length):
+ newList[length-n]=0
+ newList[length-n-1]+=1
+
+ def isZero(self):
+ for p in range (0,len(self.List)-1):
+ if self.List[p] is not 0:
+ return False
+ return True
+
+ def toString(self):
+ string=''
+ for q in range (0,len(self.List)):
+ string+=str(self.List[q]) #Make string by concatenation
+ return string
+
+
+
+#
+# TESTS. Test the class Zillion for CSci 1913 Lab 2.
+#
+# James Moen
+# 18 Sep 17
+#
+# Every test is followed by a comment which shows what must be printed if your
+# code works correctly. It also shows how many points the test is worth.
+#
+
+try:
+ z = Zillion('')
+except RuntimeError:
+ print('Empty string')
+
+# It must print 'Empty string' without apostrophes. 2 points.
+
+try:
+ z = Zillion(' , ')
+except RuntimeError:
+ print('No digits in the string')
+
+# It must print 'No digits in the string' without apostrophes. 2 points.
+
+try:
+ z = Zillion('1+0')
+except RuntimeError:
+ print('Non-digit in the string')
+
+# It must print 'Non-digit in the string' without apostrophes. 2 points.
+
+try:
+ z = Zillion('0')
+except RuntimeError:
+ print('This must not be printed')
+
+# It must print nothing. 2 points.
+
+print(z.isZero()) # It must print True. 2 points.
+
+try:
+ z = Zillion('000000000')
+except RuntimeError:
+ print('This must not be printed')
+
+# It must print nothing. 2 points.
+
+print(z.isZero()) # It must print True. 2 points.
+
+try:
+ z = Zillion('000 000 000')
+except RuntimeError:
+ print('This must not be printed')
+
+# It must print nothing. 2 points.
+
+print(z.isZero()) # It must print True. 2 points.
+
+try:
+ z = Zillion('997')
+except RuntimeError:
+ print('This must not be printed')
+
+# It must print nothing. 2 points.
+
+print(z.isZero()) # It must print False. 2 points.
+
+print(z.toString()) # It must print 997. 2 points.
+
+z.increment()
+
+print(z.toString()) # It must print 998. 2 points.
+
+z.increment()
+
+print(z.toString()) # It must print 999. 2 points.
+
+z.increment()
+
+print(z.toString()) # It must print 1000. 2 points.
+
+try:
+ z = Zillion('0 9,9 9')
+except:
+ print('This must not be printed')
+
+# It must print nothing. 3 points.
+
+z.increment()
+print(z.toString()) # It must print 1000. 2 points.