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
-rw-r--r--csci1913/Python/lab3_strap012.py63
-rw-r--r--csci1913/Python/project1_strap012.py134
4 files changed, 449 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..511e6bf
--- /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[length] += 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.
diff --git a/csci1913/Python/lab3_strap012.py b/csci1913/Python/lab3_strap012.py
new file mode 100644
index 0000000..f9df918
--- /dev/null
+++ b/csci1913/Python/lab3_strap012.py
@@ -0,0 +1,63 @@
+#"New" functions
+def Reduce(F,S):
+ if len(S)==0:
+ return -255
+ else:
+ return F(Reduce(F,S[:-1]),S[-1])
+def returnGreater(a,b):
+ if a > b:
+ return a
+ else:
+ return b
+
+#Implementation from Lab
+def Sort(T):
+ if len(T)==0:
+ return ()
+ else:
+ return (Sort(Remove(T,Maximum(T))))+(Maximum(T),)
+def Maximum(T):
+ return Reduce(returnGreater,T)
+def Remove(T,E):
+ if len(T)==0:
+ return ()
+ elif T[0] is E:
+ return T[1:]
+ else:
+ return (T[0],)+Remove(T[1:],E)
+
+
+#
+# TESTS. Tests for CSci 1913 Lab 3.
+#
+# James Moen
+# 11 Feb 19
+#
+# Each test is worth 2 points, for 40 points total. Comments show what must be
+# printed to receive credit. Note that your function SORT must work for tuples
+# with negative elements.
+#
+
+print(Maximum((1,))) # 1 2 pt.
+print(Maximum((-2, -1))) # -1 2 pt.
+print(Maximum((1, 1))) # 1 2 pt.
+print(Maximum((1, 2, 3))) # 3 2 pt.
+
+print(Remove((), 1)) # () 2 pt.
+print(Remove((1,), 1)) # () 2 pt.
+print(Remove((0, 1), 0)) # (1,) 2 pt.
+print(Remove((0, 1, 2, 1, 3), 1)) # (0, 2, 1, 3) 2 pt.
+print(Remove((0, 1, 2, 1, 3), 2)) # (0, 1, 1, 3) 2 pt.
+print(Remove((1, 2, 3), 3)) # (1, 2) 2 pt.
+
+print(Sort(())) # () 2 pt.
+print(Sort((0,))) # (0,) 2 pt.
+print(Sort((0, -1))) # (-1, 0) 2 pt.
+print(Sort((1, 0))) # (0, 1) 2 pt.
+print(Sort((0, 0, 1))) # (0, 0, 1) 2 pt.
+print(Sort((0, -1, 0))) # (-1, 0, 0) 2 pt.
+print(Sort((0, 0, 1))) # (0, 0, 1) 2 pt.
+
+print(Sort((9, 8, 7, 6, 5, 4, 3, 2, 1))) # (1, 2, 3, 4, 5, 6, 7, 8, 9) 2 pt.
+print(Sort((1, 2, 3, 4, 5, 6, 7, 8, 9))) # (1, 2, 3, 4, 5, 6, 7, 8, 9) 2 pt.
+print(Sort((1, 2, 1, 4, 2, 5, 4, 5, 3))) # (1, 1, 2, 2, 3, 4, 4, 5, 5) 2 pt.
diff --git a/csci1913/Python/project1_strap012.py b/csci1913/Python/project1_strap012.py
new file mode 100644
index 0000000..c825359
--- /dev/null
+++ b/csci1913/Python/project1_strap012.py
@@ -0,0 +1,134 @@
+class Random:
+ def __init__(self, seed):
+ self.sevenFive = 16807
+ self.twoThirtyone = 2147483647
+ self.newNum = seed
+
+ def next(self):
+ self.newNum = self.sevenFive*self.newNum % self.twoThirtyone
+ return self.newNum
+
+ def choose(self, limit):
+ return self.next()%limit
+
+
+
+class Rule:
+ def __init__(self, left, right):
+ self.left = left
+ self.right = right
+ self.count = 1
+
+ def __repr__(self):
+ string = str(self.count)
+ string += " "
+ string += str(self.left)
+ string += " -> "
+ for n in range (0, len(self.right)):
+ string += self.right[n]
+ string += " "
+ return string
+
+
+
+class Grammar:
+ def __init__(self, seed):
+ self.r = Random(seed)
+ self.dictionary = {}
+
+ def rule(self, left, right):
+ if left not in self.dictionary:
+ self.dictionary[left] = (Rule(left,right),)
+ else:
+ self.dictionary[left] += (Rule(left, right),)
+
+ def generate(self):
+ if 'Start' in self.dictionary:
+ return self.generating(('Start',))
+ else:
+ raise RuntimeError
+
+ def select(self, left):
+ rules = self.dictionary[left]
+ total = 0
+ c = 0
+ #Some of the loops are probably not needed
+ for m in range (0, len(rules)):
+ total += rules[m].count
+ index = self.r.choose(total)
+ for c in range (0, len(rules)):
+ index -= rules[c].count
+ if index <= 0:
+ break
+ chosen = rules[c]
+ for n in range (0, len(rules)):
+ if rules[n] is not chosen:
+ rules[n].count += 1
+ return chosen.right
+
+ def generating(self,strings):
+ result=''
+ for n in range (0, len(strings)):
+ if strings[n] not in self.dictionary:
+ result += strings[n]
+ result += " "
+ else:
+ result += self.generating(self.select(strings[n]))
+ return result
+
+
+
+
+G = Grammar(101)
+G.rule('Noun', ('cat',)) # 01
+G.rule('Noun', ('boy',)) # 02
+G.rule('Noun', ('dog',)) # 03
+G.rule('Noun', ('girl',)) # 04
+G.rule('Verb', ('bit',)) # 05
+G.rule('Verb', ('chased',)) # 06
+G.rule('Verb', ('kissed',)) # 07
+G.rule('Phrase', ('the', 'Noun', 'Verb', 'the', 'Noun')) # 08
+G.rule('Story', ('Phrase',)) # 09
+G.rule('Story', ('Phrase', 'and', 'Story')) # 10
+G.rule('Story', ('Phrase', 'but', 'Story')) # 11
+G.rule('Start', ('Story', '.')) # 12
+for _ in range (0, 5):
+ print(G.generate())
+
+# the cat bit the boy .
+# the cat kissed the dog and the boy chased the boy .
+# the cat chased the dog and the girl bit the boy but the girl chased the cat .
+# the girl chased the dog .
+# the boy kissed the girl and the cat kissed the girl .
+
+print()
+print()
+
+#
+#
+
+J = Grammar(1453)
+J.rule('Noun', ('sentence',))
+J.rule('Noun', ('walrus',))
+J.rule('Noun', ('sky',))
+J.rule('Noun', ('diamond',))
+J.rule('Noun', ('Ottoman Empire',))
+J.rule('Verb', ('is',))
+J.rule('Verb', ('fanned',))
+J.rule('Verb', ('generated',))
+J.rule('Verb', ('hit',))
+J.rule('Phrase', ('the', 'Noun', 'Verb', 'the', 'Noun')) # 08
+J.rule('Phrase', ('Noun', 'Verb', 'the', 'Noun'))
+J.rule('Story', ('Phrase',)) # 09
+J.rule('Story', ('Phrase', 'and', 'Story')) # 10
+J.rule('Story', ('Phrase', 'but', 'Story')) # 11
+J.rule('Start', ('Story', '.')) # 12
+
+for _ in range(0, 5):
+ print(J.generate())
+
+# the walrus is the sentence and the diamond generated the walrus .
+# the walrus generated the walrus and the walrus generated the sentence but diamond hit the sentence .
+# sentence fanned the Ottoman Empire .
+# the sky hit the diamond and sky is the sentence but the sky fanned the diamond .
+# the walrus generated the diamond and the diamond generated the Ottoman Empire but the sky generated the sentence .