A decoder with enable

An n-to-2ⁿ decoder turns a binary number into one active line. An enable input switches the whole decoder on or off.

What to build

Simulate a decoder with an enable input and print its outputs.

Requirements

  • The outputs are named Y0, Y1, …, and Y0 is printed first.
  • When the enable is 0, every output is 0.
  • When the enable is 1, only the output whose number equals the input is 1.

Input

A line with n, then any number of lines e bits, where e is the enable bit and bits is the n-bit input, most significant bit first. The lines end with a line end.

Output

For each line, the 2ⁿ output bits as one string.

Constraints

1 ≤ n ≤ 4.

Example

Input:

2
1 10
1 00
0 11
end

Output:

0010
1000
0000

Why it works

A decoder is the AND gates of the minterms: output i is the product of the enable and every input in the right form for row i. So it is 1 for only one input value, and the enable acts as an extra input of all AND gates. That is why a decoder is also called a minterm generator.

Examples

Example 1 · Input
2
1 10
1 00
0 11
end
Expected output
0010
1000
0000
Example 2 · Input
3
1 101
0 101
end
Expected output
00000100
00000000

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

Hints

Hint 1

Output `i` is 1 only when the enable is 1 and the input, read as a binary number, equals `i`.

Hint 2

int(bits, 2) turns the input string into its number.

Hint 3

Build the output string with a list: one character for each of the 2ⁿ outputs.

Solution

Show one correct solution (try it yourself first)
n = int(input())
while True:
    line = input().split()
    if line[0] == "end":
        break
    e, bits = int(line[0]), line[1]
    index = int(bits, 2)
    print("".join("1" if e == 1 and i == index else "0" for i in range(2 ** n)))

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

Lesson: Multiplexers, decoders and encoders →