diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..27b40ca --- /dev/null +++ b/.gitignore @@ -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/ diff --git a/.idea/algorithm.iml b/.idea/algorithm.iml new file mode 100644 index 0000000..6711606 --- /dev/null +++ b/.idea/algorithm.iml @@ -0,0 +1,11 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..9d9a2e5 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,33 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..8656114 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..2ba6d76 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/deterministic_finite_automat.md b/deterministic_finite_automat.md new file mode 100644 index 0000000..06a71ac --- /dev/null +++ b/deterministic_finite_automat.md @@ -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)) +``` \ No newline at end of file diff --git a/deterministic_finite_automat.py b/deterministic_finite_automat.py new file mode 100644 index 0000000..960fb8e --- /dev/null +++ b/deterministic_finite_automat.py @@ -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))