fork download
  1. from turing_machine import TuringMachine
  2.  
  3. tm = TuringMachine(
  4. states={'right', 'carry', 'left', 'done'},
  5. symbols={'0', '1', ' '},
  6. blank_symbol=' ',
  7. input_symbols={'0', '1'},
  8. initial_state='right',
  9. accepting_states={'done'},
  10. transitions={
  11. # scan to the rightmost digit
  12. 'right': {
  13. '1': ('right', 'R'),
  14. '0': ('right', 'R'),
  15. ' ': ('carry', 'L')
  16. },
  17. # then carry the 1
  18. 'carry': {
  19. '1': ('carry', 'L'),
  20. '0': ('carry', 'L'),
  21. ' ': ('done', 'L')
  22. },
  23. # halt on the leftmost digit
  24. 'left': {
  25. '1': ('left', 'L'),
  26. '0': ('left', 'L'),
  27. ' ': ('left', 'L')
  28. },
  29. # done
  30. 'done': {}
  31. }
  32. )
  33.  
  34. # Test the machine with input '1011 1101'
  35. output = tm('1011 1101')
  36. print(output)
  37.  
Success #stdin #stdout #stderr 0.02s 6912KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/gtaoGK/prog:36:13: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit