fork download
from turing_machine import TuringMachine

tm = TuringMachine(
    states={'right', 'carry', 'left', 'done'},
    symbols={'0', '1', ' '},
    blank_symbol=' ',
    input_symbols={'0', '1'},
    initial_state='right',
    accepting_states={'done'},
    transitions={
        # scan to the rightmost digit
        'right': {
            '1': ('right', 'R'),
            '0': ('right', 'R'),
            ' ': ('carry', 'L')
        },
        # then carry the 1
        'carry': {
            '1': ('carry', 'L'),
            '0': ('carry', 'L'),
            ' ': ('done', 'L')
        },
        # halt on the leftmost digit
        'left': {
            '1': ('left', 'L'),
            '0': ('left', 'L'),
            ' ': ('left', 'L')
        },
        # done
        'done': {}
    }
)

# Test the machine with input '1011 1101'
output = tm('1011 1101')
print(output)
Success #stdin #stdout #stderr 0.02s 6912KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/gtaoGK/prog:36:13: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit