|
@@ -0,0 +1,74 @@
|
|
|
+
|
|
|
+class Iterator:
|
|
|
+
|
|
|
+ def __init__(self, text: str):
|
|
|
+ self.text = text.upper()
|
|
|
+ self.index = 0
|
|
|
+
|
|
|
+ def __iter__(self):
|
|
|
+ return self
|
|
|
+
|
|
|
+ def __next__(self):
|
|
|
+ try:
|
|
|
+ result = self.text[self.index]
|
|
|
+ except IndexError:
|
|
|
+ raise StopIteration
|
|
|
+ self.index += 1
|
|
|
+ return result
|
|
|
+
|
|
|
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
+
|
|
|
+class SequenceIterator:
|
|
|
+
|
|
|
+ def __init__(self, data):
|
|
|
+ self.data = data
|
|
|
+ self.start_index = 0
|
|
|
+
|
|
|
+ def is_even(self, value):
|
|
|
+ return value%2 == 0
|
|
|
+
|
|
|
+ def __iter__(self):
|
|
|
+ return self
|
|
|
+
|
|
|
+ def __next__(self):
|
|
|
+
|
|
|
+ if self.start_index >= len(self.data):
|
|
|
+ raise StopIteration
|
|
|
+
|
|
|
+ value = self.data[self.start_index]
|
|
|
+
|
|
|
+ if self.is_even(len(self.data)) and self.start_index == len(self.data) - 2:
|
|
|
+ self.start_index = 1
|
|
|
+ elif self.is_even(len(self.data)) == False and self.start_index == len(self.data) - 1:
|
|
|
+ self.start_index = 1
|
|
|
+ else:
|
|
|
+ self.start_index += 2
|
|
|
+
|
|
|
+ return value
|
|
|
+
|
|
|
+
|
|
|
+def test_2():
|
|
|
+ container = SequenceIterator([1, 5, 4, 6, 43, True, 'hello'])
|
|
|
+ # container = SequenceIterator([1, 5, 4, 6, 43, True])
|
|
|
+ for i in container:
|
|
|
+ print(i)
|
|
|
+
|
|
|
+def test_1():
|
|
|
+ phrase = Iterator('Qwerty')
|
|
|
+ it_1 = iter(phrase)
|
|
|
+ it_2 = iter(phrase)
|
|
|
+ for i in it_1:
|
|
|
+ print(i)
|
|
|
+
|
|
|
+ for i in it_1:
|
|
|
+ print(i)
|
|
|
+
|
|
|
+ for i in it_2:
|
|
|
+ print(i)
|
|
|
+
|
|
|
+def main():
|
|
|
+ # test_1()
|
|
|
+ test_2()
|
|
|
+
|
|
|
+if __name__ == '__main__':
|
|
|
+ main()
|