Guest User

Untitled

a guest
Feb 11th, 2012
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. # for http://www.reddit.com/r/dailyprogrammer/comments/pkwgf/2112012_challenge_3_difficult/
  5.  
  6. import re
  7.  
  8. words = open('../wordlist.txt').read()
  9. scrambled = {'mkeart': '', 'sleewa': '', 'edcudls': '', 'iragoge': '', 'usrlsle': '', 'nalraoci': '', 'nsdeuto': '', 'amrhat': '', 'inknsy': '', 'iferkna': ''}
  10.  
  11. def unique_chars(chars):
  12.     ''' Find unique chars from a string '''
  13.     ret = []
  14.     for c in chars:
  15.         if not c in ret:
  16.             ret.append(c)
  17.     return "".join(ret)
  18.  
  19. for s in scrambled.keys():
  20.     u_chars = unique_chars(s)
  21.     # find words that are the right length and contain a subset of the scrambled chars
  22.     matches = re.findall('^[%s]{%s}$'%(u_chars, len(s)), words, re.M)
  23.     # now check all the chars are there
  24.     for match in matches:
  25.         add = True
  26.         for u in u_chars:
  27.             if match.find(u) == -1:
  28.                 add = False
  29.                 break
  30.         if add:
  31.             scrambled[s] = match
  32.  
  33. # sort by key length
  34. for s in sorted(scrambled.keys(), key=lambda k: len(k)):
  35.     print('%s: %s' % (s, scrambled[s]))
Add Comment
Please, Sign In to add comment