Question: Adjust the code below so that the transitions work as follows: Transactions must follow the pattern below, e . g . ( current state, symbol
Adjust the code below so that the transitions work as follows:
Transactions must follow the pattern below, eg
current state, symbol readnew state, action
the action can be writeread move right or left
transicao
qa: qb
qb: qa
q: h
q: qR
qa: qR
qb: qR
q: qR
q: qR
Here is the code to fix:
class MaquinaTuring:
def initself states, alphabet, tapealphabet, initialstate, blanksymbol, finalstates, transitions, startmarker:
self.states states
self.alphabet alphabet
self.tapealphabet tapealphabet
self.initialstate initialstate
self.blanksymbol blanksymbol
self.finalstates finalstates
self.transitions transitions
self.startmarker startmarker
self.configurations
def runself inputstring, maxtransitions:
self.initialconfiguration selfinitialstate, self.startmarker 'aabbba' self.blanksymbol,
self.configurations.appendselfinitialconfiguration
tape selfstartmarker listinputstringselfblanksymbol
head
state self.initialstate
transitionscount
while state not in self.finalstates:
if transitionscount maxtransitions:
choice inputContinue processing? yesno:
if choice.lower 'yes':
break
symbolunderhead tapehead
if state symbolunderhead not in self.transitions:
printError: No transition defined for current state and symbol."
return
newstate, newsymbol, move self.transitionsstate symbolunderhead
tapehead newsymbol
if move R:
head
elif move L:
head
elif move S and symbolunderhead self.startmarker:
head # Se ler o marcador de incio move para a direita
state newstate
transitionscount
# Armazenar a configurao atual na lista
self.configurations.appendstatejointape head
# Perguntar ao usurio se ele deseja ver todas as configuraes
showconfigurations inputDeseja visualizar todas as configuraes presentes na computao desde o estado inicial at a configurao final? simno:
if showconfigurations.lower 'sim':
for i config in enumerateselfconfigurations:
printfConfigurao i:
printfEstado: config
printfFita: config
printfCabea de leituraescrita na posio: config
print
return state, jointape
# Exemplo de uso:
estados qqq
alfabeto
alfabetofita XY
estadoinicial q
espacobranco
estadofinal q
transicao
q: qXR
q: qR
q: qS
q: qR
q: qR
q: qR
q: qS
estados qq
alfabeto ab
alfabetofita abh
estadoinicial q
espacobranco
estadofinal h
transicao
qa: qbS
qb: qaS
q: hS
q: qR
qa: qaR
qb: qbR
q: qR
q: qR
tm TuringMachineestados alfabeto, alfabetofita, estadoinicial, espacobranco, estadofinal, transicao,
#estadofinal, fitafinal tmrun
estadofinal, fitafinal tmrunaabbba
printEstado final:", estadofinal
printFita final:", fitafinal
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
