Anagram

Emile van Sebille emile at fenx.com
Tue Feb 26 09:16:29 EST 2002


From: "Anton Vredegoor"
>  I wrote
> a voluminous script to generate anagrams some time ago, and I am
> looking for options to shorten the algorithm to a few lines of code as
> has been done for the algorithm the OP asked a question about.
>
Hi Anton,

If you have a version of python that support list comprehensions, if you
switch to using them your program would be a lot shorter.  One example:

    def computetresholds(self,sf):
    #tresholds for choosing the next integer
        tresholds = []
        np = self.nperm(sf)
        n = self.sum(sf)
        for x in sf:
            if x<> 0:
                t = (np*x)/n
                tresholds.append(t)
            else:
                tresholds.append(0)
        return tresholds

can be changed to: (untested)

    def computetresholds(self,sf):
        np = self.nperm(sf)
        n = self.sum(sf)
        return [ (np*x)/n for x in sf ]

HTH,


Emile van Sebille
emile at fenx.com

---------




More information about the Python-list mailing list