mirror of
https://github.com/Fare-spec/cours.git
synced 2025-12-07 02:40:38 +00:00
Auto urgent commit.
This commit is contained in:
62
turing/machine.py
Normal file
62
turing/machine.py
Normal file
@@ -0,0 +1,62 @@
|
||||
class Tape:
|
||||
def __init__(self, input_str: str):
|
||||
self.tape = list(input_str)
|
||||
self.head = 0
|
||||
|
||||
def read(self):
|
||||
if self.head < 0:
|
||||
self.tape = ['•'] * (-self.head) + self.tape
|
||||
self.head = 0
|
||||
elif self.head >= len(self.tape):
|
||||
self.tape += ['•'] * (self.head - len(self.tape) + 1)
|
||||
return self.tape[self.head]
|
||||
|
||||
def write(self, symbol):
|
||||
self.tape[self.head] = symbol
|
||||
|
||||
def move(self, direction):
|
||||
if direction == 'g':
|
||||
self.head -= 1
|
||||
elif direction == 'd':
|
||||
self.head += 1
|
||||
|
||||
def __str__(self):
|
||||
tape_str = ''.join(self.tape)
|
||||
pointer = ' ' * self.head + '^'
|
||||
return f'{tape_str}\n{pointer}'
|
||||
|
||||
|
||||
class TuringMachine:
|
||||
def __init__(self, transitions, initial_state, final_state):
|
||||
self.transitions = transitions
|
||||
self.state = initial_state
|
||||
self.final_state = final_state
|
||||
|
||||
def run(self, input_str):
|
||||
tape = Tape(input_str)
|
||||
steps = 0
|
||||
while self.state != self.final_state:
|
||||
current_symbol = tape.read()
|
||||
if self.state not in self.transitions or current_symbol not in self.transitions[self.state]:
|
||||
print("No transition rule found. Halting.")
|
||||
break
|
||||
write_symbol, direction, next_state = self.transitions[self.state][current_symbol]
|
||||
tape.write(write_symbol)
|
||||
tape.move(direction)
|
||||
self.state = next_state
|
||||
steps += 1
|
||||
print(f"Étape {steps} - État: {self.state}")
|
||||
print(tape)
|
||||
print("Machine arrêtée.")
|
||||
|
||||
|
||||
transitions = {
|
||||
'init': {
|
||||
'1': ('1', 'd', 'init'),
|
||||
'0': ('0', 'd', 'init'),
|
||||
'•': ('1', 'g', 'end') # ajoute un 1 à la fin
|
||||
}
|
||||
}
|
||||
tm = TuringMachine(transitions, initial_state='init', final_state='end')
|
||||
tm.run("1011")
|
||||
|
||||
17
turing/mc.py
Normal file
17
turing/mc.py
Normal file
@@ -0,0 +1,17 @@
|
||||
import Tabidir, exemples
|
||||
class Configuration:
|
||||
def __init__(self, ruban,position, etat_courant):
|
||||
self.ruban = ruban
|
||||
self.position = position
|
||||
self.etat_courant = etat_courant
|
||||
|
||||
def _str__(self):
|
||||
chaine = ' '
|
||||
chaine = chaine +(self.position-ruban.imin())*2*' '+self.etat_courant
|
||||
chaine = chaine + ' '+ (self.position+ruban.imin()*2*' '+ '|'+'\n')
|
||||
chaine = chaine+slef.ruban.__str__()
|
||||
return chaine
|
||||
|
||||
if __name__ == "__main__":
|
||||
ruban = Ta
|
||||
|
||||
Reference in New Issue
Block a user