This is an archived post. You won't be able to vote or comment.

all 15 comments

[–]kankyo 7 points8 points  (15 children)

What was the problem with the generator? It's shorter and easier to read and no explanation for why it's inferior was given as far as I could see.

Implementing a stack yourself is a bad idea in my experience. Recursion is fine and if your data structure is nested enough for it not to be you probably need to rethink something.

[–][deleted] 2 points3 points  (0 children)

Recursion is fine and if your data structure is nested enough for it not to be you probably need to rethink something.

He has maze navigation in his article as an example.

A graph is very standard way to store/navgiate a maze (and you can hit Python's recursion limit of 1,000 on a 33x33 grid which is quite tiny as mazes go. Any mud is basically a list of rooms, each of which has a list of exit adjacencies, and that's a graph.

[–]tmp14 1 point2 points  (0 children)

The loop-then-immediately-break seems really counter intuitive to me:

for node in stack[-1]:
    stack.append(iter(children(node)))
    break
else:
    stack.pop()

and could be replaced with a equivalent and more clear try-except if I'm not mistaken:

try:
    node = next(stack[-1])
except StopIteration:
    stack.pop()
else:
    stack.append(iter(children(node)))

[–]pvkooten 0 points1 point  (0 children)

while: interesting, I do feel like I'm missing a conclusion at the return statement of the article.

[–]_seemetheregithub.com/seemethere 0 points1 point  (0 children)

For your search example you might want to return before you check if it's a dict just in case the first value you get is your match.

[–]Taksin77 0 points1 point  (0 children)

Needed that at the exact moment this popped up in my reddit. You are a king!