Oct-15-2019, 07:55 AM
Here is my proposal, using a deterministic finite automaton
1 2 3 4 5 6 7 8 9 10 11 12 13 |
def isinteger(x): D = {( 0 , ' ' ): 0 , ( 0 , '+' ): 1 , ( 0 , '-' ): 1 , ( 0 , '.' ): 2 , ( 1 , ' ' ): 1 , ( 1 , '.' ): 2 , ( 2 , '0' ): 4 , ( 3 , '.' ): 4 , ( 3 , ' ' ): 5 , ( 4 , '0' ): 4 , ( 4 , ' ' ): 5 , ( 5 , ' ' ): 5 ,} s = 0 for c in x: if (s, c) in D: s = D[(s, c)] elif c in '0123456789' : if s in ( 0 , 1 , 3 ): s = 3 else : return False else : return False return s > 2 |