Skip to content

Commit 8c94912

Browse files
authored
Doctests should show how the algorithm fails
1 parent b86ef12 commit 8c94912

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

Diff for: boolean_algebra/multiplexer.py

+19-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
# https://www.electrically4u.com/solved-problems-on-multiplexer/
2-
3-
41
def mux(input0: int, input1: int, select: int) -> int:
52
"""
63
Implement a 2-to-1 Multiplexer.
74
85
:param input0: The first input value (0 or 1).
96
:param input1: The second input value (0 or 1).
107
:param select: The select signal (0 or 1) to choose between input0 and input1.
11-
:return: The output based on the select signal.
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
1212
1313
>>> mux(0, 1, 0)
1414
0
@@ -18,13 +18,22 @@ def mux(input0: int, input1: int, select: int) -> int:
1818
1
1919
>>> mux(1, 0, 1)
2020
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
2133
"""
22-
if select == 0:
23-
return input0
24-
elif select == 1:
25-
return input1
26-
else:
27-
raise ValueError("Select signal must be 0 or 1")
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")
2837

2938

3039
if __name__ == "__main__":

0 commit comments

Comments
 (0)