A multiplexer with n − 1 select lines can build any function of n variables, if the last variable is used on the data inputs. Each data input is then one of 0, 1, the last variable, or its complement.
What to build
Print what each data input of the multiplexer must be.
Requirements
- The truth table is given by its output column, with the first variable as the most significant bit.
- The first
n − 1variables are the selects. Data inputD0is used when the selects are 0…0,D1for the next value, and so on. - Name the last variable by its letter (
Bfor 2 variables,Cfor 3,Dfor 4) and a complemented variable with', for exampleC'. - Print the data inputs from D0 on, separated by spaces.
Input
A line with n (2 to 4), then a line with the 2ⁿ output bits.
Output
One line with 2ⁿ⁻¹ data inputs, each 0, 1, the variable or its complement.
Constraints
2 ≤ n ≤ 4.
Example
Input:
3
00010111
Output:
0 C C 1
Why it works
For a fixed value of the selects, the function depends only on the last variable. It is either always 0, always 1, equal to the variable, or the opposite of it, and those are the only four functions of one variable. So a 2ⁿ⁻¹-to-1 multiplexer with the right four kinds of wires can be programmed to give any function of n variables.
Examples
3 00010111
0 C C 1
2 0110
B B'
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Group the rows in pairs: rows 2j and 2j + 1 have the same values for every variable except the last one.
Hint 2
For a pair with outputs (x, y) for last variable 0 and 1: (0, 0) gives 0, (1, 1) gives 1, (0, 1) gives the variable itself, and (1, 0) gives its complement.
Hint 3
The first n − 1 variables become the select lines, and the last variable is the one the data inputs use.
Solution
Show one correct solution (try it yourself first)
n = int(input())
outputs = input().strip()
last = "ABCD"[n - 1]
result = []
for j in range(0, 2 ** n, 2):
pair = outputs[j] + outputs[j + 1]
result.append({"00": "0", "11": "1", "01": last, "10": last + "'"}[pair])
print(*result)
▶ Open this solution in the compiler, then press Visualize to watch it run step by step