20a12e03e4eb6ebebb04a351612e4093ee64f41c
[cas-master-teacher-training.git] / hangman-better.ipynb
1 {
2 "metadata": {
3 "name": "",
4 "signature": "sha256:32b3b300745f022158bfde0b3fc0a50b11d36551211371d50140e9f9d2a7b8e1"
5 },
6 "nbformat": 3,
7 "nbformat_minor": 0,
8 "worksheets": [
9 {
10 "cells": [
11 {
12 "cell_type": "code",
13 "collapsed": false,
14 "input": [
15 "import re\n",
16 "import random\n",
17 "import string\n",
18 "import collections"
19 ],
20 "language": "python",
21 "metadata": {},
22 "outputs": [],
23 "prompt_number": 1
24 },
25 {
26 "cell_type": "code",
27 "collapsed": false,
28 "input": [
29 "WORDS = [w.strip() for w in open('/usr/share/dict/british-english').readlines() \n",
30 " if re.match(r'^[a-z]*$', w.strip())]"
31 ],
32 "language": "python",
33 "metadata": {},
34 "outputs": [],
35 "prompt_number": 2
36 },
37 {
38 "cell_type": "code",
39 "collapsed": false,
40 "input": [
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()]"
43 ],
44 "language": "python",
45 "metadata": {},
46 "outputs": [],
47 "prompt_number": 3
48 },
49 {
50 "cell_type": "code",
51 "collapsed": false,
52 "input": [
53 "STARTING_LIVES = 10"
54 ],
55 "language": "python",
56 "metadata": {},
57 "outputs": [],
58 "prompt_number": 4
59 },
60 {
61 "cell_type": "code",
62 "collapsed": false,
63 "input": [
64 "class Game:\n",
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",
74 " \n",
75 " def find_all(self, letter):\n",
76 " return [p for p, l in enumerate(self.target) if l == letter]\n",
77 " \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",
83 " \n",
84 " def do_turn(self):\n",
85 " if self.player:\n",
86 " guess = self.player.guess(self.discovered, self.wrong_letters, self.lives)\n",
87 " else:\n",
88 " guess = self.ask_for_guess()\n",
89 " if guess in self.target:\n",
90 " self.update_discovered_word(guess)\n",
91 " else:\n",
92 " self.lives -= 1\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",
101 " \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",
107 " return guess\n",
108 " \n",
109 " def play_game(self):\n",
110 " while not self.game_finished:\n",
111 " self.do_turn()\n",
112 " if not self.player:\n",
113 " self.report_on_game()\n",
114 " return self.game_won\n",
115 " \n",
116 " def report_on_game(self):\n",
117 " if self.game_won:\n",
118 " print('You won! The word was', self.target)\n",
119 " else:\n",
120 " print('You lost. The word was', self.target)\n",
121 " return self.game_won"
122 ],
123 "language": "python",
124 "metadata": {},
125 "outputs": [],
126 "prompt_number": 5
127 },
128 {
129 "cell_type": "code",
130 "collapsed": false,
131 "input": [
132 "class PlayerFixedOrder:\n",
133 " def __init__(self, ordered_letters):\n",
134 " self.ordered_letters = ordered_letters\n",
135 " \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]"
139 ],
140 "language": "python",
141 "metadata": {},
142 "outputs": [],
143 "prompt_number": 6
144 },
145 {
146 "cell_type": "code",
147 "collapsed": false,
148 "input": [
149 "class PlayerAlphabetical(PlayerFixedOrder):\n",
150 " def __init__(self):\n",
151 " super().__init__(string.ascii_lowercase)\n",
152 "\n",
153 "class PlayerFreqOrdered(PlayerFixedOrder):\n",
154 " def __init__(self):\n",
155 " super().__init__(LETTERS_IN_ORDER)\n"
156 ],
157 "language": "python",
158 "metadata": {},
159 "outputs": [],
160 "prompt_number": 7
161 },
162 {
163 "cell_type": "code",
164 "collapsed": false,
165 "input": [
166 "%%timeit\n",
167 "\n",
168 "wins = 0\n",
169 "for _ in range(1000):\n",
170 " g = Game(random.choice(WORDS), player=PlayerAlphabetical())\n",
171 " g.play_game()\n",
172 " if g.game_won:\n",
173 " wins += 1\n",
174 "print(wins)"
175 ],
176 "language": "python",
177 "metadata": {},
178 "outputs": [
179 {
180 "output_type": "stream",
181 "stream": "stdout",
182 "text": [
183 "50\n",
184 "59"
185 ]
186 },
187 {
188 "output_type": "stream",
189 "stream": "stdout",
190 "text": [
191 "\n",
192 "33"
193 ]
194 },
195 {
196 "output_type": "stream",
197 "stream": "stdout",
198 "text": [
199 "\n",
200 "53"
201 ]
202 },
203 {
204 "output_type": "stream",
205 "stream": "stdout",
206 "text": [
207 "\n",
208 "49"
209 ]
210 },
211 {
212 "output_type": "stream",
213 "stream": "stdout",
214 "text": [
215 "\n",
216 "42"
217 ]
218 },
219 {
220 "output_type": "stream",
221 "stream": "stdout",
222 "text": [
223 "\n",
224 "43"
225 ]
226 },
227 {
228 "output_type": "stream",
229 "stream": "stdout",
230 "text": [
231 "\n",
232 "54"
233 ]
234 },
235 {
236 "output_type": "stream",
237 "stream": "stdout",
238 "text": [
239 "\n",
240 "49"
241 ]
242 },
243 {
244 "output_type": "stream",
245 "stream": "stdout",
246 "text": [
247 "\n",
248 "54"
249 ]
250 },
251 {
252 "output_type": "stream",
253 "stream": "stdout",
254 "text": [
255 "\n",
256 "45"
257 ]
258 },
259 {
260 "output_type": "stream",
261 "stream": "stdout",
262 "text": [
263 "\n",
264 "46"
265 ]
266 },
267 {
268 "output_type": "stream",
269 "stream": "stdout",
270 "text": [
271 "\n",
272 "44"
273 ]
274 },
275 {
276 "output_type": "stream",
277 "stream": "stdout",
278 "text": [
279 "\n",
280 "45"
281 ]
282 },
283 {
284 "output_type": "stream",
285 "stream": "stdout",
286 "text": [
287 "\n",
288 "48"
289 ]
290 },
291 {
292 "output_type": "stream",
293 "stream": "stdout",
294 "text": [
295 "\n",
296 "51"
297 ]
298 },
299 {
300 "output_type": "stream",
301 "stream": "stdout",
302 "text": [
303 "\n",
304 "46"
305 ]
306 },
307 {
308 "output_type": "stream",
309 "stream": "stdout",
310 "text": [
311 "\n",
312 "54"
313 ]
314 },
315 {
316 "output_type": "stream",
317 "stream": "stdout",
318 "text": [
319 "\n",
320 "49"
321 ]
322 },
323 {
324 "output_type": "stream",
325 "stream": "stdout",
326 "text": [
327 "\n",
328 "44"
329 ]
330 },
331 {
332 "output_type": "stream",
333 "stream": "stdout",
334 "text": [
335 "\n",
336 "42"
337 ]
338 },
339 {
340 "output_type": "stream",
341 "stream": "stdout",
342 "text": [
343 "\n",
344 "44"
345 ]
346 },
347 {
348 "output_type": "stream",
349 "stream": "stdout",
350 "text": [
351 "\n",
352 "48"
353 ]
354 },
355 {
356 "output_type": "stream",
357 "stream": "stdout",
358 "text": [
359 "\n",
360 "39"
361 ]
362 },
363 {
364 "output_type": "stream",
365 "stream": "stdout",
366 "text": [
367 "\n",
368 "46"
369 ]
370 },
371 {
372 "output_type": "stream",
373 "stream": "stdout",
374 "text": [
375 "\n",
376 "43"
377 ]
378 },
379 {
380 "output_type": "stream",
381 "stream": "stdout",
382 "text": [
383 "\n",
384 "50"
385 ]
386 },
387 {
388 "output_type": "stream",
389 "stream": "stdout",
390 "text": [
391 "\n",
392 "47"
393 ]
394 },
395 {
396 "output_type": "stream",
397 "stream": "stdout",
398 "text": [
399 "\n",
400 "52"
401 ]
402 },
403 {
404 "output_type": "stream",
405 "stream": "stdout",
406 "text": [
407 "\n",
408 "46"
409 ]
410 },
411 {
412 "output_type": "stream",
413 "stream": "stdout",
414 "text": [
415 "\n",
416 "55"
417 ]
418 },
419 {
420 "output_type": "stream",
421 "stream": "stdout",
422 "text": [
423 "\n",
424 "47"
425 ]
426 },
427 {
428 "output_type": "stream",
429 "stream": "stdout",
430 "text": [
431 "\n",
432 "39"
433 ]
434 },
435 {
436 "output_type": "stream",
437 "stream": "stdout",
438 "text": [
439 "\n",
440 "55"
441 ]
442 },
443 {
444 "output_type": "stream",
445 "stream": "stdout",
446 "text": [
447 "\n",
448 "40"
449 ]
450 },
451 {
452 "output_type": "stream",
453 "stream": "stdout",
454 "text": [
455 "\n",
456 "46"
457 ]
458 },
459 {
460 "output_type": "stream",
461 "stream": "stdout",
462 "text": [
463 "\n",
464 "53"
465 ]
466 },
467 {
468 "output_type": "stream",
469 "stream": "stdout",
470 "text": [
471 "\n",
472 "43"
473 ]
474 },
475 {
476 "output_type": "stream",
477 "stream": "stdout",
478 "text": [
479 "\n",
480 "40"
481 ]
482 },
483 {
484 "output_type": "stream",
485 "stream": "stdout",
486 "text": [
487 "\n",
488 "53"
489 ]
490 },
491 {
492 "output_type": "stream",
493 "stream": "stdout",
494 "text": [
495 "\n",
496 "41"
497 ]
498 },
499 {
500 "output_type": "stream",
501 "stream": "stdout",
502 "text": [
503 "\n",
504 "10 loops, best of 3: 64.2 ms per loop\n"
505 ]
506 }
507 ],
508 "prompt_number": 54
509 },
510 {
511 "cell_type": "code",
512 "collapsed": false,
513 "input": [
514 "%%timeit\n",
515 "\n",
516 "wins = 0\n",
517 "for _ in range(1000):\n",
518 " g = Game(random.choice(WORDS), player=PlayerFreqOrdered())\n",
519 " g.play_game()\n",
520 " if g.game_won:\n",
521 " wins += 1\n",
522 "print(wins)"
523 ],
524 "language": "python",
525 "metadata": {},
526 "outputs": [
527 {
528 "output_type": "stream",
529 "stream": "stdout",
530 "text": [
531 "334\n",
532 "342"
533 ]
534 },
535 {
536 "output_type": "stream",
537 "stream": "stdout",
538 "text": [
539 "\n",
540 "318"
541 ]
542 },
543 {
544 "output_type": "stream",
545 "stream": "stdout",
546 "text": [
547 "\n",
548 "313"
549 ]
550 },
551 {
552 "output_type": "stream",
553 "stream": "stdout",
554 "text": [
555 "\n",
556 "353"
557 ]
558 },
559 {
560 "output_type": "stream",
561 "stream": "stdout",
562 "text": [
563 "\n",
564 "304"
565 ]
566 },
567 {
568 "output_type": "stream",
569 "stream": "stdout",
570 "text": [
571 "\n",
572 "332"
573 ]
574 },
575 {
576 "output_type": "stream",
577 "stream": "stdout",
578 "text": [
579 "\n",
580 "313"
581 ]
582 },
583 {
584 "output_type": "stream",
585 "stream": "stdout",
586 "text": [
587 "\n",
588 "335"
589 ]
590 },
591 {
592 "output_type": "stream",
593 "stream": "stdout",
594 "text": [
595 "\n",
596 "339"
597 ]
598 },
599 {
600 "output_type": "stream",
601 "stream": "stdout",
602 "text": [
603 "\n",
604 "328"
605 ]
606 },
607 {
608 "output_type": "stream",
609 "stream": "stdout",
610 "text": [
611 "\n",
612 "334"
613 ]
614 },
615 {
616 "output_type": "stream",
617 "stream": "stdout",
618 "text": [
619 "\n",
620 "322"
621 ]
622 },
623 {
624 "output_type": "stream",
625 "stream": "stdout",
626 "text": [
627 "\n",
628 "347"
629 ]
630 },
631 {
632 "output_type": "stream",
633 "stream": "stdout",
634 "text": [
635 "\n",
636 "334"
637 ]
638 },
639 {
640 "output_type": "stream",
641 "stream": "stdout",
642 "text": [
643 "\n",
644 "340"
645 ]
646 },
647 {
648 "output_type": "stream",
649 "stream": "stdout",
650 "text": [
651 "\n",
652 "319"
653 ]
654 },
655 {
656 "output_type": "stream",
657 "stream": "stdout",
658 "text": [
659 "\n",
660 "365"
661 ]
662 },
663 {
664 "output_type": "stream",
665 "stream": "stdout",
666 "text": [
667 "\n",
668 "315"
669 ]
670 },
671 {
672 "output_type": "stream",
673 "stream": "stdout",
674 "text": [
675 "\n",
676 "307"
677 ]
678 },
679 {
680 "output_type": "stream",
681 "stream": "stdout",
682 "text": [
683 "\n",
684 "314"
685 ]
686 },
687 {
688 "output_type": "stream",
689 "stream": "stdout",
690 "text": [
691 "\n",
692 "317"
693 ]
694 },
695 {
696 "output_type": "stream",
697 "stream": "stdout",
698 "text": [
699 "\n",
700 "310"
701 ]
702 },
703 {
704 "output_type": "stream",
705 "stream": "stdout",
706 "text": [
707 "\n",
708 "324"
709 ]
710 },
711 {
712 "output_type": "stream",
713 "stream": "stdout",
714 "text": [
715 "\n",
716 "313"
717 ]
718 },
719 {
720 "output_type": "stream",
721 "stream": "stdout",
722 "text": [
723 "\n",
724 "318"
725 ]
726 },
727 {
728 "output_type": "stream",
729 "stream": "stdout",
730 "text": [
731 "\n",
732 "314"
733 ]
734 },
735 {
736 "output_type": "stream",
737 "stream": "stdout",
738 "text": [
739 "\n",
740 "324"
741 ]
742 },
743 {
744 "output_type": "stream",
745 "stream": "stdout",
746 "text": [
747 "\n",
748 "297"
749 ]
750 },
751 {
752 "output_type": "stream",
753 "stream": "stdout",
754 "text": [
755 "\n",
756 "335"
757 ]
758 },
759 {
760 "output_type": "stream",
761 "stream": "stdout",
762 "text": [
763 "\n",
764 "335"
765 ]
766 },
767 {
768 "output_type": "stream",
769 "stream": "stdout",
770 "text": [
771 "\n",
772 "343"
773 ]
774 },
775 {
776 "output_type": "stream",
777 "stream": "stdout",
778 "text": [
779 "\n",
780 "342"
781 ]
782 },
783 {
784 "output_type": "stream",
785 "stream": "stdout",
786 "text": [
787 "\n",
788 "318"
789 ]
790 },
791 {
792 "output_type": "stream",
793 "stream": "stdout",
794 "text": [
795 "\n",
796 "306"
797 ]
798 },
799 {
800 "output_type": "stream",
801 "stream": "stdout",
802 "text": [
803 "\n",
804 "353"
805 ]
806 },
807 {
808 "output_type": "stream",
809 "stream": "stdout",
810 "text": [
811 "\n",
812 "332"
813 ]
814 },
815 {
816 "output_type": "stream",
817 "stream": "stdout",
818 "text": [
819 "\n",
820 "330"
821 ]
822 },
823 {
824 "output_type": "stream",
825 "stream": "stdout",
826 "text": [
827 "\n",
828 "334"
829 ]
830 },
831 {
832 "output_type": "stream",
833 "stream": "stdout",
834 "text": [
835 "\n",
836 "307"
837 ]
838 },
839 {
840 "output_type": "stream",
841 "stream": "stdout",
842 "text": [
843 "\n",
844 "306"
845 ]
846 },
847 {
848 "output_type": "stream",
849 "stream": "stdout",
850 "text": [
851 "\n",
852 "10 loops, best of 3: 96.2 ms per loop\n"
853 ]
854 }
855 ],
856 "prompt_number": 56
857 },
858 {
859 "cell_type": "code",
860 "collapsed": false,
861 "input": [
862 "%%timeit\n",
863 "\n",
864 "wins = 0\n",
865 "for _ in range(1000):\n",
866 " g = Game(random.choice(WORDS), player=PlayerFixedOrder(list(reversed(string.ascii_lowercase))))\n",
867 " g.play_game()\n",
868 " if g.game_won:\n",
869 " wins += 1\n",
870 "print(wins)"
871 ],
872 "language": "python",
873 "metadata": {},
874 "outputs": [
875 {
876 "output_type": "stream",
877 "stream": "stdout",
878 "text": [
879 "7\n",
880 "5"
881 ]
882 },
883 {
884 "output_type": "stream",
885 "stream": "stdout",
886 "text": [
887 "\n",
888 "6"
889 ]
890 },
891 {
892 "output_type": "stream",
893 "stream": "stdout",
894 "text": [
895 "\n",
896 "7"
897 ]
898 },
899 {
900 "output_type": "stream",
901 "stream": "stdout",
902 "text": [
903 "\n",
904 "7"
905 ]
906 },
907 {
908 "output_type": "stream",
909 "stream": "stdout",
910 "text": [
911 "\n",
912 "10"
913 ]
914 },
915 {
916 "output_type": "stream",
917 "stream": "stdout",
918 "text": [
919 "\n",
920 "8"
921 ]
922 },
923 {
924 "output_type": "stream",
925 "stream": "stdout",
926 "text": [
927 "\n",
928 "5"
929 ]
930 },
931 {
932 "output_type": "stream",
933 "stream": "stdout",
934 "text": [
935 "\n",
936 "5"
937 ]
938 },
939 {
940 "output_type": "stream",
941 "stream": "stdout",
942 "text": [
943 "\n",
944 "5"
945 ]
946 },
947 {
948 "output_type": "stream",
949 "stream": "stdout",
950 "text": [
951 "\n",
952 "9"
953 ]
954 },
955 {
956 "output_type": "stream",
957 "stream": "stdout",
958 "text": [
959 "\n",
960 "3"
961 ]
962 },
963 {
964 "output_type": "stream",
965 "stream": "stdout",
966 "text": [
967 "\n",
968 "8"
969 ]
970 },
971 {
972 "output_type": "stream",
973 "stream": "stdout",
974 "text": [
975 "\n",
976 "13"
977 ]
978 },
979 {
980 "output_type": "stream",
981 "stream": "stdout",
982 "text": [
983 "\n",
984 "8"
985 ]
986 },
987 {
988 "output_type": "stream",
989 "stream": "stdout",
990 "text": [
991 "\n",
992 "10"
993 ]
994 },
995 {
996 "output_type": "stream",
997 "stream": "stdout",
998 "text": [
999 "\n",
1000 "9"
1001 ]
1002 },
1003 {
1004 "output_type": "stream",
1005 "stream": "stdout",
1006 "text": [
1007 "\n",
1008 "9"
1009 ]
1010 },
1011 {
1012 "output_type": "stream",
1013 "stream": "stdout",
1014 "text": [
1015 "\n",
1016 "12"
1017 ]
1018 },
1019 {
1020 "output_type": "stream",
1021 "stream": "stdout",
1022 "text": [
1023 "\n",
1024 "6"
1025 ]
1026 },
1027 {
1028 "output_type": "stream",
1029 "stream": "stdout",
1030 "text": [
1031 "\n",
1032 "6"
1033 ]
1034 },
1035 {
1036 "output_type": "stream",
1037 "stream": "stdout",
1038 "text": [
1039 "\n",
1040 "14"
1041 ]
1042 },
1043 {
1044 "output_type": "stream",
1045 "stream": "stdout",
1046 "text": [
1047 "\n",
1048 "9"
1049 ]
1050 },
1051 {
1052 "output_type": "stream",
1053 "stream": "stdout",
1054 "text": [
1055 "\n",
1056 "1"
1057 ]
1058 },
1059 {
1060 "output_type": "stream",
1061 "stream": "stdout",
1062 "text": [
1063 "\n",
1064 "8"
1065 ]
1066 },
1067 {
1068 "output_type": "stream",
1069 "stream": "stdout",
1070 "text": [
1071 "\n",
1072 "8"
1073 ]
1074 },
1075 {
1076 "output_type": "stream",
1077 "stream": "stdout",
1078 "text": [
1079 "\n",
1080 "7"
1081 ]
1082 },
1083 {
1084 "output_type": "stream",
1085 "stream": "stdout",
1086 "text": [
1087 "\n",
1088 "9"
1089 ]
1090 },
1091 {
1092 "output_type": "stream",
1093 "stream": "stdout",
1094 "text": [
1095 "\n",
1096 "10"
1097 ]
1098 },
1099 {
1100 "output_type": "stream",
1101 "stream": "stdout",
1102 "text": [
1103 "\n",
1104 "6"
1105 ]
1106 },
1107 {
1108 "output_type": "stream",
1109 "stream": "stdout",
1110 "text": [
1111 "\n",
1112 "7"
1113 ]
1114 },
1115 {
1116 "output_type": "stream",
1117 "stream": "stdout",
1118 "text": [
1119 "\n",
1120 "6"
1121 ]
1122 },
1123 {
1124 "output_type": "stream",
1125 "stream": "stdout",
1126 "text": [
1127 "\n",
1128 "4"
1129 ]
1130 },
1131 {
1132 "output_type": "stream",
1133 "stream": "stdout",
1134 "text": [
1135 "\n",
1136 "6"
1137 ]
1138 },
1139 {
1140 "output_type": "stream",
1141 "stream": "stdout",
1142 "text": [
1143 "\n",
1144 "3"
1145 ]
1146 },
1147 {
1148 "output_type": "stream",
1149 "stream": "stdout",
1150 "text": [
1151 "\n",
1152 "7"
1153 ]
1154 },
1155 {
1156 "output_type": "stream",
1157 "stream": "stdout",
1158 "text": [
1159 "\n",
1160 "7"
1161 ]
1162 },
1163 {
1164 "output_type": "stream",
1165 "stream": "stdout",
1166 "text": [
1167 "\n",
1168 "10"
1169 ]
1170 },
1171 {
1172 "output_type": "stream",
1173 "stream": "stdout",
1174 "text": [
1175 "\n",
1176 "11"
1177 ]
1178 },
1179 {
1180 "output_type": "stream",
1181 "stream": "stdout",
1182 "text": [
1183 "\n",
1184 "6"
1185 ]
1186 },
1187 {
1188 "output_type": "stream",
1189 "stream": "stdout",
1190 "text": [
1191 "\n",
1192 "4"
1193 ]
1194 },
1195 {
1196 "output_type": "stream",
1197 "stream": "stdout",
1198 "text": [
1199 "\n",
1200 "10 loops, best of 3: 74.6 ms per loop\n"
1201 ]
1202 }
1203 ],
1204 "prompt_number": 57
1205 },
1206 {
1207 "cell_type": "code",
1208 "collapsed": false,
1209 "input": [
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()]"
1212 ],
1213 "language": "python",
1214 "metadata": {},
1215 "outputs": [],
1216 "prompt_number": 11
1217 },
1218 {
1219 "cell_type": "code",
1220 "collapsed": false,
1221 "input": [
1222 "DICT_COUNTS"
1223 ],
1224 "language": "python",
1225 "metadata": {},
1226 "outputs": [
1227 {
1228 "metadata": {},
1229 "output_type": "pyout",
1230 "prompt_number": 12,
1231 "text": [
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})"
1233 ]
1234 }
1235 ],
1236 "prompt_number": 12
1237 },
1238 {
1239 "cell_type": "code",
1240 "collapsed": false,
1241 "input": [
1242 "print(DICT_LETTERS_IN_ORDER)\n",
1243 "print(LETTERS_IN_ORDER)"
1244 ],
1245 "language": "python",
1246 "metadata": {},
1247 "outputs": [
1248 {
1249 "output_type": "stream",
1250 "stream": "stdout",
1251 "text": [
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"
1254 ]
1255 }
1256 ],
1257 "prompt_number": 13
1258 },
1259 {
1260 "cell_type": "code",
1261 "collapsed": false,
1262 "input": [
1263 "wins = 0\n",
1264 "for _ in range(1000):\n",
1265 " g = Game(random.choice(WORDS), player=PlayerFixedOrder(DICT_LETTERS_IN_ORDER))\n",
1266 " g.play_game()\n",
1267 " if g.game_won:\n",
1268 " wins += 1\n",
1269 "print(wins)"
1270 ],
1271 "language": "python",
1272 "metadata": {},
1273 "outputs": [
1274 {
1275 "output_type": "stream",
1276 "stream": "stdout",
1277 "text": [
1278 "440\n"
1279 ]
1280 }
1281 ],
1282 "prompt_number": 14
1283 },
1284 {
1285 "cell_type": "code",
1286 "collapsed": false,
1287 "input": [
1288 "class PlayerAdaptiveLength:\n",
1289 " def __init__(self, words):\n",
1290 " self.all_words = words\n",
1291 " self.candidate_words = None\n",
1292 " \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",
1298 " \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()]"
1303 ],
1304 "language": "python",
1305 "metadata": {},
1306 "outputs": [],
1307 "prompt_number": 33
1308 },
1309 {
1310 "cell_type": "code",
1311 "collapsed": false,
1312 "input": [
1313 "wins = 0\n",
1314 "for _ in range(1000):\n",
1315 " g = Game(random.choice(WORDS), player=PlayerAdaptiveLength(WORDS))\n",
1316 " g.play_game()\n",
1317 " if g.game_won:\n",
1318 " wins += 1\n",
1319 "print(wins)"
1320 ],
1321 "language": "python",
1322 "metadata": {},
1323 "outputs": [
1324 {
1325 "output_type": "stream",
1326 "stream": "stdout",
1327 "text": [
1328 "474\n"
1329 ]
1330 }
1331 ],
1332 "prompt_number": 34
1333 },
1334 {
1335 "cell_type": "code",
1336 "collapsed": false,
1337 "input": [
1338 "class PlayerAdaptiveIncludedLetters:\n",
1339 " def __init__(self, words):\n",
1340 " self.candidate_words = words\n",
1341 " \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",
1347 " \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",
1351 " \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()]"
1355 ],
1356 "language": "python",
1357 "metadata": {},
1358 "outputs": [],
1359 "prompt_number": 35
1360 },
1361 {
1362 "cell_type": "code",
1363 "collapsed": false,
1364 "input": [
1365 "wins = 0\n",
1366 "for _ in range(1000):\n",
1367 " g = Game(random.choice(WORDS), player=PlayerAdaptiveIncludedLetters(WORDS))\n",
1368 " g.play_game()\n",
1369 " if g.game_won:\n",
1370 " wins += 1\n",
1371 "print(wins)"
1372 ],
1373 "language": "python",
1374 "metadata": {},
1375 "outputs": [
1376 {
1377 "output_type": "stream",
1378 "stream": "stdout",
1379 "text": [
1380 "982\n"
1381 ]
1382 }
1383 ],
1384 "prompt_number": 36
1385 },
1386 {
1387 "cell_type": "code",
1388 "collapsed": false,
1389 "input": [
1390 "re.match('^[^xaz]*$', 'happy')"
1391 ],
1392 "language": "python",
1393 "metadata": {},
1394 "outputs": [],
1395 "prompt_number": 37
1396 },
1397 {
1398 "cell_type": "code",
1399 "collapsed": false,
1400 "input": [
1401 "class PlayerAdaptiveExcludedLetters:\n",
1402 " def __init__(self, words):\n",
1403 " self.candidate_words = words\n",
1404 " \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",
1410 " \n",
1411 " def filter_candidate_words(self, missed):\n",
1412 " if 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",
1415 " \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()]"
1419 ],
1420 "language": "python",
1421 "metadata": {},
1422 "outputs": [],
1423 "prompt_number": 38
1424 },
1425 {
1426 "cell_type": "code",
1427 "collapsed": false,
1428 "input": [
1429 "wins = 0\n",
1430 "for _ in range(1000):\n",
1431 " g = Game(random.choice(WORDS), player=PlayerAdaptiveExcludedLetters(WORDS))\n",
1432 " g.play_game()\n",
1433 " if g.game_won:\n",
1434 " wins += 1\n",
1435 "print(wins)"
1436 ],
1437 "language": "python",
1438 "metadata": {},
1439 "outputs": [
1440 {
1441 "output_type": "stream",
1442 "stream": "stdout",
1443 "text": [
1444 "502\n"
1445 ]
1446 }
1447 ],
1448 "prompt_number": 39
1449 },
1450 {
1451 "cell_type": "code",
1452 "collapsed": false,
1453 "input": [
1454 "class PlayerAdaptivePattern:\n",
1455 " def __init__(self, words):\n",
1456 " self.candidate_words = words\n",
1457 " \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",
1463 " \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",
1468 " else:\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",
1472 " \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"
1476 ],
1477 "language": "python",
1478 "metadata": {},
1479 "outputs": [],
1480 "prompt_number": 40
1481 },
1482 {
1483 "cell_type": "code",
1484 "collapsed": false,
1485 "input": [
1486 "wins = 0\n",
1487 "for _ in range(1000):\n",
1488 " g = Game(random.choice(WORDS), player=PlayerAdaptivePattern(WORDS))\n",
1489 " g.play_game()\n",
1490 " if g.game_won:\n",
1491 " wins += 1\n",
1492 "print(wins)"
1493 ],
1494 "language": "python",
1495 "metadata": {},
1496 "outputs": [
1497 {
1498 "output_type": "stream",
1499 "stream": "stdout",
1500 "text": [
1501 "993\n"
1502 ]
1503 }
1504 ],
1505 "prompt_number": 41
1506 },
1507 {
1508 "cell_type": "code",
1509 "collapsed": false,
1510 "input": [
1511 "%%timeit\n",
1512 "\n",
1513 "wins = 0\n",
1514 "for _ in range(1000):\n",
1515 " g = Game(random.choice(WORDS), player=PlayerAdaptivePattern(WORDS))\n",
1516 " g.play_game()\n",
1517 " if g.game_won:\n",
1518 " wins += 1\n",
1519 "print(wins)"
1520 ],
1521 "language": "python",
1522 "metadata": {},
1523 "outputs": [
1524 {
1525 "output_type": "stream",
1526 "stream": "stdout",
1527 "text": [
1528 "994\n",
1529 "993"
1530 ]
1531 },
1532 {
1533 "output_type": "stream",
1534 "stream": "stdout",
1535 "text": [
1536 "\n",
1537 "987"
1538 ]
1539 },
1540 {
1541 "output_type": "stream",
1542 "stream": "stdout",
1543 "text": [
1544 "\n",
1545 "993"
1546 ]
1547 },
1548 {
1549 "output_type": "stream",
1550 "stream": "stdout",
1551 "text": [
1552 "\n",
1553 "1 loops, best of 3: 30.6 s per loop\n"
1554 ]
1555 }
1556 ],
1557 "prompt_number": 42
1558 },
1559 {
1560 "cell_type": "code",
1561 "collapsed": false,
1562 "input": [
1563 "class PlayerAdaptive:\n",
1564 " def __init__(self, words):\n",
1565 " self.candidate_words = words\n",
1566 " \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",
1572 " \n",
1573 " def filter_candidate_words(self, discovered, missed):\n",
1574 " pass\n",
1575 " \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()]"
1581 ],
1582 "language": "python",
1583 "metadata": {},
1584 "outputs": [],
1585 "prompt_number": 43
1586 },
1587 {
1588 "cell_type": "code",
1589 "collapsed": false,
1590 "input": [
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",
1596 " \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",
1601 " \n",
1602 " def set_ordered_letters(self):\n",
1603 " if not self.ordered_letters:\n",
1604 " super().set_ordered_letters()"
1605 ],
1606 "language": "python",
1607 "metadata": {},
1608 "outputs": [],
1609 "prompt_number": 44
1610 },
1611 {
1612 "cell_type": "code",
1613 "collapsed": false,
1614 "input": [
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)]"
1619 ],
1620 "language": "python",
1621 "metadata": {},
1622 "outputs": [],
1623 "prompt_number": 45
1624 },
1625 {
1626 "cell_type": "code",
1627 "collapsed": false,
1628 "input": [
1629 "class PlayerAdaptiveExcludedLetters(PlayerAdaptive):\n",
1630 " def filter_candidate_words(self, discovered, missed):\n",
1631 " if missed:\n",
1632 " exp = re.compile('^[^' + ''.join(missed) + ']*$')\n",
1633 " self.candidate_words = [w for w in self.candidate_words if exp.match(w)] "
1634 ],
1635 "language": "python",
1636 "metadata": {},
1637 "outputs": [],
1638 "prompt_number": 46
1639 },
1640 {
1641 "cell_type": "code",
1642 "collapsed": false,
1643 "input": [
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",
1649 " else:\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)]"
1653 ],
1654 "language": "python",
1655 "metadata": {},
1656 "outputs": [],
1657 "prompt_number": 47
1658 },
1659 {
1660 "cell_type": "code",
1661 "collapsed": false,
1662 "input": [
1663 "%%timeit\n",
1664 "\n",
1665 "wins = 0\n",
1666 "for _ in range(1000):\n",
1667 " g = Game(random.choice(WORDS), player=PlayerAdaptiveLength(WORDS))\n",
1668 " g.play_game()\n",
1669 " if g.game_won:\n",
1670 " wins += 1\n",
1671 "print(wins)"
1672 ],
1673 "language": "python",
1674 "metadata": {},
1675 "outputs": [
1676 {
1677 "output_type": "stream",
1678 "stream": "stdout",
1679 "text": [
1680 "479\n",
1681 "455"
1682 ]
1683 },
1684 {
1685 "output_type": "stream",
1686 "stream": "stdout",
1687 "text": [
1688 "\n",
1689 "460"
1690 ]
1691 },
1692 {
1693 "output_type": "stream",
1694 "stream": "stdout",
1695 "text": [
1696 "\n",
1697 "498"
1698 ]
1699 },
1700 {
1701 "output_type": "stream",
1702 "stream": "stdout",
1703 "text": [
1704 "\n",
1705 "1 loops, best of 3: 15.9 s per loop\n"
1706 ]
1707 }
1708 ],
1709 "prompt_number": 48
1710 },
1711 {
1712 "cell_type": "code",
1713 "collapsed": false,
1714 "input": [
1715 "%%timeit\n",
1716 "\n",
1717 "wins = 0\n",
1718 "for _ in range(1000):\n",
1719 " g = Game(random.choice(WORDS), player=PlayerAdaptiveIncludedLetters(WORDS))\n",
1720 " g.play_game()\n",
1721 " if g.game_won:\n",
1722 " wins += 1\n",
1723 "print(wins)"
1724 ],
1725 "language": "python",
1726 "metadata": {},
1727 "outputs": [
1728 {
1729 "output_type": "stream",
1730 "stream": "stdout",
1731 "text": [
1732 "981\n",
1733 "983"
1734 ]
1735 },
1736 {
1737 "output_type": "stream",
1738 "stream": "stdout",
1739 "text": [
1740 "\n",
1741 "980"
1742 ]
1743 },
1744 {
1745 "output_type": "stream",
1746 "stream": "stdout",
1747 "text": [
1748 "\n",
1749 "980"
1750 ]
1751 },
1752 {
1753 "output_type": "stream",
1754 "stream": "stdout",
1755 "text": [
1756 "\n",
1757 "1 loops, best of 3: 36.9 s per loop\n"
1758 ]
1759 }
1760 ],
1761 "prompt_number": 49
1762 },
1763 {
1764 "cell_type": "code",
1765 "collapsed": false,
1766 "input": [
1767 "%%timeit\n",
1768 "\n",
1769 "wins = 0\n",
1770 "for _ in range(1000):\n",
1771 " g = Game(random.choice(WORDS), player=PlayerAdaptiveExcludedLetters(WORDS))\n",
1772 " g.play_game()\n",
1773 " if g.game_won:\n",
1774 " wins += 1\n",
1775 "print(wins)"
1776 ],
1777 "language": "python",
1778 "metadata": {},
1779 "outputs": [
1780 {
1781 "output_type": "stream",
1782 "stream": "stdout",
1783 "text": [
1784 "521\n",
1785 "484"
1786 ]
1787 },
1788 {
1789 "output_type": "stream",
1790 "stream": "stdout",
1791 "text": [
1792 "\n",
1793 "491"
1794 ]
1795 },
1796 {
1797 "output_type": "stream",
1798 "stream": "stdout",
1799 "text": [
1800 "\n",
1801 "518"
1802 ]
1803 },
1804 {
1805 "output_type": "stream",
1806 "stream": "stdout",
1807 "text": [
1808 "\n",
1809 "1 loops, best of 3: 7min 18s per loop\n"
1810 ]
1811 }
1812 ],
1813 "prompt_number": 50
1814 },
1815 {
1816 "cell_type": "code",
1817 "collapsed": false,
1818 "input": [
1819 "%%timeit\n",
1820 "\n",
1821 "wins = 0\n",
1822 "for _ in range(1000):\n",
1823 " g = Game(random.choice(WORDS), player=PlayerAdaptivePattern(WORDS))\n",
1824 " g.play_game()\n",
1825 " if g.game_won:\n",
1826 " wins += 1\n",
1827 "print(wins)"
1828 ],
1829 "language": "python",
1830 "metadata": {},
1831 "outputs": [
1832 {
1833 "output_type": "stream",
1834 "stream": "stdout",
1835 "text": [
1836 "987\n",
1837 "988"
1838 ]
1839 },
1840 {
1841 "output_type": "stream",
1842 "stream": "stdout",
1843 "text": [
1844 "\n",
1845 "996"
1846 ]
1847 },
1848 {
1849 "output_type": "stream",
1850 "stream": "stdout",
1851 "text": [
1852 "\n",
1853 "995"
1854 ]
1855 },
1856 {
1857 "output_type": "stream",
1858 "stream": "stdout",
1859 "text": [
1860 "\n",
1861 "1 loops, best of 3: 31.1 s per loop\n"
1862 ]
1863 }
1864 ],
1865 "prompt_number": 51
1866 },
1867 {
1868 "cell_type": "code",
1869 "collapsed": false,
1870 "input": [
1871 "for _ in range(1000):\n",
1872 " g = Game(random.choice(WORDS), player=PlayerAdaptivePattern(WORDS))\n",
1873 " g.play_game()\n",
1874 " if not g.game_won:\n",
1875 " print(g.target, g.discovered, g.wrong_letters)"
1876 ],
1877 "language": "python",
1878 "metadata": {},
1879 "outputs": [
1880 {
1881 "output_type": "stream",
1882 "stream": "stdout",
1883 "text": [
1884 "naked ['_', 'a', '_', 'e', 'd'] ['s', 'r', 'w', 'c', 't', 'g', 'l', 'f', 'p', 'm']\n",
1885 "wound"
1886 ]
1887 },
1888 {
1889 "output_type": "stream",
1890 "stream": "stdout",
1891 "text": [
1892 " ['_', 'o', 'u', 'n', 'd'] ['s', 'e', 'a', 'y', 'p', 'm', 'f', 'h', 'b', 'r']\n",
1893 "hut"
1894 ]
1895 },
1896 {
1897 "output_type": "stream",
1898 "stream": "stdout",
1899 "text": [
1900 " ['_', 'u', 't'] ['a', 'o', 'e', 'i', 'b', 'g', 'n', 'p', 'm', 'c']\n",
1901 "fucker"
1902 ]
1903 },
1904 {
1905 "output_type": "stream",
1906 "stream": "stdout",
1907 "text": [
1908 " ['_', 'u', '_', '_', 'e', 'r'] ['d', 's', 'i', 'a', 'o', 't', 'b', 'n', 'm', 'l']\n",
1909 "fox"
1910 ]
1911 },
1912 {
1913 "output_type": "stream",
1914 "stream": "stdout",
1915 "text": [
1916 " ['_', 'o', '_'] ['a', 't', 'b', 'd', 'w', 'p', 'n', 's', 'g', 'y']\n",
1917 "wills"
1918 ]
1919 },
1920 {
1921 "output_type": "stream",
1922 "stream": "stdout",
1923 "text": [
1924 " ['_', 'i', 'l', 'l', 's'] ['e', 'o', 'a', 'g', 'p', 'm', 'k', 'f', 'h', 'd']\n",
1925 "bunny"
1926 ]
1927 },
1928 {
1929 "output_type": "stream",
1930 "stream": "stdout",
1931 "text": [
1932 " ['_', 'u', 'n', 'n', 'y'] ['s', 'e', 'a', 'o', 'i', 'm', 'l', 'g', 'f', 't']\n",
1933 "curving"
1934 ]
1935 },
1936 {
1937 "output_type": "stream",
1938 "stream": "stdout",
1939 "text": [
1940 " ['c', 'u', '_', '_', 'i', 'n', 'g'] ['e', 'a', 'o', 'l', 's', 'f', 'p', 'b', 't', 'm']\n",
1941 "hoe"
1942 ]
1943 },
1944 {
1945 "output_type": "stream",
1946 "stream": "stdout",
1947 "text": [
1948 " ['_', 'o', '_'] ['a', 't', 'b', 'd', 'w', 'p', 'n', 's', 'g', 'y']\n",
1949 "butt"
1950 ]
1951 },
1952 {
1953 "output_type": "stream",
1954 "stream": "stdout",
1955 "text": [
1956 " ['_', 'u', '_', '_'] ['e', 's', 'o', 'a', 'i', 'l', 'f', 'r', 'n', 'm']\n",
1957 "mucked"
1958 ]
1959 },
1960 {
1961 "output_type": "stream",
1962 "stream": "stdout",
1963 "text": [
1964 " ['_', 'u', '_', '_', 'e', 'd'] ['a', 'o', 'i', 'l', 's', 't', 'f', 'p', 'g', 'b']\n",
1965 "flaw"
1966 ]
1967 },
1968 {
1969 "output_type": "stream",
1970 "stream": "stdout",
1971 "text": [
1972 " ['f', 'l', 'a', '_'] ['e', 's', 'o', 'r', 'y', 'g', 'k', 'x', 'p', 'n']\n"
1973 ]
1974 }
1975 ],
1976 "prompt_number": 52
1977 },
1978 {
1979 "cell_type": "code",
1980 "collapsed": false,
1981 "input": [
1982 "iterations = 10000\n",
1983 "wins = 0\n",
1984 "for _ in range(iterations):\n",
1985 " g = Game(random.choice(WORDS), player=PlayerAdaptivePattern(WORDS))\n",
1986 " g.play_game()\n",
1987 " if g.game_won:\n",
1988 " wins += 1\n",
1989 "print(wins / iterations)"
1990 ],
1991 "language": "python",
1992 "metadata": {},
1993 "outputs": [
1994 {
1995 "output_type": "stream",
1996 "stream": "stdout",
1997 "text": [
1998 "0.9936\n"
1999 ]
2000 }
2001 ],
2002 "prompt_number": 53
2003 },
2004 {
2005 "cell_type": "code",
2006 "collapsed": false,
2007 "input": [
2008 "class PlayerAdaptiveNoRegex:\n",
2009 " def __init__(self, words):\n",
2010 " self.candidate_words = words\n",
2011 " \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",
2017 " \n",
2018 " def filter_candidate_words(self, discovered, missed):\n",
2019 " pass\n",
2020 " \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",
2026 "\n",
2027 " def match(self, pattern, target, excluded=None):\n",
2028 " if not excluded:\n",
2029 " excluded = ''\n",
2030 " if len(pattern) != len(target):\n",
2031 " return False\n",
2032 " for m, c in zip(pattern, target):\n",
2033 " if m == '_' and c not in excluded:\n",
2034 " # true\n",
2035 " pass\n",
2036 " elif m != '_' and m == c:\n",
2037 " # true\n",
2038 " pass\n",
2039 " else:\n",
2040 " return False\n",
2041 " return True "
2042 ],
2043 "language": "python",
2044 "metadata": {},
2045 "outputs": [],
2046 "prompt_number": 59
2047 },
2048 {
2049 "cell_type": "code",
2050 "collapsed": false,
2051 "input": [
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",
2057 " \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",
2062 " \n",
2063 " def set_ordered_letters(self):\n",
2064 " if not self.ordered_letters:\n",
2065 " super().set_ordered_letters()"
2066 ],
2067 "language": "python",
2068 "metadata": {},
2069 "outputs": [],
2070 "prompt_number": 60
2071 },
2072 {
2073 "cell_type": "code",
2074 "collapsed": false,
2075 "input": [
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)]"
2079 ],
2080 "language": "python",
2081 "metadata": {},
2082 "outputs": [],
2083 "prompt_number": 61
2084 },
2085 {
2086 "cell_type": "code",
2087 "collapsed": false,
2088 "input": [
2089 "class PlayerAdaptiveExcludedLettersNoRegex(PlayerAdaptiveNoRegex):\n",
2090 " def filter_candidate_words(self, discovered, missed):\n",
2091 " if 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)] "
2094 ],
2095 "language": "python",
2096 "metadata": {},
2097 "outputs": [],
2098 "prompt_number": 66
2099 },
2100 {
2101 "cell_type": "code",
2102 "collapsed": false,
2103 "input": [
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)]"
2108 ],
2109 "language": "python",
2110 "metadata": {},
2111 "outputs": [],
2112 "prompt_number": 67
2113 },
2114 {
2115 "cell_type": "code",
2116 "collapsed": false,
2117 "input": [
2118 "%%timeit\n",
2119 "\n",
2120 "wins = 0\n",
2121 "for _ in range(1000):\n",
2122 " g = Game(random.choice(WORDS), player=PlayerAdaptiveLengthNoRegex(WORDS))\n",
2123 " g.play_game()\n",
2124 " if g.game_won:\n",
2125 " wins += 1\n",
2126 "print(wins)"
2127 ],
2128 "language": "python",
2129 "metadata": {},
2130 "outputs": [
2131 {
2132 "output_type": "stream",
2133 "stream": "stdout",
2134 "text": [
2135 "471\n",
2136 "492"
2137 ]
2138 },
2139 {
2140 "output_type": "stream",
2141 "stream": "stdout",
2142 "text": [
2143 "\n",
2144 "502"
2145 ]
2146 },
2147 {
2148 "output_type": "stream",
2149 "stream": "stdout",
2150 "text": [
2151 "\n",
2152 "469"
2153 ]
2154 },
2155 {
2156 "output_type": "stream",
2157 "stream": "stdout",
2158 "text": [
2159 "\n",
2160 "1 loops, best of 3: 16 s per loop\n"
2161 ]
2162 }
2163 ],
2164 "prompt_number": 69
2165 },
2166 {
2167 "cell_type": "code",
2168 "collapsed": false,
2169 "input": [
2170 "%%timeit\n",
2171 "\n",
2172 "wins = 0\n",
2173 "for _ in range(1000):\n",
2174 " g = Game(random.choice(WORDS), player=PlayerAdaptiveIncludedLettersNoRegex(WORDS))\n",
2175 " g.play_game()\n",
2176 " if g.game_won:\n",
2177 " wins += 1\n",
2178 "print(wins)"
2179 ],
2180 "language": "python",
2181 "metadata": {},
2182 "outputs": [
2183 {
2184 "output_type": "stream",
2185 "stream": "stdout",
2186 "text": [
2187 "978\n",
2188 "979"
2189 ]
2190 },
2191 {
2192 "output_type": "stream",
2193 "stream": "stdout",
2194 "text": [
2195 "\n",
2196 "983"
2197 ]
2198 },
2199 {
2200 "output_type": "stream",
2201 "stream": "stdout",
2202 "text": [
2203 "\n",
2204 "979"
2205 ]
2206 },
2207 {
2208 "output_type": "stream",
2209 "stream": "stdout",
2210 "text": [
2211 "\n",
2212 "1 loops, best of 3: 48 s per loop\n"
2213 ]
2214 }
2215 ],
2216 "prompt_number": 70
2217 },
2218 {
2219 "cell_type": "code",
2220 "collapsed": false,
2221 "input": [
2222 "%%timeit\n",
2223 "\n",
2224 "wins = 0\n",
2225 "for _ in range(1000):\n",
2226 " g = Game(random.choice(WORDS), player=PlayerAdaptiveExcludedLettersNoRegex(WORDS))\n",
2227 " g.play_game()\n",
2228 " if g.game_won:\n",
2229 " wins += 1\n",
2230 "print(wins)"
2231 ],
2232 "language": "python",
2233 "metadata": {},
2234 "outputs": [
2235 {
2236 "output_type": "stream",
2237 "stream": "stdout",
2238 "text": [
2239 "582\n",
2240 "595"
2241 ]
2242 },
2243 {
2244 "output_type": "stream",
2245 "stream": "stdout",
2246 "text": [
2247 "\n",
2248 "587"
2249 ]
2250 },
2251 {
2252 "output_type": "stream",
2253 "stream": "stdout",
2254 "text": [
2255 "\n",
2256 "611"
2257 ]
2258 },
2259 {
2260 "output_type": "stream",
2261 "stream": "stdout",
2262 "text": [
2263 "\n",
2264 "1 loops, best of 3: 4min 59s per loop\n"
2265 ]
2266 }
2267 ],
2268 "prompt_number": 71
2269 },
2270 {
2271 "cell_type": "code",
2272 "collapsed": false,
2273 "input": [
2274 "%%timeit\n",
2275 "\n",
2276 "wins = 0\n",
2277 "for _ in range(1000):\n",
2278 " g = Game(random.choice(WORDS), player=PlayerAdaptivePatternNoRegex(WORDS))\n",
2279 " g.play_game()\n",
2280 " if g.game_won:\n",
2281 " wins += 1\n",
2282 "print(wins)"
2283 ],
2284 "language": "python",
2285 "metadata": {},
2286 "outputs": [
2287 {
2288 "output_type": "stream",
2289 "stream": "stdout",
2290 "text": [
2291 "991\n",
2292 "992"
2293 ]
2294 },
2295 {
2296 "output_type": "stream",
2297 "stream": "stdout",
2298 "text": [
2299 "\n",
2300 "993"
2301 ]
2302 },
2303 {
2304 "output_type": "stream",
2305 "stream": "stdout",
2306 "text": [
2307 "\n",
2308 "994"
2309 ]
2310 },
2311 {
2312 "output_type": "stream",
2313 "stream": "stdout",
2314 "text": [
2315 "\n",
2316 "1 loops, best of 3: 37.9 s per loop\n"
2317 ]
2318 }
2319 ],
2320 "prompt_number": 72
2321 },
2322 {
2323 "cell_type": "code",
2324 "collapsed": false,
2325 "input": [],
2326 "language": "python",
2327 "metadata": {},
2328 "outputs": [],
2329 "prompt_number": 72
2330 }
2331 ],
2332 "metadata": {}
2333 }
2334 ]
2335 }