-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7_ReverseInteger.py
109 lines (82 loc) · 2.96 KB
/
7_ReverseInteger.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#-------------------------------------------------------------------------------
#
#-------------------------------------------------------------------------------
# By Will Shin
#
#-------------------------------------------------------------------------------
# LeetCode prompt
#-------------------------------------------------------------------------------
"""
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123
Output: 321
Example 2:
Input: -123
Output: -321
Example 3:
Input: 120
Output: 21
Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
"""
# #
#-------------------------------------------------------------------------------
# Approach
#-------------------------------------------------------------------------------
"""
* The easiest way to do this would be convert to a string, and then reverse and make sure it has the correct sign.
"""
#-------------------------------------------------------------------------------
# Solution
#-------------------------------------------------------------------------------
def checkoverflow(num_to_check, is_neg):
mymax = 2**31 - 1
mymin = -2**31
if is_neg:
num_to_check *= -1
if num_to_check < mymin:
print(num_to_check)
num_to_check = 0
else:
if num_to_check > mymax:
num_to_check = 0
return(num_to_check)
def myreverse(num_to_reverse):
str_to_return = ""
is_neg = False
split_as_string = list(str(num_to_reverse))
end_ind = 0
if split_as_string[0] is '-':
is_neg = True
end_ind = 1 # end one before the beginning
for ind in reversed(range(end_ind, len(split_as_string))):
str_to_return += split_as_string[ind]
num_to_return = int(str_to_return)
# check overflow
num_to_return = checkoverflow(num_to_return, is_neg)
return(num_to_return)
#-------------------------------------------------------------------------------
# Main Leetcode Input Driver
#-------------------------------------------------------------------------------
class Solution(object):
def reverse(self, x):
"""
:type x: int
"""
return myreverse(x)
#-------------------------------------------------------------------------------
# Unit Test
#-------------------------------------------------------------------------------
import unittest
import sys
class TestSolution(unittest.TestCase):
def test_123(self):
self.assertEqual(Solution().reverse(123), 321)
def test_neg123(self):
self.assertEqual(Solution().reverse(-123), -321)
def test_120(self):
self.assertEqual(Solution().reverse(120), 21)
def test_overflow(self):
self.assertEqual(Solution().reverse(1534236469), 0)
unittest.main()