[Tutor] word challenge
gyro funch
gyromagnetic@excite.com
Sat Jul 19 12:48:03 2003
Raymond Hettinger wrote:
>>Since the number of paths through the phrase is very large,
>>a good bet is to start with a dictionary of available words
>>and test to see if they can be found in the phrase (while
>>paying attention to letter order and the requirement that
>>each group be represented).
<snip excellent comments and inspiring code>
Excellent Raymond!
Thanks for the great analysis and well though out code.
On the other end of the spectrum is my code below.
This uninspiring, brute-force code relies on the 'batteries included' aspect of Python and makes the regular expression engine do all of the work. We basically construct a regular expression satisfying the criteria outlined earlier and 'let it rip' on the word list. As Raymond and others pointed out, the word list could be pre-processed to eliminate words clearly not satisfying the criteria.
Thanks to all for your great code and efforts!
-g
My apologies if my mailer screws up the code formatting.
class FindWords(object):
def __init__(self, wfile = '/usr/dict/words'):
self.wfile = wfile
self.wlist = []
self._make_word_list()
def _make_word_list(self):
import re
wrd_re = re.compile(r'^[a-z]+$')
fh = open(self.wfile,'r')
all_lines = fh.readlines()
fh.close()
for line in all_lines:
word = line.strip().lower()
if wrd_re.search(word):
self.wlist.append(word)
return self.wlist
def _build_regex(self, phrase):
mlist = []
for word in phrase.split():
wr,wlist = range(len(word)),[]
for j in wr:
wre = ''
for i in wr:
if i == j:
char = ''
else:
char = '?'
wre += '%s%s' % (word[i],char)
wlist.append(wre)
mlist.append('(%s)' % '|'.join(wlist))
full_re = '^%s$' % ''.join(mlist)
return full_re
def get_words(self, phrase):
import re
found_words = []
p_regex = re.compile(self._build_regex(phrase))
for word in self.wlist:
if p_regex.search(word):
found_words.append(word)
return found_words
def main():
mw = FindWords()
phrase = 'handy cool phrase'
wl = mw.get_words(phrase)
# let's get the longest and shortest words
wdict = {}
for word in wl:
wdict.setdefault(len(word),[]).append(word)
wrds = wdict.keys()
mn,mx = min(wrds),max(wrds)
print wdict[mn]
print wdict[mx]
return
_______________________________________________
Express Yourself - Share Your Mood in Emails!
Visit www.SmileyCentral.com - the happiest place on the Web.