Skip to content
Advertisement

Tree levels in lazy Python using “Long zip with”

In this blog post about breadth-first traversal, Gibbons talks about implementing BFT with “long zip with” function

JavaScript

Then for instance

JavaScript

My question: My version of levels in Python is working, but my lazy version using generators is not. It’s adding an extra [] level at the end. Why?

I consider this practice for using generators and laziness in Python, and I think the problem is some imprecision in how I’m using yield, return, itertools.zip_longest, etc.

An example of what’s going wrong:

JavaScript

Here’s my code. Thanks in advance!

JavaScript

Advertisement

Answer

Since the generator version is not concatenating sublists, it should not yield an empty list:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement