Why is my Python solution behaving differently on my local machine?

  • 0

    I know this implementation isn't super-efficient, so with that aside: locally, the results of hasNext are different from the judge's results and I have no idea why. Can anyone shed some light on it?

    import copy
    class PeekingIterator(object):
        def __init__(self, iterator):
            self.iter = iterator
        def peek(self):
            peekIter = copy.copy(self.iter)
            return next(peekIter)
        def next(self):
            return next(self.iter)
        def hasNext(self):
            peekIter = copy.copy(self.iter)
            return next(peekIter, None) is not None

    On input [1,2,3,4] with operations [0,1,1,2,2,1,1,2,0,1,0,2,0] (0: hasNext, 1: peek, 2: next), my local results match the expected results:

    But the judge's output for my code is:

    There's no static class state. It fails in exactly the same way when I perform a deep copy instead of a copy. I'm stumped.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.