4 "signature": "sha256:32b3b300745f022158bfde0b3fc0a50b11d36551211371d50140e9f9d2a7b8e1"
29 "WORDS = [w.strip() for w in open('/usr/share/dict/british-english').readlines() \n",
30 " if re.match(r'^[a-z]*$', w.strip())]"
41 "LETTER_COUNTS = collections.Counter(l.lower() for l in open('sherlock-holmes.txt').read() if l in string.ascii_letters)\n",
42 "LETTERS_IN_ORDER = [p[0] for p in LETTER_COUNTS.most_common()]"
65 " def __init__(self, target, player=None, lives=STARTING_LIVES):\n",
66 " self.lives = lives\n",
67 " self.player = player\n",
68 " self.target = target\n",
69 " self.discovered = list('_' * len(target))\n",
70 " self.wrong_letters = []\n",
71 " self.game_finished = False\n",
72 " self.game_won = False\n",
73 " self.game_lost = False\n",
75 " def find_all(self, letter):\n",
76 " return [p for p, l in enumerate(self.target) if l == letter]\n",
78 " def update_discovered_word(self, guessed_letter):\n",
79 " locations = self.find_all(guessed_letter)\n",
80 " for location in locations:\n",
81 " self.discovered[location] = guessed_letter\n",
82 " return self.discovered\n",
84 " def do_turn(self):\n",
86 " guess = self.player.guess(self.discovered, self.wrong_letters, self.lives)\n",
88 " guess = self.ask_for_guess()\n",
89 " if guess in self.target:\n",
90 " self.update_discovered_word(guess)\n",
93 " if guess not in self.wrong_letters:\n",
94 " self.wrong_letters += [guess]\n",
95 " if self.lives == 0:\n",
96 " self.game_finished = True\n",
97 " self.game_lost = True\n",
98 " if '_' not in self.discovered:\n",
99 " self.game_finished = True\n",
100 " self.game_won = True\n",
102 " def ask_for_guess(self):\n",
103 " print('Word:', ' '.join(self.discovered), \n",
104 " ' : Lives =', self.lives, \n",
105 " ', wrong guesses:', ' '.join(sorted(self.wrong_letters)))\n",
106 " guess = input('Enter letter: ').strip().lower()[0]\n",
109 " def play_game(self):\n",
110 " while not self.game_finished:\n",
112 " if not self.player:\n",
113 " self.report_on_game()\n",
114 " return self.game_won\n",
116 " def report_on_game(self):\n",
117 " if self.game_won:\n",
118 " print('You won! The word was', self.target)\n",
120 " print('You lost. The word was', self.target)\n",
121 " return self.game_won"
123 "language": "python",
132 "class PlayerFixedOrder:\n",
133 " def __init__(self, ordered_letters):\n",
134 " self.ordered_letters = ordered_letters\n",
136 " def guess(self, discovered, missed, lives):\n",
137 " guessed_letters = [l.lower() for l in discovered + missed if l in string.ascii_letters]\n",
138 " return [l for l in self.ordered_letters if l not in guessed_letters][0]"
140 "language": "python",
149 "class PlayerAlphabetical(PlayerFixedOrder):\n",
150 " def __init__(self):\n",
151 " super().__init__(string.ascii_lowercase)\n",
153 "class PlayerFreqOrdered(PlayerFixedOrder):\n",
154 " def __init__(self):\n",
155 " super().__init__(LETTERS_IN_ORDER)\n"
157 "language": "python",
169 "for _ in range(1000):\n",
170 " g = Game(random.choice(WORDS), player=PlayerAlphabetical())\n",
176 "language": "python",
180 "output_type": "stream",
188 "output_type": "stream",
196 "output_type": "stream",
204 "output_type": "stream",
212 "output_type": "stream",
220 "output_type": "stream",
228 "output_type": "stream",
236 "output_type": "stream",
244 "output_type": "stream",
252 "output_type": "stream",
260 "output_type": "stream",
268 "output_type": "stream",
276 "output_type": "stream",
284 "output_type": "stream",
292 "output_type": "stream",
300 "output_type": "stream",
308 "output_type": "stream",
316 "output_type": "stream",
324 "output_type": "stream",
332 "output_type": "stream",
340 "output_type": "stream",
348 "output_type": "stream",
356 "output_type": "stream",
364 "output_type": "stream",
372 "output_type": "stream",
380 "output_type": "stream",
388 "output_type": "stream",
396 "output_type": "stream",
404 "output_type": "stream",
412 "output_type": "stream",
420 "output_type": "stream",
428 "output_type": "stream",
436 "output_type": "stream",
444 "output_type": "stream",
452 "output_type": "stream",
460 "output_type": "stream",
468 "output_type": "stream",
476 "output_type": "stream",
484 "output_type": "stream",
492 "output_type": "stream",
500 "output_type": "stream",
504 "10 loops, best of 3: 64.2 ms per loop\n"
517 "for _ in range(1000):\n",
518 " g = Game(random.choice(WORDS), player=PlayerFreqOrdered())\n",
524 "language": "python",
528 "output_type": "stream",
536 "output_type": "stream",
544 "output_type": "stream",
552 "output_type": "stream",
560 "output_type": "stream",
568 "output_type": "stream",
576 "output_type": "stream",
584 "output_type": "stream",
592 "output_type": "stream",
600 "output_type": "stream",
608 "output_type": "stream",
616 "output_type": "stream",
624 "output_type": "stream",
632 "output_type": "stream",
640 "output_type": "stream",
648 "output_type": "stream",
656 "output_type": "stream",
664 "output_type": "stream",
672 "output_type": "stream",
680 "output_type": "stream",
688 "output_type": "stream",
696 "output_type": "stream",
704 "output_type": "stream",
712 "output_type": "stream",
720 "output_type": "stream",
728 "output_type": "stream",
736 "output_type": "stream",
744 "output_type": "stream",
752 "output_type": "stream",
760 "output_type": "stream",
768 "output_type": "stream",
776 "output_type": "stream",
784 "output_type": "stream",
792 "output_type": "stream",
800 "output_type": "stream",
808 "output_type": "stream",
816 "output_type": "stream",
824 "output_type": "stream",
832 "output_type": "stream",
840 "output_type": "stream",
848 "output_type": "stream",
852 "10 loops, best of 3: 96.2 ms per loop\n"
865 "for _ in range(1000):\n",
866 " g = Game(random.choice(WORDS), player=PlayerFixedOrder(list(reversed(string.ascii_lowercase))))\n",
872 "language": "python",
876 "output_type": "stream",
884 "output_type": "stream",
892 "output_type": "stream",
900 "output_type": "stream",
908 "output_type": "stream",
916 "output_type": "stream",
924 "output_type": "stream",
932 "output_type": "stream",
940 "output_type": "stream",
948 "output_type": "stream",
956 "output_type": "stream",
964 "output_type": "stream",
972 "output_type": "stream",
980 "output_type": "stream",
988 "output_type": "stream",
996 "output_type": "stream",
1004 "output_type": "stream",
1012 "output_type": "stream",
1020 "output_type": "stream",
1028 "output_type": "stream",
1036 "output_type": "stream",
1044 "output_type": "stream",
1052 "output_type": "stream",
1060 "output_type": "stream",
1068 "output_type": "stream",
1076 "output_type": "stream",
1084 "output_type": "stream",
1092 "output_type": "stream",
1100 "output_type": "stream",
1108 "output_type": "stream",
1116 "output_type": "stream",
1124 "output_type": "stream",
1132 "output_type": "stream",
1140 "output_type": "stream",
1148 "output_type": "stream",
1156 "output_type": "stream",
1164 "output_type": "stream",
1172 "output_type": "stream",
1180 "output_type": "stream",
1188 "output_type": "stream",
1196 "output_type": "stream",
1200 "10 loops, best of 3: 74.6 ms per loop\n"
1207 "cell_type": "code",
1210 "DICT_COUNTS = collections.Counter(l.lower() for l in open('/usr/share/dict/british-english').read() if l in string.ascii_letters)\n",
1211 "DICT_LETTERS_IN_ORDER = [p[0] for p in DICT_COUNTS.most_common()]"
1213 "language": "python",
1219 "cell_type": "code",
1224 "language": "python",
1229 "output_type": "pyout",
1230 "prompt_number": 12,
1232 "Counter({'s': 91332, 'e': 88692, 'i': 66900, 'a': 64468, 'r': 57460, 'n': 57128, 't': 52949, 'o': 49121, 'l': 40995, 'c': 31854, 'd': 28505, 'u': 26372, 'g': 22693, 'm': 22549, 'p': 22249, 'h': 19337, 'b': 15540, 'y': 12652, 'f': 10679, 'k': 8386, 'v': 8000, 'w': 7505, 'x': 2125, 'z': 2058, 'j': 1950, 'q': 1536})"
1239 "cell_type": "code",
1242 "print(DICT_LETTERS_IN_ORDER)\n",
1243 "print(LETTERS_IN_ORDER)"
1245 "language": "python",
1249 "output_type": "stream",
1252 "['s', 'e', 'i', 'a', 'r', 'n', 't', 'o', 'l', 'c', 'd', 'u', 'g', 'm', 'p', 'h', 'b', 'y', 'f', 'k', 'v', 'w', 'x', 'z', 'j', 'q']\n",
1253 "['e', 't', 'a', 'o', 'i', 'h', 'n', 's', 'r', 'd', 'l', 'u', 'm', 'w', 'c', 'y', 'f', 'g', 'p', 'b', 'v', 'k', 'x', 'j', 'q', 'z']\n"
1260 "cell_type": "code",
1264 "for _ in range(1000):\n",
1265 " g = Game(random.choice(WORDS), player=PlayerFixedOrder(DICT_LETTERS_IN_ORDER))\n",
1267 " if g.game_won:\n",
1271 "language": "python",
1275 "output_type": "stream",
1285 "cell_type": "code",
1288 "class PlayerAdaptiveLength:\n",
1289 " def __init__(self, words):\n",
1290 " self.all_words = words\n",
1291 " self.candidate_words = None\n",
1293 " def guess(self, discovered, missed, lives):\n",
1294 " if not self.candidate_words:\n",
1295 " self.set_ordered_letters(len(discovered))\n",
1296 " guessed_letters = [l.lower() for l in discovered + missed if l in string.ascii_letters]\n",
1297 " return [l for l in self.ordered_letters if l not in guessed_letters][0]\n",
1299 " def set_ordered_letters(self, word_len):\n",
1300 " self.candidate_words = [w for w in self.all_words if len(w) == word_len]\n",
1301 " counts = collections.Counter(l.lower() for l in ''.join(self.candidate_words) if l in string.ascii_letters)\n",
1302 " self.ordered_letters = [p[0] for p in counts.most_common()]"
1304 "language": "python",
1310 "cell_type": "code",
1314 "for _ in range(1000):\n",
1315 " g = Game(random.choice(WORDS), player=PlayerAdaptiveLength(WORDS))\n",
1317 " if g.game_won:\n",
1321 "language": "python",
1325 "output_type": "stream",
1335 "cell_type": "code",
1338 "class PlayerAdaptiveIncludedLetters:\n",
1339 " def __init__(self, words):\n",
1340 " self.candidate_words = words\n",
1342 " def guess(self, discovered, missed, lives):\n",
1343 " self.filter_candidate_words(discovered)\n",
1344 " self.set_ordered_letters()\n",
1345 " guessed_letters = [l.lower() for l in discovered + missed if l in string.ascii_letters]\n",
1346 " return [l for l in self.ordered_letters if l not in guessed_letters][0]\n",
1348 " def filter_candidate_words(self, discovered):\n",
1349 " exp = re.compile('^' + ''.join(discovered).replace('_', '.') + '$')\n",
1350 " self.candidate_words = [w for w in self.candidate_words if exp.match(w)]\n",
1352 " def set_ordered_letters(self):\n",
1353 " counts = collections.Counter(l.lower() for l in ''.join(self.candidate_words) if l in string.ascii_letters)\n",
1354 " self.ordered_letters = [p[0] for p in counts.most_common()]"
1356 "language": "python",
1362 "cell_type": "code",
1366 "for _ in range(1000):\n",
1367 " g = Game(random.choice(WORDS), player=PlayerAdaptiveIncludedLetters(WORDS))\n",
1369 " if g.game_won:\n",
1373 "language": "python",
1377 "output_type": "stream",
1387 "cell_type": "code",
1390 "re.match('^[^xaz]*$', 'happy')"
1392 "language": "python",
1398 "cell_type": "code",
1401 "class PlayerAdaptiveExcludedLetters:\n",
1402 " def __init__(self, words):\n",
1403 " self.candidate_words = words\n",
1405 " def guess(self, discovered, missed, lives):\n",
1406 " self.filter_candidate_words(missed)\n",
1407 " self.set_ordered_letters()\n",
1408 " guessed_letters = [l.lower() for l in discovered + missed if l in string.ascii_letters]\n",
1409 " return [l for l in self.ordered_letters if l not in guessed_letters][0]\n",
1411 " def filter_candidate_words(self, missed):\n",
1413 " exp = re.compile('^[^' + ''.join(missed) + ']*$')\n",
1414 " self.candidate_words = [w for w in self.candidate_words if exp.match(w)]\n",
1416 " def set_ordered_letters(self):\n",
1417 " counts = collections.Counter(l.lower() for l in ''.join(self.candidate_words) if l in string.ascii_letters)\n",
1418 " self.ordered_letters = [p[0] for p in counts.most_common()]"
1420 "language": "python",
1426 "cell_type": "code",
1430 "for _ in range(1000):\n",
1431 " g = Game(random.choice(WORDS), player=PlayerAdaptiveExcludedLetters(WORDS))\n",
1433 " if g.game_won:\n",
1437 "language": "python",
1441 "output_type": "stream",
1451 "cell_type": "code",
1454 "class PlayerAdaptivePattern:\n",
1455 " def __init__(self, words):\n",
1456 " self.candidate_words = words\n",
1458 " def guess(self, discovered, missed, lives):\n",
1459 " self.filter_candidate_words(discovered, missed)\n",
1460 " self.set_ordered_letters()\n",
1461 " guessed_letters = [l.lower() for l in discovered + missed if l in string.ascii_letters]\n",
1462 " return [l for l in self.ordered_letters if l not in guessed_letters][0]\n",
1464 " def filter_candidate_words(self, discovered, missed):\n",
1465 " attempted_letters = list(set(l.lower() for l in discovered + missed if l in string.ascii_letters))\n",
1466 " if attempted_letters:\n",
1467 " exclusion_pattern = '[^' + ''.join(attempted_letters) + ']'\n",
1469 " exclusion_pattern = '.'\n",
1470 " exp = re.compile('^' + ''.join(discovered).replace('_', exclusion_pattern) + '$')\n",
1471 " self.candidate_words = [w for w in self.candidate_words if exp.match(w)]\n",
1473 " def set_ordered_letters(self):\n",
1474 " counts = collections.Counter(l.lower() for l in ''.join(self.candidate_words) if l in string.ascii_letters)\n",
1475 " self.ordered_letters = [p[0] for p in counts.most_common()]\n"
1477 "language": "python",
1483 "cell_type": "code",
1487 "for _ in range(1000):\n",
1488 " g = Game(random.choice(WORDS), player=PlayerAdaptivePattern(WORDS))\n",
1490 " if g.game_won:\n",
1494 "language": "python",
1498 "output_type": "stream",
1508 "cell_type": "code",
1514 "for _ in range(1000):\n",
1515 " g = Game(random.choice(WORDS), player=PlayerAdaptivePattern(WORDS))\n",
1517 " if g.game_won:\n",
1521 "language": "python",
1525 "output_type": "stream",
1533 "output_type": "stream",
1541 "output_type": "stream",
1549 "output_type": "stream",
1553 "1 loops, best of 3: 30.6 s per loop\n"
1560 "cell_type": "code",
1563 "class PlayerAdaptive:\n",
1564 " def __init__(self, words):\n",
1565 " self.candidate_words = words\n",
1567 " def guess(self, discovered, missed, lives):\n",
1568 " self.filter_candidate_words(discovered, missed)\n",
1569 " self.set_ordered_letters()\n",
1570 " guessed_letters = [l.lower() for l in discovered + missed if l in string.ascii_letters]\n",
1571 " return [l for l in self.ordered_letters if l not in guessed_letters][0]\n",
1573 " def filter_candidate_words(self, discovered, missed):\n",
1576 " def set_ordered_letters(self):\n",
1577 " counts = collections.Counter(l.lower() \n",
1578 " for l in ''.join(self.candidate_words) + string.ascii_lowercase \n",
1579 " if l in string.ascii_letters)\n",
1580 " self.ordered_letters = [p[0] for p in counts.most_common()]"
1582 "language": "python",
1588 "cell_type": "code",
1591 "class PlayerAdaptiveLength(PlayerAdaptive):\n",
1592 " def __init__(self, words):\n",
1593 " super().__init__(words)\n",
1594 " self.word_len = None\n",
1595 " self.ordered_letters = None\n",
1597 " def filter_candidate_words(self, discovered, missed):\n",
1598 " if not self.word_len:\n",
1599 " self.word_len = len(discovered)\n",
1600 " self.candidate_words = [w for w in self.candidate_words if len(w) == self.word_len]\n",
1602 " def set_ordered_letters(self):\n",
1603 " if not self.ordered_letters:\n",
1604 " super().set_ordered_letters()"
1606 "language": "python",
1612 "cell_type": "code",
1615 "class PlayerAdaptiveIncludedLetters(PlayerAdaptive):\n",
1616 " def filter_candidate_words(self, discovered, missed):\n",
1617 " exp = re.compile('^' + ''.join(discovered).replace('_', '.') + '$')\n",
1618 " self.candidate_words = [w for w in self.candidate_words if exp.match(w)]"
1620 "language": "python",
1626 "cell_type": "code",
1629 "class PlayerAdaptiveExcludedLetters(PlayerAdaptive):\n",
1630 " def filter_candidate_words(self, discovered, missed):\n",
1632 " exp = re.compile('^[^' + ''.join(missed) + ']*$')\n",
1633 " self.candidate_words = [w for w in self.candidate_words if exp.match(w)] "
1635 "language": "python",
1641 "cell_type": "code",
1644 "class PlayerAdaptivePattern(PlayerAdaptive):\n",
1645 " def filter_candidate_words(self, discovered, missed):\n",
1646 " attempted_letters = [l for l in discovered if l != '_'] + missed\n",
1647 " if attempted_letters:\n",
1648 " exclusion_pattern = '[^' + ''.join(attempted_letters) + ']'\n",
1650 " exclusion_pattern = '.'\n",
1651 " exp = re.compile('^' + ''.join(discovered).replace('_', exclusion_pattern) + '$')\n",
1652 " self.candidate_words = [w for w in self.candidate_words if exp.match(w)]"
1654 "language": "python",
1660 "cell_type": "code",
1666 "for _ in range(1000):\n",
1667 " g = Game(random.choice(WORDS), player=PlayerAdaptiveLength(WORDS))\n",
1669 " if g.game_won:\n",
1673 "language": "python",
1677 "output_type": "stream",
1685 "output_type": "stream",
1693 "output_type": "stream",
1701 "output_type": "stream",
1705 "1 loops, best of 3: 15.9 s per loop\n"
1712 "cell_type": "code",
1718 "for _ in range(1000):\n",
1719 " g = Game(random.choice(WORDS), player=PlayerAdaptiveIncludedLetters(WORDS))\n",
1721 " if g.game_won:\n",
1725 "language": "python",
1729 "output_type": "stream",
1737 "output_type": "stream",
1745 "output_type": "stream",
1753 "output_type": "stream",
1757 "1 loops, best of 3: 36.9 s per loop\n"
1764 "cell_type": "code",
1770 "for _ in range(1000):\n",
1771 " g = Game(random.choice(WORDS), player=PlayerAdaptiveExcludedLetters(WORDS))\n",
1773 " if g.game_won:\n",
1777 "language": "python",
1781 "output_type": "stream",
1789 "output_type": "stream",
1797 "output_type": "stream",
1805 "output_type": "stream",
1809 "1 loops, best of 3: 7min 18s per loop\n"
1816 "cell_type": "code",
1822 "for _ in range(1000):\n",
1823 " g = Game(random.choice(WORDS), player=PlayerAdaptivePattern(WORDS))\n",
1825 " if g.game_won:\n",
1829 "language": "python",
1833 "output_type": "stream",
1841 "output_type": "stream",
1849 "output_type": "stream",
1857 "output_type": "stream",
1861 "1 loops, best of 3: 31.1 s per loop\n"
1868 "cell_type": "code",
1871 "for _ in range(1000):\n",
1872 " g = Game(random.choice(WORDS), player=PlayerAdaptivePattern(WORDS))\n",
1874 " if not g.game_won:\n",
1875 " print(g.target, g.discovered, g.wrong_letters)"
1877 "language": "python",
1881 "output_type": "stream",
1884 "naked ['_', 'a', '_', 'e', 'd'] ['s', 'r', 'w', 'c', 't', 'g', 'l', 'f', 'p', 'm']\n",
1889 "output_type": "stream",
1892 " ['_', 'o', 'u', 'n', 'd'] ['s', 'e', 'a', 'y', 'p', 'm', 'f', 'h', 'b', 'r']\n",
1897 "output_type": "stream",
1900 " ['_', 'u', 't'] ['a', 'o', 'e', 'i', 'b', 'g', 'n', 'p', 'm', 'c']\n",
1905 "output_type": "stream",
1908 " ['_', 'u', '_', '_', 'e', 'r'] ['d', 's', 'i', 'a', 'o', 't', 'b', 'n', 'm', 'l']\n",
1913 "output_type": "stream",
1916 " ['_', 'o', '_'] ['a', 't', 'b', 'd', 'w', 'p', 'n', 's', 'g', 'y']\n",
1921 "output_type": "stream",
1924 " ['_', 'i', 'l', 'l', 's'] ['e', 'o', 'a', 'g', 'p', 'm', 'k', 'f', 'h', 'd']\n",
1929 "output_type": "stream",
1932 " ['_', 'u', 'n', 'n', 'y'] ['s', 'e', 'a', 'o', 'i', 'm', 'l', 'g', 'f', 't']\n",
1937 "output_type": "stream",
1940 " ['c', 'u', '_', '_', 'i', 'n', 'g'] ['e', 'a', 'o', 'l', 's', 'f', 'p', 'b', 't', 'm']\n",
1945 "output_type": "stream",
1948 " ['_', 'o', '_'] ['a', 't', 'b', 'd', 'w', 'p', 'n', 's', 'g', 'y']\n",
1953 "output_type": "stream",
1956 " ['_', 'u', '_', '_'] ['e', 's', 'o', 'a', 'i', 'l', 'f', 'r', 'n', 'm']\n",
1961 "output_type": "stream",
1964 " ['_', 'u', '_', '_', 'e', 'd'] ['a', 'o', 'i', 'l', 's', 't', 'f', 'p', 'g', 'b']\n",
1969 "output_type": "stream",
1972 " ['f', 'l', 'a', '_'] ['e', 's', 'o', 'r', 'y', 'g', 'k', 'x', 'p', 'n']\n"
1979 "cell_type": "code",
1982 "iterations = 10000\n",
1984 "for _ in range(iterations):\n",
1985 " g = Game(random.choice(WORDS), player=PlayerAdaptivePattern(WORDS))\n",
1987 " if g.game_won:\n",
1989 "print(wins / iterations)"
1991 "language": "python",
1995 "output_type": "stream",
2005 "cell_type": "code",
2008 "class PlayerAdaptiveNoRegex:\n",
2009 " def __init__(self, words):\n",
2010 " self.candidate_words = words\n",
2012 " def guess(self, discovered, missed, lives):\n",
2013 " self.filter_candidate_words(discovered, missed)\n",
2014 " self.set_ordered_letters()\n",
2015 " guessed_letters = [l.lower() for l in discovered + missed if l in string.ascii_letters]\n",
2016 " return [l for l in self.ordered_letters if l not in guessed_letters][0]\n",
2018 " def filter_candidate_words(self, discovered, missed):\n",
2021 " def set_ordered_letters(self):\n",
2022 " counts = collections.Counter(l.lower() \n",
2023 " for l in ''.join(self.candidate_words) + string.ascii_lowercase \n",
2024 " if l in string.ascii_letters)\n",
2025 " self.ordered_letters = [p[0] for p in counts.most_common()]\n",
2027 " def match(self, pattern, target, excluded=None):\n",
2028 " if not excluded:\n",
2030 " if len(pattern) != len(target):\n",
2032 " for m, c in zip(pattern, target):\n",
2033 " if m == '_' and c not in excluded:\n",
2036 " elif m != '_' and m == c:\n",
2043 "language": "python",
2049 "cell_type": "code",
2052 "class PlayerAdaptiveLengthNoRegex(PlayerAdaptiveNoRegex):\n",
2053 " def __init__(self, words):\n",
2054 " super().__init__(words)\n",
2055 " self.word_len = None\n",
2056 " self.ordered_letters = None\n",
2058 " def filter_candidate_words(self, discovered, missed):\n",
2059 " if not self.word_len:\n",
2060 " self.word_len = len(discovered)\n",
2061 " self.candidate_words = [w for w in self.candidate_words if len(w) == self.word_len]\n",
2063 " def set_ordered_letters(self):\n",
2064 " if not self.ordered_letters:\n",
2065 " super().set_ordered_letters()"
2067 "language": "python",
2073 "cell_type": "code",
2076 "class PlayerAdaptiveIncludedLettersNoRegex(PlayerAdaptiveNoRegex):\n",
2077 " def filter_candidate_words(self, discovered, missed):\n",
2078 " self.candidate_words = [w for w in self.candidate_words if self.match(discovered, w)]"
2080 "language": "python",
2086 "cell_type": "code",
2089 "class PlayerAdaptiveExcludedLettersNoRegex(PlayerAdaptiveNoRegex):\n",
2090 " def filter_candidate_words(self, discovered, missed):\n",
2092 " empty_target = '_' * len(discovered)\n",
2093 " self.candidate_words = [w for w in self.candidate_words if self.match(empty_target, w, missed)] "
2095 "language": "python",
2101 "cell_type": "code",
2104 "class PlayerAdaptivePatternNoRegex(PlayerAdaptiveNoRegex):\n",
2105 " def filter_candidate_words(self, discovered, missed):\n",
2106 " attempted_letters = [l for l in discovered if l != '_'] + missed\n",
2107 " self.candidate_words = [w for w in self.candidate_words if self.match(discovered, w, attempted_letters)]"
2109 "language": "python",
2115 "cell_type": "code",
2121 "for _ in range(1000):\n",
2122 " g = Game(random.choice(WORDS), player=PlayerAdaptiveLengthNoRegex(WORDS))\n",
2124 " if g.game_won:\n",
2128 "language": "python",
2132 "output_type": "stream",
2140 "output_type": "stream",
2148 "output_type": "stream",
2156 "output_type": "stream",
2160 "1 loops, best of 3: 16 s per loop\n"
2167 "cell_type": "code",
2173 "for _ in range(1000):\n",
2174 " g = Game(random.choice(WORDS), player=PlayerAdaptiveIncludedLettersNoRegex(WORDS))\n",
2176 " if g.game_won:\n",
2180 "language": "python",
2184 "output_type": "stream",
2192 "output_type": "stream",
2200 "output_type": "stream",
2208 "output_type": "stream",
2212 "1 loops, best of 3: 48 s per loop\n"
2219 "cell_type": "code",
2225 "for _ in range(1000):\n",
2226 " g = Game(random.choice(WORDS), player=PlayerAdaptiveExcludedLettersNoRegex(WORDS))\n",
2228 " if g.game_won:\n",
2232 "language": "python",
2236 "output_type": "stream",
2244 "output_type": "stream",
2252 "output_type": "stream",
2260 "output_type": "stream",
2264 "1 loops, best of 3: 4min 59s per loop\n"
2271 "cell_type": "code",
2277 "for _ in range(1000):\n",
2278 " g = Game(random.choice(WORDS), player=PlayerAdaptivePatternNoRegex(WORDS))\n",
2280 " if g.game_won:\n",
2284 "language": "python",
2288 "output_type": "stream",
2296 "output_type": "stream",
2304 "output_type": "stream",
2312 "output_type": "stream",
2316 "1 loops, best of 3: 37.9 s per loop\n"
2323 "cell_type": "code",
2326 "language": "python",