I am working through a coding challenge where I have to reverse an integer. If the integer has only 1 digit, I return the integer as it is. If its less than zero, I reverse it but keep the sign intact. I have to check for integer overflow to make sure that it is in the range [−231, 231 − 1]. The following is my code:
def reverse(self, x: int) -> int:
if x >=0 and x <= 9:
return x
elif x > 9 and x < (2**31):
rever1 = str(x)[::-1]
return int(rever1)
elif x < 0 and x > (-2**31):
rever2 = str(x)[::-1]
rever2 = rever2[:-1]
rever2 = -1 * int(rever2)
return rever2
elif x <= (-2**31) or x >= (2**31):
return 0
I know how to make this code shorter so the inefficiency is fine for now. I just want to know why my code fails for 1534236469.
As much as I appreciate all help, it helps me more if you just tell me why the code fails instead of giving me a quick solve. The whole point behind these challenges is to learn so it defeats the purpose if someone just puts the answer.
self?2**31-1?Solution.