-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathis_numeric.py
121 lines (95 loc) · 2.86 KB
/
is_numeric.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
110
111
112
113
114
115
116
117
118
119
120
121
# -*- coding: utf-8 -*-
# author: Xiguang Liu<[email protected]>
# 2018-05-04 15:40
# 题目描述:https://www.nowcoder.com/practice/6f8c901d091949a5837e24bb82a731f2?tpId=13&tqId=11206&rp=1&ru=%2Fta%2Fcoding-interviews&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking
import re
class Solution2:
def __init__(self):
self.pattern = re.compile(r'^[\+\-]?\d*(\.\d+)?([eE][\+\-]?\d+)?$')
def isNumeric(self, s):
return self.pattern.match(s)
class Solution:
def __init__(self):
# 如果是以 0 开头,后面就不能够跟随其他数字
self.unsigned_pattern = re.compile(r'^[1-9][0-9]*|^0+')
# s字符串
def isNumeric(self, s):
"""
判断规则:
(+|-)?(无符号整数)(.无符号整数)*(E|e[+-]?无符号整数)
"""
# +.123 也是合法的
if not s.startswith(('+.', '-.')):
s = self.signed(s)
if s is None:
return False
if s == '':
return True
else:
s = s[1:]
if s[0] == '.':
s = self.unsigned(s[1:])
if s is None:
return False
if s == '':
return True
if s[0] == 'E' or s[0] == 'e':
s = self.signed(s[1:])
if s is None:
return False
return s == ''
def unsigned(self, s):
"""
判断是否是无符号整数
"""
r = self.unsigned_pattern.match(s)
if r is None:
return None
return s[r.end():]
def signed(self, s):
"""
判断是否是有符号数
"""
if s.startswith(('+', '-')):
return self.unsigned(s[1:])
return self.unsigned(s)
import unittest
class TestCase(unittest.TestCase):
def setUp(self):
self.s = Solution2()
def test_1(self):
s = '100'
self.assertTrue(self.s.isNumeric(s))
def test_2(self):
s = '5e2'
self.assertTrue(self.s.isNumeric(s))
def test_3(self):
s = '-123'
self.assertTrue(self.s.isNumeric(s))
def test_4(self):
s = '3.1415926'
self.assertTrue(self.s.isNumeric(s))
def test_5(self):
s = '-1e-16'
self.assertTrue(self.s.isNumeric(s))
def test_6(self):
s = '12e'
self.assertFalse(self.s.isNumeric(s))
def test_7(self):
s = '1a3.14'
self.assertFalse(self.s.isNumeric(s))
def test_8(self):
s = '1.2.3'
self.assertFalse(self.s.isNumeric(s))
def test_9(self):
s = '+5'
self.assertTrue(self.s.isNumeric(s))
def test_10(self):
s = '+-5'
self.assertFalse(self.s.isNumeric(s))
def test_11(self):
s = '12e+3.4'
self.assertFalse(self.s.isNumeric(s))
def test_12(self):
s = '-.123'
self.assertTrue(self.s.isNumeric(s))