algorithm for sorting functional expressions

Fredrik Lundh fredrik at pythonware.com
Tue Dec 5 02:14:08 EST 2006


MRAB wrote:

> It's left as an exercise for the reader as to how it works. :-)

*if* it works, you mean.  should the following script really print anything?

import random, sys

# input variables replaced with zeros
L = [
     ['b', ['w','z']],
     ['a', ['z','y']],
     ['w', ['z','0']],
     ['z', ['0','0']],
     ['y', ['0','0']]
     ]

def Compare(X, Y):
     if X[0] in Y[1]:
         return -1
     elif Y[0] in X[1]:
         return 1
     else:
         return 0

while 1:
     random.shuffle(L)
     L.sort(cmp=Compare)
     # check that all steps only use known values
     i = set("0")
     for d, s in L:
         if s[0] not in i or s[1] not in i:
             print L
             print d, s, i
         i.add(d)

</F>




More information about the Python-list mailing list