-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTuringMachine.py
72 lines (53 loc) · 2.18 KB
/
TuringMachine.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
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 15 22:17:05 2021
@author: Remy
"""
# alphabet is list of strings
# strip is list of elements of alphabet
# brain is list of states
# state is list of instructions in order of alphabet
# instruction goes [thing to write, where to move, what next]
# thing to write is index of alphabet
# where to move is -1, 0 or +1
# what next is index of next state
def SolveDemo(brain, strip, alphabet, endstate, pos = 1, state = 0) :
while state != endstate :
i = 0
while strip[pos] != alphabet[i] :
i += 1
strip[pos] = alphabet[brain[state][i][0]]
pos += brain[state][i][1]
state = brain[state][i][2]
print("Strip : " + str(strip) + " ; State : " + str(state) + " ; Position : " + str(pos))
return "end"
def Solve(brain, strip, alphabet, endstate, pos = 1, state = 0) :
for i in range(len(strip)) :
j = 0
while strip[i] != alphabet[j] :
j += 1
strip[i] = j
while state != endstate :
instruction = strip[pos]
strip[pos] = brain[state][instruction][0]
pos += brain[state][instruction][1]
state = brain[state][instruction][2]
return strip
alphabet = ["a", "b", "A", "B", "null"]
strip = ["null", "a", "a", "b", "b", "null"] # exo 3.1 du TD
brain3 = [[[2,1,1],[],[],[3,1,0],[4,0,3]],[[0,1,1],[3,-1,2],[],[3,1,1],[]],[[0,-1,2],[],[2,1,0],[3,-1,2],[]]]
print(SolveDemo(brain3, strip, alphabet, 3))
alphabet = ["0", "1", "null"]
strip = ["null", "0", "0", "0", "0", "null"] # exo 4 du TD
brain4 = [[[1,1,1],[1,1,0],[]],[[0,1,4],[1,1,1],[2,1,2]],[],[[0,-1,3],[1,-1,3],[2,1,0]],[[1,1,5],[1,1,4],[2,-1,3]],[[0,1,4],[1,1,5],[]]]
print(SolveDemo(brain4, strip, alphabet, 2))
# Soit un ensemble fini A.
# Soit un ensemble fini S.
# Soit B l ensemble des fonctions injectives de Z dans A
# Soit M = {p, s, n}
# p : x -> x - 1
# Soit la fonction w : B * A * P -> B
# (b1, a, p) -> b2 : Z -> A
# x -> { b1(x) si x != p
# { a si x = p
# Une fonction t : A * S -> A * M * S