On 11/20/2017 4:57 PM, Steven D'Aprano wrote:
On Mon, Nov 20, 2017 at 12:15:50PM -0500, Terry Reedy wrote:

Your version truncates the results to A B C instead of A B C D E as the
itertools recipe gives.

This is due to an off-by-1 error which I corrected 3 hours later in a
follow-up post, repeated below.

Ah, I missed that, thanks.

def roundrobin(*iterables):
      "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
      nexts = cycle(iter(it).__next__ for it in iterables)
      for reduced_len in reversed(range(1, len(iterables))):

Make that 0 rather than 1 for start value.

Is there a reason for calling reversed() instead of reversing the order
of range in the first place?

range(len(iterables)-1, -1, -1)

Readability. Accurately and automatically reversing ranges was one of the use cases motivating the addition of 'reversed'. Ranges have a __reversed__ method which returns a iterator for a reversed range.

>>> reversed(range(0, n))
<range_iterator object at 0x000001FC6BBD5030>
>>> list(reversed(range(0, n))) == list(iter(range(n-1, -1, -1)))
True

--
Terry Jan Reedy

_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to