-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverse Integer.py
More file actions
42 lines (35 loc) · 1.08 KB
/
Reverse Integer.py
File metadata and controls
42 lines (35 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/python
'''
Given a signed 32-bit integer x, return x with its digits reversed.
If reversing x causes the value to go outside the signed 32-bit integer
range [-231, 231 - 1], then return 0.
'''
def main():
num = int(input("Enter a number : "))
rev = 0
neg = -0x80000000
pos = 0x7fffffff
def revFunc(num):
nonlocal rev # nonlocal works as global variable
if (num > 0):
remainder = num % 10
rev = (rev * 10) + remainder
num = num // 10 # // for integer devision
revFunc(num)
elif (num < 0):
num = abs(num)
remainder = num % 10
rev = (rev * 10) + remainder
num = num // 10
revFunc(num)
return rev
if (num >= neg) and (num < 0):
rev = revFunc(num) * -1
print(rev)
elif (num <= pos) and (num >= 0):
rev = revFunc(num)
print(rev)
else:
return 0
if __name__ == "__main__":
main()