-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadder.py
45 lines (34 loc) · 1.34 KB
/
adder.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
# -*- coding: utf-8 -*-
"""
Test script for running a Turing machine unary adder
Usage: python adder.py input-string
Created on Fri Mar 29 21:57:42 2019
@author: shakes
"""
from turing_machine import TuringMachine
import sys
def main(input):
#create the Turing machine
adder = TuringMachine(
{
# if we read a '0' from the input string, halt and accept
("rd", "0"): ("qa", "0", "R"),
# if we read a '1' from the input string, begin moving right
("rd", "1"): ("mv_r", "x", "R"),
# if moving right and we read a '0' or '1', continue moving right
("mv_r", "0"): ("mv_r", "0", "R"),
("mv_r", "1"): ("mv_r", "1", "R"),
# if moving right and we read a blank symbol, write a 1 and begin moving left
("mv_r", "b"): ("mv_l", "1", "L"),
# if moving left and we read a '0' or '1', keep moving left
("mv_l", "0"): ("mv_l", "0", "L"),
("mv_l", "1"): ("mv_l", "1", "L"),
# if moving left and we read an 'x', move right and read from the input string again
("mv_l", "x"): ("rd", "x", "R")
},
start_state = "rd", # rd represents read from input string
blank_symbol = "b"
)
adder.debug(input)
if __name__ == "__main__":
main(sys.argv[1])