Lesson 15: Hangman search algorithm

posted Feb 9, 2018, 7:59 AM by Attila Lengyel
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:
    add = True
    for x in range(0, len(w)):
      if pattern[x] != "_" and w[x] != pattern[x]:
          add = False
          break
    if add:
      returnspace.append(w)
  return returnspace

def eliminatemissed(searchspace, missedletters):
  returnspace = []
  for w in searchspace:
    add = True
    for l in w:
      if missedletters.find(l) != -1:
        add = False
        break
    if add:
      returnspace.append(w)
  return returnspace
  
space = lengthwords(word_dict.db, 5)
space = match(space, "_ab_e")
space = eliminatemissed(space, "smc")

print(space)



Comments