[Python-de] Re: [Python-de] Re: [Python-de] Kleine Aufgabe, große Wirkung
Christian Tismer
tismer at tismer.com
Sun Apr 6 22:59:53 EDT 2003
Tomek Meka wrote:
> Nicht übel. Jetzt muß man nur noch erkennen, daß es
> sich um eine Tail-Rekursion handelt und man es mit
> einer einfachen Schleife kürzer und nicht-rekursiv
> schafft.
>
>
> Die Aufgabe war wirklich nicht schwierig, eine nichtrekursive Loesung:
>
>>>>def traverse(root, count):
>
> list = [ root ]
> while list and count:
> val, child1, child2 = list[0]
> del list[0]
> print val
> count = count - 1
> if child1 is not None:
> list.append(child1)
> if child2 is not None:
> list.append(child2)
Nicht übel. Aber wozu brauchst Du den count?
Hier eine weitere Lösung:
def walk2(t, search=None):
work = [t]
while work:
node = value, child1, child2 = work.pop(0)
print value
if value == search:
return node
work.extend(filter(None, node[1:]))
--
Christian Tismer :^) <mailto:tismer at tismer.com>
Mission Impossible 5oftware : Have a break! Take a ride on Python's
Johannes-Niemeyer-Weg 9a : *Starship* http://starship.python.net/
14109 Berlin : PGP key -> http://wwwkeys.pgp.net/
work +49 30 89 09 53 34 home +49 30 802 86 56 pager +49 173 24 18 776
PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04
whom do you want to sponsor today? http://www.stackless.com/
More information about the Python-de
mailing list