LeetCode 65 Valid Number (Python)

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.


Solution Python


 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
class Solution:
    # @param s, a string
    # @return a boolean
    def isNumber(self, s):
        ss = s.strip()
        i = 0
        INITIAL = 0
        NUMBER = 1
        DECIMAL = 3
        SCIENTIFIC = 4
        MINUS = 5
        PLUS = 6
        status = INITIAL
        ldecimal = False
        decimal = 0
        scientific = 0
        while True:
            if i == len(ss): break
            if status == INITIAL:
                if ss[i] == '.':
                    status = DECIMAL
                    ldecimal = True
                    decimal += 1
                elif '0' <= ss[i] <= '9':
                    status = NUMBER
                elif ss[i] == '-':
                    status = MINUS
                elif ss[i] == '+':
                    status = PLUS
                else:
                    return False
            elif status == NUMBER:
                if ss[i] == '.':
                    if scientific:
                        return False
                    status = DECIMAL
                    if decimal:
                        return False
                    decimal += 1
                elif '0' <= ss[i] <= '9':
                    pass
                elif ss[i] == 'e':
                    if scientific:
                        return False
                    scientific += 1
                    status = SCIENTIFIC
                else:
                    return False
            elif status == DECIMAL:
                if '0' <= ss[i] <= '9':
                    status = NUMBER
                elif ss[i] == 'e':
                    if ldecimal: return False
                    scientific += 1
                    status = SCIENTIFIC
                else:
                    return False
            elif status == SCIENTIFIC:
                if '0' <= ss[i] <= '9':
                    status = NUMBER
                elif ss[i] == '+':
                    status = PLUS
                elif ss[i] == '-':
                    status = MINUS
                else:
                    return False
            elif status == MINUS:
                if ss[i] == '.':
                    status = DECIMAL
                    ldecimal = True
                    decimal += 1
                elif '0' <= ss[i] <= '9':
                    status = NUMBER
                else:
                    return False
            elif status == PLUS:
                if ss[i] == '.':
                    status = DECIMAL
                    ldecimal = True
                    decimal += 1
                elif '0' <= ss[i] <= '9':
                    status = NUMBER
                else:
                    return False
            else:    
                raise
            i += 1
        if status == NUMBER:
            pass
        elif status == DECIMAL and not ldecimal:
            pass
        else:
            return False
        return True

Popular posts from this blog

How Django Works (4) URL Resolution

Python Class Method and Class Only Method

How Django works (3): Testing Server and Static Files Serving