-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP1_1_9731019.py
64 lines (51 loc) · 1.41 KB
/
P1_1_9731019.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
f = open("DFA_input_1.txt", "r")
lines = []
# at first, we read the dfa from the text file
while True:
line = f.readline()
if line == '':
break
lines.append(line)
print(* lines)
n = len(lines[0].split())
# print(n)
dfa = {}
i = 4
# implementing the dfa wih dictionary to dictionary data structure
while i < len(lines):
d = {}
for j in range(n):
x = lines[i + j].split()
# print(i , "," ,j)
d.update({x[1]: x[2]})
temp = x[0]
x = lines[i].split()
dfa[x[0]] = d
i = i + 2
print("the dfa is:")
print(dfa)
def next_state(current, char): # this function returns the next state
di = dfa[current]
# print(di[char])
return di[char]
def check_input(string):
allowed_chars = set(lines[0])
if set(string).issubset(allowed_chars):
return True
else:
return False
# here we check if the string which we enter is accepted by the dfa or not
print("please enter the string :")
string = input()
state = lines[2].rstrip("\n")
if check_input(string) :
for ch in string:
state = next_state(state, ch)
print("the last state of the string is: " + state)
if state == lines[3].rstrip("\n"):
print("the string is accepted by the dfa")
else:
print("the string is not accepted by the dfa")
else:
print("the string is not valid")
f.close()