Optimised riddle creation
[riddle-generator.git] / riddle_creator.md
index 36b939ac7ec65a26c9628b47325467ccadf2a199..5b4b43e4071fa125cd0a6424765d1d05f08663e2 100644 (file)
@@ -1,7 +1,7 @@
 ---
 jupyter:
   jupytext:
-    formats: ipynb,md,py:percent
+    formats: ipynb,md
     text_representation:
       extension: .md
       format_name: markdown
@@ -83,43 +83,34 @@ def random_clue( letter: str
                , ie_limit: int = 3
                , ii_limit: int = 2
                , ee_limit: int = 2) -> (RiddleClue, RiddleClue):
-  r = random.random()
-  if r <= 0.7:
+  clue_type = random.choices(['include_exclude', 'include_include', 'exclude_exclude'],
+                             weights=[7, 2, 1],
+                             k=1)[0]
+  if clue_type == 'include_exclude':
     return include_exclude_clue(letter, limit=ie_limit)
-  elif r <= 0.9:
+  elif clue_type =='include_include':
     return include_include_clue(letter, limit=ii_limit)
   else:
     return exclude_exclude_clue(letter, limit=ee_limit)
 ```
 
 ```python
-def random_riddle(word: str, limit: int = 3) -> Riddle:
-  return {i+1 : random_clue(l, ie_limit=limit)
+def random_riddle( word: str
+                 , ie_limit: int = 3
+                 , ii_limit: int = 2
+                 , ee_limit: int = 2
+                 ) -> Riddle:
+  return {i+1 : 
+    random_clue(l, 
+                ie_limit=ie_limit, ii_limit=ii_limit, ee_limit=ee_limit)
           for i, l in enumerate(word)}  
 ```
 
 ```python
-sample_riddle = random_riddle('sonnet')
+sample_riddle = random_riddle('teacup')
 sample_riddle
 ```
 
-```python
-
-```
-
-```python
-sample_riddle = random_riddle('sonnet', limit=4)
-sample_riddle
-```
-
-```python
-sample_riddle
-```
-
-```python
-collapse_riddle_clues(sample_riddle)
-```
-
 ```python
 solve_riddle(collapse_riddle_clues(sample_riddle))
 ```
@@ -134,6 +125,30 @@ def valid_random_riddle(word: str) -> Riddle:
   return riddle
 ```
 
+```python
+import time
+import csv
+reports = []
+for _ in range(1000):
+  w1, c1 = time.perf_counter(), time.process_time()
+  r = valid_random_riddle(random.choice(dictionary))
+  w2, c2 = time.perf_counter(), time.process_time()
+  linecount = len(r)
+  reports.append({'wall_time': w2 - w1,
+                  'cpu_time': c2 - c1,
+                  'riddle_lines': linecount})
+  w_times.append(w2 - w1)
+  c_times.append(c2 - c1)
+
+with open('metrics_original.csv', 'w', newline='') as csvfile:
+  fieldnames = list(reports[0].keys())
+  writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
+
+  writer.writeheader()
+  for r in reports:
+    writer.writerow(r)
+```
+
 ```python
 def write_include_exclude_line(clue_a: RiddleClue, clue_b: RiddleClue) -> str:
   line = f"is in {clue_a.word} but not in {clue_b.word}"
@@ -142,7 +157,10 @@ def write_include_exclude_line(clue_a: RiddleClue, clue_b: RiddleClue) -> str:
 
 ```python
 def write_include_include_line(clue_a: RiddleClue, clue_b: RiddleClue) -> str:
-  line = f"is in {clue_a.word} and also in {clue_b.word}"
+  if random.randrange(2) == 0:
+    line = f"is in {clue_a.word} and also in {clue_b.word}"
+  else:
+    line = f"is in both {clue_a.word} and {clue_b.word}"
   return line
 ```