-1

I am trying to get a round value after division between two values.For example, for inputs dividend=10 and divisor =3 , my expected output is 3 but it's saying 3.0 and for inputs p = 7 and t = -3 it's expected out -2 but it's showing -3.0. When I try to run this code in Pycharm I get intended results but when I run this in Leetcode compiler I get following error. Can I optimise this solution? Below is my code

import sys
class Solution(object):

def divide_integer(self,dividend,divisor):

    res = dividend/divisor
    return round(res)

if __name__ == "__main__":
p=7
t=-3
dividend =10
divisor =3
print(sys.version)
print(Solution().divide_integer(p,t))
print(Solution().divide_integer(dividend,divisor))

I have changed my leetcode Python version to Python 3 just like it is in my Pycharm and now these inputs get passed but it's failing For inputs dividend = -2147483648 and divisor = -1 in leetcode when I click on submit.I am adding screenshot belowenter image description here

  • 2
    Could you please confirm whether you're using python2 or python3? The behaviour of `/` is very different from one to the other. This would also explain the difference between your pycharm and your leetcode environments, if one of them is using python2 and the other is using python3 – Stef Jan 14 '23 at 11:18
  • 1
    Python 3.9 Interpreter in pycharm and Python 2 in leetcode –  Jan 14 '23 at 11:22
  • 1
    Is [this](https://leetcode.com/problems/divide-two-integers/) the relevant problem? – Mark Dickinson Jan 14 '23 at 12:18
  • @MarkDickinson this is my problem statement. Initially when I was coding in Python 2 in leetcode compiler I was getting wrong answer for the inputs mentioned above , later after being suggested to change the version to Python 3 in leetcode, those inputs gave correct answer but **when I click on Submit Button it shows the wrong answer as shown in the screenshot above** –  Jan 14 '23 at 12:18
  • @Aurora19: It seems there are parts of the original problem statement (like the limit on the integer values) that explain exactly why the expected output is `2147483647` for the case of interest. You need to edit your question to include _all_ the relevant information. – Mark Dickinson Jan 14 '23 at 17:47

1 Answers1

3

What you describe is a notable difference between python2 and python3, which can almost be considered to be different programming languages.

Python3

In python3, / always returns a float, and round always returns an int.

print(2 / 1)
# 2.0

print(round(3.5))
# 4

If you want to get integer (Euclidean) division in python3, then do not use / nor round, but use // instead:

print(9 / 2)
# 2.25

print(9 // 2)
# 2

Python2

In python2, / returns either an int or a float depending on whether you're dividing ints or floats; and round always returns a float.

print (8 / 4)
# 2
print (9 / 4)
# 2

print (8.0 / 4.0)
# 2.0
print (9.0 / 4.0)
# 2.25

print (round(3))
# 3.0

Identifying the python version

See this related question: Which version of python do I have installed?

In the console you can type: python -v or python --version.

Inside a python interpreter, you can type:

import sys

print(sys.version)

There is also this fun "hack" to exploit the difference in behaviour of / to print "2" in python2 and "3.0" in python3:

print( (3/2) * 2 )

#python3:  3.0
#python2:  2
Stef
  • 13,242
  • 2
  • 17
  • 28
  • Where should I modify the code? And the Python version that i am working in Pycharm is 3.9.6 –  Jan 14 '23 at 11:27
  • 2
    @Aurora19 Somewhere on leetcode there should be a menu to choose your favourite programming language. In that menu, make sure to choose "python3" and not "python2". – Stef Jan 14 '23 at 11:36
  • I was asking about changes in my code but yeah I changed it to Python 3 and these inputs started showing me desired output but they are failing For inputs dividend = -2147483648 and divisor = -1, expected output is 2147483647 but I get 2147483648 –  Jan 14 '23 at 11:40
  • Why did you expect output 2147483647? Obviously `(-2147483648) // (-1)` is `2147483648`. – Stef Jan 14 '23 at 11:41
  • @Stef No, I am telling that leetcode compiler is failing this test. –  Jan 14 '23 at 11:46
  • @JetLeg The question was why pycharm and leetcode were giving two different results for `/` and for `round`. My answer is that one is using python2, the other is using python3. If you have constructive criticism that you wish to express, be my guest, but "wtf? forreal?" is not helpful. Also, the conclusion is not "move to python3" but rather "one of the interpreters is not using the python version that you think it is using", so, yes, an obvious fix is to make sure you're using the version that you want, rather than another version without knowing which one it is. – Stef Jan 14 '23 at 11:53
  • @Aurora19 Well that's very odd. I don't know why they expect that. But I haven't actually seen the text of the problem. I've only seen your code but I don't know what task they asked you. – Stef Jan 14 '23 at 12:09
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/251138/discussion-between-aurora19-and-stef). –  Jan 14 '23 at 12:10