forked from subsr97/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwo-d-iterator.py
72 lines (52 loc) · 1.83 KB
/
two-d-iterator.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
"""
#166
Uber
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.
"""
class TwoDIterator:
def __init__(self, array):
self.array = array
def next(self):
while True:
try:
element = self.array[0][0]
if len(self.array[0]) == 1:
self.array = self.array[1:]
else:
self.array[0] = self.array[0][1:]
return element
except Exception as e:
if len(self.array) > 1:
self.array = self.array[1:]
else:
raise e
def has_next(self):
while True:
try:
self.array[0][0]
return True
except:
if len(self.array) > 1:
self.array = self.array[1:]
else:
return False
def main():
twoDIterator = TwoDIterator([[1, 2], [3], [], [4, 5, 6]])
while twoDIterator.has_next():
print(twoDIterator.next(), end=" ")
# 1 2 3 4 5 6
print()
twoDIterator = TwoDIterator([[1, 2], [3], [], [4, 5, 6]])
while True:
try:
print(twoDIterator.next(), end=" ")
except Exception as e:
print(e)
break
# 1 2 3 4 5 6 list index out of range
if __name__ == "__main__":
main()