-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalid_number.py
52 lines (38 loc) · 1.32 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
52
class Solution:
# @param A : string
# @return an integer
VALID_CHARS = '0123456789-.e+'
BASE_NUM = '0123456789'
def isNumber(self, A):
A = A.strip()
if len(A) == 0:
return 0
for a in A:
if a not in self.VALID_CHARS:
return 0
if self.isBasicNumeric(A):
return 1
B = A.split('e')
if len(B) == 2:
return self.isBasicNumeric(B[0]) * self.isValidNeg(B[1])
return 0
def isInteger(self, A):
if len(A) == 0:
return 0
for a in A:
if a not in self.BASE_NUM:
return 0
return 1
def isBasicNumeric(self, A):
B = A.split('.')
if len(B) > 2 or len(B) == 0:
return 0
if len(B) == 2:
B[0] = '0' if B[0] == '' else B[0]
B[1] = 'xx' if B[1] == '' else B[1]
return self.isValidNeg(B[0]) * self.isInteger(B[1])
return self.isValidNeg(A)
def isValidNeg(self, A):
if A[0] == '-' or A[0] == '+':
return self.isInteger(A[1:])
return self.isInteger(A)