Compare commits

...

17 Commits

Author SHA1 Message Date
Reniz Shah
78bd385643
Merge 914b0b0528 into f3f32ae3ca 2024-11-21 23:27:57 +01:00
pre-commit-ci[bot]
f3f32ae3ca
[pre-commit.ci] pre-commit autoupdate (#12385)
updates:
- [github.com/astral-sh/ruff-pre-commit: v0.7.3 → v0.7.4](https://github.com/astral-sh/ruff-pre-commit/compare/v0.7.3...v0.7.4)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-11-18 22:07:12 +01:00
Christian Clauss
e3bd7721c8
validate_filenames.py Shebang python for Windows (#12371) 2024-11-15 14:59:14 +01:00
pre-commit-ci[bot]
e3f3d668be
[pre-commit.ci] pre-commit autoupdate (#12370)
* [pre-commit.ci] pre-commit autoupdate

updates:
- [github.com/astral-sh/ruff-pre-commit: v0.7.2 → v0.7.3](https://github.com/astral-sh/ruff-pre-commit/compare/v0.7.2...v0.7.3)
- [github.com/abravalheri/validate-pyproject: v0.22 → v0.23](https://github.com/abravalheri/validate-pyproject/compare/v0.22...v0.23)

* Update sudoku_solver.py

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
2024-11-11 21:05:50 +01:00
pre-commit-ci[bot]
3e9ca92ca9
[pre-commit.ci] pre-commit autoupdate (#12349)
updates:
- [github.com/astral-sh/ruff-pre-commit: v0.7.1 → v0.7.2](https://github.com/astral-sh/ruff-pre-commit/compare/v0.7.1...v0.7.2)
- [github.com/tox-dev/pyproject-fmt: v2.4.3 → v2.5.0](https://github.com/tox-dev/pyproject-fmt/compare/v2.4.3...v2.5.0)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-11-04 21:09:03 +01:00
reniz-shah
914b0b0528 resolve ruff check failing 2024-08-08 10:38:10 +05:30
reniz-shah
272b0caf6a resolve ruff check failing 2024-08-08 10:26:12 +05:30
pre-commit-ci[bot]
1b1df64534 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-08-08 10:26:12 +05:30
reniz-shah
2d5eebf842 resolve comments which is create enum for states and char types extract state machine outside function 2024-08-08 10:26:12 +05:30
reniz-shah
13d5527a5e rebase branch with master 2024-08-08 10:16:15 +05:30
reniz-shah
3faf2cd53e change variable name from s to number_string as it is more descriptive 2024-08-08 10:09:44 +05:30
reniz-shah
0b4271a5d7 resolve ruff check failing 2024-08-08 10:09:44 +05:30
pre-commit-ci[bot]
aaf90cdb38 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-08-08 10:09:44 +05:30
reniz-shah
42c24d6f3c resolve comments which is create enum for states and char types extract state machine outside function 2024-08-08 10:08:08 +05:30
pre-commit-ci[bot]
ab442394d1 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-08-07 17:54:23 +05:30
reniz-shah
eed534f951 resolve comments which is create enum for states and char types extract state machine outside function 2024-08-07 17:54:23 +05:30
Reniz Shah
e82e93ac0d add algorithm to check if given string is valid number or not
Add a new file string_is_valid_number.py in string directory
2024-08-07 17:54:23 +05:30
4 changed files with 175 additions and 5 deletions

View File

@ -16,7 +16,7 @@ repos:
- id: auto-walrus
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.1
rev: v0.7.4
hooks:
- id: ruff
- id: ruff-format
@ -29,7 +29,7 @@ repos:
- tomli
- repo: https://github.com/tox-dev/pyproject-fmt
rev: "v2.4.3"
rev: "v2.5.0"
hooks:
- id: pyproject-fmt
@ -42,7 +42,7 @@ repos:
pass_filenames: false
- repo: https://github.com/abravalheri/validate-pyproject
rev: v0.22
rev: v0.23
hooks:
- id: validate-pyproject

View File

@ -172,7 +172,7 @@ def solved(values):
def from_file(filename, sep="\n"):
"Parse a file into a list of strings, separated by sep."
return open(filename).read().strip().split(sep) # noqa: SIM115
return open(filename).read().strip().split(sep)
def random_puzzle(assignments=17):

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python3
#!python
import os
try:

View File

@ -0,0 +1,170 @@
"""
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/
"""
from enum import Enum
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:
"""
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')
<CharType.NUMERIC: 'NUMERIC'>
>>> classify_char('-')
<CharType.SIGN: 'SIGN'>
>>> classify_char('e')
<CharType.EXPONENT: 'EXPONENT'>
>>> classify_char('.')
<CharType.DECIMAL: 'DECIMAL'>
>>> classify_char('')
>>> classify_char('0')
<CharType.NUMERIC: 'NUMERIC'>
>>> classify_char('01')
"""
if len(char) != 1:
return None
if char.isdigit():
return CharType.NUMERIC
if char in "+-":
return CharType.SIGN
if char in "eE":
return CharType.EXPONENT
if char == ".":
return CharType.DECIMAL
return None
def 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
"""
valid_final_states = {State.WHOLE, State.FRACTION, State.EXP_NUMBER}
current_state = State.INITIAL
for char in number_string:
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()