Exemplo n.º 1
0
                ('q0', '1'): ('q1', 'y', 'R'),
                ('q1', '1'): ('q1', '1', 'R'),
                ('q1', '_'): ('q2', '_', 'L'),
                ('q2', '1'): ('q3', '_', 'L'),
                ('q3', '1'): ('q3', '1', 'L'),
                ('q3', 'y'): ('q0', 'y', 'R'),
                ('q0', '_'): ('q4', '_', 'R'),
            }

            divide_turing_machine = TuringMachine(
                tape_for_divide, initial_state_for_divide,
                final_states_for_divide, transition_function_for_divide)

            while not divide_turing_machine.final():
                divide_turing_machine.step()
            input_machine_string = divide_turing_machine.get_tape()
            print(len(input_machine_string), end=' ')

        else:
            tape_for_multiply = input_machine_string + 's111'
            initial_state_multiply = 'q0'
            final_states_multiply = {'q12'}

            transition_function_multiply = {
                ('q0', '1'): ('q0', '1', 'R'),
                ('q0', 's'): ('q1', 's', 'R'),
                ('q1', '1'): ('q1', '1', 'R'),
                ('q1', '_'): ('q2', 's', 'L'),
                ('q2', '1'): ('q2', '1', 'L'),
                ('q2', 's'): ('q3', 's', 'R'),
                ('q3', 'x'): ('q3', 'x', 'R'),
Exemplo n.º 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())
Exemplo n.º 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")
Exemplo n.º 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())