Advertisement
Dantenerosas

Turing Machine

Apr 25th, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.92 KB | None | 0 0
  1. def main():
  2.     end_s = '_'
  3.     A = ('м', 'н', 'о', 'п', 'р', 'е', end_s)
  4.     # Словари с правилами. Первый словарь - левая часть правила
  5.     # Оставшиеся 3 - правая часть разбитая следующим образом
  6.     # new_state - следующее состояние
  7.     # replacement - замена текущего символа
  8.     # move - куда сдвигать ленту. L - влево, R - вправо, H - стоп
  9.     # Здесь кратенько, что такое сосбтвенно словарь
  10.     # http://pythonworld.ru/tipy-dannyx-v-python/slovari-dict-funkcii-i-metody-slovarej.html
  11.     to_replace ={'q2': ('q2м', 'q2н', 'q2о', 'q2п', 'q2р', 'q2е', 'q2' + end_s),
  12.                  'q3': ('q3м', 'q3н', 'q3о', 'q3п', 'q3р', 'q3е', 'q3' + end_s),
  13.                  'q1': ''}
  14.     new_state = {'q2': ('q2', 'q2', 'q3', 'q2', 'q2', 'q3', 'q0'),
  15.                  'q3': ('q0', 'q3', 'q1', 'q3', 'q3', 'q1', 'q0')}
  16.     replacement = {'q2': ('м','н','о', 'п', 'р', 'е', end_s),
  17.                    'q3': ('м','н','о', 'п', 'р', 'е', end_s)}
  18.     move = {'q2': ('L', 'L', 'L', 'L', 'L', 'L', 'H'),
  19.             'q3': ('L', 'L', 'H', 'L', 'L', 'L', 'H')}
  20.     # Кортеж заключительных состояний
  21.     end_states = ('q0', 'q1')
  22.     n = len(move[list((move.keys()))[0]])
  23.     start_state = 'q2'
  24.     word = end_s + start_state + 'моомо' + end_s
  25.     if (not set(word[3:]) - set(A)) == False:
  26.         return
  27.     print(word[1:len(word) - 1])
  28.     current = word[1:3]
  29.     while True:
  30.         for i in range(n):
  31.             sub_word = to_replace[current][i]
  32.             if word.find(sub_word) != -1:
  33.                 if move[current][i] == 'L':
  34.                     new_sub_word= replacement[current][i]+ new_state[current][i]
  35.                 elif move[current][i] == 'R':
  36.                     pos_prev = word.find(sub_word) - 1
  37.                     sub_word = word[pos_prev] + sub_word
  38.                     new_sub_word = new_state[current][i] + word[pos_prev] +\
  39.                                    replacement[current][i]
  40.                 elif move[current][i] == 'H':
  41.                     new_sub_word= new_state[current][i]+ replacement[current][i]
  42.                 print(to_replace[current][i],'->',new_sub_word
  43.                           + move[current][i],end='')
  44.                 word = word.replace(sub_word, new_sub_word, 1)
  45.                 if word[-3:].find('q') == 0:
  46.                     if word[-3:].find('_') == 2:
  47.                         print(' |',word[1:])
  48.                 elif word[0] == '_':
  49.                     print(' |',word[1:len(word) - 1])
  50.                 else:
  51.                     print(' |',word[:len(word) - 1])
  52.                 current = new_state[current][i]
  53.                 break
  54.         if current in end_states:
  55.             break
  56.  
  57. if __name__ == '__main__':
  58.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement