Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def main():
- end_s = '_'
- A = ('м', 'н', 'о', 'п', 'р', 'е', end_s)
- # Словари с правилами. Первый словарь - левая часть правила
- # Оставшиеся 3 - правая часть разбитая следующим образом
- # new_state - следующее состояние
- # replacement - замена текущего символа
- # move - куда сдвигать ленту. L - влево, R - вправо, H - стоп
- # Здесь кратенько, что такое сосбтвенно словарь
- # http://pythonworld.ru/tipy-dannyx-v-python/slovari-dict-funkcii-i-metody-slovarej.html
- to_replace ={'q2': ('q2м', 'q2н', 'q2о', 'q2п', 'q2р', 'q2е', 'q2' + end_s),
- 'q3': ('q3м', 'q3н', 'q3о', 'q3п', 'q3р', 'q3е', 'q3' + end_s),
- 'q1': ''}
- new_state = {'q2': ('q2', 'q2', 'q3', 'q2', 'q2', 'q3', 'q0'),
- 'q3': ('q0', 'q3', 'q1', 'q3', 'q3', 'q1', 'q0')}
- replacement = {'q2': ('м','н','о', 'п', 'р', 'е', end_s),
- 'q3': ('м','н','о', 'п', 'р', 'е', end_s)}
- move = {'q2': ('L', 'L', 'L', 'L', 'L', 'L', 'H'),
- 'q3': ('L', 'L', 'H', 'L', 'L', 'L', 'H')}
- # Кортеж заключительных состояний
- end_states = ('q0', 'q1')
- n = len(move[list((move.keys()))[0]])
- start_state = 'q2'
- word = end_s + start_state + 'моомо' + end_s
- if (not set(word[3:]) - set(A)) == False:
- return
- print(word[1:len(word) - 1])
- current = word[1:3]
- while True:
- for i in range(n):
- sub_word = to_replace[current][i]
- if word.find(sub_word) != -1:
- if move[current][i] == 'L':
- new_sub_word= replacement[current][i]+ new_state[current][i]
- elif move[current][i] == 'R':
- pos_prev = word.find(sub_word) - 1
- sub_word = word[pos_prev] + sub_word
- new_sub_word = new_state[current][i] + word[pos_prev] +\
- replacement[current][i]
- elif move[current][i] == 'H':
- new_sub_word= new_state[current][i]+ replacement[current][i]
- print(to_replace[current][i],'->',new_sub_word
- + move[current][i],end='')
- word = word.replace(sub_word, new_sub_word, 1)
- if word[-3:].find('q') == 0:
- if word[-3:].find('_') == 2:
- print(' |',word[1:])
- elif word[0] == '_':
- print(' |',word[1:len(word) - 1])
- else:
- print(' |',word[:len(word) - 1])
- current = new_state[current][i]
- break
- if current in end_states:
- break
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement