1
- # https://www.electrically4u.com/solved-problems-on-multiplexer/
2
-
3
-
4
1
def mux (input0 : int , input1 : int , select : int ) -> int :
5
2
"""
6
3
Implement a 2-to-1 Multiplexer.
7
4
8
5
:param input0: The first input value (0 or 1).
9
6
:param input1: The second input value (0 or 1).
10
7
: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
12
12
13
13
>>> mux(0, 1, 0)
14
14
0
@@ -18,13 +18,22 @@ def mux(input0: int, input1: int, select: int) -> int:
18
18
1
19
19
>>> mux(1, 0, 1)
20
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
21
33
"""
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" )
28
37
29
38
30
39
if __name__ == "__main__" :
0 commit comments