aboutsummaryrefslogtreecommitdiffstats
path: root/csci1913
diff options
context:
space:
mode:
Diffstat (limited to 'csci1913')
-rw-r--r--csci1913/Python/project1_strap012.py17
1 files changed, 11 insertions, 6 deletions
diff --git a/csci1913/Python/project1_strap012.py b/csci1913/Python/project1_strap012.py
index e0c73bb..8355400 100644
--- a/csci1913/Python/project1_strap012.py
+++ b/csci1913/Python/project1_strap012.py
@@ -50,12 +50,17 @@ class Grammar:
def select(self, left):
rules=self.dictionary[left]
- total=len(rules)
- index=self.r.choose(total)
- chosen=rules[0] #Debug Kludge
+ total=0
+ chosenOne=0
+ for m in range (0, len(rules)):
+ total+=rules[m].count
+ index = self.r.choose(total)
+ chosen=rules[0]
while index > 0:
- chosen=rules[index]
- index-=rules[index].count
+ index-=rules[chosenOne].count
+ chosenOne+=1
+ else:
+ chosen=rules[chosenOne]
for n in range (0, len(rules)):
if rules[n] is not chosen:
rules[n].count+=1
@@ -88,4 +93,4 @@ G.rule('Story', ('Phrase', 'and', 'Story')) # 10
G.rule('Story', ('Phrase', 'but', 'Story')) # 11
G.rule('Start', ('Story', '.')) # 12
for n in range (0,5):
- print(G.generate()) \ No newline at end of file
+ print(G.generate())