5/30/2020

[LeetCode] 65. Valid Number

Problem : https://leetcode.com/problems/valid-number/

Not hard but annoying.  Use FSM to check if character is allowed in the current state.

class Solution:
    def isNumber(self, s: str) -> bool:
        
        state = 'sign'
        
        for w in s:   
            if state == 'sign':
                if w in '+-':
                    state = 'integer_start'
                    continue
                
                if w.isdigit():
                    state = 'integer'
                    continue
                
                if w == '.':
                    state = 'decimal_start'
                    continue
            
                return False
            
            if state == 'integer_start':
                if w.isdigit():
                    state = 'integer'
                    continue
                
                if w == '.':
                    state = 'decimal_start'
                    continue
                       
                return False
            
            if state == 'integer':
                
                if w.isdigit():
                    continue
                    
                if w in 'eE':
                    state = 'sign_e'
                    continue
                
                if w == '.':
                    state = 'decimal'
                    continue
                
                return False
            
            if state == 'decimal_start':
                if w.isdigit():
                    state = 'decimal'
                    continue
                
                return False
            
            if state == 'decimal':
                if w.isdigit():
                    continue
                
                if w in 'eE':
                    state = 'sign_e'
                    continue
                
                return False
                        
            
            if state == 'sign_e':
                
                if w in '+-':
                    state = 'num_e_start'
                    continue
                
                if w.isdigit():
                    state = 'num_e'
                    continue
                    
                return False
            
            if state == 'num_e_start':
                if w.isdigit():
                    state = 'num_e'
                    continue
                
                return False
            
            if state == 'num_e':
                if w.isdigit():
                    continue
                
                return False
            
        if state == 'integer_start':
            return False
                    
        if state == 'decimal_start':
            return False
        
        if state == 'num_e_start':
            return False
        
        if state == 'sign_e':
            return False
        
        
        return True

Edited on 05/15/2021. Refactor FSM code.

No comments:

Post a Comment