From 6d32ad90879c2377f94a980e6b49a9dec5e6d048 Mon Sep 17 00:00:00 2001 From: Alexander Shvets Date: Tue, 24 Jun 2025 16:06:55 +0200 Subject: [PATCH] Updated examples. --- src/Iterator/Conceptual/Output.txt | 12 ++++++------ src/Iterator/Conceptual/main.py | 30 ++++++++++++++++++++---------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/Iterator/Conceptual/Output.txt b/src/Iterator/Conceptual/Output.txt index 48107cd..99ad164 100644 --- a/src/Iterator/Conceptual/Output.txt +++ b/src/Iterator/Conceptual/Output.txt @@ -1,9 +1,9 @@ Straight traversal: -First -Second -Third +A +B +C Reverse traversal: -Third -Second -First \ No newline at end of file +C +B +A diff --git a/src/Iterator/Conceptual/main.py b/src/Iterator/Conceptual/main.py index e2f60ce..7da6e7a 100644 --- a/src/Iterator/Conceptual/main.py +++ b/src/Iterator/Conceptual/main.py @@ -10,7 +10,6 @@ объектов, не раскрывая их внутреннего представления. """ - from __future__ import annotations from collections.abc import Iterable, Iterator from typing import Any @@ -58,9 +57,22 @@ class AlphabeticalOrderIterator(Iterator): def __init__(self, collection: WordsCollection, reverse: bool = False) -> None: self._collection = collection self._reverse = reverse - self._position = -1 if reverse else 0 + self._sorted_items = None # Will be set on first __next__ call + self._position = 0 def __next__(self) -> Any: + """ + EN: Optimization: sorting happens only when the first items is actually + requested. + + RU: Оптимизация: сортировка происходит только тогда, когда первый элемент + впервые запрашивается. + """ + if self._sorted_items is None: + self._sorted_items = sorted(self._collection._collection) + if self._reverse: + self._sorted_items = list(reversed(self._sorted_items)) + """ EN: The __next__() method must return the next item in the sequence. On reaching the end, and in subsequent calls, it must raise StopIteration. @@ -69,12 +81,10 @@ def __next__(self) -> Any: последовательности. При достижении конца коллекции и в последующих вызовах должно вызываться исключение StopIteration. """ - try: - value = self._collection[self._position] - self._position += -1 if self._reverse else 1 - except IndexError: + if self._position >= len(self._sorted_items): raise StopIteration() - + value = self._sorted_items[self._position] + self._position += 1 return value @@ -120,9 +130,9 @@ def add_item(self, item: Any) -> None: # классах Коллекций, в зависимости от уровня косвенности, который вы хотите # сохранить в своей программе. collection = WordsCollection() - collection.add_item("First") - collection.add_item("Second") - collection.add_item("Third") + collection.add_item("B") + collection.add_item("A") + collection.add_item("C") print("Straight traversal:") print("\n".join(collection))