forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix the comment on line 33 (left->right) * add DFA.py * Update README.md * Update merge_sort.py * Create test_automata.py * Update test_automata.py * Update DFA.py * Update test_automata.py * Update test_automata.py * Create __init__.py * Update DFA.py * Update test_automata.py * Update test_automata.py * Update test_automata.py
- Loading branch information
1 parent
bbad761
commit cd4f9c7
Showing
4 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
def DFA(transitions, start, final, string): | ||
|
||
num = len(string) | ||
num_final = len(final) | ||
cur = start | ||
|
||
for i in range (num): | ||
|
||
if transitions[cur][string[i]] == None: | ||
return False | ||
else: | ||
cur = transitions[cur][string[i]] | ||
|
||
for i in range (num_final): | ||
if cur == final[i]: | ||
return True | ||
return False | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .DFA import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from algorithms.automata import DFA | ||
|
||
|
||
import unittest | ||
|
||
|
||
class TestDFA(unittest.TestCase): | ||
def test_DFA(self): | ||
|
||
transitions = { | ||
'a': {'1': 'a', '0': 'b'}, | ||
'b': {'1': 'b', '0': 'a'} | ||
} | ||
|
||
final=['a'] | ||
start = 'a' | ||
|
||
self.assertEqual(False, DFA(transitions, start, final, "000111100")) | ||
self.assertEqual(True, DFA(transitions, start, final, "111000011")) | ||
|
||
transitions1 = { | ||
'0': {'0': '1', '1': '0'}, | ||
'1': {'0': '2', '1': '0'}, | ||
'2': {'0': '2', '1': '3'}, | ||
'3': {'0': '3', '1': '3'} | ||
} | ||
|
||
final1 = ['0', '1', '2'] | ||
start1 = '0' | ||
|
||
self.assertEqual(False, DFA(transitions1, start1, final1, "0001111")) | ||
self.assertEqual(True, DFA(transitions1, start1, final1, "01010101")) | ||
|
||
transitions2 = { | ||
'0': {'a': '0', 'b': '1'}, | ||
'1': {'a': '0', 'b': '2'}, | ||
'2': {'a': '3', 'b': '2'}, | ||
'3': {'a': '3', 'b': '3'} | ||
} | ||
|
||
final2=['3'] | ||
start2 = '0' | ||
|
||
self.assertEqual(False, DFA(transitions2, start2, final2, "aaabbb")) | ||
self.assertEqual(True, DFA(transitions2, start2, final2, "baabba")) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |