Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def main():
- end_s = '_'
- A = ('a', 'b', end_s, 'A')
- to_replace ={'q1': ('q1a', 'q1b','q1' + end_s, 'q1A',),
- 'q2': ('q2a', 'q2b','q2' + end_s, 'q2A',),
- 'q3': ('q3a', 'q3b','q3' + end_s, 'q3A',),
- 'q4': ('q4a', 'q4b','q4' + end_s, 'q4A',),
- 'q5': ('q5a', 'q5b','q5' + end_s, 'q5A',),
- 'q6': ('q6a', 'q6b','q6' + end_s, 'q6A',),
- 'q7': ('q7a', 'q7b','q7' + end_s, 'q7A',)
- }
- new_state = {'q1': ('q2', 'q3', 'q0', 'q'),
- 'q2': ('q2', 'q2', 'q4', 'q'),
- 'q3': ('q3', 'q3', 'q5', 'q'),
- 'q4': ('q0', 'q6', 'q', 'q'),
- 'q5': ('q7', 'q0', 'q', 'q'),
- 'q6': ('q6', 'q6', 'q', 'q0'),
- 'q7': ('q7', 'q7', 'q', 'q0')
- }
- replacement={'q1': ('A', 'A', end_s, 'q'),
- 'q2': ('a', 'b', end_s, 'q'),
- 'q3': ('a', 'b', end_s, 'q'),
- 'q4': ('a', 'a', 'q', 'q'),
- 'q5': ('b', 'b', 'q', 'q'),
- 'q6': ('a', 'b', 'q', 'b'),
- 'q7': ('a', 'b', 'q', 'a')
- }
- move = { 'q1': ('L', 'L', 'H', 'q'),
- 'q2': ('L', 'L', 'R', 'q'),
- 'q3': ('L', 'L', 'R', 'q'),
- 'q4': ('H', 'R', 'q', 'q'),
- 'q5': ('H', 'R', 'q', 'q'),
- 'q6': ('R', 'R', 'q', 'H'),
- 'q7': ('R', 'R', 'q', 'H')
- }
- end_states = ('q0')
- n = len(move[list((move.keys()))[0]])
- start_state = 'q1'
- word = end_s + start_state + input() + 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