-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfa1.py
More file actions
36 lines (31 loc) · 846 Bytes
/
Copy pathdfa1.py
File metadata and controls
36 lines (31 loc) · 846 Bytes
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
# DFA 1 (alphabet {0,1})
def dfa1(word):
state = 0 # start at a
for ch in word:
if state == 0: # a
if ch == "0":
state = 0
elif ch == "1":
state = 1
elif state == 1: # b
if ch == "0":
state = 2
elif ch == "1":
state = 0
elif state == 2: # c (final)
state = 2 # loop back to c
if state == 2:
return True
else:
return False
# MAIN PROGRAM
while True:
print("\n--- DFA 1 SIMULATOR ---")
s = input("Enter string of 0 and 1 (or type exit): ")
if s.lower() == "exit":
print("Program ended.")
break
if dfa1(s):
print("ACCEPTED by DFA 1")
else:
print("REJECTED by DFA 1")