extension: .md
format_name: markdown
format_version: '1.3'
- jupytext_version: 1.14.5
+ jupytext_version: 1.15.0
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
, 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
+solve_riddle(collapse_riddle_clues(sample_riddle))
+```
+```python
+# write_riddle(sample_riddle)
```
```python
-sample_riddle = random_riddle('sonnet', limit=4)
-sample_riddle
+# sample_riddle = random_riddle('sonnet', limit=4)
+# sample_riddle
```
```python
return riddle
```
+```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 = []
+for _ in range(1000):
+ gencount = 0
+ r = valid_random_riddle(random.choice(dictionary))
+ linecount = len(r)
+ glcounts.append((gencount, linecount))
+
+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)
+```
+
```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}"
extension: .md
format_name: markdown
format_version: '1.3'
- jupytext_version: 1.14.5
+ jupytext_version: 1.15.0
kernelspec:
display_name: Python 3 (ipykernel)
language: python
```
```python
-stop_words = set('my is in within lies and also always you will find the found'.split())
-negative_words = set('but not never neither nor'.split())
+stop_words = set('my is in within lies and also always you will find the found but'.split())
+negative_words = set('not never neither nor'.split())
```
```python
return len(t)
if t == "":
return len(s)
- if s[-1] == t[-1]:
+ if s[0] == t[0]:
cost = 0
else:
cost = 1
res = min(
- [ edit_distance(s[:-1], t)+1
- , edit_distance(s, t[:-1])+1
- , edit_distance(s[:-1], t[:-1]) + cost
+ [ edit_distance(s[1:], t) + 1
+ , edit_distance(s, t[1:]) + 1
+ , edit_distance(s[1:], t[1:]) + cost
])
return res
```
+```python
+dictionary_neighbours = {
+ w: [o for o in dictionary
+ if edit_distance(w, o) <= 5
+ if not set(w) <= set(o)
+ if not set(o) <= set(w)]
+ for w in dictionary}
+
+dictionary_neighbours = {w: ns
+ for w, ns in dictionary_neighbours.items()
+ if ns}
+```
+
```python
def collapse_riddle_clues(elems : Riddle) -> RiddleElems:
"""Combine the two parts of a riddle line into one element for solving.