sign, get the
reversed absolute integer, and return their product if
r didn't "overflow".
def reverse(self, x): s = cmp(x, 0) r = int(`s*x`[::-1]) return s*r * (r < 2**31)
As compressed one-liner, for potential comparison:
def reverse(self, x): s=cmp(x,0);r=int(`s*x`[::-1]);return(r<2**31)*s*r
Anybody got something shorter?
@totalawesomeness I made it a string. Don't you see the backticks? https://docs.python.org/2.7/reference/expressions.html#string-conversions
I get the first two lines but I don't understand what is the parenthesis here doing?
---> return sr * (r < 2*31)
The backtick could make a string like '-123' to '321-', I guess I'm confused with where exactly the last '-' is being taken off in the return line.
(r < 2*31) is either True or False, which the multiplication then treats as 1 or 0.
The backticks get
s*x, which is non-negative.
@totalawesomeness he has include the s*x with ' '
def reverse(self, x): n = cmp(x, 0) * int(str(abs(x))[::-1]) return n if n.bit_length() < 32 else 0
@StefanPochmann Backticks are deprecated in 2.6 and removed in Python 3.x
@StefanPochmann Python 3 removed: https://docs.python.org/3.1/whatsnew/3.0.html#removed-syntax
They were deprecated quite a while ago in Python 2. https://trac.sagemath.org/ticket/15992 and here is explanation why: http://www.diveintopython3.net/porting-code-to-python-3-with-2to3.html#repr
They were deprecated quite a while ago in Python 2. https://trac.sagemath.org/ticket/15992
That's a claim, without reference, in a Trac ticket, not for Python but for SageMath, by someone I never heard of. And it doesn't even mention Python 2. Can you show something like official Python documentation saying that it got deprecated in Python 2? The current reference documentation about them doesn't say anything about them being deprecated, and that page does talk about two other things being deprecated.
and here is explanation why: http://www.diveintopython3.net/porting-code-to-python-3-with-2to3.html#repr
That page talks about three things being deprecated. But not backticks. To me that's further evidence that they aren't.
Nice example there, though. I didn't know they can be nested and I don't see how that works. And now I'll of course have to keep an eye out for opportunities to do that myself :-)
@StefanPochmann There are not many references since it was deprecated since like 7 years ago, here's PEP8 in 2010 added Warning against usage of the backtick https://pypi.python.org/pypi/pep8 . It's good enough for me :)
I referenced http://www.diveintopython3.net/porting-code-to-python-3-with-2to3.html#repr as they give a short explanation why using backticks is confusing.
@valeriy2 That's not PEP8 (or PEP 8). It's pep8. Which is independent from PEP 8 and Python. Guido even requested that it's renamed, and it did get renamed (read the note at the top). The actual PEP 8 as far as I can see doesn't even mention backticks at all. Neither "backtick", nor "back tick", nor "reverse quote", nor the backtick symbol, nor
repr appear anywhere in it. Also, pep8 doesn't say that they're deprecated in Python 2. As far as I can tell, they got somewhat unofficially deprecated between Python 2 and Python 3, the deprecation then being replaced by the actual removal (in Python 3).
@jiang42152 cpm didn't work for me either, I used plain if/else conditions :)
I am thinking about the overflow case. The range of a 32-bit signed integer would be
-2**31 ~ 2**31-1, which is
-2147483648 ~ 2147483647. So if I input
-8463847412 (the reverse integer is exactly
-2**31, inside the range), the output should be
-2147483648. But when I put it into the custom testcase, the expected answer is
0, meaning it overflows. Could anyone explain this? Is it an error?
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.