table = { ('q0', '0', 'z'): ('q0', '0z'), ('q0', '0', '0'): ('q0', '00'), ('q0', '1', '0'): ('q1', ''), ('q1', '1', '0'): ('q1', ''), ('q1', '', 'z'): ('q2', 'z'), ('q2', '', 'z'): ('q2', 'z'), } stack = ['z'] s = input("Type the string\n") curr = ['q0', '', ''] final = {'q2'} for ch in s + '': top = stack.pop() curr[1], curr[2] = ch, top if tuple(curr) not in table: print(f"Rejected at state: {curr}") exit() next = table[tuple(curr)[0]] stack.extend(reversed(table[tuple(curr)[1]])) curr[0] = next print("Accepted" if curr[0] in final else "Rejected")