from tmachine.VirtualHardware import VirtualHardware tape = [None,1,1,None,1,1,1,1,1,1,1,None] turing_machine = VirtualHardware(tape_length=50,init = tape) running = True state = 0 while running == True: print "state=",state print "tape position=",turing_machine.position() read_val = turing_machine.read() print "read_val=",read_val #print turing_machine.position() if state == 0 : if read_val == None: turing_machine.write(None) turing_machine.moveLeft() elif read_val == 1: turing_machine.write("1") turing_machine.moveLeft() state = 1 elif state == 1: if read_val == None: turing_machine.write(None) turing_machine.moveLeft() state = 2 elif read_val == 1: turing_machine.write(1) turing_machine.moveLeft() elif state == 2 : if read_val == None: turing_machine.write(None)
# defines what a turing machine is from tmachine.VirtualHardware import VirtualHardware #creates virtual turing machine, including how long the tape of machine is turing_machine = VirtualHardware(tape_length=100) # asks for input state = raw_input("enter # ") #says input given will always be a number state = int(state) #converts integer given to binary number state = bin(state) #from second on, to remove 0,b from binary tape state = state[2:] #as long as there are still more digits in binary number... while state !="": print state # write binary number turing_machine.moveLeft()#move virtual tape left once(default is once, can be specified) turing_machine.write(state[0])#write first digit of binary code state = state[1:]#redueces state to what ever's left in binary code # show turing tape w turing_machine.tape()