Thank you both, Alan and Gregor. Sorry I didn't reply earlier; just a bit
overwhelmed with a family situation over here.

Alan, looks like I'll have to go over list comprehension as a way to
initialize variables. I recall when I first started programming this, I
tried wordlist = [] * 31, completely overlooking the fact that all of the
list were in fact the same, and when debugging finding that my wordlist had
been copied 31 times!

Gregor, I like your version since you don't have to constrain the list
artificially, as I did. For this problem in particular, the longest
interlaced word I could find was 11 characters long, and the longer you go,
the less probable it is that you'll find one. 31 seemed like a good enough
value, but I'll probably change my version to look like yours.

Thanks again, guys!

Tony R.

On 7/12/07, Gregor Lingl <[EMAIL PROTECTED]> wrote:

taserian schrieb:
> I've been programming for years before encountering Pythoin, but in
> much more verbose languages, and I appreciate the fact that Python is
> so terse and still accomplishes so much. Still, I have difficulty
> thinking in Python, and tend to revert to longer programs for what
> seems to be simple tasks. Sometimes it's because I'm just not aware of
> a library function, but many times it's because there's just some
> elegant way to solve my kludge.
>
> As an exercise in Python, I decided to solve a toy problem called
> "interleave". The idea is that I want to determine how many words in
> the English language are decomposable into two words by taking it
> apart one letter at a time. So the word "theorems" could be decomposed
> into "term" and "hoes" (original word = abababab, decomposed words =
> aaaa and bbbb, with all letters in the same order as in the original
> word).
>
> I'd like to see how I could improve the program below for this, and
> make it more Python-ish.
>
Hi taserian,

I've just produced an alternative solution of your problem
(based on your ideas). The main difference is, that I use
a wordlength-dictionary. This version has the advantage, that
it doesn't make an assumption about the max word length.
I don't consider it to be an improvement, just an example
that uses different language elements of Python in some places.
Hope you enjoy it.

==== START

wordlengthdict = {}

for word in open("wordlist.txt").read().split():
    wordlengthdict.setdefault(len(word),[]).append(word)

outfile = open('InterleaveEnglishYAWL.txt', 'w')

for l in sorted(wordlengthdict.keys()):
    print l
    for w in wordlengthdict[l]:
        wordtriple = (w1, w2, dummy)  = w[0::2], w[1::2], w
        if  w1 in wordlengthdict.get(len(w1),[]) and w2 in
wordlengthdict.get(len(w2),[]):
            outfile.write("(%s, %s, %s)\n" % wordtriple)
            print wordtriple

outfile.close();

==== END

Best regards,
Gregor

> = = = = START
>
> def decompose(longword):
>     word1 = longword[0::2]
>     word2 = longword[1::2]
>     return (word1, word2)
>
> wordlengthlist = [None] * 31
> for i in range(0,len(wordlengthlist)):
>     wordlengthlist[i] = []
> results = []
>
> for lineread in open("word.list"):
>     for word in lineread.split():
>         if len(word)<31:
>             wordlengthlist[len(word)].append(word)
>
> outfile = open('InterleaveEnglishYAWL.txt', 'w')
>
> for x in range(4, 31):
>     print x
>     for i in wordlengthlist[x]:
>         twowords = decompose(i)
>         word1 = twowords[0]
>         word2 = twowords[1]
>         if     word1 in wordlengthlist[len(word1)] and word2 in
> wordlengthlist[len(word2)]:
>             outfile.write("(" + word1 + ", " + word2 + ", " + i + ")\n")
>             print (word1, word2, i)
>
> outfile.close();
>
> = = = = END
>
> Tony R.
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to