formatte file

This commit is contained in:
2025-04-01 14:28:43 +02:00
parent f2ae2cbc13
commit e03e5458aa
77 changed files with 1231 additions and 945 deletions

54
turing/bitab.py Normal file
View File

@@ -0,0 +1,54 @@
class Tableau:
def __init__(self, gauche, droite):
self.gauche = gauche
self.droite = droite
def imin(self):
return -len(self.gauche)
def imax(self):
return len(self.droite) - 1
def append(self, elt):
self.droite.append(elt)
def prepend(self, elt):
self.gauche.append(elt)
def __getitem__(self, i):
if i < 0:
try:
return self.gauche[i]
except IndexError:
print(f"erreur lors de l'index")
else:
try:
return self.droite[i]
except IndexError:
print("error")
def __setitem__(self, i, v):
if i < 0:
try:
self.gauche[i] = v
except IndexError:
print("error")
else:
try:
self.droite[i] = v
except IndexError:
print("error")
def __str__(self) -> str:
string = "|"
for i in self.gauche:
string += str(i) + "|"
for i in self.droite:
string += str(i) + "|"
return string
if __name__ == "__main__":
tab = Tableau([1, 2], [3, 4])
tab.prepend(5)
print(tab.__str__())

View File

@@ -5,25 +5,25 @@ class Tape:
def read(self):
if self.head < 0:
self.tape = [''] * (-self.head) + self.tape
self.tape = [""] * (-self.head) + self.tape
self.head = 0
elif self.head >= len(self.tape):
self.tape += [''] * (self.head - len(self.tape) + 1)
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':
if direction == "g":
self.head -= 1
elif direction == 'd':
elif direction == "d":
self.head += 1
def __str__(self):
tape_str = ''.join(self.tape)
pointer = ' ' * self.head + '^'
return f'{tape_str}\n{pointer}'
tape_str = "".join(self.tape)
pointer = " " * self.head + "^"
return f"{tape_str}\n{pointer}"
class TuringMachine:
@@ -37,10 +37,15 @@ class TuringMachine:
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]:
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]
write_symbol, direction, next_state = self.transitions[self.state][
current_symbol
]
tape.write(write_symbol)
tape.move(direction)
self.state = next_state
@@ -51,12 +56,7 @@ class TuringMachine:
transitions = {
'init': {
'1': ('1', 'd', 'init'),
'0': ('0', 'd', 'init'),
'': ('1', 'g', 'end') # ajoute un 1 à la fin
}
"init": {"1": ("1", "d", "init"), "0": ("0", "d", "init"), "": ("1", "g", "end")}
}
tm = TuringMachine(transitions, initial_state='init', final_state='end')
tm = TuringMachine(transitions, initial_state="init", final_state="end")
tm.run("1011")

View File

@@ -15,3 +15,5 @@ class Configuration:
if __name__ == "__main__":
ruban = Ta
class Machine: