📘
Flatten 2D Vector
Implement an iterator to flatten a 2D vector. Support next() and has_next() operations.
Input: 2D vector as JSON, then operations separated by semicolons. Output results of next() calls.
Example:
Input:
[[1,2],[3],[4]] next;next;next;has_next;next;has_next
Output:
1 2 3 True 4 False
Reasoning:
- The input 2D vector
[[1,2],[3],[4]]is initialized for iteration. - The
next()operation is called three times, yielding the values1,2, and3, which are the first elements of each of the first three sub-vectors. - The
has_next()operation checks if there are more elements and returnsTruebecause there is still an element4left in the vector. - The
next()operation is called again, yielding the value4, which is the last element in the vector. - The final
has_next()operation returnsFalsebecause there are no more elements left to iterate over in the vector.
Constraints:
- 0 <= rows <= 100
- 0 <= cols <= 100
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.