-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturing.py
50 lines (41 loc) · 1.52 KB
/
turing.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
# =============================================================================
# Created By : Dominique Zeise
# GitHub : https://github.com/CharliesCodes
# Created Date: 2021/11/11
# Version : 1.0
# © Copyright : 2021 Dominique Zeise
# =============================================================================
'''The Module is a Turing machine with excel spreadsheets for instructions'''
# =============================================================================
def main():
import pandas as pd
# Enter spreadsheet name E.g. "Multiplication", "Invert" or "Addition"
instructions = "Invert"
df = pd.read_excel('instructions.xlsx', header=0,
sheet_name=instructions).astype("string")
tape = list("#110101#")
run_machine(df, tape)
def run_machine(df, tape):
index, state, run = 0, 0, True
while run:
symbol = tape[index]
current_row = df.loc[
(df.State == str(state)) & (df.Symbol == str(symbol))]
state = current_row.State_new.values[0]
tape[index] = current_row.Symbol_new.values[0]
move = current_row.Move.values[0]
if move == "r":
if index == len(tape) - 1:
tape.append("#")
index += 1
elif move == "l":
if index == 0:
tape.insert(0, "#")
index -= 1
else:
run = False
break
print(tape)
print(f"End State reached.\nResult: {''.join(tape)}")
if __name__ == '__main__':
main()