Hand crafted FSM


  • 3
    B
    class Solution:
        # @param s, a string
        # @return a boolean
        def isNumber(self, s):
            transition = {
                1 : {' ': 1,
                     '0': 3,
                     '1': 3,
                     '2': 3,
                     '3': 3,
                     '4': 3,
                     '5': 3,
                     '6': 3,
                     '7': 3,
                     '8': 3,
                     '9': 3,
                     '.': 5,
                     '+': 0,
                     '-': 0},
                0 : {'0': 3,
                     '1': 3,
                     '2': 3,
                     '3': 3,
                     '4': 3,
                     '5': 3,
                     '6': 3,
                     '7': 3,
                     '8': 3,
                     '9': 3,
                     '.': 2},
                2 : {'0': 3,
                     '1': 3,
                     '2': 3,
                     '3': 3,
                     '4': 3,
                     '5': 3,
                     '6': 3,
                     '7': 3,
                     '8': 3,
                     '9': 3},
                3 : {'0': 3,
                     '1': 3,
                     '2': 3,
                     '3': 3,
                     '4': 3,
                     '5': 3,
                     '6': 3,
                     '7': 3,
                     '8': 3,
                     '9': 3,
                     '.': 10,
                     'e': 6,
                     ' ': 8,
                     None: 9},
                5 : {'0': 10,
                     '1': 10,
                     '2': 10,
                     '3': 10,
                     '4': 10,
                     '5': 10,
                     '6': 10,
                     '7': 10,
                     '8': 10,
                     '9': 10},
                10: {'0': 10,
                     '1': 10,
                     '2': 10,
                     '3': 10,
                     '4': 10,
                     '5': 10,
                     '6': 10,
                     '7': 10,
                     '8': 10,
                     '9': 10,
                     'e': 6,
                     ' ': 8,
                     None: 9},
                6 : {'+': 4,
                     '-': 4,
                     '0': 7,
                     '1': 7,
                     '2': 7,
                     '3': 7,
                     '4': 7,
                     '5': 7,
                     '6': 7,
                     '7': 7,
                     '8': 7,
                     '9': 7},
                4 : {'0': 7,
                     '1': 7,
                     '2': 7,
                     '3': 7,
                     '4': 7,
                     '5': 7,
                     '6': 7,
                     '7': 7,
                     '8': 7,
                     '9': 7},
                7 : {'0': 7,
                     '1': 7,
                     '2': 7,
                     '3': 7,
                     '4': 7,
                     '5': 7,
                     '6': 7,
                     '7': 7,
                     '8': 7,
                     '9': 7,
                     ' ': 8,
                     None: 9},
                8 : {' ': 8,
                     None: 9}
            }
            STATE = 1
            for c in s:
                if c not in transition[STATE]:
                    return False
                else:
                    STATE = transition[STATE][c]
            if STATE in [3, 7, 8, 10]:
                return True
            else:
                return False

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.