Solution 1

class NegativeNumbersIterator(object):
    def __init__(self):
        self.next_number = 0

    def __iter__(self):
        return self

    def __next__(self):
        number = self.next_number
        self.next_number -= 1
        return number

    next = __next__

Negative Numbers Iterators

The NegativeNumbersIterator is really similar to NumbersIterator, with the difference that it should count backwards starting from 0.

iterator = iter(NegativeNumbersIterator())

print(next(iterator))  # 0
print(next(iterator))  # -1
print(next(iterator))  # -2
print(next(iterator))  # -3

Warning! This is also an infinite iterator.

Test Cases

test negative numbers - Run Test

def test_negative_numbers():
    iterator = iter(NegativeNumbersIterator())

    assert next(iterator) == 0
    assert next(iterator) == -1
    assert next(iterator) == -2
    assert next(iterator) == -3
class NegativeNumbersIterator(object): pass