From d427b75cd83d1817ae9b036d62f9cea2a91527ab Mon Sep 17 00:00:00 2001 From: jxie0755 <30805062+jxie0755@users.noreply.github.com> Date: Wed, 24 Jan 2018 11:46:01 -0500 Subject: [PATCH 1/2] Python 3 compatibility Hello, I found that the first method `reverse` only runs in python 2. I've made a python 3 compatible version under it, called reverse_py3. Thanks, --- Python/reverse-integer.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Python/reverse-integer.py b/Python/reverse-integer.py index 014a66fee..8382ba25b 100644 --- a/Python/reverse-integer.py +++ b/Python/reverse-integer.py @@ -34,7 +34,21 @@ def reverse(self, x): result = result * 10 + x % 10 x /= 10 return result if result <= 0x7fffffff else 0 # Handle overflow. - + + def reverse_py3(self, x): # Same principle as above, but python3 compatible + """ + :type x: int + :rtype: int + """ + absX = abs(x) + result = 0 + while absX > 0: + result = result * 10 + absX % 10 + absX //= 10 + if x < 0: + result = -result + return result if 2147483647 > result > -2147483648 else 0 + def reverse2(self, x): """ :type x: int From 424ca6f15e5959e83a04f56678e35fa6717f8b6d Mon Sep 17 00:00:00 2001 From: kamyu Date: Thu, 25 Jan 2018 01:02:50 +0800 Subject: [PATCH 2/2] Update reverse-integer.py --- Python/reverse-integer.py | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/Python/reverse-integer.py b/Python/reverse-integer.py index 8382ba25b..6b213a576 100644 --- a/Python/reverse-integer.py +++ b/Python/reverse-integer.py @@ -32,22 +32,8 @@ def reverse(self, x): result = 0 while x: result = result * 10 + x % 10 - x /= 10 + x //= 10 return result if result <= 0x7fffffff else 0 # Handle overflow. - - def reverse_py3(self, x): # Same principle as above, but python3 compatible - """ - :type x: int - :rtype: int - """ - absX = abs(x) - result = 0 - while absX > 0: - result = result * 10 + absX % 10 - absX //= 10 - if x < 0: - result = -result - return result if 2147483647 > result > -2147483648 else 0 def reverse2(self, x): """