Validate if a given string is numeric.
Some examples:
"0"
=> true
" 0.1 "
=> true
"abc"
=> false
"1 a"
=> false
"2e10"
=> true
Solution:
This problem deals with a common problem in compilers in the lexical analysis, i.e. knowing if a certain string is a valid token.We accept the following regular grammar, shown as UNIX regular expression by simplicity:
\s*[\+\-]?(([0-9]+(\.[0-9]*)?)|\.[0-9]+)([eE][+-]?[0-9]+)?\s*
where \s represents [ \t].
This regular expression is accepted by the underneath DFA(Deterministic Finite Automata):
where any transition not shown takes to an error state, that in the code is represented by -1.
class Solution: # @param s, a string # @return a boolean def isNumber(self, s): state = 0 for i in xrange(0,len(s)): state = self.nextState(state, s[i]) if state == -1: return False state = self.nextState(state, ' ') return state == 8 def nextState(self, state,char): transititionTable = [[0,2,1,3,-1,-1], [-1,2,-1,3,-1,-1], [8,2,-1,4,5,-1], [-1,4,-1,-1,-1,-1], [8,4,-1,-1,5,-1],[-1,7,6,-1,-1,-1],[-1,7,-1,-1,-1,-1], [8,7,-1,-1,-1,-1], [8,-1,-1,-1,-1,-1]]; return transititionTable[state][self.getSymbol(char)] def getSymbol(self, char): if char == ' ' or char == '\t': return 0 if char.isdigit(): return 1 if char == '+' or char == '-': return 2 if char == '.': return 3 if char == 'E' or char =='e': return 4 return 5
More information:
[1, Chapter 1] J. Glen Brookshear, "Theory of Computation: Formal Languages, Automata, and Complexity", Addison-Wesley.
[2, Chapters 2-3] Rajeev Motwani, Jeffrey Ullman, and John Hopcroft, "Introduction to Automata Theory, Languages, and Computation", Addison-Wesley.
No comments :
Post a Comment