Exemple #1
0
    ('q0', '#'): ('End', '#', 'R'),
    ('End', ''): ('qa', '', 'R'),
    ('q0', '0'): ('FindDelimiter0', 'X', 'R'),
    ('FindDelimiter0', '#'): ('Check0', '#', 'R'),
    ('Check0', '0'): ('FindLeftmost', 'X', 'L'),
    ('q0', '1'): ('FindDelimiter1', 'X', 'R'),
    ('FindDelimiter1', '#'): ('Check1', '#', 'R'),
    ('Check1', '1'): ('FindLeftmost', 'X', 'L'),
    ('FindLeftmost', '0'): ('FindLeftmost', '0', 'L'),
    ('FindLeftmost', '1'): ('FindLeftmost', '1', 'L'),
    ('FindLeftmost', 'X'): ('FindLeftmost', 'X', 'L'),
    ('FindLeftmost', '#'): ('FindLeftmost', '#', 'L'),
    ('FindLeftmost', ''): ('FindNext', '', 'R'),
    ('FindNext', 'X'): ('FindNext', 'X', 'R'),
    ('FindNext', '0'): ('FindDelimiter0', 'X', 'R'),
    ('FindNext', '1'): ('FindDelimiter1', 'X', 'R'),
    ('FindNext', '#'): ('End', '#', 'R'),
    ('FindDelimiter0', '0'): ('FindDelimiter0', '0', 'R'),
    ('FindDelimiter0', '1'): ('FindDelimiter0', '1', 'R'),
    ('FindDelimiter1', '0'): ('FindDelimiter1', '0', 'R'),
    ('FindDelimiter1', '1'): ('FindDelimiter1', '1', 'R'),
    ('Check0', 'X'): ('Check0', 'X', 'R'),
    ('Check1', 'X'): ('Check1', 'X', 'R'),
    ('End', 'X'): ('End', 'X', 'R')
})

w = "1#1#00#100#100"  #try some strings here to find out what the machine accepts and rejects
print("Input:", w)
machine.debug(w)
print("Accepted?", machine.accepts(w))
# -*- coding: utf-8 -*-
"""
Test script for running a Turing machine unary adder

Created on Fri Mar 29 21:57:42 2019

@author: shakes
"""
from turing_machine import TuringMachine

#create the Turing machine
adder = TuringMachine({
    #Write your transition rules here as entries to a Python dictionary
    #For example, the key will be a pair (state, character)
    #The value will be the triple (next state, character to write, move head L or R)
    #such as ('q0', '1'): ('q1', '0', 'R'), which says if current state is q0 and 1 encountered
    #then transition to state q1, write a 0 and move head right.
    ('q0', '1'): ('q0', '1', 'R'),
    ('q0', '0'): ('q0', '', 'R'),
    ('q0', ''): ('qa', '', 'R')
})

print("Input:", '110111')
print("Accepted?", adder.accepts('110111'))
adder.debug('110111')

print("Input:", '11101111')
print("Accepted?", adder.accepts('11101111'))

adder.debug('11101111')
Exemple #3
0
    ('FindNext', '0'): ('FindDelimiter0', 'X', 'R'),
    ('FindNext', '1'): ('FindDelimiter1', 'X', 'R'),
    ('FindNext', '#'): ('End', '#', 'R'),

    ('End', 'X'): ('End', 'X', 'R')
}

if __name__ == "__main__":
    print_transitions(transitions)

    machine = TuringMachine(transitions)

    def run(input_):
        w = input_
        print("Input:",w)
        print("Accepted" if machine.accepts(w) else "Rejected")
        machine.debug(w)
        print()

    # ACCEPTS
    run("#")

    # REJECTS - the first character is a 0
    # and the character at the end of the 1s is a 1
    run("0000#XXXX1")

    # REJECTS - it doesn't what character you have after tht first character
    # This flips the machine into  FindDelimiter0 and Check0 chain
    run("0111#XXXX1")

    # REJECTS - it doesn't what character you have after tht first character
        ('FindLeftmost', 'X'): ('FindLeftmost', 'X', 'L'),
        ('FindLeftmost', '#'): ('FindLeftmost', '#', 'L'),
        ('FindLeftmost', ''): ('FindNext', '', 'R'),

        ('FindNext', 'X'): ('FindNext', 'X', 'R'),
        ('FindNext', '0'): ('FindDelimiter0', 'X', 'R'),
        ('FindNext', '1'): ('FindDelimiter1', 'X', 'R'),
        ('FindNext', '#'): ('End', '#', 'R'),

        ('FindDelimiter0', '0'): ('FindDelimiter0', '0', 'R'),
        ('FindDelimiter0', '1'): ('FindDelimiter0', '1', 'R'),
        ('FindDelimiter1', '0'): ('FindDelimiter1', '0', 'R'),
        ('FindDelimiter1', '1'): ('FindDelimiter1', '1', 'R'),

        ('Check0', 'X'): ('Check0', 'X', 'R'),
        ('Check1', 'X'): ('Check1', 'X', 'R'),

        ('End', 'X'): ('End', 'X', 'R')
    }
)


assert w_hash_w.accepts('#')
assert w_hash_w.accepts('1#1')


assert w_hash_w.rejects('##')


w_hash_w.debug('10#10')