GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||
GeezerVison is showing python-course.eu — 9. Turing Machine in Python | Applications | python-course.eu 9. Turing Machine in PythonBy Bernd Klein. Last modified: 21 Feb 2025. On this page ➤ Just to let you know straight-away: The Turing machine is not a machine. It is a mathematical model, which was formulated by the English mathematician Alan Turing in 1936. It's a very simple model of a computer, yet it has the complete computing capability of a general purpose computer. The Turing machine (TM) serves two needs in theoretical computer science:
A Turing machine consists only of a few components: A tape on which data can be sequentially stored. The tape consists of fields, which are sequentially arranged. Each field can contain a character of a finite alphabet. This tape has no limits, it goes on infinitely in both directions. In a real machine, the tape would have to be large enough to contain all the data for the algorithm. A TM also contains a head moving in both directions over the tape. This head can read and write one character from the field over which the head resides. The Turing machine is at every moment in a certain state, one of a finite number of states. A Turing program is a list of transitions, which determine for a given state and character ("under" the head) a new state, a character which has to be written into the field under the head and a movement direction for the head, i.e. either left, right or static (motionless). Formal Definition of a Turing machineA deterministic Turing machine can be defined as a 7-tuple
Live Python training See our Python training courses Example: Binary Complement functionLet's define a Turing machine, which complements a binary input on the tape, i.e. an input "1100111" e.g. will be turned into "0011000".
Implementation of a Turing machine in PythonWe implement a Turing Machine in Python as a class. We define another class for the read/write tape of the Turing Machine. The core of the tape inside the class Tape is a dictionary, which contains the entries of the tape. This way, we can have negative indices. A Python list is not a convenient data structure, because Python lists are bounded on one side, i.e. bounded by 0. We define the method str(self) for the class Tape. str(self) is called by the built-in str() function. The print function uses also the str function to calculate the "informal" string representation of an object, in our case the tape of the TM. The method get_tape() of our class TuringMachine makes use of the str representation returned by str. With the aid of the method getitem(), we provide a reading access to the tape via indices. The definition of the method setitem() allows a writing access as well, as we can see e.g. in the statement
of our class TuringMachine implementation. class Tape(object): blank_symbol = " " def __init__(self, tape_string = ""): self.__tape = dict((enumerate(tape_string))) # last line is equivalent to the following three lines: #self.__tape = {} #for i in range(len(tape_string)): # self.__tape[i] = input[i] def __str__(self): s = "" min_used_index = min(self.__tape.keys()) max_used_index = max(self.__tape.keys()) for i in range(min_used_index, max_used_index): s += self.__tape[i] return s def __getitem__(self,index): if index in self.__tape: return self.__tape[index] else: return Tape.blank_symbol def __setitem__(self, pos, char): self.__tape[pos] = char class TuringMachine(object): def __init__(self, tape = "", blank_symbol = " ", initial_state = "", final_states = None, transition_function = None): self.__tape = Tape(tape) self.__head_position = 0 self.__blank_symbol = blank_symbol self.__current_state = initial_state if transition_function == None: self.__transition_function = {} else: self.__transition_function = transition_function if final_states == None: self.__final_states = set() else: self.__final_states = set(final_states) def get_tape(self): return str(self.__tape) def step(self): char_under_head = self.__tape[self.__head_position] x = (self.__current_state, char_under_head) if x in self.__transition_function: y = self.__transition_function[x] self.__tape[self.__head_position] = y[1] if y[2] == "R": self.__head_position += 1 elif y[2] == "L": self.__head_position -= 1 self.__current_state = y[0] def final(self): if self.__current_state in self.__final_states: return True else: return False Using the TuringMachine class: initial_state = "init", accepting_states = ["final"], transition_function = {("init","0"):("init", "1", "R"), ("init","1"):("init", "0", "R"), ("init"," "):("final"," ", "N"), } final_states = {"final"} t = TuringMachine("010011001 ", initial_state = "init", final_states = final_states, transition_function=transition_function) print("Input on Tape:\n" + t.get_tape()) while not t.final(): t.step() print("Result of the Turing machine calculation:") print(t.get_tape()) OUTPUT:Input on Tape: 010011001 Result of the Turing machine calculation: 101100110 | ||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |