Skip to content

add algorithm to check if given string is valid number or not #11479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] authored and reniz-shah committed Aug 7, 2024
commit ab442394d15d302c41dee7dcd877c966f6a71be7
56 changes: 37 additions & 19 deletions strings/string_is_valid_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,50 @@
Output: True
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'
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},
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.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.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},
}
Expand Down Expand Up @@ -62,7 +80,7 @@ def classify_char(char: str) -> CharType | None:
>>> classify_char('.')
<CharType.DECIMAL: 'DECIMAL'>
>>> classify_char('')

>>> classify_char('0')
<CharType.NUMERIC: 'NUMERIC'>
>>> classify_char('01')
Expand Down