Skip to content

Create multiplexer.py #11064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions boolean_algebra/multiplexer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
def mux(input0: int, input1: int, select: int) -> int:
"""
Implement a 2-to-1 Multiplexer.
:param input0: The first input value (0 or 1).
:param input1: The second input value (0 or 1).
:param select: The select signal (0 or 1) to choose between input0 and input1.
:return: The output based on the select signal. input1 if select else input0.
https://www.electrically4u.com/solved-problems-on-multiplexer
https://en.wikipedia.org/wiki/Multiplexer
>>> mux(0, 1, 0)
0
>>> mux(0, 1, 1)
1
>>> mux(1, 0, 0)
1
>>> mux(1, 0, 1)
0
>>> mux(2, 1, 0)
Traceback (most recent call last):
...
ValueError: Inputs and select signal must be 0 or 1
>>> mux(0, -1, 0)
Traceback (most recent call last):
...
ValueError: Inputs and select signal must be 0 or 1
>>> mux(0, 1, 1.1)
Traceback (most recent call last):
...
ValueError: Inputs and select signal must be 0 or 1
"""
if all(i in (0, 1) for i in (input0, input1, select)):
return input1 if select else input0
raise ValueError("Inputs and select signal must be 0 or 1")


if __name__ == "__main__":
import doctest

doctest.testmod()