forked from y-ncao/Python-Study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValid_Number.py
51 lines (45 loc) · 1.54 KB
/
Valid_Number.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
"""
Validate if a given string is numeric.
Some examples:
* "0" => true
* " 0.1 " => true
* "abc" => false
* "1 a" => false
* "2e10" => true
Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.
"""
class Solution:
# @param s, a string
# @return a boolean
def isNumber(self, s):
s = s.strip()
if len(s.split('e')) > 2 or len(s.split('E')) > 2:
return False
if 'e' in s:
return self.isNumberwoE(s.split('e')[0]) and self.isNumberwoE(s.split('e')[1], False)
elif 'E' in s:
return self.isNumberwoE(s.split('E')[0]) and self.isNumberwoE(s.split('E')[1], False)
else:
return self.isNumberwoE(s)
def isNumberwoE(self, s, allow_digit = True):
has_num = False
for i, char in enumerate(s):
if i == 0 and char in ['+', '-']:
continue
if char == '.' and allow_digit:
allow_digit = False
continue
if char.isdigit():
has_num = True
continue
return False
return has_num
# Note:
# 1. Strip white space
# 2. Check if multiple E/e, split by E/e
# 3. Check each part of num if they are valid with/wo digit
# 4. Things that can pass:
# i. i == 0 and char in ['+', '-']
# ii. char.isdigit(), pass and set hasNum = True
# iii. char == '.': need to check if allow_digit
# Set all the rest cases to False