-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfa.py
210 lines (183 loc) · 5.14 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import sys
# La roche-posay
# Main Program
psi = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
pi = ['.']
phi = ['@']
# [x for x in original_list if x not in excluded_elements]
# Trap state (Q10) for non-specified states
# def q10(string):
# print(f'{string[0]}: Q10')
# State Q9
def q9(string):
if not len(string):
print("Success")
# . -> Q5
elif len(string)>1 and string[0] in pi:
print(f'{string[0]}: Q5')
string = string[1:]
q5(string)
# E -> Q4
elif len(string)>1 and string[0] in psi:
print(f'{string[0]}: Q4')
string = string[1:]
q4(string)
else:
string = string[1:]
for i in string:
print(f'{string[0]}: Q10')
print("Alphabet is in trap state")
# State Q8
def q8(string):
if not len(string):
print("Success")
# . -> Q5
elif len(string)>1 and string[0] in pi:
print(f'{string[0]}: Q5')
string = string[1:]
q5(string)
# E -> Q4
elif len(string)>1 and string[0] in psi:
print(f'{string[0]}: Q4')
string = string[1:]
q4(string)
else:
string = string[1:]
for i in string:
print(f'{string[0]}: Q10')
print("Alphabet is in trap state")
# State Q7
def q7(string):
# . -> Q5
if len(string)>1 and string[0] in pi:
print(f'{string[0]}: Q5')
string = string[1:]
q5(string)
# E-{v} -> Q4
elif len(string)>1 and string[0] in [x for x in psi if x not in ['v']]:
print(f'{string[0]}: Q4')
string = string[1:]
q4(string)
# v -> Q9
elif string[0] == 'v':
print(f'{string[0]}: Q9')
string = string[1:]
q9(string)
else:
string = string[1:]
for i in string:
print(f'{string[0]}: Q10')
print("Alphabet is in trap state")
# State Q6
def q6(string):
# o -> Q7
if len(string)>1 and string[0] == 'o':
print(f'{string[0]}: Q7')
string = string[1:]
q7(string)
# r -> Q8, gr - FINALE
elif string[0] == 'r':
print(f'{string[0]}: Q8')
string = string[1:]
q8(string)
# E-{o,r} -> Q4
elif len(string)>1 and string[0] in [x for x in psi if x not in ['o', 'r']]:
print(f'{string[0]}: Q4')
string = string[1:]
q4(string)
# . -> Q5
elif len(string)>1 and string[0] in pi:
print(f'{string[0]}: Q5')
string = string[1:]
q5(string)
else:
string = string[1:]
for i in string:
print(f'{string[0]}: Q10')
print("Alphabet is in trap state")
# State Q5
def q5(string):
# g -> Q6
if len(string)>1 and string[0] == 'g':
print(f'{string[0]}: Q6')
string = string[1:]
q6(string)
# E-{g} -> Q4
elif len(string)>1 and string[0] in [x for x in psi if x not in ['g']]:
print(f'{string[0]}: Q4')
string = string[1:]
q4(string)
else:
string = string[1:]
for i in string:
print(f'{string[0]}: Q10')
print("Alphabet is in trap state")
# State Q4
def q4(string):
# . -> Q5
if len(string)>1 and string[0] in pi:
print(f'{string[0]}: Q5')
string = string[1:]
q5(string)
# E -> Q4
elif len(string)>1 and string[0] in psi:
print(f'{string[0]}: Q4')
string = string[1:]
q4(string)
else:
string = string[1:]
for i in string:
print(f'{string[0]}: Q10')
print("Alphabet is in trap state")
# State Q3
def q3(string):
# E -> Q3
if len(string)>1 and string[0] in psi:
print(f'{string[0]}: Q3')
string = string[1:]
q4(string)
else:
string = string[1:]
for i in string:
print(f'{string[0]}: Q10')
print("Alphabet is in trap state")
# State Q2
def q2(string):
# E -> Q2
if len(string)>1 and string[0] in psi:
print(f'{string[0]}: Q2')
string = string[1:]
q2(string)
# . -> Q1
elif len(string)>1 and string[0] in pi:
print(f'{string[0]}: Q1')
string = string[1:]
q1(string)
# @ -> Q3
elif len(string)>1 and string[0] in phi:
print(f'{string[0]}: Q3')
string = string[1:]
q3(string)
else:
string = string[1:]
for i in string:
print(f'{string[0]}: Q10')
print("Alphabet is in trap state")
# State Q1
def q1(string):
# E -> Q2
print(f"'{string[0]}' started on State Q1\n")
if len(string)>1 and string[0] in psi:
print(f'{string[0]}: Q2')
string = string[1:]
q2(string)
else:
string = string[1:]
for i in string:
print(f'{string[0]}: Q10')
print("Alphabet is in trap state")
n = int(input("Please enter number of iterations: "))
# This block of code iterates through every letter in the string and runs them through the state machine
for i in range(n):
str_ = input(f'\nEnter string {i+1} of {n}: ')
q1(str_)