-
Notifications
You must be signed in to change notification settings - Fork 0
/
machine_turning.py
132 lines (116 loc) · 3.11 KB
/
machine_turning.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
from stringcolor import *
head=0
tape=list()
def push_to_tape(string):
l=[i for i in string]
tape=l
return tape
def check_list(tape):
l1=len(tape)
if tape[0]==" ":
return False
elif tape[0]=="b":
return False
elif tape[0]=='c':
return False
elif tape[l1-1]!='c':
return False
else:
return True
def states(tape,head,header):
a_str='''
(a,a,R)
^
^
'''
b_str='''
(b,b,R)
^
^
'''
c_str='''
(c,c,R)
^
^
'''
a_counter=0
b_counter=0
if tape[head]=='a':
print(cs(a_str,"green"))
print("=========> (a,a,R) moving to next state")
header="a,a,r"
return 'a'
elif tape[head]=='b':
print(cs(b_str,"red"))
print("=========>(b,b,R) moving to the next state")
header="b,b,r"
return 'b'
elif tape[head]=='c':
print(cs(c_str,"orange"))
print(" =========>(c,c,R) moving to next state")
return 'c'
def turing(string):
stack=list()
head=''
a_counter=0
b_counter=0
c_counter=0
stack_counter=0
tape=push_to_tape(string)
i=0
if check_list(tape):
while len(tape)>i:
l=states(tape,i,head)
if l=='a':
stack.append('a')
a_counter+=1
stack_counter+=1
elif l=='b':
if stack[stack_counter-1]=='a' or stack[stack_counter-1]=='b' :
stack.append('b')
b_counter+=1
stack_counter+=1
else:
print("reject the c most be at the end of string")
return False
elif l=='c':
stack.append("c")
c_counter+=1
i+=1
if a_counter==b_counter and c_counter>=1:
return True
else:
return False
# print(a_counter)
# print(b_counter)
# print(c_counter)
# print(stack_counter)
def accept_state(string):
final_check=turing(string)
accept_str='''
ACCEPTED
############################
############################
# The STRING is accept######
############################ '''
reject_str='''
REJECTED
###########################
###########################
# The STRING is Rejected####
############################ '''
if final_check:
print(cs("======> (Δ,Δ,R)----->Final state","DarkRed2"))
print(cs(accept_str,"yellow").bold())
else:
print(cs(reject_str,"yellow").bold())
if __name__=="__main__":
print(cs(" ENTER THE STRING ","DarkRed2").bold())
string =input()
print("============")
print(cs("TAPE","Maroon").bold())
print("=============")
l=[i for i in string]
print(cs(l,"DarkRed2").bold())
print("#########")
accept_state(string)