Advertisement
Guest User

Universal Machine Python Matti565

a guest
Apr 3rd, 2015
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.80 KB | None | 0 0
  1. class TuringMachine(object):
  2.     def __init__(self, alphabet, states, initial_state, accept_state, initial_tape):
  3.  
  4.         '''
  5.        alphabet is a string of non-whitespace characters. They determine the possible values of the tape
  6.        states is a string of whitespace-separated characters. They are the states that the machine can use
  7.        initial_state is a string that is one element of states. It determines the starting state of the machine
  8.        accept_state is a string that is one element of states. If accept_state gets called, the machine halts
  9.        initial_tape is the starting tape of the machine
  10.        '''
  11.  
  12.         self.alphabet = [e for e in alphabet]
  13.         self.states = states.split()
  14.         self.curr_state = initial_state
  15.         if self.curr_state not in states:
  16.             raise ValueError('initial_state not in states')
  17.         self.accept_state = accept_state
  18.         if self.accept_state not in states:
  19.             raise ValueError('accept_state not in states')
  20.         self.tape = [e for e in initial_tape]
  21.         for e in self.tape:
  22.             if e not in self.alphabet:
  23.                 raise ValueError('tape contains arguments that are not in alphabet')
  24.         self.rules = {}
  25.         self.position = 0
  26.  
  27.     def add_rule(self, rule):
  28.         '''
  29.        rule is string in the format StateBefore SymbolBefore = StateAfter SymbolAfter DirectionToMove
  30.        '''
  31.         rule = rule.split()
  32.         self.rules[(rule[0], rule[1])] = (rule[3],rule[4],rule[5])
  33.  
  34.     def add_rulebook(self, rulebook):
  35.         for e in rulebook.splitlines():
  36.             self.add_rule(e)
  37.  
  38.     def next(self):
  39.         "executes one cycle"
  40.         if self.position >= self.tape.__len__():
  41.             self.tape.append('_')
  42.         rule = self.rules[(self.curr_state, self.tape[self.position])]
  43.         self.curr_state = rule[0]
  44.         self.tape[self.position] = rule[1]
  45.         if rule[2] == '>':
  46.             self.position += 1
  47.         elif rule[2] == '<':
  48.             self.position -= 1
  49.  
  50.     def print(self):
  51.         for e in self.tape:
  52.             print(e,end = '')
  53.         print()
  54.         print('|',end = '')
  55.         for e in range(1, self.tape.__len__()):
  56.             if e == self.position:
  57.                 print('^', end = '')
  58.             else:
  59.                 print(' ', end = '')
  60.         print('',end = '\n')
  61.  
  62.     def execute(self):
  63.         while self.curr_state != self.accept_state:
  64.             self.next()
  65.         self.print()
  66.  
  67. alphabet = input('alphabet: ')
  68. states = input('states: ')
  69. initial_state = input('initial_state: ')
  70. accept_state = input('accept_state: ')
  71. initial_tape = input('initial_tape: ')
  72. t = TuringMachine(alphabet, states, initial_state, accept_state, initial_tape)
  73. for line in iter(input('rulebook', '')):
  74.     t.add_rule(line)
  75. t.execute()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement