All problems
Flatten Nested List Iterator
Problem
Simulate an iterator that flattens a nested list lazily.
nested is a list whose elements are integers or nested lists; operations contains "next" and "has_next". next returns the next integer in flattened order and has_next reports whether more remain. The iterator is consumed in order.
Return the list of recorded results.
Hint
You may flatten eagerly into a list and then serve it with a position pointer.
Test cases
case 1
in: [[1,[2,3],4],["has_next","next","next","has_next","next","next","has_next"]]
expected: [true,1,2,true,3,4,false]
case 2
in: [[],["has_next"]]
expected: [false]
case 3
in: [[[[1]]],["next","has_next"]]
expected: [1,false]
case 4
in: [[[1,2]],["next","next"]]
expected: [1,2]
Starter codePython
def nested_iterator_ops(nested, operations):
# Your code here
pass4 test cases · run your code against them in the editor