Mar-26-2018, 04:19 PM
In python 3, range() has a lot of extra features that weren't available in 2.x, such as indexing and slicing. It's fast, even on large ranges, as it computes the values when indexed, instead of iterating over the range.
So another, probably worse, way of doing it, could be like so:
So another, probably worse, way of doing it, could be like so:
>>> def spiral(size): ... spinner = range(size) ... right = len(spinner) // 2 ... left = right - 1 ... exhaused = False ... while not exhaused: ... exhaused = True ... if right < len(spinner): ... yield spinner[right] ... right += 1 ... exhaused = False ... if left >= 0: ... yield spinner[left] ... left -= 1 ... exhaused = False ... >>> list(spiral(6)) [3, 2, 4, 1, 5, 0]