# Python2.x get strange result of (6/-132 -> -1)

• ``````#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: kidjourney
# @Date:   2015-06-17 22:45:12
class Solution:
# @param {string[]} tokens
# @return {integer}
def evalRPN(self, tokens):
stack = []
symbol = ['+' , '-' , '*' , '/']
for i in tokens :
print(stack)
if i in symbol :
b = stack.pop()
a = stack.pop()
stack.append(int(eval("{0} {1} {2}".format(a,i,b))))
else :
stack.append(int(i))
return int(stack[0])

job = Solution()
print(job.evalRPN(["10","6","9","3","+","-11","*","/","*","17","+","5","+"]))
``````

This is my code .

It runs well in python3.x and get current answer , but it get 6/-132 -> -1 running in Python2.x that leads to wrong answer .

How can I solve it ?

• class Solution:
# @param {string[]} tokens
# @return {integer}
def evalRPN(self, tokens):
st=[]
for x in tokens:
if x in '+-/':
after=st.pop()
before=st.pop()
if x=='+':cur=before+after
elif x=='-':cur=before-after
elif x=='
':cur=beforeafter
else:cur=int(before
1.0/after)
st.append(cur)
else:
st.append(int(x))
return st.pop()

• class Solution:
# @param {string[]} tokens
# @return {integer}
def evalRPN(self, tokens):
st=[]
for x in tokens:
if x in '+-/':
after=st.pop()
before=st.pop()
if x=='+':cur=before+after
elif x=='-':cur=before-after
elif x=='
':cur=beforeafter
else:
if before
after<0:
cur=-((-before)/after)
else:
cur=before/after
st.append(cur)
else:
st.append(int(x))
return st.pop()

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