I want to write an infinte generator using itertools that combines a the count
and repeat
iterators, by repeating each number n
times before incrementing. Is there a more concise way to do it than what I came up with?
JavaScript
x
17
17
1
from itertools import *
2
def repeating_counter(times):
3
c = count()
4
while True:
5
for x in repeat(next(c), times):
6
yield x
7
8
>>> rc = repeating_counter(2)
9
>>> next(rc)
10
0
11
>>> next(rc)
12
0
13
>>> next(rc)
14
1
15
>>> next(rc)
16
1
17
Advertisement
Answer
Use integer division!
JavaScript
1
3
1
def repeating_counter(times):
2
return (x // times for x in count())
3