-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path20_NumericStrings.py
52 lines (41 loc) · 1.36 KB
/
20_NumericStrings.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
def scan_unsigned_int(str, index):
# start_index == index 用来标注是否有数位
start_index = index
while index<len(str) and str[index] in "0123456789":
index+=1
# 当str中存在若干0-9的数字时,返回true
return index != start_index,index
def scan_int(str, index):
if index<len(str) and str[index] in ['-','+']:
index+=1
return scan_unsigned_int(str, index)
def isNum(str):
"""
数字的格式可以用A[.[B]][e|EC]或者.B[e|EC]表示,其中A和C都是
整数(可以有正负号,也可以没有),而B是一个无符号整数
"""
if not str:
return False
# 对A,A可以有或者没有
index = 0
result, index = scan_int(str,index)
if index<len(str) and str[index] == '.':
# 如果有B,B是无符号整数
index+=1
has_float,index = scan_unsigned_int(str,index)
result = result or has_float
if index<len(str) and str[index] in ['e','E']:
index+=1
has_exp, index = scan_int(str,index)
result = result and has_exp
return result and index == len(str)
if __name__ == "__main__":
print(isNum("+100"))
print(isNum("5e2"))
print(isNum("-123"))
print(isNum("3.1415"))
print(isNum("-1E-16"))
print(isNum("12e"))
print(isNum("1a3.14"))
print(isNum("1.2.3"))
print(isNum("+-5"))