From f2ae2cbc135e101133a2814fe673769ea24054f6 Mon Sep 17 00:00:00 2001 From: Spectre Date: Mon, 31 Mar 2025 09:50:06 +0200 Subject: [PATCH] Auto urgent commit. --- turing/machine.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++ turing/mc.py | 17 +++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 turing/machine.py create mode 100644 turing/mc.py diff --git a/turing/machine.py b/turing/machine.py new file mode 100644 index 0000000..fa64663 --- /dev/null +++ b/turing/machine.py @@ -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") + diff --git a/turing/mc.py b/turing/mc.py new file mode 100644 index 0000000..025731a --- /dev/null +++ b/turing/mc.py @@ -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 +