Lesson 15: Finding words with a pattern
Post date: Feb 8, 2018 4:05:48 PM
import word_dict
def wordlength(x):
searchspace = []
for w in word_dict.db:
if len(w) == x:
searchspace.append(w)
return searchspace
def lengthstat(searchspace):
maxlength = 0
for w in searchspace:
if len(w) > maxlength:
maxlength = len(w)
lstat = {}
for x in range(0,maxlength + 1):
lstat[x] = 0
for w in searchspace:
lstat[len(w)] += 1
return lstat
def letterstat(searchspace):
abc = "abcdefghijklmnopqrstuvwxyz"
lstat = {}
for l in abc:
lstat[l] = 0
for w in searchspace:
letters = ""
w = w.lower()
for l in w:
if letters.find(l) == -1:
letters += l
for l in letters:
lstat[l] += 1
return lstat
def match(searchspace, pattern):
space = []
for w in searchspace:
addword = True
for x in range(0, len(w)):
if pattern[x] != "_":
if pattern[x] != w[x]:
addword = False
break
if addword:
space.append(w)
return space
space = wordlength(4)
space = match(space, "j__l")
print(space)
lstat = letterstat(space)
for l in "abcdefghijklmnopqrstuvwxyz":
if lstat[l] != 0: print(l, "=", lstat[l])