Three state parser – why is it not working on LeetCode but locally?


  • 0
    J

    I made a simple state machine with three states: start –> numeric <–> special-Char

    The code works fine on my computer but on Leetcode the space-character doesn't seem to be recognized. Any ideas why?

        def number(c):        	
            if isNumeric(c):
                return 1
            elif c is '.' or c is 'e':
                return 2
            return -1
        def period(c):
                if isNumeric(c):
                    return 1
                return -1
        def start(c):
            if c is '+' or c is '-' or isNumeric(c):
                return 1
            return -1
        def isNumeric(c):
            return c >= '0' and c <= '9'
        
        states = {
            0: start,
            1: number,
            2: period
        }
        state = 0
        for c in s:
            if c is ' ':
                continue
            state = states[state](c)
            if state == -1:
                return False
        return True

Log in to reply
 

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