Turing machine in Python