peeking_iterator

 1class Iterator:
 2    def __init__(self, nums):
 3        pass
 4
 5    def hasNext(self):
 6        pass
 7
 8    def next(self):
 9        pass
10
11
12# @leet start
13class PeekingIterator:
14    """
15    This question asks us to create a peekable iterator from an iterator that
16    has next and hasNext methods.
17
18    To do this most cleanly, we can keep a pointer to the next item in the iterator
19    that we've created, and then query if the iterator still has items to go.
20
21    When we peek or check for hasNext, we simply check the next value we've stored
22    is not None. For the next function, we raise StopIteration if it's None,
23    and otherwise, we try to poll the iterator for its next value if it has
24    a next value. If it doesn't, we set the next value to None, signalling to
25    peek and hasNext that we have no more items to go.
26    """
27
28    def __init__(self, iterator):
29        self._next = iterator.next()
30        self._iterator = iterator
31
32    def peek(self):
33        return self._next
34
35    def next(self):
36        if self._next is None:
37            raise StopIteration()
38        to_return = self._next
39        self._next = None
40        if self._iterator.hasNext():
41            self._next = self._iterator.next()
42        return to_return
43
44    def hasNext(self):
45        return self._next is not None
46
47
48# @leet end
49
50
51def test():
52    assert 2 + 2 == 4
class Iterator:
 2class Iterator:
 3    def __init__(self, nums):
 4        pass
 5
 6    def hasNext(self):
 7        pass
 8
 9    def next(self):
10        pass
Iterator(nums)
3    def __init__(self, nums):
4        pass
def hasNext(self):
6    def hasNext(self):
7        pass
def next(self):
 9    def next(self):
10        pass
class PeekingIterator:
14class PeekingIterator:
15    """
16    This question asks us to create a peekable iterator from an iterator that
17    has next and hasNext methods.
18
19    To do this most cleanly, we can keep a pointer to the next item in the iterator
20    that we've created, and then query if the iterator still has items to go.
21
22    When we peek or check for hasNext, we simply check the next value we've stored
23    is not None. For the next function, we raise StopIteration if it's None,
24    and otherwise, we try to poll the iterator for its next value if it has
25    a next value. If it doesn't, we set the next value to None, signalling to
26    peek and hasNext that we have no more items to go.
27    """
28
29    def __init__(self, iterator):
30        self._next = iterator.next()
31        self._iterator = iterator
32
33    def peek(self):
34        return self._next
35
36    def next(self):
37        if self._next is None:
38            raise StopIteration()
39        to_return = self._next
40        self._next = None
41        if self._iterator.hasNext():
42            self._next = self._iterator.next()
43        return to_return
44
45    def hasNext(self):
46        return self._next is not None

This question asks us to create a peekable iterator from an iterator that has next and hasNext methods.

To do this most cleanly, we can keep a pointer to the next item in the iterator that we've created, and then query if the iterator still has items to go.

When we peek or check for hasNext, we simply check the next value we've stored is not None. For the next function, we raise StopIteration if it's None, and otherwise, we try to poll the iterator for its next value if it has a next value. If it doesn't, we set the next value to None, signalling to peek and hasNext that we have no more items to go.

PeekingIterator(iterator)
29    def __init__(self, iterator):
30        self._next = iterator.next()
31        self._iterator = iterator
def peek(self):
33    def peek(self):
34        return self._next
def next(self):
36    def next(self):
37        if self._next is None:
38            raise StopIteration()
39        to_return = self._next
40        self._next = None
41        if self._iterator.hasNext():
42            self._next = self._iterator.next()
43        return to_return
def hasNext(self):
45    def hasNext(self):
46        return self._next is not None
def test():
52def test():
53    assert 2 + 2 == 4