-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfa.py
75 lines (75 loc) · 2.11 KB
/
dfa.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# A general DFA
def dfa_anystring(string):
machine = {
0:{
'a':0,'b':0
}
}
start = 0
final = [0]
state = start
for elem in string: state = machine[state][elem]
if state in final: return True
else: return False
def dfa_2(string):
machine = {
0:{'a':2,'b':1},
1:{'a':3,'b':0},
2:{'a':0,'b':3},
3:{'a':1,'b':2}
}
start = 0
final = [1]
state = start
for elem in string: state = machine[state][elem]
if state in final: return True
else: return False
def dfa_3(string):
machine = {
0:{'a':5,'b':1},
1:{'a':6,'b':2},
2:{'a':7,'b':3},
3:{'a':8,'b':4},
4:{'a':9,'b':0},10:{'a':0,'b':11},
5:{'a':10,'b':6},11:{'a':1,'b':12},
6:{'a':11,'b':7},12:{'a':2,'b':13},
7:{'a':12,'b':8},13:{'a':3,'b':14},
8:{'a':13,'b':9},14:{'a':4,'b':10},
9:{'a':14,'b':5}
}
start = 0
final = [3,7,11]
state = start
for elem in string: state = machine[state][elem]
if state in final: return True
else: return False
def dfa_4(string):
machine = {
0:{'a':1,'b':0},
1:{'a':2,'b':0},
2:{'a':3,'b':6},
3:{'a':6,'b':4},
4:{'a':6,'b':5},
5:{'a':5,'b':5},
6:{'a':6,'b':6}
}
start = 0
final = [5]
state = start
for elem in string: state = machine[state][elem]
if state in final: return True
else: return False
print('Now we are testing a DFA that accepts any string.')
print('abab',dfa_anystring('abab'))
print('Now we are testing a DFA that accepts strings with')
print('even number of \'a\'s and odd number of \'b\'s')
print('aba',dfa_2('aba'))
print('abb',dfa_2('abb'))
print('Now we are testing a DFA that accepts strings where')
print('|a| mod 3 + |b| mod 5 = 3, where |k| means number of \'k\'s')
print('ababababababbb',dfa_3('ababababababbb'))
print('abababb',dfa_3('abababb'))
print('Now we are testing a DFA that accepts strings')
print('in the regular language b*a(bb*a)*aabb(a+b)*')
print('ababaaabbbaba',dfa_4('ababaaabbbaba'))
print('abababba',dfa_4('abababba'))