solutions

List Iterable

Following our previous assignment, the objetive of this one is to split the iterator from the iterable. You should be able to use your previous implementation for the iterable (you can actually copy and paste the code, removing the __iter__ part). Example:

iterator = ListIterator([3, 2, 1])

for elem in iterator:
    print(elem)
# Prints: 3, 2, 1

print("===============")

for elem in iterator:
    print(elem)
# Prints: 3, 2, 1 AGAIN

Test Cases

test with empty list - Run Test

import pytest

def test_with_empty_list():
    iterator = ListIterator([])
    it = iter(iterator)
    with pytest.raises(StopIteration):
        next(it)

    it = iter(iterator)
    with pytest.raises(StopIteration):
        next(it)

test with elements - Run Test

import pytest

def test_with_elements():
    iterator = ListIterator(['a', 'b', 'c'])
    it = iter(iterator)

    assert next(it) == 'a'
    assert next(it) == 'b'
    assert next(it) == 'c'

    with pytest.raises(StopIteration):
        next(it)

    it = iter(iterator)

    assert next(it) == 'a'
    assert next(it) == 'b'
    assert next(it) == 'c'

    with pytest.raises(StopIteration):
        next(it)