resolve comments which is create enum for states and char types extract state machine outside function

This commit is contained in:
reniz-shah 2024-08-07 17:52:07 +05:30
parent ab442394d1
commit 42c24d6f3c

View File

@ -6,6 +6,82 @@ Input: s = -90E3
Output: True Output: True
Leetcode link: https://leetcode.com/problems/valid-number/description/ Leetcode link: https://leetcode.com/problems/valid-number/description/
""" """
from enum import Enum
from typing import Dict
class CharType(Enum):
NUMERIC = 'NUMERIC'
SIGN = 'SIGN'
EXPONENT = 'EXPONENT'
DECIMAL = 'DECIMAL'
class State(Enum):
INITIAL = 'INITIAL'
SIGNED = 'SIGNED'
WHOLE = 'WHOLE'
FRACTIONAL = 'FRACTIONAL'
FRACTION = 'FRACTION'
EXPONENTIAL = 'EXPONENTIAL'
EXP_SIGN = 'EXP_SIGN'
EXP_NUMBER = 'EXP_NUMBER'
state_machine : Dict[State, Dict[CharType, State]] = {
State.INITIAL: {CharType.NUMERIC: State.WHOLE, CharType.SIGN: State.SIGNED, CharType.DECIMAL: State.FRACTIONAL},
State.SIGNED: {CharType.NUMERIC: State.WHOLE, CharType.DECIMAL: State.FRACTIONAL},
State.WHOLE: {CharType.NUMERIC: State.WHOLE, CharType.DECIMAL: State.FRACTION, CharType.EXPONENT: State.EXPONENTIAL},
State.FRACTIONAL: {CharType.NUMERIC: State.FRACTION},
State.FRACTION: {CharType.NUMERIC: State.FRACTION, CharType.EXPONENT: State.EXPONENTIAL},
State.EXPONENTIAL: {CharType.NUMERIC: State.EXP_NUMBER, CharType.SIGN: State.EXP_SIGN},
State.EXP_SIGN: {CharType.NUMERIC: State.EXP_NUMBER},
State.EXP_NUMBER: {CharType.NUMERIC: State.EXP_NUMBER},
}
from enum import Enum
from typing import Dict
class CharType(Enum):
NUMERIC = "NUMERIC"
SIGN = "SIGN"
EXPONENT = "EXPONENT"
DECIMAL = "DECIMAL"
class State(Enum):
INITIAL = "INITIAL"
SIGNED = "SIGNED"
WHOLE = "WHOLE"
FRACTIONAL = "FRACTIONAL"
FRACTION = "FRACTION"
EXPONENTIAL = "EXPONENTIAL"
EXP_SIGN = "EXP_SIGN"
EXP_NUMBER = "EXP_NUMBER"
state_machine: Dict[State, Dict[CharType, State]] = {
State.INITIAL: {
CharType.NUMERIC: State.WHOLE,
CharType.SIGN: State.SIGNED,
CharType.DECIMAL: State.FRACTIONAL,
},
State.SIGNED: {CharType.NUMERIC: State.WHOLE, CharType.DECIMAL: State.FRACTIONAL},
State.WHOLE: {
CharType.NUMERIC: State.WHOLE,
CharType.DECIMAL: State.FRACTION,
CharType.EXPONENT: State.EXPONENTIAL,
},
State.FRACTIONAL: {CharType.NUMERIC: State.FRACTION},
State.FRACTION: {
CharType.NUMERIC: State.FRACTION,
CharType.EXPONENT: State.EXPONENTIAL,
},
State.EXPONENTIAL: {
CharType.NUMERIC: State.EXP_NUMBER,
CharType.SIGN: State.EXP_SIGN,
},
State.EXP_SIGN: {CharType.NUMERIC: State.EXP_NUMBER},
State.EXP_NUMBER: {CharType.NUMERIC: State.EXP_NUMBER},
}
from enum import Enum from enum import Enum
from typing import Dict from typing import Dict
@ -78,12 +154,8 @@ def classify_char(char: str) -> CharType | None:
>>> classify_char('e') >>> classify_char('e')
<CharType.EXPONENT: 'EXPONENT'> <CharType.EXPONENT: 'EXPONENT'>
>>> classify_char('.') >>> classify_char('.')
<CharType.DECIMAL: 'DECIMAL'> 'decimal'
>>> classify_char('') >>> classify_char('r')
>>> classify_char('0')
<CharType.NUMERIC: 'NUMERIC'>
>>> classify_char('01')
""" """
if len(char) != 1: if len(char) != 1: