-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
170 lines (143 loc) · 6.47 KB
/
main.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
# ANALIZA ALGORITMILOR TEMA 1
# TOMA ANDREI 321CB
import sys
# encode - encodarea masinii Turing
# functie citire masina Turing data ca o encodare
def readtm(encode):
final_states = []
# initializare dictionar pentru citirea masinii turing cu cheile "number of states",
# "final states", "transtions"
TM = {}
TM["number of states"] = encode[0]
# parcurge linia cu stari finale, le adauga intr-o lista si dupa in dictionar
for p in encode[1].split():
final_states.append(p)
TM["final states"] = final_states
# parcurge urmatoarele linii cu tranzitii, le adauga intr-o lista si dupa in dictionar
transitions = []
for transition in encode[2:len(encode)]:
transitions.append(transition)
TM["transitions"] = transitions
return TM
# TM - masina turing retinuta ca dictionar
# configuration - configuratia pe care trebuie sa se faca un pas
# functie care realizeaza un pas in configuratia data pe baza tranzitiilor din Masina Turing
def step(TM, configuration):
# transfomare configuratie intr-o lista pe componente
new_configuration = configuration.split(',')
ok = 0
for transition in TM["transitions"]:
# transformare tranzitie intr-o lista pe componente
new_transition = transition.split(' ')
# verificare stare curenta
if new_configuration[1] == new_transition[0]:
# verificare caracter pe care se afla ciursorul
if new_configuration[2][0] == new_transition[1]:
# s-a gasit tranzitia corecta si urmeaza executarea pasului
# actualizare stare
new_configuration[1] = new_transition[2]
ok = 1
# actualizare configuratie in functie de pozitia urmatoare a cursorului
if new_transition[4] == 'R':
new_configuration[0] += new_transition[3]
if len(new_configuration[2]) == 1:
new_configuration[2] = "#"
else:
new_configuration[2] = new_configuration[2][1:len(new_configuration[2])]
elif new_transition[4] == 'H':
new_configuration[2] = new_transition[3] + new_configuration[2][1:len(new_configuration[2])]
elif new_transition[4] == 'L':
new_configuration[2] = new_transition[3] + new_configuration[2][1:len(new_configuration[2])]
if len(new_configuration[0]) == 1:
new_configuration[2] = new_configuration[0] + new_configuration[2]
new_configuration[0] = "#"
else:
new_configuration[2] = new_configuration[0][len(new_configuration[0]) - 1] + \
new_configuration[2]
new_configuration[0] = new_configuration[0][0:len(new_configuration[0]) - 1]
# returnarea noii configuratii
return '(' + new_configuration[0] + ',' + new_configuration[1] + ',' + new_configuration[2] + ')'
# masina nu reuseasca sa faca un pas pentru configuratia data
if ok == 0:
return "False"
# configurations - stringul de configuratii dat de la input
# functia converteste stringul intr-o lista de configuratii si o returneaza
def convertStringToListStep(configurations):
l = []
for p in configurations.split(") ("):
if p[0] == '(':
l.append(p.replace('(', ''))
elif p[len(p) - 1] == ')':
l.append(p.replace(')', ''))
else:
l.append(p)
return l
# words - cuvintele date la input care trebuie testate daca sunt acceptate de Masina Turing
# functia converteste stringul de cuvinte intr-o lista
def convertStringToListAccept(words):
l = []
for p in words.split(" "):
l.append(p)
return l
# word - cuvant scris normal de la input
# functia returneaza cuvantul scris ca o configuratie cu 3 componente
def convertWordToConfiguration(word):
return "#,0," + word
# TM - Masina Turing retinuta in dictionar
# word - cuvant care trebuie testat daca este acceptat de Masina Turing
def accept(TM, word):
# converteste cuvantul sub forma unei configuratii cu 3 elemente (u, q, v)
configuration = convertWordToConfiguration(word)
# converteste configuratia intr-o noua configuratie sub forma de lista
new_configuration = configuration.split(',')
while 1:
for final_state in TM["final states"]:
# verificare daca s-a ajuns intr-o stare finala
if new_configuration[1] == final_state:
return "True"
configuration = step(TM, configuration)
# daca nu se poate face un pas atunci masina s-a agatat
if (configuration == "False"):
return "False"
# actualizare configuratie
configuration = configuration[1:len(configuration) - 1]
new_configuration = configuration.split(',')
# k - numarul de pasi care trebuie facut sa se verifice daca masina acceota cuvantul
# TM - Masina Turing retinuta in dictionar
# word - cuvantul care este testat daca este acceptat de TM in k pasi
def k_accept(k, TM, word):
steps = 0
configuration = convertWordToConfiguration(word)
new_configuration = configuration.split(',')
while steps <= int(k):
for final_state in TM["final states"]:
# verificare daca este in stare finala
if new_configuration[1] == final_state:
return "True"
configuration = step(TM, configuration)
if (configuration == "False"):
return "False"
# actualizare configuratie
configuration = configuration[1:len(configuration) - 1]
new_configuration = configuration.split(',')
steps += 1
return "False"
if __name__ == '__main__':
# citire input
lines = sys.stdin.read().splitlines()
TM = readtm(lines[2:len(lines)])
# in functie de prima linie apeleaza functia repspectiva
if lines[0] == "step":
configurations = convertStringToListStep(lines[1])
for configuration in configurations:
print(step(TM, configuration), end=" ")
elif lines[0] == "accept":
words = convertStringToListAccept(lines[1])
for word in words:
print(accept(TM, word), end=" ")
elif lines[0] == "k_accept":
k_words = convertStringToListAccept(lines[1])
for k_word in k_words:
word = k_word.split(',')[0]
k = k_word.split(',')[1]
print(k_accept(k, TM, word), end=" ")