--- /dev/null
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:32b3b300745f022158bfde0b3fc0a50b11d36551211371d50140e9f9d2a7b8e1"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import re\n",
+ "import random\n",
+ "import string\n",
+ "import collections"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "WORDS = [w.strip() for w in open('/usr/share/dict/british-english').readlines() \n",
+ " if re.match(r'^[a-z]*$', w.strip())]"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "LETTER_COUNTS = collections.Counter(l.lower() for l in open('sherlock-holmes.txt').read() if l in string.ascii_letters)\n",
+ "LETTERS_IN_ORDER = [p[0] for p in LETTER_COUNTS.most_common()]"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "STARTING_LIVES = 10"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "class Game:\n",
+ " def __init__(self, target, player=None, lives=STARTING_LIVES):\n",
+ " self.lives = lives\n",
+ " self.player = player\n",
+ " self.target = target\n",
+ " self.discovered = list('_' * len(target))\n",
+ " self.wrong_letters = []\n",
+ " self.game_finished = False\n",
+ " self.game_won = False\n",
+ " self.game_lost = False\n",
+ " \n",
+ " def find_all(self, letter):\n",
+ " return [p for p, l in enumerate(self.target) if l == letter]\n",
+ " \n",
+ " def update_discovered_word(self, guessed_letter):\n",
+ " locations = self.find_all(guessed_letter)\n",
+ " for location in locations:\n",
+ " self.discovered[location] = guessed_letter\n",
+ " return self.discovered\n",
+ " \n",
+ " def do_turn(self):\n",
+ " if self.player:\n",
+ " guess = self.player.guess(self.discovered, self.wrong_letters, self.lives)\n",
+ " else:\n",
+ " guess = self.ask_for_guess()\n",
+ " if guess in self.target:\n",
+ " self.update_discovered_word(guess)\n",
+ " else:\n",
+ " self.lives -= 1\n",
+ " if guess not in self.wrong_letters:\n",
+ " self.wrong_letters += [guess]\n",
+ " if self.lives == 0:\n",
+ " self.game_finished = True\n",
+ " self.game_lost = True\n",
+ " if '_' not in self.discovered:\n",
+ " self.game_finished = True\n",
+ " self.game_won = True\n",
+ " \n",
+ " def ask_for_guess(self):\n",
+ " print('Word:', ' '.join(self.discovered), \n",
+ " ' : Lives =', self.lives, \n",
+ " ', wrong guesses:', ' '.join(sorted(self.wrong_letters)))\n",
+ " guess = input('Enter letter: ').strip().lower()[0]\n",
+ " return guess\n",
+ " \n",
+ " def play_game(self):\n",
+ " while not self.game_finished:\n",
+ " self.do_turn()\n",
+ " if not self.player:\n",
+ " self.report_on_game()\n",
+ " return self.game_won\n",
+ " \n",
+ " def report_on_game(self):\n",
+ " if self.game_won:\n",
+ " print('You won! The word was', self.target)\n",
+ " else:\n",
+ " print('You lost. The word was', self.target)\n",
+ " return self.game_won"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "class PlayerFixedOrder:\n",
+ " def __init__(self, ordered_letters):\n",
+ " self.ordered_letters = ordered_letters\n",
+ " \n",
+ " def guess(self, discovered, missed, lives):\n",
+ " guessed_letters = [l.lower() for l in discovered + missed if l in string.ascii_letters]\n",
+ " return [l for l in self.ordered_letters if l not in guessed_letters][0]"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "class PlayerAlphabetical(PlayerFixedOrder):\n",
+ " def __init__(self):\n",
+ " super().__init__(string.ascii_lowercase)\n",
+ "\n",
+ "class PlayerFreqOrdered(PlayerFixedOrder):\n",
+ " def __init__(self):\n",
+ " super().__init__(LETTERS_IN_ORDER)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "%%timeit\n",
+ "\n",
+ "wins = 0\n",
+ "for _ in range(1000):\n",
+ " g = Game(random.choice(WORDS), player=PlayerAlphabetical())\n",
+ " g.play_game()\n",
+ " if g.game_won:\n",
+ " wins += 1\n",
+ "print(wins)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "50\n",
+ "59"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "33"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "53"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "49"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "42"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "43"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "54"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "49"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "54"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "45"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "46"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "44"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "45"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "48"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "51"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "46"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "54"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "49"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "44"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "42"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "44"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "48"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "39"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "46"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "43"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "50"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "47"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "52"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "46"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "55"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "47"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "39"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "55"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "40"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "46"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "53"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "43"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "40"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "53"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "41"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "10 loops, best of 3: 64.2 ms per loop\n"
+ ]
+ }
+ ],
+ "prompt_number": 54
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "%%timeit\n",
+ "\n",
+ "wins = 0\n",
+ "for _ in range(1000):\n",
+ " g = Game(random.choice(WORDS), player=PlayerFreqOrdered())\n",
+ " g.play_game()\n",
+ " if g.game_won:\n",
+ " wins += 1\n",
+ "print(wins)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "334\n",
+ "342"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "318"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "313"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "353"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "304"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "332"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "313"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "335"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "339"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "328"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "334"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "322"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "347"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "334"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "340"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "319"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "365"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "315"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "307"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "314"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "317"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "310"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "324"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "313"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "318"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "314"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "324"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "297"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "335"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "335"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "343"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "342"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "318"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "306"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "353"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "332"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "330"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "334"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "307"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "306"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "10 loops, best of 3: 96.2 ms per loop\n"
+ ]
+ }
+ ],
+ "prompt_number": 56
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "%%timeit\n",
+ "\n",
+ "wins = 0\n",
+ "for _ in range(1000):\n",
+ " g = Game(random.choice(WORDS), player=PlayerFixedOrder(list(reversed(string.ascii_lowercase))))\n",
+ " g.play_game()\n",
+ " if g.game_won:\n",
+ " wins += 1\n",
+ "print(wins)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "7\n",
+ "5"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "6"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "7"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "7"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "10"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "8"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "5"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "5"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "5"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "9"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "3"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "8"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "13"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "8"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "10"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "9"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "9"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "12"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "6"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "6"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "14"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "9"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "1"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "8"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "8"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "7"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "9"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "10"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "6"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "7"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "6"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "4"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "6"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "3"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "7"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "7"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "10"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "11"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "6"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "4"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "10 loops, best of 3: 74.6 ms per loop\n"
+ ]
+ }
+ ],
+ "prompt_number": 57
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "DICT_COUNTS = collections.Counter(l.lower() for l in open('/usr/share/dict/british-english').read() if l in string.ascii_letters)\n",
+ "DICT_LETTERS_IN_ORDER = [p[0] for p in DICT_COUNTS.most_common()]"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "DICT_COUNTS"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 12,
+ "text": [
+ "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})"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "print(DICT_LETTERS_IN_ORDER)\n",
+ "print(LETTERS_IN_ORDER)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "['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",
+ "['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"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "wins = 0\n",
+ "for _ in range(1000):\n",
+ " g = Game(random.choice(WORDS), player=PlayerFixedOrder(DICT_LETTERS_IN_ORDER))\n",
+ " g.play_game()\n",
+ " if g.game_won:\n",
+ " wins += 1\n",
+ "print(wins)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "440\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "class PlayerAdaptiveLength:\n",
+ " def __init__(self, words):\n",
+ " self.all_words = words\n",
+ " self.candidate_words = None\n",
+ " \n",
+ " def guess(self, discovered, missed, lives):\n",
+ " if not self.candidate_words:\n",
+ " self.set_ordered_letters(len(discovered))\n",
+ " guessed_letters = [l.lower() for l in discovered + missed if l in string.ascii_letters]\n",
+ " return [l for l in self.ordered_letters if l not in guessed_letters][0]\n",
+ " \n",
+ " def set_ordered_letters(self, word_len):\n",
+ " self.candidate_words = [w for w in self.all_words if len(w) == word_len]\n",
+ " counts = collections.Counter(l.lower() for l in ''.join(self.candidate_words) if l in string.ascii_letters)\n",
+ " self.ordered_letters = [p[0] for p in counts.most_common()]"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 33
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "wins = 0\n",
+ "for _ in range(1000):\n",
+ " g = Game(random.choice(WORDS), player=PlayerAdaptiveLength(WORDS))\n",
+ " g.play_game()\n",
+ " if g.game_won:\n",
+ " wins += 1\n",
+ "print(wins)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "474\n"
+ ]
+ }
+ ],
+ "prompt_number": 34
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "class PlayerAdaptiveIncludedLetters:\n",
+ " def __init__(self, words):\n",
+ " self.candidate_words = words\n",
+ " \n",
+ " def guess(self, discovered, missed, lives):\n",
+ " self.filter_candidate_words(discovered)\n",
+ " self.set_ordered_letters()\n",
+ " guessed_letters = [l.lower() for l in discovered + missed if l in string.ascii_letters]\n",
+ " return [l for l in self.ordered_letters if l not in guessed_letters][0]\n",
+ " \n",
+ " def filter_candidate_words(self, discovered):\n",
+ " exp = re.compile('^' + ''.join(discovered).replace('_', '.') + '$')\n",
+ " self.candidate_words = [w for w in self.candidate_words if exp.match(w)]\n",
+ " \n",
+ " def set_ordered_letters(self):\n",
+ " counts = collections.Counter(l.lower() for l in ''.join(self.candidate_words) if l in string.ascii_letters)\n",
+ " self.ordered_letters = [p[0] for p in counts.most_common()]"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 35
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "wins = 0\n",
+ "for _ in range(1000):\n",
+ " g = Game(random.choice(WORDS), player=PlayerAdaptiveIncludedLetters(WORDS))\n",
+ " g.play_game()\n",
+ " if g.game_won:\n",
+ " wins += 1\n",
+ "print(wins)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "982\n"
+ ]
+ }
+ ],
+ "prompt_number": 36
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "re.match('^[^xaz]*$', 'happy')"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 37
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "class PlayerAdaptiveExcludedLetters:\n",
+ " def __init__(self, words):\n",
+ " self.candidate_words = words\n",
+ " \n",
+ " def guess(self, discovered, missed, lives):\n",
+ " self.filter_candidate_words(missed)\n",
+ " self.set_ordered_letters()\n",
+ " guessed_letters = [l.lower() for l in discovered + missed if l in string.ascii_letters]\n",
+ " return [l for l in self.ordered_letters if l not in guessed_letters][0]\n",
+ " \n",
+ " def filter_candidate_words(self, missed):\n",
+ " if missed:\n",
+ " exp = re.compile('^[^' + ''.join(missed) + ']*$')\n",
+ " self.candidate_words = [w for w in self.candidate_words if exp.match(w)]\n",
+ " \n",
+ " def set_ordered_letters(self):\n",
+ " counts = collections.Counter(l.lower() for l in ''.join(self.candidate_words) if l in string.ascii_letters)\n",
+ " self.ordered_letters = [p[0] for p in counts.most_common()]"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 38
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "wins = 0\n",
+ "for _ in range(1000):\n",
+ " g = Game(random.choice(WORDS), player=PlayerAdaptiveExcludedLetters(WORDS))\n",
+ " g.play_game()\n",
+ " if g.game_won:\n",
+ " wins += 1\n",
+ "print(wins)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "502\n"
+ ]
+ }
+ ],
+ "prompt_number": 39
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "class PlayerAdaptivePattern:\n",
+ " def __init__(self, words):\n",
+ " self.candidate_words = words\n",
+ " \n",
+ " def guess(self, discovered, missed, lives):\n",
+ " self.filter_candidate_words(discovered, missed)\n",
+ " self.set_ordered_letters()\n",
+ " guessed_letters = [l.lower() for l in discovered + missed if l in string.ascii_letters]\n",
+ " return [l for l in self.ordered_letters if l not in guessed_letters][0]\n",
+ " \n",
+ " def filter_candidate_words(self, discovered, missed):\n",
+ " attempted_letters = list(set(l.lower() for l in discovered + missed if l in string.ascii_letters))\n",
+ " if attempted_letters:\n",
+ " exclusion_pattern = '[^' + ''.join(attempted_letters) + ']'\n",
+ " else:\n",
+ " exclusion_pattern = '.'\n",
+ " exp = re.compile('^' + ''.join(discovered).replace('_', exclusion_pattern) + '$')\n",
+ " self.candidate_words = [w for w in self.candidate_words if exp.match(w)]\n",
+ " \n",
+ " def set_ordered_letters(self):\n",
+ " counts = collections.Counter(l.lower() for l in ''.join(self.candidate_words) if l in string.ascii_letters)\n",
+ " self.ordered_letters = [p[0] for p in counts.most_common()]\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 40
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "wins = 0\n",
+ "for _ in range(1000):\n",
+ " g = Game(random.choice(WORDS), player=PlayerAdaptivePattern(WORDS))\n",
+ " g.play_game()\n",
+ " if g.game_won:\n",
+ " wins += 1\n",
+ "print(wins)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "993\n"
+ ]
+ }
+ ],
+ "prompt_number": 41
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "%%timeit\n",
+ "\n",
+ "wins = 0\n",
+ "for _ in range(1000):\n",
+ " g = Game(random.choice(WORDS), player=PlayerAdaptivePattern(WORDS))\n",
+ " g.play_game()\n",
+ " if g.game_won:\n",
+ " wins += 1\n",
+ "print(wins)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "994\n",
+ "993"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "987"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "993"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "1 loops, best of 3: 30.6 s per loop\n"
+ ]
+ }
+ ],
+ "prompt_number": 42
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "class PlayerAdaptive:\n",
+ " def __init__(self, words):\n",
+ " self.candidate_words = words\n",
+ " \n",
+ " def guess(self, discovered, missed, lives):\n",
+ " self.filter_candidate_words(discovered, missed)\n",
+ " self.set_ordered_letters()\n",
+ " guessed_letters = [l.lower() for l in discovered + missed if l in string.ascii_letters]\n",
+ " return [l for l in self.ordered_letters if l not in guessed_letters][0]\n",
+ " \n",
+ " def filter_candidate_words(self, discovered, missed):\n",
+ " pass\n",
+ " \n",
+ " def set_ordered_letters(self):\n",
+ " counts = collections.Counter(l.lower() \n",
+ " for l in ''.join(self.candidate_words) + string.ascii_lowercase \n",
+ " if l in string.ascii_letters)\n",
+ " self.ordered_letters = [p[0] for p in counts.most_common()]"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 43
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "class PlayerAdaptiveLength(PlayerAdaptive):\n",
+ " def __init__(self, words):\n",
+ " super().__init__(words)\n",
+ " self.word_len = None\n",
+ " self.ordered_letters = None\n",
+ " \n",
+ " def filter_candidate_words(self, discovered, missed):\n",
+ " if not self.word_len:\n",
+ " self.word_len = len(discovered)\n",
+ " self.candidate_words = [w for w in self.candidate_words if len(w) == self.word_len]\n",
+ " \n",
+ " def set_ordered_letters(self):\n",
+ " if not self.ordered_letters:\n",
+ " super().set_ordered_letters()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 44
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "class PlayerAdaptiveIncludedLetters(PlayerAdaptive):\n",
+ " def filter_candidate_words(self, discovered, missed):\n",
+ " exp = re.compile('^' + ''.join(discovered).replace('_', '.') + '$')\n",
+ " self.candidate_words = [w for w in self.candidate_words if exp.match(w)]"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 45
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "class PlayerAdaptiveExcludedLetters(PlayerAdaptive):\n",
+ " def filter_candidate_words(self, discovered, missed):\n",
+ " if missed:\n",
+ " exp = re.compile('^[^' + ''.join(missed) + ']*$')\n",
+ " self.candidate_words = [w for w in self.candidate_words if exp.match(w)] "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 46
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "class PlayerAdaptivePattern(PlayerAdaptive):\n",
+ " def filter_candidate_words(self, discovered, missed):\n",
+ " attempted_letters = [l for l in discovered if l != '_'] + missed\n",
+ " if attempted_letters:\n",
+ " exclusion_pattern = '[^' + ''.join(attempted_letters) + ']'\n",
+ " else:\n",
+ " exclusion_pattern = '.'\n",
+ " exp = re.compile('^' + ''.join(discovered).replace('_', exclusion_pattern) + '$')\n",
+ " self.candidate_words = [w for w in self.candidate_words if exp.match(w)]"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 47
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "%%timeit\n",
+ "\n",
+ "wins = 0\n",
+ "for _ in range(1000):\n",
+ " g = Game(random.choice(WORDS), player=PlayerAdaptiveLength(WORDS))\n",
+ " g.play_game()\n",
+ " if g.game_won:\n",
+ " wins += 1\n",
+ "print(wins)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "479\n",
+ "455"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "460"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "498"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "1 loops, best of 3: 15.9 s per loop\n"
+ ]
+ }
+ ],
+ "prompt_number": 48
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "%%timeit\n",
+ "\n",
+ "wins = 0\n",
+ "for _ in range(1000):\n",
+ " g = Game(random.choice(WORDS), player=PlayerAdaptiveIncludedLetters(WORDS))\n",
+ " g.play_game()\n",
+ " if g.game_won:\n",
+ " wins += 1\n",
+ "print(wins)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "981\n",
+ "983"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "980"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "980"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "1 loops, best of 3: 36.9 s per loop\n"
+ ]
+ }
+ ],
+ "prompt_number": 49
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "%%timeit\n",
+ "\n",
+ "wins = 0\n",
+ "for _ in range(1000):\n",
+ " g = Game(random.choice(WORDS), player=PlayerAdaptiveExcludedLetters(WORDS))\n",
+ " g.play_game()\n",
+ " if g.game_won:\n",
+ " wins += 1\n",
+ "print(wins)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "521\n",
+ "484"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "491"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "518"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "1 loops, best of 3: 7min 18s per loop\n"
+ ]
+ }
+ ],
+ "prompt_number": 50
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "%%timeit\n",
+ "\n",
+ "wins = 0\n",
+ "for _ in range(1000):\n",
+ " g = Game(random.choice(WORDS), player=PlayerAdaptivePattern(WORDS))\n",
+ " g.play_game()\n",
+ " if g.game_won:\n",
+ " wins += 1\n",
+ "print(wins)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "987\n",
+ "988"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "996"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "995"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "1 loops, best of 3: 31.1 s per loop\n"
+ ]
+ }
+ ],
+ "prompt_number": 51
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "for _ in range(1000):\n",
+ " g = Game(random.choice(WORDS), player=PlayerAdaptivePattern(WORDS))\n",
+ " g.play_game()\n",
+ " if not g.game_won:\n",
+ " print(g.target, g.discovered, g.wrong_letters)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "naked ['_', 'a', '_', 'e', 'd'] ['s', 'r', 'w', 'c', 't', 'g', 'l', 'f', 'p', 'm']\n",
+ "wound"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " ['_', 'o', 'u', 'n', 'd'] ['s', 'e', 'a', 'y', 'p', 'm', 'f', 'h', 'b', 'r']\n",
+ "hut"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " ['_', 'u', 't'] ['a', 'o', 'e', 'i', 'b', 'g', 'n', 'p', 'm', 'c']\n",
+ "fucker"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " ['_', 'u', '_', '_', 'e', 'r'] ['d', 's', 'i', 'a', 'o', 't', 'b', 'n', 'm', 'l']\n",
+ "fox"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " ['_', 'o', '_'] ['a', 't', 'b', 'd', 'w', 'p', 'n', 's', 'g', 'y']\n",
+ "wills"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " ['_', 'i', 'l', 'l', 's'] ['e', 'o', 'a', 'g', 'p', 'm', 'k', 'f', 'h', 'd']\n",
+ "bunny"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " ['_', 'u', 'n', 'n', 'y'] ['s', 'e', 'a', 'o', 'i', 'm', 'l', 'g', 'f', 't']\n",
+ "curving"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " ['c', 'u', '_', '_', 'i', 'n', 'g'] ['e', 'a', 'o', 'l', 's', 'f', 'p', 'b', 't', 'm']\n",
+ "hoe"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " ['_', 'o', '_'] ['a', 't', 'b', 'd', 'w', 'p', 'n', 's', 'g', 'y']\n",
+ "butt"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " ['_', 'u', '_', '_'] ['e', 's', 'o', 'a', 'i', 'l', 'f', 'r', 'n', 'm']\n",
+ "mucked"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " ['_', 'u', '_', '_', 'e', 'd'] ['a', 'o', 'i', 'l', 's', 't', 'f', 'p', 'g', 'b']\n",
+ "flaw"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " ['f', 'l', 'a', '_'] ['e', 's', 'o', 'r', 'y', 'g', 'k', 'x', 'p', 'n']\n"
+ ]
+ }
+ ],
+ "prompt_number": 52
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "iterations = 10000\n",
+ "wins = 0\n",
+ "for _ in range(iterations):\n",
+ " g = Game(random.choice(WORDS), player=PlayerAdaptivePattern(WORDS))\n",
+ " g.play_game()\n",
+ " if g.game_won:\n",
+ " wins += 1\n",
+ "print(wins / iterations)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "0.9936\n"
+ ]
+ }
+ ],
+ "prompt_number": 53
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "class PlayerAdaptiveNoRegex:\n",
+ " def __init__(self, words):\n",
+ " self.candidate_words = words\n",
+ " \n",
+ " def guess(self, discovered, missed, lives):\n",
+ " self.filter_candidate_words(discovered, missed)\n",
+ " self.set_ordered_letters()\n",
+ " guessed_letters = [l.lower() for l in discovered + missed if l in string.ascii_letters]\n",
+ " return [l for l in self.ordered_letters if l not in guessed_letters][0]\n",
+ " \n",
+ " def filter_candidate_words(self, discovered, missed):\n",
+ " pass\n",
+ " \n",
+ " def set_ordered_letters(self):\n",
+ " counts = collections.Counter(l.lower() \n",
+ " for l in ''.join(self.candidate_words) + string.ascii_lowercase \n",
+ " if l in string.ascii_letters)\n",
+ " self.ordered_letters = [p[0] for p in counts.most_common()]\n",
+ "\n",
+ " def match(self, pattern, target, excluded=None):\n",
+ " if not excluded:\n",
+ " excluded = ''\n",
+ " if len(pattern) != len(target):\n",
+ " return False\n",
+ " for m, c in zip(pattern, target):\n",
+ " if m == '_' and c not in excluded:\n",
+ " # true\n",
+ " pass\n",
+ " elif m != '_' and m == c:\n",
+ " # true\n",
+ " pass\n",
+ " else:\n",
+ " return False\n",
+ " return True "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 59
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "class PlayerAdaptiveLengthNoRegex(PlayerAdaptiveNoRegex):\n",
+ " def __init__(self, words):\n",
+ " super().__init__(words)\n",
+ " self.word_len = None\n",
+ " self.ordered_letters = None\n",
+ " \n",
+ " def filter_candidate_words(self, discovered, missed):\n",
+ " if not self.word_len:\n",
+ " self.word_len = len(discovered)\n",
+ " self.candidate_words = [w for w in self.candidate_words if len(w) == self.word_len]\n",
+ " \n",
+ " def set_ordered_letters(self):\n",
+ " if not self.ordered_letters:\n",
+ " super().set_ordered_letters()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 60
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "class PlayerAdaptiveIncludedLettersNoRegex(PlayerAdaptiveNoRegex):\n",
+ " def filter_candidate_words(self, discovered, missed):\n",
+ " self.candidate_words = [w for w in self.candidate_words if self.match(discovered, w)]"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 61
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "class PlayerAdaptiveExcludedLettersNoRegex(PlayerAdaptiveNoRegex):\n",
+ " def filter_candidate_words(self, discovered, missed):\n",
+ " if missed:\n",
+ " empty_target = '_' * len(discovered)\n",
+ " self.candidate_words = [w for w in self.candidate_words if self.match(empty_target, w, missed)] "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 66
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "class PlayerAdaptivePatternNoRegex(PlayerAdaptiveNoRegex):\n",
+ " def filter_candidate_words(self, discovered, missed):\n",
+ " attempted_letters = [l for l in discovered if l != '_'] + missed\n",
+ " self.candidate_words = [w for w in self.candidate_words if self.match(discovered, w, attempted_letters)]"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 67
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "%%timeit\n",
+ "\n",
+ "wins = 0\n",
+ "for _ in range(1000):\n",
+ " g = Game(random.choice(WORDS), player=PlayerAdaptiveLengthNoRegex(WORDS))\n",
+ " g.play_game()\n",
+ " if g.game_won:\n",
+ " wins += 1\n",
+ "print(wins)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "471\n",
+ "492"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "502"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "469"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "1 loops, best of 3: 16 s per loop\n"
+ ]
+ }
+ ],
+ "prompt_number": 69
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "%%timeit\n",
+ "\n",
+ "wins = 0\n",
+ "for _ in range(1000):\n",
+ " g = Game(random.choice(WORDS), player=PlayerAdaptiveIncludedLettersNoRegex(WORDS))\n",
+ " g.play_game()\n",
+ " if g.game_won:\n",
+ " wins += 1\n",
+ "print(wins)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "978\n",
+ "979"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "983"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "979"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "1 loops, best of 3: 48 s per loop\n"
+ ]
+ }
+ ],
+ "prompt_number": 70
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "%%timeit\n",
+ "\n",
+ "wins = 0\n",
+ "for _ in range(1000):\n",
+ " g = Game(random.choice(WORDS), player=PlayerAdaptiveExcludedLettersNoRegex(WORDS))\n",
+ " g.play_game()\n",
+ " if g.game_won:\n",
+ " wins += 1\n",
+ "print(wins)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "582\n",
+ "595"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "587"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "611"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "1 loops, best of 3: 4min 59s per loop\n"
+ ]
+ }
+ ],
+ "prompt_number": 71
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "%%timeit\n",
+ "\n",
+ "wins = 0\n",
+ "for _ in range(1000):\n",
+ " g = Game(random.choice(WORDS), player=PlayerAdaptivePatternNoRegex(WORDS))\n",
+ " g.play_game()\n",
+ " if g.game_won:\n",
+ " wins += 1\n",
+ "print(wins)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "991\n",
+ "992"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "993"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "994"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "1 loops, best of 3: 37.9 s per loop\n"
+ ]
+ }
+ ],
+ "prompt_number": 72
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 72
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file