-
Notifications
You must be signed in to change notification settings - Fork 0
/
task_03.py
49 lines (36 loc) · 1.15 KB
/
task_03.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def flatten_list(nested_list):
def flatten(lst):
for item in lst:
if isinstance(item, list):
flatten(item)
else:
flat_list.append(item)
flat_list = []
flatten(nested_list)
return flat_list
class FlatIterator:
def __init__(self, list_of_lists):
self.index = 0
self.flat_list = flatten_list(list_of_lists)[::-1]
self.total = len(self.flat_list)
def __iter__(self):
return self
def __next__(self):
if self.index >= self.total:
raise StopIteration
self.index += 1
return self.flat_list.pop()
def test_3():
list_of_lists_2 = [
[['a'], ['b', 'c']],
['d', 'e', [['f'], 'h'], False],
[1, 2, None, [[[[['!']]]]], []]
]
for flat_iterator_item, check_item in zip(
FlatIterator(list_of_lists_2),
['a', 'b', 'c', 'd', 'e', 'f', 'h', False, 1, 2, None, '!']
):
assert flat_iterator_item == check_item
assert list(FlatIterator(list_of_lists_2)) == ['a', 'b', 'c', 'd', 'e', 'f', 'h', False, 1, 2, None, '!']
if __name__ == '__main__':
test_3()