Skip to content
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

deterministic finite automat #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
195 changes: 195 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
# Created by .ignore support plugin (hsz.mobi)
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

### Python template
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
11 changes: 11 additions & 0 deletions .idea/algorithm.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions deterministic_finite_automat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
```
class State:
def __init__(self, value: str, is_end: bool):
self.value = value
self.is_end = is_end


class Rule:
def __init__(self):
self.table = dict()

def add_rule(self, input_character: str, current_state: State, target_state: State):
self.table[(input_character, current_state)] = target_state

def lookup_rule(self, input_character: str, current_state):
return self.table[(input_character, current_state)]


class Graph:
def __init__(self, rule: Rule, alphabet: list, start_state: State):
self.rule = rule
self.alphabet = alphabet
self.current_state = start_state

def is_valid(self, input_string: str) -> bool:
temp = self.current_state
for input_char in input_string:
if input_char in self.alphabet:
temp = self.rule.lookup_rule(input_char, temp)
else:
raise ValueError("State is not full")
return temp.is_end


if __name__ == '__main__':
q0 = State("q0", True)

alphabet = ['0', '1']

q1 = State("q1", False)
q2 = State("q2", False)
q3 = State("q3", False)
rule = Rule()
rule.add_rule("1", q0, q1)
rule.add_rule("1", q1, q0)

rule.add_rule("1", q2, q3)
rule.add_rule("1", q3, q2)

rule.add_rule("0", q0, q2)
rule.add_rule("0", q2, q0)

rule.add_rule("0", q0, q1)
rule.add_rule("0", q1, q0)

graph = Graph(rule, alphabet, q0)
line = input("Please enter a string: ")
print(graph.is_valid(line))
```
57 changes: 57 additions & 0 deletions deterministic_finite_automat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class State:
def __init__(self, value: str, is_end: bool):
self.value = value
self.is_end = is_end


class Rule:
def __init__(self):
self.table = dict()

def add_rule(self, input_character: str, current_state: State, target_state: State):
self.table[(input_character, current_state)] = target_state

def lookup_rule(self, input_character: str, current_state):
return self.table[(input_character, current_state)]


class Graph:
def __init__(self, rule: Rule, alphabet: list, start_state: State):
self.rule = rule
self.alphabet = alphabet
self.current_state = start_state

def is_valid(self, input_string: str) -> bool:
temp = self.current_state
for input_char in input_string:
if input_char in self.alphabet:
temp = self.rule.lookup_rule(input_char, temp)
else:
raise ValueError("State is not full")
return temp.is_end


if __name__ == '__main__':
q0 = State("q0", True)

alphabet = ['0', '1']

q1 = State("q1", False)
q2 = State("q2", False)
q3 = State("q3", False)
rule = Rule()
rule.add_rule("1", q0, q1)
rule.add_rule("1", q1, q0)

rule.add_rule("1", q2, q3)
rule.add_rule("1", q3, q2)

rule.add_rule("0", q0, q2)
rule.add_rule("0", q2, q0)

rule.add_rule("0", q0, q1)
rule.add_rule("0", q1, q0)

graph = Graph(rule, alphabet, q0)
line = input("Please enter a string: ")
print(graph.is_valid(line))