Beispiel #1
0
        initial_state_for_check_even = 'q0'
        final_states_for_check_even = {'q2'}

        transition_function_for_check_even = {
            ('q0', '1'): ('q1', '_', 'R'),
            ('q1', '1'): ('q0', '_', 'R'),
            ('q1', '_'): ('q3', '_', 'R'),
            ('q0', '_'): ('q2', '_', 'R'),
        }
        # ---------------------------------

        check_even_turing_machine = TuringMachine(
            tape_for_check_even, initial_state_for_check_even,
            final_states_for_check_even, transition_function_for_check_even)
        even_flag = True
        while not check_even_turing_machine.final():
            if time.time() - check_even_turing_machine.time > 0.1:
                even_flag = False
                break
            check_even_turing_machine.step()
        if even_flag:
            tape_for_divide = input_machine_string + '_'
            initial_state_for_divide = 'q0'
            final_states_for_divide = {'q4'}

            transition_function_for_divide = {
                ('q0', '1'): ('q1', 'y', 'R'),
                ('q1', '1'): ('q1', '1', 'R'),
                ('q1', '_'): ('q2', '_', 'L'),
                ('q2', '1'): ('q3', '_', 'L'),
                ('q3', '1'): ('q3', '1', 'L'),
Beispiel #2
0
from turing import TuringMachine

#  Example configuration ----------
tape = '01_'
initial_state = 'q0'
final_states = {'q1'}

transition_function = {
    ('q0', '0'): ('q0', '1', 'R'),
    ('q0', '1'): ('q0', '0', 'R'),
    ('q0', '_'): ('q1', '_', 'N')
}
# ---------------------------------

turing_machine = TuringMachine(tape, initial_state, final_states,
                               transition_function)

print("initial tape: " + turing_machine.get_tape())

while not turing_machine.final():
    turing_machine.step()

print("final tape: " + turing_machine.get_tape())
Beispiel #3
0
        ('q4', 'a'): ('q3', 'x', 'R'),
        ('q3', '_'): ('q5', '_', 'L'),
        ('q5', 'a'): ('q5', 'a', 'L'),
        ('q5', 'x'): ('q5', 'x', 'L'),
        ('q5', '_'): ('q2', '_', 'R'),
        ('q2', '_'): ('q6', '_', 'R'),
        ('q1', '_'): ('q7', '_', 'R'),
        ('q1', 'x'): ('q7', 'x', 'R'),
        ('q4', '_'): ('q7', '_', 'R'),
    }
    # ---------------------------------

    turing_machine = TuringMachine(
        tape,
        initial_state,
        final_states,
        transition_function
    )


    while not turing_machine.final() and time.time() - turing_machine.time < 0.1:
        turing_machine.step()

    accepted_tape = '_'
    for i in range(1, len(input_string)):
        accepted_tape += 'x'

    if turing_machine.get_tape() == accepted_tape:
        print("ALLOWED")
    else:
        print("REJECTED")
Beispiel #4
0
from turing import TuringMachine

initial_state = 'init'

accepting_states = ['final']

transition_function = {
    ('init', '0'): ('init', '1', 'R'),
    ('init', '1'): ('init', '0', 'R'),
    ('init', ' '): ('final', ' ', 'N'),
}
final_states = {'final'}

t = TuringMachine('010011 ',
                  inital_state='init',
                  final_states=final_states,
                  transition_function=transition_function)

print('Input on Tape:\n' + t.get_tape())

while not t.final():
    t.step()
print('Result of Turning Machine Calculation:')
print(t.get_tape())