X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=riddle_creator.md;h=5b4b43e4071fa125cd0a6424765d1d05f08663e2;hb=95c4c545a4abe7ef5f222b674da8535739ef1fcb;hp=36b939ac7ec65a26c9628b47325467ccadf2a199;hpb=ce34915246926441c163272e09f1343db3fd1955;p=riddle-generator.git diff --git a/riddle_creator.md b/riddle_creator.md index 36b939a..5b4b43e 100644 --- a/riddle_creator.md +++ b/riddle_creator.md @@ -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 ```