def test_move_head_stay(): tape = Tape('B', ['a', 'b', 'X', 'B'], ['a', 'b']) tape.move_head('S') assert tape.get_content() == 'a'
def test_move_head_right_left(): tape = Tape('B', ['a', 'b', 'X', 'B'], ['a', 'b']) tape.move_head('R') tape.move_head('L') assert tape.get_content() == 'a'
import sys from tape import Direction from tape import Tape if __name__ == '__main__': if len(sys.argv) == 1: print('usage: {} <palindrome>'.format(sys.argv[0])) print('where the format of a palindrome is L = { ww^R | w = (0+1)^* }') sys.exit(0) tape = Tape(sys.argv[1]) while True: if tape.read() == '0': tape.write('x') tape.move_head(Direction.RIGHT) if tape.read() in ['0', '1']: tape.move_head(Direction.RIGHT) elif tape.read() in ['d', 'x', 'y']: break while tape.read() in ['0', '1']: tape.move_head(Direction.RIGHT) if tape.read() in ['d', 'x', 'y']: tape.move_head(Direction.LEFT) if tape.read() == '0': tape.write('x') tape.move_head(Direction.LEFT)