Last active
January 31, 2021 18:07
-
-
Save lobrien/ce8e63d8193d63252ede9c520e36445b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import nltk | |
nltk.download('words') | |
from nltk.corpus import words | |
tetrawords = set(w for w in words.words() if len(w) == 4) | |
from constraint import * | |
wordladder = Problem() | |
for v in ['b','c','d','e','f','g','h'] : | |
wordladder.addVariable(v, Domain(tetrawords)) | |
wordladder.addVariable('a', ["earn"]) | |
wordladder.addVariable('i', ["free"]) | |
# Constraint is: zip chars |> piecewise compare |> count matches |> matches == length - 1 | |
singleLetterDifferenceFn = lambda init, xform : sum(map(lambda t : t[0] == t[1], zip(init,xform))) == len(init) - 1 | |
for x in range(ord('a'), ord('i')) : | |
wordladder.addConstraint(singleLetterDifferenceFn, [chr(x),chr(x+1)]) | |
for soln in wordladder.getSolutionIter() : | |
print(soln) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment