Feb-23-2018, 12:44 AM
Iterating both up and down from the middle of a range, with only the range max value input:
import itertools def spiral(n): r1 = range(n // 2, n) r2 = range(n // 2 - 1, -1, -1) for a, b in itertools.zip_longest(r1, r2): yield a if b is not None: yield bAnd some application:
list(spiral(6)) [3, 2, 4, 1, 5, 0]