rebase branch with master

This commit is contained in:
reniz-shah 2024-08-08 10:16:15 +05:30
parent 3faf2cd53e
commit 13d5527a5e

View File

@ -2,8 +2,6 @@
Solution By: Reniz Shah Solution By: Reniz Shah
Topic: Deterministic Finite Automaton (DFA) Topic: Deterministic Finite Automaton (DFA)
Given a string s, return whether s is a valid number or not Given a string s, return whether s is a valid number or not
Input: s = -90E3
Output: True
Leetcode link: https://leetcode.com/problems/valid-number/description/ Leetcode link: https://leetcode.com/problems/valid-number/description/
""" """
@ -53,100 +51,6 @@ state_machine: dict[State, dict[CharType, State]] = {
State.EXP_NUMBER: {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 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},
}
def classify_char(char: str) -> CharType | None: def classify_char(char: str) -> CharType | None:
""" """
@ -179,9 +83,6 @@ def classify_char(char: str) -> CharType | None:
>>> classify_char('0') >>> classify_char('0')
<CharType.NUMERIC: 'NUMERIC'> <CharType.NUMERIC: 'NUMERIC'>
>>> classify_char('01') >>> classify_char('01')
'decimal'
>>> classify_char('r')
""" """
if len(char) != 1: if len(char) != 1:
return None return None
@ -199,15 +100,12 @@ def classify_char(char: str) -> CharType | None:
def is_valid_number(number_string: str) -> bool: def is_valid_number(number_string: str) -> bool:
""" """
This function checks if the input string represents a valid number. This function checks if the input string represents a valid number.
It uses a finite state machine to parse the input string, It uses a finite state machine to parse the input string,
transitioning between states based on the character type. transitioning between states based on the character type.
The function returns True if the input string represents a valid number, The function returns True if the input string represents a valid number,
and False otherwise. and False otherwise.
A valid number is defined as a string that can be parsed into an A valid number is defined as a string that can be parsed into an
integer, decimal, or exponent. integer, decimal, or exponent.
>>> is_valid_number("2") >>> is_valid_number("2")
True True
>>> is_valid_number("0089") >>> is_valid_number("0089")