I have a positive integer n
. I want to lazily loop over a range of integers less than n
consisting of a) integers divisible by 3 (excluding zero) in reversed sorted order and b) zero and integers not divisible by 3 in any order. Is this a very stupid solution? I couldn't think of another way of doing it without having to make use two loops.
d = divisor = 3
list(itertools.chain(
itertools.chain(*zip(*(((i),(i-1),(i-2)) for i in range(d*((n-1)//d),0,-d)))),
(0,),range(n-1,d*((n-1)//d),-1)))
>>> n = 18
[15, 12, 9, 6, 3, 14, 11, 8, 5, 2, 13, 10, 7, 4, 1, 0, 17, 16]
>>> n = 17
[15, 12, 9, 6, 3, 14, 11, 8, 5, 2, 13, 10, 7, 4, 1, 0, 16]
>>> n = 16
[15, 12, 9, 6, 3, 14, 11, 8, 5, 2, 13, 10, 7, 4, 1, 0]
>>> n = 4
[3, 2, 1, 0]
>>> n = 3
[0, 2, 1]
>>> n = 2
[0, 1]
>>> n = 1
[0]
0 % 3 == 0
) \$\endgroup\$