-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturingmachine.py
59 lines (44 loc) · 1.11 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
transitions = [[1,5,4,5,3],
[1,2,5,5,1],
[2,5,5,0,2],
[5,5,4,5,3],
[4,4,4,4,4],
[5,5,5,5,5]]
symbols = ["0","1"," ","x","y"]
pointer = [[1,1,0,1,1], [1,0,1,1,1],
[0,1,1,1,0],
[1,1,0,1,1],
[1,1,1,1,1],
[1,1,1,1,1]]
replace = [[3,3,3,3,4], [0,4,3,3,4],
[0,3,3,3,4],
[0,1,2,3,4],
[0,1,2,3,4],
[0,1,2,3,4]]
state = 0
s = input("enter the string: ")
s = list(s + " ")
acc = 4
dec = 5
tape = s
head = 0
i = 20
print("Tape: ", end="")
print(tape)
print("State = "+str(state) + ", Head = "+ str(head))
while state < 4:
c = state
symbol = symbols.index(tape[head]) # get index of symbol
tape[head] = symbols[replace[c][symbol]]
state = transitions[c][symbol]
if pointer[c][symbol] == 0:
head -= 1
else:
head += 1
print("Tape: ", end="")
print(tape)
print("State = "+str(state) + ", Head = "+ str(head))
if state == 4:
print("the given string passes")
else:
print("the given string does not pass")