-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTuring_Machine.py
More file actions
55 lines (42 loc) · 1.33 KB
/
Copy pathTuring_Machine.py
File metadata and controls
55 lines (42 loc) · 1.33 KB
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
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 2 16:19:13 2021
@author: Arthur Querido Lopes
"""
def turing_machine(transitions, state, acceptance_state, strip, pos):
#verify if state is in acceptance state and stop if it is
if state in acceptance_state:
return True
for transition in transitions:
if state == transition[0] and strip[pos] == transition[1]:
state = transition[2]
strip[pos] = transition[3]
if transition[4] == 'D':
pos = pos+1
elif transition[4] == 'E':
pos = pos-1
if turing_machine(transitions, state, acceptance_state, strip, pos):
return True
break
return False
#Receiving acceptance states from user
acceptance_state = set(input())
#Receiving number of transitions from user
t = int(input())
transitions = []
#Receiving transitions from user
for i in range(t):
trans = input().split()
transitions.append(trans)
#Receiving number of strings to be tested
c = int(input())
#Receiving strings one by one
tape = []
for i in range(c):
tape.append(list(input() + 'B'))
#Testing all strings
for strip in tape:
if (turing_machine(transitions, '0', acceptance_state, strip, 0)):
print("aceita")
else:
print("rejeita")