# Lesson 16: Is this artificial intelligence? Automated Hangman algorithm

Post date: Feb 16, 2018 4:03:14 PM

import word_dict

def lengthwords(searchspace, length):

returnspace = []

for w in searchspace:

if len(w) == length:

returnspace.append(w)

return returnspace

def match(searchspace, pattern):

returnspace = []

for w in searchspace:

for x in range(0, len(w)):

if pattern[x] != "_" and w[x] != pattern[x]:

break

returnspace.append(w)

return returnspace

def eliminatemissed(searchspace, missedletters):

returnspace = []

for w in searchspace:

for l in w:

if missedletters.find(l) != -1:

break

returnspace.append(w)

return returnspace

def stat(searchspace, pattern):

abc = "abcdefghijklmnopqrstuvwxyz"

lstat = {}

for c in abc:

lstat[c] = 0

for w in searchspace:

letters = ""

for c in w:

if letters.find(c) == -1:

letters += c

for c in letters:

lstat[c] += 1

count = 0

letter = ""

for c in abc:

if pattern.find(c) == -1:

if lstat[c] > count:

count = lstat[c]

letter = c

return letter

space = lengthwords(word_dict.db, 9)

space = match(space, "_________")

space = eliminatemissed(space, "")

lstat = stat(space, "_________")

print(lstat)