[Tutor] More Pythonesque or a more efficient method
W W
srilyk at gmail.com
Wed Oct 8 02:39:33 CEST 2008
On Tue, Oct 7, 2008 at 5:58 PM, Emile van Sebille <emile at fenx.com> wrote:
> Robert Berman wrote:
>
>> Hi,
>>
>> The below script which prints anagrams available for any word available
>> within a given database. It does work, but it is not very fast. I am
>> relatively certain there are more Python friendly coding techniques but I am
>> more concerned with a faster algorithm.
>>
>
> You might consider looking up the permutations only (up to some length --
> there are a lot of permutations for 8+ letter words :)
>
I'm not sure if this would be any faster, but I'd be curious at least to
know what the difference would be to have a database of every possible
permutation for every length up to 10, i.e:
0
01
10
012
021
102
120
201
210
.
.
.
etc.
And then simply use the key values of the letter... perhaps the anagrams
stored in a dict by length as the key:
mutations = {1: [(0,)], 2: [(0,1), (1,0)],}
so for instance:
myword = "if"
mylen = len(myword)
for mytuple in mutations[mylen]:
for x in mytuple:
print myword[x]
I think that would work anyway. I don't know if I have the code right, and
I'm not sure if it would be faster, slower, or no change, but it may be
worth a look.
HTH,
Wayne
--
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn't. - Primo Levi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20081007/ea8174c1/attachment-0001.htm>
More information about the Tutor
mailing list