strings.string_is_valid_number ============================== .. py:module:: strings.string_is_valid_number .. autoapi-nested-parse:: 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/ Attributes ---------- .. autoapisummary:: strings.string_is_valid_number.state_machine Classes ------- .. autoapisummary:: strings.string_is_valid_number.CharType strings.string_is_valid_number.State Functions --------- .. autoapisummary:: strings.string_is_valid_number.classify_char strings.string_is_valid_number.is_valid_number Module Contents --------------- .. py:class:: CharType Bases: :py:obj:`enum.Enum` Create a collection of name/value pairs. Example enumeration: >>> class Color(Enum): ... RED = 1 ... BLUE = 2 ... GREEN = 3 Access them by: - attribute access: >>> Color.RED - value lookup: >>> Color(1) - name lookup: >>> Color['RED'] Enumerations can be iterated over, and know how many members they have: >>> len(Color) 3 >>> list(Color) [, , ] Methods can be added to enumerations, and members can have their own attributes -- see the documentation for details. .. py:attribute:: DECIMAL :value: 'DECIMAL' .. py:attribute:: EXPONENT :value: 'EXPONENT' .. py:attribute:: NUMERIC :value: 'NUMERIC' .. py:attribute:: SIGN :value: 'SIGN' .. py:class:: State Bases: :py:obj:`enum.Enum` Create a collection of name/value pairs. Example enumeration: >>> class Color(Enum): ... RED = 1 ... BLUE = 2 ... GREEN = 3 Access them by: - attribute access: >>> Color.RED - value lookup: >>> Color(1) - name lookup: >>> Color['RED'] Enumerations can be iterated over, and know how many members they have: >>> len(Color) 3 >>> list(Color) [, , ] Methods can be added to enumerations, and members can have their own attributes -- see the documentation for details. .. py:attribute:: EXPONENTIAL :value: 'EXPONENTIAL' .. py:attribute:: EXP_NUMBER :value: 'EXP_NUMBER' .. py:attribute:: EXP_SIGN :value: 'EXP_SIGN' .. py:attribute:: FRACTION :value: 'FRACTION' .. py:attribute:: FRACTIONAL :value: 'FRACTIONAL' .. py:attribute:: INITIAL :value: 'INITIAL' .. py:attribute:: SIGNED :value: 'SIGNED' .. py:attribute:: WHOLE :value: 'WHOLE' .. py:function:: classify_char(char: str) -> CharType | None Classifies a character into one of the following categories: - 'CharType.NUMERIC': if the character is a digit (0-9) - 'CharType.SIGN': if the character is a plus sign (+) or a minus sign (-) - 'CharType.EXPONENT': if the character is an 'e' or 'E' (used in exponential notation) - 'CharType.DECIMAL': if the character is a decimal point (.) - None: if the character does not fit into any of the above categories - None: if size of char is not 1 Parameters: char (str): The character to be classified Returns: CharType: The classification of the character >>> classify_char('2') >>> classify_char('-') >>> classify_char('e') >>> classify_char('.') >>> classify_char('') >>> classify_char('0') >>> classify_char('01') .. py:function:: is_valid_number(number_string: str) -> bool 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 .. py:data:: state_machine :type: dict[State, dict[CharType, State]]