diff options
author | RossTheRoss <msattr@gmail.com> | 2019-10-19 20:58:00 -0500 |
---|---|---|
committer | RossTheRoss <msattr@gmail.com> | 2019-10-19 20:58:00 -0500 |
commit | 87c3f791e7588aa1aed4130e9a53000c8edcc993 (patch) | |
tree | b987468321dd630f618a7098d2f7ccc5736a4510 /csci1913 | |
parent | Start project (halp) (diff) | |
download | homework-87c3f791e7588aa1aed4130e9a53000c8edcc993.tar homework-87c3f791e7588aa1aed4130e9a53000c8edcc993.tar.gz homework-87c3f791e7588aa1aed4130e9a53000c8edcc993.tar.bz2 homework-87c3f791e7588aa1aed4130e9a53000c8edcc993.tar.lz homework-87c3f791e7588aa1aed4130e9a53000c8edcc993.tar.xz homework-87c3f791e7588aa1aed4130e9a53000c8edcc993.tar.zst homework-87c3f791e7588aa1aed4130e9a53000c8edcc993.zip |
It's still fucked
Diffstat (limited to 'csci1913')
-rw-r--r-- | csci1913/Python/project1_strap012.py | 30 |
1 files changed, 20 insertions, 10 deletions
diff --git a/csci1913/Python/project1_strap012.py b/csci1913/Python/project1_strap012.py index af39315..ad75334 100644 --- a/csci1913/Python/project1_strap012.py +++ b/csci1913/Python/project1_strap012.py @@ -3,17 +3,22 @@ class Random: 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 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 += " " @@ -24,40 +29,40 @@ class Rule: string += " " return string -def filter(D, E): - if len(D) is 0: - return False - elif D[1] is E: - return True - else: - return filter(D[1:],E) + class Grammar: def __init__(self, seed): self.r = Random(seed) self.dictionary = {} + def rule(self, left, right): if left in self.dictionary: self.dictionary[left]+=(Rule(left,right),) else: self.dictionary[left]=(Rule(left, right),) + def generate(self): if 'Start' in self.dictionary: self.generating(self.dictionary['Start']) else: raise RuntimeError + def generating(self,strings): result='' for n in range (0, len(strings)): if strings[n] in self.dictionary: - result += select(strings[n]) + newStr = select(strings[n]) + generating(newStr) else: - result += strings[n] + result += str(strings[n]) result += " " return result + def select(self, left): rules=self.dictionary[left] - index=self.r.choose(len(left)) + index=self.r.choose(len(rules)) + @@ -74,3 +79,8 @@ G.rule('Story', ('Phrase',)) # 09 G.rule('Story', ('Phrase', 'and', 'Story')) # 10 G.rule('Story', ('Phrase', 'but', 'Story')) # 11 G.rule('Start', ('Story', '.')) # 12 +G.generate() +G.generate() +G.generate() +G.generate() +G.generate() |