solutions

List Iterator

The objective of this assignment is to build a simple ListIterator that receives a list and its only purpose is to iterate it. It's silly and not really useful, but the idea is to reinforce on the iteration methods. Example of usage:

for elem in ListIterator([5, 4, 3, 2, 1]):
  print(elem)

>>> 5
    4
    3
    2
    1

The code in the editor has some sample code that you can uncomment and use the RUN CODE button to try out.

Test Cases

test with empty list - Run Test

import pytest

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

test with elements - Run Test

import pytest

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

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

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