#!/usr/bin/env python # -*- coding: utf-8 -*- # for http://www.reddit.com/r/dailyprogrammer/comments/pkwgf/2112012_challenge_3_difficult/ import re words = open('../wordlist.txt').read() scrambled = {'mkeart': '', 'sleewa': '', 'edcudls': '', 'iragoge': '', 'usrlsle': '', 'nalraoci': '', 'nsdeuto': '', 'amrhat': '', 'inknsy': '', 'iferkna': ''} def unique_chars(chars): ''' Find unique chars from a string ''' ret = [] for c in chars: if not c in ret: ret.append(c) return "".join(ret) for s in scrambled.keys(): u_chars = unique_chars(s) # find words that are the right length and contain a subset of the scrambled chars matches = re.findall('^[%s]{%s}$'%(u_chars, len(s)), words, re.M) # now check all the chars are there for match in matches: add = True for u in u_chars: if match.find(u) == -1: add = False break if add: scrambled[s] = match # sort by key length for s in sorted(scrambled.keys(), key=lambda k: len(k)): print('%s: %s' % (s, scrambled[s]))