---
jupyter:
jupytext:
- formats: ipynb,md,py:percent
+ formats: ipynb,md
text_representation:
extension: .md
format_name: markdown
format_version: '1.3'
- jupytext_version: 1.15.0
+ jupytext_version: 1.14.5
kernelspec:
display_name: Python 3 (ipykernel)
language: python
import random
```
-```python
-gencount = 0
-```
-
-```python
-len([w for w in dictionary if 's' in w])
-```
-
-```python
-len([w for w in dictionary if 's' not in w])
-```
-
-```python
-dset = set(frozenset(w) for w in dictionary)
-len(dset), len(dictionary)
-```
-
-```python
-len([w for w in dset if 's' in w])
-```
-
-```python
-len([w for w in dset if 's' not in w])
-```
-
-```python
-sw = random.choice([w for w in dictionary if 's' in w])
-sw
-```
-
-```python
-swrel = [w for w in dictionary if 's' not in w if edit_distance(w, sw) <=3]
-len(swrel)
-```
-
-```python
-swrel
-```
-
```python
def include_exclude_clue(letter: str, limit: int = 3) -> (RiddleClue, RiddleClue):
with_letter = [w for w in dictionary if letter in w]
finished = False
while not finished:
- global gencount
- gencount +=1
a = random.choice(with_letter)
b = random.choice(without_letter)
finished = ((edit_distance(a, b) <= limit) and
finished = False
while not finished:
- global gencount
- gencount +=1
a = random.choice(with_letter)
b = random.choice(with_letter)
finished = ((a != b) and
finished = False
while not finished:
- global gencount
- gencount +=1
a = random.choice(without_letter)
b = random.choice(without_letter)
finished = ((a != b) and
solve_riddle(collapse_riddle_clues(sample_riddle))
```
-```python
-# write_riddle(sample_riddle)
-```
-
-```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))
-```
-
```python
def valid_random_riddle(word: str) -> Riddle:
finished = False
```
```python
-# import time
-# w_times = []
-# c_times = []
-# for _ in range(1000):
-# w1, c1 = time.perf_counter(), time.process_time()
-# valid_random_riddle(random.choice(dictionary))
-# w2, c2 = time.perf_counter(), time.process_time()
-# w_times.append(w2 - w1)
-# c_times.append(c2 - c1)
-
-# with open('cpu_times.txt', 'w') as f:
-# f.writelines(f'{t}\n' for t in c_times)
-# with open('wall_times.txt', 'w') as f:
-# f.writelines(f'{t}\n' for t in w_times)
-
-```
-
-```python
-glcounts = []
+import time
+import csv
+reports = []
for _ in range(1000):
- gencount = 0
+ 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)
- glcounts.append((gencount, linecount))
+ 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)
-with open('linecounts.txt', 'w') as f:
- f.write('"Lines generated","Lines in riddle"\n')
- f.writelines(f'{g},{l}\n' for g, l in glcounts)
+ writer.writeheader()
+ for r in reports:
+ writer.writerow(r)
```
```python
```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
```
```
```python
-sample_riddle = valid_random_riddle("elephant")
+sample_riddle = valid_random_riddle("riddle")
sample_riddle
```