Flatten 2D Vector
Implement an iterator class Vector2D that flattens a 2D vector (a list of rows, each a list of integers; rows may be empty). Its constructor takes the 2D vector vec. Support two operations:
- next(): return the next value, reading rows in order and each row left to right.
- has_next(): return True if at least one value has not yet been returned by next(), else False.
Input: the 2D vector, then a sequence of operations separated by semicolons. Output: the result of every operation (next() values and has_next() booleans), one per line.
Example
[[1,2],[3],[4]] next;next;next;has_next;next;has_next
1 2 3 True 4 False
- The input
[[1,2],[3],[4]]has three rows of lengths , and , so it holds values. Read row by row, left to right, they form the single sequence ; the row boundaries never show up in the output. - The first three calls to
nextreturn the 1st, 2nd and 3rd values of that sequence:1and2from row 0, then3from row 1. After them, of the values have been consumed. has_nextasks whether any value is still unconsumed: , so it printsTrue. It does not consume anything, so the nextnextstill gets4.- Now of values are consumed, so the final
has_nextprintsFalse. - Notice that a row can be empty: if the first row is empty and the second holds just , the first
nextcall returns1(the empty row contributes nothing to the sequence) andhas_nextis thenFalse.
Constraints
- 0 <= rows <= 100
- 0 <= cols <= 100