Gabriel Genellina wrote: > En Wed, 16 Apr 2008 17:53:16 -0300, <[EMAIL PROTECTED]> escribió: > > >> On Apr 16, 3:27 pm, Jean-Paul Calderone <[EMAIL PROTECTED]> wrote: >> >> >>> Any function can be implemented without recursion, although it isn't >>> always easy or fun. >>> >>> >> Really? I'm curious about that, I can't figure out how that would >> work. Could give an example? Say, for example, the typical: walking >> through the file system hierarchy (without using os.walk(), which uses >> recursion anyway!). >> > > Use a queue of pending directories to visit: > > start with empty queue > queue.put(starting dir) > while queue is not empty: > dir = queue.get() > list names in dir > for each name: > if is subdirectory: queue.put(name) > else: process file > Hi,
In that case, I'm not sure you get any performance gain since the queue has basically the same role as the stack in the recursive version. A definitive answer calls for an actual test, though. Anyway if you want to process the tree depth-first, the queue version falls in the "not fun" category. Cheers, RB -- http://mail.python.org/mailman/listinfo/python-list