-
Notifications
You must be signed in to change notification settings - Fork 0
/
166.py
69 lines (50 loc) · 1.87 KB
/
166.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
"""
Problem:
Implement a 2D iterator class. It will be initialized with an array of arrays, and
should implement the following methods:
next(): returns the next element in the array of arrays. If there are no more
elements, raise an exception.
has_next(): returns whether or not the iterator still has elements left.
For example, given the input [[1, 2], [3], [], [4, 5, 6]], calling next() repeatedly
should output 1, 2, 3, 4, 5, 6.
Do not use flatten or otherwise clone the arrays. Some of the arrays can be empty.
"""
from typing import Generator, List, Optional
class Iterator2D:
def __init__(self, iteratable2d: List[List[int]]) -> None:
self.iteratable2d = iteratable2d
self.generator = Iterator2D.generator_func(iteratable2d)
self.next_value = next(self.generator)
def __repr__(self) -> str:
return str(self.iteratable2d)
@staticmethod
def generator_func(iteratable2d: List[List[int]]) -> Generator[int, None, None]:
for iteratable in iteratable2d:
for element in iteratable:
yield element
def has_next(self) -> bool:
return self.next_value is not None
def next(self) -> Optional[int]:
curr_value = self.next_value
try:
self.next_value = next(self.generator)
except StopIteration:
self.next_value = None
return curr_value
if __name__ == "__main__":
iter_obj = Iterator2D([[1, 2], [3], [], [4, 5, 6]])
print(iter_obj)
print(iter_obj.has_next())
print(iter_obj.next())
print(iter_obj.has_next())
print(iter_obj.next())
print(iter_obj.has_next())
print(iter_obj.next())
print(iter_obj.has_next())
print(iter_obj.next())
print(iter_obj.has_next())
print(iter_obj.next())
print(iter_obj.has_next())
print(iter_obj.next())
print(iter_obj.has_next())
print(iter_obj.next())