From cc63532964849e875bccfebaed41842a11b47935 Mon Sep 17 00:00:00 2001 From: Tapas Singhal <98687345+Shocker-lov-t@users.noreply.github.com> Date: Sun, 29 Oct 2023 05:24:22 +0530 Subject: [PATCH 1/3] Create multiplexer.py --- boolean_algebra/multiplexer.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 boolean_algebra/multiplexer.py diff --git a/boolean_algebra/multiplexer.py b/boolean_algebra/multiplexer.py new file mode 100644 index 000000000000..ec212e02b9a7 --- /dev/null +++ b/boolean_algebra/multiplexer.py @@ -0,0 +1,31 @@ +# https://www.electrically4u.com/solved-problems-on-multiplexer/ + +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. + + >>> mux(0, 1, 0) + 0 + >>> mux(0, 1, 1) + 1 + >>> mux(1, 0, 0) + 1 + >>> mux(1, 0, 1) + 0 + """ + if select == 0: + return input0 + elif select == 1: + return input1 + else: + raise ValueError("Select signal must be 0 or 1") + +if __name__ == "__main__": + import doctest + + doctest.testmod() From b86ef12dc19f8572b60d83b7de47f8f6614d80df Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 28 Oct 2023 23:56:02 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- boolean_algebra/multiplexer.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/boolean_algebra/multiplexer.py b/boolean_algebra/multiplexer.py index ec212e02b9a7..abeb3f070f59 100644 --- a/boolean_algebra/multiplexer.py +++ b/boolean_algebra/multiplexer.py @@ -1,5 +1,6 @@ # https://www.electrically4u.com/solved-problems-on-multiplexer/ + def mux(input0: int, input1: int, select: int) -> int: """ Implement a 2-to-1 Multiplexer. @@ -25,6 +26,7 @@ def mux(input0: int, input1: int, select: int) -> int: else: raise ValueError("Select signal must be 0 or 1") + if __name__ == "__main__": import doctest From 8c949124f5093b493ab7777fad4645f92e203431 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 29 Oct 2023 08:39:34 +0100 Subject: [PATCH 3/3] Doctests should show how the algorithm fails --- boolean_algebra/multiplexer.py | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/boolean_algebra/multiplexer.py b/boolean_algebra/multiplexer.py index abeb3f070f59..7e65c785c829 100644 --- a/boolean_algebra/multiplexer.py +++ b/boolean_algebra/multiplexer.py @@ -1,6 +1,3 @@ -# https://www.electrically4u.com/solved-problems-on-multiplexer/ - - def mux(input0: int, input1: int, select: int) -> int: """ Implement a 2-to-1 Multiplexer. @@ -8,7 +5,10 @@ def mux(input0: int, input1: int, select: int) -> int: :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. + :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 @@ -18,13 +18,22 @@ def mux(input0: int, input1: int, select: int) -> int: 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 select == 0: - return input0 - elif select == 1: - return input1 - else: - raise ValueError("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__":