A T flip-flop

A T flip-flop (T for toggle) changes its output on a clock edge when T is 1, and holds it when T is 0. It is the building block of counters.

What to build

Simulate a T flip-flop and print its output after every clock.

Requirements

  • Q starts at the given value.
  • On each clock the flip-flop reads the next T value.
  • Print the value of Q after each clock, in one line, separated by spaces.

Input

The first line has the starting value of Q. The second line has the T values, one for each clock, separated by spaces.

Output

One line with Q after each clock.

Constraints

Q starts as 0 or 1. There are 1 to 20 clocks.

Example

Input:

0
1 1 0 1 1

Output:

1 0 0 1 0

Why it works

Toggling is the same as XOR with T: Q = Q ^ T. With T = 1 all the time, Q changes every clock, so it makes a signal at half the clock frequency, which is why T flip-flops divide frequency by 2.

Examples

Example 1 · Input
0
1 1 0 1 1
Expected output
1 0 0 1 0
Example 2 · Input
1
0 0 0
Expected output
1 1 1

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Keep Q, starting from the given value.

Hint 2

At each clock: if T is 1, Q becomes 1 − Q; if T is 0, Q stays the same.

Hint 3

Print Q after every clock, separated by spaces.

Solution

Show one correct solution (try it yourself first)
q = int(input())
ts = list(map(int, input().split()))
out = []
for t in ts:
    q ^= t
    out.append(q)
print(*out)

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Latches and flip-flops →