2024-07-08 10:49:54 +00:00
|
|
|
"""
|
|
|
|
Solution By: Reniz Shah
|
|
|
|
Topic: Deterministic Finite Automaton (DFA)
|
|
|
|
Given a string s, return whether s is a valid number or not
|
|
|
|
Leetcode link: https://leetcode.com/problems/valid-number/description/
|
|
|
|
"""
|
2024-08-07 12:22:07 +00:00
|
|
|
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},
|
|
|
|
}
|
2024-08-07 12:22:54 +00:00
|
|
|
|
2024-08-07 12:22:07 +00:00
|
|
|
from enum import Enum
|
|
|
|
|
2024-08-07 12:22:54 +00:00
|
|
|
|
2024-08-07 12:22:07 +00:00
|
|
|
class CharType(Enum):
|
2024-08-07 12:22:54 +00:00
|
|
|
NUMERIC = "NUMERIC"
|
|
|
|
SIGN = "SIGN"
|
|
|
|
EXPONENT = "EXPONENT"
|
|
|
|
DECIMAL = "DECIMAL"
|
|
|
|
|
2024-08-07 12:22:07 +00:00
|
|
|
|
|
|
|
class State(Enum):
|
2024-08-07 12:22:54 +00:00
|
|
|
INITIAL = "INITIAL"
|
|
|
|
SIGNED = "SIGNED"
|
|
|
|
WHOLE = "WHOLE"
|
|
|
|
FRACTIONAL = "FRACTIONAL"
|
|
|
|
FRACTION = "FRACTION"
|
|
|
|
EXPONENTIAL = "EXPONENTIAL"
|
|
|
|
EXP_SIGN = "EXP_SIGN"
|
|
|
|
EXP_NUMBER = "EXP_NUMBER"
|
|
|
|
|
|
|
|
|
2024-08-07 12:35:08 +00:00
|
|
|
state_machine: dict[State, dict[CharType, State]] = {
|
2024-08-07 12:22:54 +00:00
|
|
|
State.INITIAL: {
|
|
|
|
CharType.NUMERIC: State.WHOLE,
|
|
|
|
CharType.SIGN: State.SIGNED,
|
|
|
|
CharType.DECIMAL: State.FRACTIONAL,
|
|
|
|
},
|
2024-08-07 12:22:07 +00:00
|
|
|
State.SIGNED: {CharType.NUMERIC: State.WHOLE, CharType.DECIMAL: State.FRACTIONAL},
|
2024-08-07 12:22:54 +00:00
|
|
|
State.WHOLE: {
|
|
|
|
CharType.NUMERIC: State.WHOLE,
|
|
|
|
CharType.DECIMAL: State.FRACTION,
|
|
|
|
CharType.EXPONENT: State.EXPONENTIAL,
|
|
|
|
},
|
2024-08-07 12:22:07 +00:00
|
|
|
State.FRACTIONAL: {CharType.NUMERIC: State.FRACTION},
|
2024-08-07 12:22:54 +00:00
|
|
|
State.FRACTION: {
|
|
|
|
CharType.NUMERIC: State.FRACTION,
|
|
|
|
CharType.EXPONENT: State.EXPONENTIAL,
|
|
|
|
},
|
|
|
|
State.EXPONENTIAL: {
|
|
|
|
CharType.NUMERIC: State.EXP_NUMBER,
|
|
|
|
CharType.SIGN: State.EXP_SIGN,
|
|
|
|
},
|
2024-08-07 12:22:07 +00:00
|
|
|
State.EXP_SIGN: {CharType.NUMERIC: State.EXP_NUMBER},
|
|
|
|
State.EXP_NUMBER: {CharType.NUMERIC: State.EXP_NUMBER},
|
|
|
|
}
|
|
|
|
|
2024-08-07 12:22:07 +00:00
|
|
|
|
|
|
|
def classify_char(char: str) -> CharType | None:
|
2024-07-08 10:49:54 +00:00
|
|
|
"""
|
|
|
|
Classifies a character into one of the following categories:
|
|
|
|
|
2024-08-07 12:22:07 +00:00
|
|
|
- 'CharType.NUMERIC': if the character is a digit (0-9)
|
|
|
|
- 'CharType.SIGN': if the character is a plus sign (+) or a minus sign (-)
|
2024-08-07 12:35:08 +00:00
|
|
|
- 'CharType.EXPONENT': if the character is an 'e' or 'E'
|
|
|
|
(used in exponential notation)
|
2024-08-07 12:22:07 +00:00
|
|
|
- 'CharType.DECIMAL': if the character is a decimal point (.)
|
2024-08-07 12:35:08 +00:00
|
|
|
- None: if the character does not fit into any of the above categories
|
|
|
|
- None: if size of char is not 1
|
2024-07-08 10:49:54 +00:00
|
|
|
|
|
|
|
Parameters:
|
|
|
|
char (str): The character to be classified
|
|
|
|
|
|
|
|
Returns:
|
2024-08-07 12:22:07 +00:00
|
|
|
CharType: The classification of the character
|
2024-07-08 10:49:54 +00:00
|
|
|
|
|
|
|
>>> classify_char('2')
|
2024-08-07 12:22:07 +00:00
|
|
|
<CharType.NUMERIC: 'NUMERIC'>
|
2024-07-08 10:49:54 +00:00
|
|
|
>>> classify_char('-')
|
2024-08-07 12:22:07 +00:00
|
|
|
<CharType.SIGN: 'SIGN'>
|
2024-07-08 10:49:54 +00:00
|
|
|
>>> classify_char('e')
|
2024-08-07 12:22:07 +00:00
|
|
|
<CharType.EXPONENT: 'EXPONENT'>
|
2024-07-08 10:49:54 +00:00
|
|
|
>>> classify_char('.')
|
2024-08-07 12:22:54 +00:00
|
|
|
<CharType.DECIMAL: 'DECIMAL'>
|
|
|
|
>>> classify_char('')
|
|
|
|
|
|
|
|
>>> classify_char('0')
|
|
|
|
<CharType.NUMERIC: 'NUMERIC'>
|
|
|
|
>>> classify_char('01')
|
2024-07-08 10:49:54 +00:00
|
|
|
"""
|
2024-08-07 12:22:07 +00:00
|
|
|
if len(char) != 1:
|
|
|
|
return None
|
2024-07-08 10:49:54 +00:00
|
|
|
if char.isdigit():
|
2024-08-07 12:22:07 +00:00
|
|
|
return CharType.NUMERIC
|
2024-07-08 10:49:54 +00:00
|
|
|
if char in "+-":
|
2024-08-07 12:22:07 +00:00
|
|
|
return CharType.SIGN
|
2024-07-08 10:49:54 +00:00
|
|
|
if char in "eE":
|
2024-08-07 12:22:07 +00:00
|
|
|
return CharType.EXPONENT
|
2024-07-08 10:49:54 +00:00
|
|
|
if char == ".":
|
2024-08-07 12:22:07 +00:00
|
|
|
return CharType.DECIMAL
|
2024-07-08 10:49:54 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
2024-08-07 12:41:57 +00:00
|
|
|
def is_valid_number(number_string: str) -> bool:
|
2024-07-08 10:49:54 +00:00
|
|
|
"""
|
|
|
|
This function checks if the input string represents a valid number.
|
|
|
|
It uses a finite state machine to parse the input string,
|
|
|
|
transitioning between states based on the character type.
|
|
|
|
The function returns True if the input string represents a valid number,
|
|
|
|
and False otherwise.
|
|
|
|
A valid number is defined as a string that can be parsed into an
|
|
|
|
integer, decimal, or exponent.
|
|
|
|
>>> is_valid_number("2")
|
|
|
|
True
|
|
|
|
>>> is_valid_number("0089")
|
|
|
|
True
|
|
|
|
>>> is_valid_number("-0.1")
|
|
|
|
True
|
|
|
|
>>> is_valid_number("+3.14")
|
|
|
|
True
|
|
|
|
>>> is_valid_number("4.")
|
|
|
|
True
|
|
|
|
>>> is_valid_number("-.9")
|
|
|
|
True
|
|
|
|
>>> is_valid_number("2e10")
|
|
|
|
True
|
|
|
|
>>> is_valid_number("-90E3")
|
|
|
|
True
|
|
|
|
>>> is_valid_number("3e+7")
|
|
|
|
True
|
|
|
|
>>> is_valid_number("+6e-1")
|
|
|
|
True
|
|
|
|
>>> is_valid_number("53.5e93")
|
|
|
|
True
|
|
|
|
>>> is_valid_number("-123.456e789")
|
|
|
|
True
|
|
|
|
|
|
|
|
|
|
|
|
>>> is_valid_number("abc")
|
|
|
|
False
|
|
|
|
>>> is_valid_number("1a")
|
|
|
|
False
|
|
|
|
>>> is_valid_number("1e")
|
|
|
|
False
|
|
|
|
>>> is_valid_number("e3")
|
|
|
|
False
|
|
|
|
>>> is_valid_number("99e2.5")
|
|
|
|
False
|
|
|
|
>>> is_valid_number("--6")
|
|
|
|
False
|
|
|
|
>>> is_valid_number("-+3")
|
|
|
|
False
|
|
|
|
>>> is_valid_number("95a54e53")
|
|
|
|
False
|
|
|
|
>>> is_valid_number(".")
|
|
|
|
False
|
|
|
|
"""
|
|
|
|
|
2024-08-07 12:22:07 +00:00
|
|
|
valid_final_states = {State.WHOLE, State.FRACTION, State.EXP_NUMBER}
|
|
|
|
current_state = State.INITIAL
|
2024-07-08 10:49:54 +00:00
|
|
|
|
2024-08-07 12:41:57 +00:00
|
|
|
for char in number_string:
|
2024-07-08 10:49:54 +00:00
|
|
|
char_type = classify_char(char)
|
|
|
|
if char_type is None or char_type not in state_machine[current_state]:
|
|
|
|
return False
|
|
|
|
current_state = state_machine[current_state][char_type]
|
|
|
|
|
|
|
|
return current_state in valid_final_states
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import doctest
|
|
|
|
|
|
|
|
doctest.testmod()
|