-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.py
214 lines (150 loc) · 5.63 KB
/
solution.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import types
from itertools import chain
# _________________________
# task 1
class FlatIteratorV1:
def __init__(self, list_of_list):
self.list_of_list = list_of_list
def __iter__(self):
self.cursor_inner = -1
self.cursor_outer = 0
return self
def __next__(self):
self.cursor_inner += 1
if self.cursor_inner >= len(self.list_of_list[self.cursor_outer]):
self.cursor_outer += 1
self.cursor_inner = 0
if self.cursor_outer >= len(self.list_of_list):
raise StopIteration
return self.list_of_list[self.cursor_outer][self.cursor_inner]
class FlatIteratorV2:
def __init__(self, list_of_list):
self.list_of_list = list_of_list
def __iter__(self):
self.iterators = iter(iter(l) for l in self.list_of_list)
self.current_iter = next(self.iterators)
return self
def __next__(self):
try:
next_item = next(self.current_iter)
except StopIteration:
self.current_iter = next(self.iterators)
next_item = next(self.current_iter)
return next_item
class FlatIteratorV3:
def __init__(self, list_of_list):
self.list_of_list = list_of_list
def __iter__(self):
self.flat_iter = chain.from_iterable(self.list_of_list)
return self
def __next__(self):
return next(self.flat_iter)
class FlatIteratorV4:
def __init__(self, list_of_list):
self.list_of_list = list_of_list
def __iter__(self):
return chain.from_iterable(self.list_of_list)
# _________________________
# task 2
def flat_generator_v1(list_of_lists):
for inner_list in list_of_lists:
for item in inner_list:
yield item
def flat_generator_v2(list_of_lists):
for inner_list in list_of_lists:
yield from inner_list
def flat_generator_v3(list_of_lists):
for item in chain.from_iterable(list_of_lists):
yield item
def flat_generator_v4(list_of_lists):
return (item for item in chain.from_iterable(list_of_lists))
# _________________________
# task 3
class FlatIteratorHard:
def __init__(self, list_of_list):
self.list_of_list = list_of_list
def __iter__(self):
self.iters_stack = [iter(self.list_of_list)]
return self
def __next__(self):
while self.iters_stack:
try:
next_item = next(self.iters_stack[-1])
# пытаемся получить следующий элемент
except StopIteration:
self.iters_stack.pop()
# если не получилось, значит итератор пустой
continue
if isinstance(next_item, list):
# если следующий элемент оказался списком, то
# добавляем его итератор в стек
self.iters_stack.append(iter(next_item))
else:
return next_item
raise StopIteration
# _________________________
# task 4
def flat_generator_v5(list_of_list):
for i in list_of_list:
if isinstance(i, list):
for j in flat_generator_v5(i):
yield j
else:
yield i
def test_task_1():
list_of_lists_1 = [
['a', 'b', 'c'],
['d', 'e', 'f', 'h', False],
[1, 2, None]
]
for IteratorClass in (FlatIteratorV1, FlatIteratorV2, FlatIteratorV3, FlatIteratorV4):
for flat_iterator_item, check_item in zip(
IteratorClass(list_of_lists_1),
['a', 'b', 'c', 'd', 'e', 'f', 'h', False, 1, 2, None]
):
assert flat_iterator_item == check_item
assert list(IteratorClass(list_of_lists_1)) == ['a', 'b', 'c', 'd', 'e', 'f', 'h', False, 1, 2, None]
def test_task_2():
list_of_lists_1 = [
['a', 'b', 'c'],
['d', 'e', 'f', 'h', False],
[1, 2, None]
]
for yield_function in (flat_generator_v1, flat_generator_v2, flat_generator_v3, flat_generator_v4):
for flat_iterator_item, check_item in zip(
yield_function(list_of_lists_1),
['a', 'b', 'c', 'd', 'e', 'f', 'h', False, 1, 2, None]
):
assert flat_iterator_item == check_item
assert list(yield_function(list_of_lists_1)) == ['a', 'b', 'c', 'd', 'e', 'f', 'h', False, 1, 2, None]
assert isinstance(yield_function(list_of_lists_1), types.GeneratorType)
def test_task_3():
list_of_lists_2 = [
[['a'], ['b', 'c']],
['d', 'e', [['f'], 'h'], False],
[1, 2, None, [[[[['!']]]]], []]
]
for flat_iterator_item, check_item in zip(
FlatIteratorHard(list_of_lists_2),
['a', 'b', 'c', 'd', 'e', 'f', 'h', False, 1, 2, None, '!']
):
assert flat_iterator_item == check_item
assert list(FlatIteratorHard(list_of_lists_2)) == ['a', 'b', 'c', 'd', 'e', 'f', 'h', False, 1, 2, None, '!']
def test_task_4():
list_of_lists_2 = [
[['a'], ['b', 'c']],
['d', 'e', [['f'], 'h'], False],
[1, 2, None, [[[[['!']]]]], []]
]
for flat_iterator_item, check_item in zip(
flat_generator_v5(list_of_lists_2),
['a', 'b', 'c', 'd', 'e', 'f', 'h', False, 1, 2, None, '!']
):
assert flat_iterator_item == check_item
assert list(flat_generator_v5(list_of_lists_2)) == ['a', 'b', 'c', 'd', 'e', 'f', 'h', False, 1, 2, None, '!']
assert isinstance(flat_generator_v5(list_of_lists_2), types.GeneratorType)
if __name__ == '__main__':
test_task_1()
test_task_2()
test_task_3()
test_task_4()