0

when I do Leetcode questions I write the code in PyCharm on my PC and when it's working I copy paste it into Leetcode.

This is the code I have written.

def main():
    # tokens = ["2", "1", "+", "3", "*"]
    # tokens = ["4", "13", "5", "/", "+"]
    tokens = ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]
    print(eval_rpn(tokens))


def eval_rpn(tokens):
    stack = []
    ops = ["+", "-", "*", "/"]

    for c in tokens:
        if c not in ops:
            stack.append(c)
        else:
            num2 = int(stack.pop())
            num1 = int(stack.pop())

            if c == "+":
                total = num1 + num2
            elif c == "-":
                total = num1 - num2
            elif c == "*":
                total = num1 * num2
            else:  # The only operator left is division
                total = num1 / num2

            stack.append(total)

    return stack[-1]


if __name__ == "__main__":
    main()

This is the code I have pasted and submitted in Leetcode.

class Solution(object):
    def evalRPN(self, tokens):
        stack = []
        ops = ["+", "-", "*", "/"]

        for c in tokens:
            if c not in ops:
                stack.append(c)
            else:
                num2 = int(stack.pop())
                num1 = int(stack.pop())

                if c == "+":
                    total = num1 + num2
                elif c == "-":
                    total = num1 - num2
                elif c == "*":
                    total = num1 * num2
                else:
                    total = num1 / num2

                stack.append(total)

        return stack[-1]

But for some reason for the input tokens = ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"] Leetcode returns 12. When I run the same code on my PC it returns 22. 22 is the expected output. Not 12.

Can anyone point out what I am doing wrong?

The Leetcode question

  • 3
    Different python version? E.g. in python2 `/` is integer division when supplied with ints, but in python3 it will return float – Julien Jul 25 '23 at 06:19
  • 1
    Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take the SO [tour], and read [ask]. Also please read [how to write the "perfect" question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/), especially its [checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Then please copy-paste the assignment/exercise into your question, to make it self-contained. Linked pages can change, or disappear, making your question close to worthless. – Some programmer dude Jul 25 '23 at 06:21
  • One possible problem comes with `6 / -132` which is `−0,045454545`. If you're supposed to only deal with integers then that will not work. – Some programmer dude Jul 25 '23 at 06:39
  • 1
    Leetcode has never bothered to update to Python3. They are teaching you a dead language. – Frank Yellin Jul 25 '23 at 06:40
  • @FrankYellin, that is not true. LeetCode has many language options, including Python 3. – trincot Jul 25 '23 at 06:42
  • @trincot. My apologies then. This is recent. When a played around with it a couple of years ago it didn't. And that was long after Python2's death. – Frank Yellin Jul 25 '23 at 06:45

1 Answers1

1

This is happening because of different version(Python 2.7) you might have selected in leetcode editor

enter image description here

And this is happening because of the way how python 2.7 handles negative number division , if you follow your code you will see at one point the value in stack would be -132 and then the subsequent division with 6 is making the result as -1

print 6/-132 # results in -1

For detailed explanation see this Negative integer division surprising result

This behaviour is handled in python3 and if you change your editor version to python3 it will work as expected.

abhinav kumar
  • 222
  • 2
  • 10
  • Idk why your comment is downvoted. You explained it well and now i was able to make it work. All this time I have been using python 2 in the Leetcode editor it seems. I always selected python and never saw there was a python3 option right below it. Anyways now I switched to python3 and it works. – usefulmellow Jul 25 '23 at 06:58