Stackless 3.0 alpha 1 at blinding speed

Anton Vredegoor anton at vredegoor.doge.nl
Sun Apr 20 11:42:24 EDT 2003


"A.M. Kuchling" <amk at amk.ca> wrote:

>http://mail.python.org/pipermail/python-dev/1999-July/000467.html

Thanks. It seems Tim is making a heroic effort to explain the new in
terms of the old. This necessarily must fail, but what other option is
there instead of rebuilding all terminology out of gotos?

By the way, I have a hunch the anagram script below is somehow related
to the operations described, since the script "changes directions"
depending on the order the lists items are popped. A simple
permutation could just do a series of divmods. 

If not, please excuse me, it's just a natural defensive reaction to
try to hold of the unknown with the shield of the known. In fact
knowledgeable people are welcome to hide behind *my* ignorance :-)

Anton

#anagram.py by Anton Vredegoor anton at vredegoor.doge.nl Apr 20, 2003
"""
    Starters(L) returns a 2-tuple for each *different* item
    in the list L:
    
    a) the index of the first occurrence of the item,
    b) the number of anagrams starting with this item.

    The function starters is used by the function anagram to 
    construct indexed anagrams of a list.
"""

def starters(L):
    #return a list of 2-tuples containing info about list L
    n,np,R = len(L),1,range(len(L))
    bf = [L[:i].count(L[i]) for i in R]
    for i in R: np = np*(n-i)/(bf[i]+1)
    return [(i,np*L[i:].count(L[i])/n) for i in R if not bf[i]]
    
def anagram(L,index=0):
    #return an anagram of list L
    remain, res, T = index, [], L[:]
    while T:
        for j,k in starters(T):
            if remain-k < 0:
                res.append(T.pop(j))
                break
            remain -= k
    return res

def nperm(L):
    #return the number of possible anagrams of list L
    return reduce(lambda a,b:a+b,[k for j,k in starters(L)])

def test():
    L = [[1],[1],[2],[3]]
    np = nperm(L)
    for i in range(np):
        print '%2i %s' %(i,anagram(L,i))

if __name__=='__main__':
    test()





More information about the Python-list mailing list