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

docs: add teaching example #212

Closed
wants to merge 1 commit into from
Closed
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
77 changes: 77 additions & 0 deletions docs/examples/teaching-examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Teaching example

This example demonstrate how `automata` can be easily used for teaching purpose.
We adopt [this introduction to finite automata](https://www.geeksforgeeks.org/introduction-of-finite-automata/) to show how `automata` can be incorporated into the material.

## Deterministic finite automaton

We start by building and visualizing the deterministic finite automaton in the tutorial, which accepts any string ending in "a".

```python
from automata.fa.dfa import DFA

dfa = DFA(
states={"q0", "q1"},
input_symbols={"a", "b"},
transitions={"q0": {"a": "q1", "b": "q0"}, "q1": {"a": "q1", "b": "q0"}},
initial_state="q0",
final_states={"q1"},
)
dfa.show_diagram()
```

To verify whether our automaton is functioning properly, we supply a list of input strings and check if they're accepted by the automaton.

```python
inputs = [
"a",
"aa",
"aaa",
"aaaaa",
"ba",
"bba",
"bbbaa",
"aba",
"abba",
"aaba",
"abaa",
"bb",
"bbbbb",
"bab",
"baab",
"bbab",
"babb",
]
for in_str in inputs:
if dfa.accepts_input(in_str):
print("Accepted: {}".format(in_str))
else:
print("Rejected: {}".format(in_str))
```

## Nondeterministic finite automaton

We then build a nondeterministic finite automaton in the tutorial that's equivalent to the previous one.

```python
from automata.fa.nfa import NFA

nfa = NFA(
states={"q0", "q1"},
input_symbols={"a", "b"},
transitions={"q0": {"a": {"q0", "q1"}, "b": {"q0"}}},
initial_state="q0",
final_states={"q1"},
)
nfa.show_diagram()
```

We can verify that they're equivalent by checking with the same list of input strings:

```python
for in_str in inputs:
if nfa.accepts_input(in_str):
print("Accepted: {}".format(in_str))
else:
print("Rejected: {}".format(in_str))
```
Loading