Skip to content

Commit 5019561

Browse files
its-tapaspre-commit-ci[bot]cclauss
authored
Create multiplexer.py (#11064)
* Create multiplexer.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Doctests should show how the algorithm fails --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
1 parent e3eb9da commit 5019561

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

Diff for: boolean_algebra/multiplexer.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
def mux(input0: int, input1: int, select: int) -> int:
2+
"""
3+
Implement a 2-to-1 Multiplexer.
4+
5+
:param input0: The first input value (0 or 1).
6+
:param input1: The second input value (0 or 1).
7+
:param select: The select signal (0 or 1) to choose between input0 and input1.
8+
:return: The output based on the select signal. input1 if select else input0.
9+
10+
https://www.electrically4u.com/solved-problems-on-multiplexer
11+
https://en.wikipedia.org/wiki/Multiplexer
12+
13+
>>> mux(0, 1, 0)
14+
0
15+
>>> mux(0, 1, 1)
16+
1
17+
>>> mux(1, 0, 0)
18+
1
19+
>>> mux(1, 0, 1)
20+
0
21+
>>> mux(2, 1, 0)
22+
Traceback (most recent call last):
23+
...
24+
ValueError: Inputs and select signal must be 0 or 1
25+
>>> mux(0, -1, 0)
26+
Traceback (most recent call last):
27+
...
28+
ValueError: Inputs and select signal must be 0 or 1
29+
>>> mux(0, 1, 1.1)
30+
Traceback (most recent call last):
31+
...
32+
ValueError: Inputs and select signal must be 0 or 1
33+
"""
34+
if all(i in (0, 1) for i in (input0, input1, select)):
35+
return input1 if select else input0
36+
raise ValueError("Inputs and select signal must be 0 or 1")
37+
38+
39+
if __name__ == "__main__":
40+
import doctest
41+
42+
doctest.testmod()

0 commit comments

Comments
 (0)