Solution 1

``````class CountUpToIterator(object):
def __init__(self, up_to):
self.current = 0
self.up_to = up_to

def __iter__(self):
return self

def __next__(self):
if self.current > self.up_to:
raise StopIteration()

number = self.current
self.current += 1
return number

next = __next__
``````

# Count Up to X

Build a `CountUpToIterator` that receives a number and counts from 0 up to that number. Example:

``````iterator = iter(CountUpToIterator(up_to=10))
next(iterator) # 0
next(iterator) # 1
next(iterator) # 2
next(iterator) # 3
# ... Fast Forward ...
next(iterator) # 9
next(iterator) # 10
next(iterator) # StopIteration() Raised!
``````

### Test Cases

test count up to 1 -

``````import pytest

def test_count_up_to_1():
iterator = iter(CountUpToIterator(up_to=1))

assert next(iterator) == 0
assert next(iterator) == 1

with pytest.raises(StopIteration):
next(iterator)
``````

test count up to 3 -

``````import pytest

def test_count_up_to_3():
iterator = iter(CountUpToIterator(up_to=3))

assert next(iterator) == 0
assert next(iterator) == 1
assert next(iterator) == 2
assert next(iterator) == 3

with pytest.raises(StopIteration):
next(iterator)
``````
class CountUpToIterator(object): pass