words in word

My son, Jayden (8 years old), had a homework assignment yesterday to find words made up of the letters of another word (e.g. pat in part). So, instead of thinking, we wrote this:

from itertools import permutations
import sys

word = sys.argv[1]

words = open('/usr/share/dict/words').read().split("\n")
valid = set()

for k in range(1, len(word)):
    for combination in permutations(word, k + 1):
        to_check = "".join(combination)
        if to_check in words:

print list(valid)

Use like:

$ python words-in-word.py part

He also now has some understanding of strings and lists in python.

close enough

comments powered by Disqus