Advertisement
Dantenerosas

Untitled

May 21st, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. def main():
  2. end_s = '_'
  3. A = ('a', 'b', end_s, 'A')
  4. to_replace ={'q1': ('q1a', 'q1b','q1' + end_s, 'q1A',),
  5. 'q2': ('q2a', 'q2b','q2' + end_s, 'q2A',),
  6. 'q3': ('q3a', 'q3b','q3' + end_s, 'q3A',),
  7. 'q4': ('q4a', 'q4b','q4' + end_s, 'q4A',),
  8. 'q5': ('q5a', 'q5b','q5' + end_s, 'q5A',),
  9. 'q6': ('q6a', 'q6b','q6' + end_s, 'q6A',),
  10. 'q7': ('q7a', 'q7b','q7' + end_s, 'q7A',)
  11. }
  12. new_state = {'q1': ('q2', 'q3', 'q0', 'q'),
  13. 'q2': ('q2', 'q2', 'q4', 'q'),
  14. 'q3': ('q3', 'q3', 'q5', 'q'),
  15. 'q4': ('q0', 'q6', 'q', 'q'),
  16. 'q5': ('q7', 'q0', 'q', 'q'),
  17. 'q6': ('q6', 'q6', 'q', 'q0'),
  18. 'q7': ('q7', 'q7', 'q', 'q0')
  19. }
  20. replacement={'q1': ('A', 'A', end_s, 'q'),
  21. 'q2': ('a', 'b', end_s, 'q'),
  22. 'q3': ('a', 'b', end_s, 'q'),
  23. 'q4': ('a', 'a', 'q', 'q'),
  24. 'q5': ('b', 'b', 'q', 'q'),
  25. 'q6': ('a', 'b', 'q', 'b'),
  26. 'q7': ('a', 'b', 'q', 'a')
  27. }
  28. move = { 'q1': ('L', 'L', 'H', 'q'),
  29. 'q2': ('L', 'L', 'R', 'q'),
  30. 'q3': ('L', 'L', 'R', 'q'),
  31. 'q4': ('H', 'R', 'q', 'q'),
  32. 'q5': ('H', 'R', 'q', 'q'),
  33. 'q6': ('R', 'R', 'q', 'H'),
  34. 'q7': ('R', 'R', 'q', 'H')
  35. }
  36. end_states = ('q0')
  37. n = len(move[list((move.keys()))[0]])
  38. start_state = 'q1'
  39. word = end_s + start_state + input() + end_s
  40. if (not set(word[3:]) - set(A)) == False:
  41. return
  42. print(word[1:len(word) - 1])
  43. current = word[1:3]
  44. while True:
  45. for i in range(n):
  46. sub_word = to_replace[current][i]
  47. if word.find(sub_word) != -1:
  48. if move[current][i] == 'L':
  49. new_sub_word= replacement[current][i]+ new_state[current][i]
  50. elif move[current][i] == 'R':
  51. pos_prev = word.find(sub_word) - 1
  52. sub_word = word[pos_prev] + sub_word
  53. new_sub_word = new_state[current][i] + word[pos_prev] +\
  54. replacement[current][i]
  55. elif move[current][i] == 'H':
  56. new_sub_word= new_state[current][i]+ replacement[current][i]
  57. print(to_replace[current][i],'->',new_sub_word
  58. + move[current][i],end='')
  59. word = word.replace(sub_word, new_sub_word, 1)
  60. if word[-3:].find('q') == 0:
  61. if word[-3:].find('_') == 2:
  62. print(' |',word[1:])
  63. elif word[0] == '_':
  64. print(' |',word[1:len(word) - 1])
  65. else:
  66. print(' |',word[:len(word) - 1])
  67. current = new_state[current][i]
  68. break
  69. if current in end_states:
  70. break
  71.  
  72. if __name__ == '__main__':
  73. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement