[Tutor] depth first with a twist

gp@pooryorick.com gp@pooryorick.com
Sun Mar 2 11:43:02 2003


Hi, I majored in a social science, so I'm hoping this group can help me
fill in the gaps;)  I have a dictionary where each key contains a list of
values=2E  Values 1 and 2 are parent, and older_sibling, respectively=2E  =
I
would like to append an order number onto each list, using parent and
older_sibling to determine the order, and am looking for an iterative
solution=2E  I have written a function which works, but boy, is it ugly!=20=

Anyone care to comment?

---begin code---

d =3D {'monty': [None, None, "Monty Python's Flying Circus"],
     'actors': ['monty', None, 'Actor Data'],
     'sketches': ['monty', 'actors', 'Sketch Data'],
     'basic': ['actors', None, 'Basic Bio'],
     'filmography': ['actors', 'basic', 'Filmography']}

PARENT =3D 0
OLDER_SIBLING =3D 1
NAME =3D 2
ORDER =3D 3


    def OrderDepth(self):
        keys =3D d=2Ekeys()
        order =3D 1
        parent =3D None
        sibling =3D None
        search =3D []
        for i in keys:
            if d[i][PARENT] =3D=3D parent:
                if d[i][OLDER_SIBLING] =3D=3D sibling:
                    search=2Eappend(keys=2Epop(keys=2Eindex(i)))
                    d[i]=2Eappend(order)
                    order +=3D 1
                    parent =3D i
                    sibling =3D d[i][PARENT]
                    break
        foundChild =3D 0
        foundSibling =3D 0
        while len(keys) > 0:
            print 'searching for parent %s and sibling %s', (parent,
sibling)
            for i in keys:
                if d[i][PARENT] =3D=3D parent:
                    if d[i][OLDER_SIBLING] =3D=3D sibling:
                        print 'found a match:  ', i
                        search=2Eappend(keys=2Epop(keys=2Eindex(i)))
                        d[i]=2Eappend(order)
                        order +=3D1
                        if sibling =3D=3D None:
                            foundChild =3D 1
                        else:
                            foundSibling =3D 1
                        break
            if foundChild =3D=3D 1:
                print 'option 1'
                foundChild =3D 0
                parent =3D i
                sibling =3D None
            elif foundSibling =3D=3D 1:
                print 'option 2'
                foundSibling =3D 0
                parent =3D i
                sibling =3D None
            else:
                print 'option 3'
                sibling =3D parent
                parent =3D d[search[-1]][PARENT]
                if parent =3D=3D sibling:
                    search=2Epop()
        result =3D {}
        for i in d:
            result[d[i][ORDER]] =3D i
        i =3D result=2Ekeys()
        i=2Esort()
        for j in i:
            print j, '\t\t', result[j]

-- End Code --

Thanks,

Poor Yorick

--------------------------------------------------------------------
mail2web - Check your email from the web at
http://mail2web=2Ecom/ =2E