Task: Iterators


C++: #include <iostream>
#include <list>
#include <string>
#include <vector>

using namespace std;




void PrintContainer(forward_iterator auto start, forward_iterator auto sentinel)
{
  for(auto it = start; it != sentinel; ++it)
  {
    cout << *it << " "; 
  }
  cout << "\n";
}


void FirstFourthFifth(input_iterator auto it)
{
  cout << *it;
  advance(it, 3);
  cout << ", " << *it;
  advance(it, 1);
  cout << ", " << *it;
  cout << "\n";
}

int main()
{
  
  vector<string> days{"Sunday", "Monday", "Tuesday", "Wednesday",
   "Thursday", "Friday", "Saturday"};
  list<string> colors{"Red", "Orange", "Yellow", "Green", "Blue", "Purple"};

  cout << "All elements:\n";
  PrintContainer(days.begin(), days.end());
  PrintContainer(colors.begin(), colors.end());
  
  cout << "\nFirst, fourth, and fifth elements:\n";
  FirstFourthFifth(days.begin());
  FirstFourthFifth(colors.begin());

  cout << "\nReverse first, fourth, and fifth elements:\n";
  FirstFourthFifth(days.rbegin());
  FirstFourthFifth(colors.rbegin());
}






Python: 
from collections import deque
from typing import Iterable
from typing import Iterator
from typing import Reversible


days = [
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
    "Sunday",
]


colors = deque(
    [
        "red",
        "yellow",
        "pink",
        "green",
        "purple",
        "orange",
        "blue",
    ]
)


class MyIterable:
    class MyIterator:
        def __init__(self) -> None:
            self._day = -1

        def __iter__(self):
            return self

        def __next__(self):
            if self._day >= 6:
                raise StopIteration

            self._day += 1
            return days[self._day]

    class MyReversedIterator:
        def __init__(self) -> None:
            self._day = 7

        def __iter__(self):
            return self

        def __next__(self):
            if self._day <= 0:
                raise StopIteration

            self._day -= 1
            return days[self._day]

    def __iter__(self):
        return self.MyIterator()

    def __reversed__(self):
        return self.MyReversedIterator()


def print_elements(container: Iterable[object]) -> None:
    for element in container:
        print(element, end=" ")
    print("")  


def _drop(it: Iterator[object], n: int) -> None:
    
    try:
        for _ in range(n):
            next(it)
    except StopIteration:
        pass


def print_first_fourth_fifth(container: Iterable[object]) -> None:
    
    it = iter(container)
    print(next(it), end=" ")
    _drop(it, 2)
    print(next(it), end=" ")
    print(next(it))


def print_reversed_first_fourth_fifth(container: Reversible[object]) -> None:
    
    it = reversed(container)
    print(next(it), end=" ")
    _drop(it, 2)
    print(next(it), end=" ")
    print(next(it))


def main() -> None:
    my_iterable = MyIterable()

    print("All elements:")
    print_elements(days)
    print_elements(colors)
    print_elements(my_iterable)

    print("\nFirst, fourth, fifth:")
    print_first_fourth_fifth(days)
    print_first_fourth_fifth(colors)
    print_first_fourth_fifth(my_iterable)

    print("\nLast, fourth to last, fifth to last:")
    print_reversed_first_fourth_fifth(days)
    print_reversed_first_fourth_fifth(colors)
    print_reversed_first_fourth_fifth(my_iterable)


if __name__ == "__main__":
    main()