[Tutor] String made of list elements' nth characters?

R. Alan Monroe amonroe at columbus.rr.com
Sun Apr 20 18:39:18 CEST 2008


> R. Alan Monroe wrote:

>> def checksolution(sol):
>>     maybes = []
>>     big = ''.join(solution)
>>     for x in range(len(sol)):
>>         maybes.append( big[x::6] )
>>     print maybes
>> 
>> solution=['abject','poetry','keypad']
>> checksolution(solution)
>> 
>> ['apk', 'boe', 'jey']

> That's nice. Why do you use len(sol) instead of len(sol[0])? You only 
> print the first three items. Here is another way to do it:

> In [8]: solution=['abject','poetry','keypad']
> In [9]: [''.join(x) for x in zip(*solution)]
> Out[9]: ['apk', 'boe', 'jey', 'etp', 'cra', 'tyd']

If there are no valid words beginning with 'apk', 'boe', or 'jey'
(there aren't) I know immediately that this word square can't possibly
be completed, so no need to evaluate the remaining three.

(I create a dict with all possible prefixes to speed up validity
checks too.)

In case it's not clear, a word square is basically a 6x6 crossword
puzzle with no blacked out cells. Valid words are form on all the
acrosses and all the downs simultaneously.

Playing this game is what inspired me to experiment with it:
http://boardgamegeek.com/game/8032


Alan



More information about the Tutor mailing list