Peeking Iterator
To solve this coding challenge, we need to implement a custom iterator class called
that wraps an existing iterator and adds a
method. This method allows us to view the next element without consuming it. We'll also need to implement the existing methods
and
such that they work seamlessly with our new
functionality.
class that satisfies the requirements laid out in the problem description. This class will be capable of peeking at the next element without consuming it, moving to the next element, and checking if more elements are available in the underlying iterator.
PeekingIterator
peek()
next()
hasNext()
peek()
Explanation
Let's break down the problem and the functionalities we need to implement:-
Initialization
: We need to initialize the
with an existing iterator. We'll also need a variable to store the peeked value.
PeekingIterator -
Peek Operation
: The
method will return the next element without advancing the iterator. If no element has been peeked yet, it will retrieve it from the wrapped iterator and store it for future use.
peek() -
Next Operation
: The
method will return the next element from the iterator. If an element has been peeked (using the
next()method), it should return that element and reset the peeked value.peek() -
HasNext Operation
: The
method will check if there are any elements left in the iterator. It should account for both the peeked value and the state of the wrapped iterator.
hasNext()
Step-by-Step Explanation/Detailed Steps in Pseudocode
Step 1: Initialization
-
Initialize the
with the given iterator.
PeekingIterator -
Set the
to
peeked_valueindicating that no value has been peeked yet.None
Step 2: Peek Method
-
Check if there is a
already stored. If yes, return it.
peeked_value -
If no
is stored, get the next value from the iterator, store it in
peeked_value, and then return it.peeked_value
Step 3: Next Method
-
Check if there is a
. If yes, return it and reset
peeked_valuetopeeked_value.None -
If no
is stored, directly return the next value from the iterator.
peeked_value
Step 4: HasNext Method
-
Check if there is a
which would mean there is at least one element left. If yes, return
peeked_value.True -
If
is
peeked_value, return the result ofNoneon the wrapped iterator.hasNext
Pseudocode with Comments
# PeekingIterator class to extend standard iterator functionalities
class PeekingIterator:
# Constructor method to initialize the class with an existing iterator
method __init__(iterator):
# Store the provided iterator
this.iterator = iterator
# Initialize the peeked_value to None
this.peeked_value = None
# Method to peek the next element without moving the iterator
method peek() -> int:
# If there is no peeked value yet
if this.peeked_value is None:
# Retrieve the next value from the iterator and store in peeked_value
this.peeked_value = this.iterator.next()
# Return the peeked value
return this.peeked_value
# Method to get the next element and move the iterator
method next() -> int:
# If there is a peeked value
if this.peeked_value is not None:
# Store the peeked value in a variable to return it
result = this.peeked_value
# Reset peeked_value to None
this.peeked_value = None
# Return the previous peeked value
return result
# If there is no peeked value, return the next value from the iterator
return this.iterator.next()
# Method to check if there are more elements in the iterator
method hasNext() -> bool:
# Return True if there is a peeked value or if the iterator has more elements
return this.peeked_value is not None or this.iterator.hasNext()
By following this pseudocode, we can implement the
PeekingIterator