From 5496ac619326d50ec1270c875c6d78493257a09b Mon Sep 17 00:00:00 2001 From: Karthik S <73390717+karthiks2611@users.noreply.github.com> Date: Mon, 24 Oct 2022 17:13:38 +0530 Subject: [PATCH 01/16] Added Implementation for XNOR gate --- boolean_algebra/xnor_gate.py | 46 ++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 boolean_algebra/xnor_gate.py diff --git a/boolean_algebra/xnor_gate.py b/boolean_algebra/xnor_gate.py new file mode 100644 index 000000000000..5db0a8953224 --- /dev/null +++ b/boolean_algebra/xnor_gate.py @@ -0,0 +1,46 @@ +""" +A XNOR Gate is a logic gate in boolean algebra which results to 0 (False) if both the +inputs are different, and 1 (True), if the inputs are same. It's basically adding NOT gate to an XOR gate +Following is the truth table of a XNOR Gate: + ------------------------------ + | Input 1 | Input 2 | Output | + ------------------------------ + | 0 | 0 | 1 | + | 0 | 1 | 0 | + | 1 | 0 | 0 | + | 1 | 1 | 1 | + ------------------------------ +Refer - https://www.geeksforgeeks.org/logic-gates-in-python/ +""" + + +def xnor_gate(input_1: int, input_2: int) -> int: + """ + Calculate XOR of the input values + >>> xnor_gate(0, 0) + 1 + >>> xnor_gate(0, 1) + 0 + >>> xnor_gate(1, 0) + 0 + >>> xnor_gate(1, 1) + 1 + """ + return 1 if input_1 == input_2 else 0 + + +def test_xnor_gate() -> None: + """ + Tests the xor_gate function + """ + assert xnor_gate(0, 0) == 1 + assert xnor_gate(0, 1) == 0 + assert xnor_gate(1, 0) == 0 + assert xnor_gate(1, 1) == 1 + + +if __name__ == "__main__": + print(xnor_gate(0, 0)) + print(xnor_gate(0, 1)) + print(xnor_gate(1, 0)) + print(xnor_gate(1, 1)) \ No newline at end of file From 77eaeafc81bba2415165e77da2c2301ac2112ec8 Mon Sep 17 00:00:00 2001 From: Karthik S <73390717+karthiks2611@users.noreply.github.com> Date: Mon, 24 Oct 2022 17:15:41 +0530 Subject: [PATCH 02/16] Added Implementation for OR gate --- boolean_algebra/or_gate.py | 46 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 boolean_algebra/or_gate.py diff --git a/boolean_algebra/or_gate.py b/boolean_algebra/or_gate.py new file mode 100644 index 000000000000..99af402771c4 --- /dev/null +++ b/boolean_algebra/or_gate.py @@ -0,0 +1,46 @@ +""" +An OR Gate is a logic gate in boolean algebra which results to 0 (False) if both the +inputs are 0, and 1 (True) otherwise. +Following is the truth table of an AND Gate: + ------------------------------ + | Input 1 | Input 2 | Output | + ------------------------------ + | 0 | 0 | 0 | + | 0 | 1 | 1 | + | 1 | 0 | 1 | + | 1 | 1 | 1 | + ------------------------------ +Refer - https://www.geeksforgeeks.org/logic-gates-in-python/ +""" + + +def or_gate(input_1: int, input_2: int) -> int: + """ + Calculate OR of the input values + >>> or_gate(0, 0) + 0 + >>> or_gate(0, 1) + 1 + >>> or_gate(1, 0) + 1 + >>> or_gate(1, 1) + 1 + """ + return int((input_1, input_2).count(1) != 0) + + +def test_or_gate() -> None: + """ + Tests the or_gate function + """ + assert or_gate(0, 0) == 0 + assert or_gate(0, 1) == 1 + assert or_gate(1, 0) == 1 + assert or_gate(1, 1) == 1 + + +if __name__ == "__main__": + print(or_gate(0,1)) + print(or_gate(1,0)) + print(or_gate(0,0)) + print(or_gate(1,1)) \ No newline at end of file From d97d8a7cd4885692fc368aeaeae9b622080924fc Mon Sep 17 00:00:00 2001 From: Karthik S <73390717+karthiks2611@users.noreply.github.com> Date: Mon, 24 Oct 2022 17:16:12 +0530 Subject: [PATCH 03/16] Added implementation of NAND gate --- nand_gate.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 nand_gate.py diff --git a/nand_gate.py b/nand_gate.py new file mode 100644 index 000000000000..107c8ae99378 --- /dev/null +++ b/nand_gate.py @@ -0,0 +1,46 @@ +""" +A NAND Gate is a logic gate in boolean algebra which results to 0 (False) if both the +inputs are 1, and 1 (True) otherwise. It's similar to adding a NOT gate along with an AND gate. +Following is the truth table of a NAND Gate: + ------------------------------ + | Input 1 | Input 2 | Output | + ------------------------------ + | 0 | 0 | 1 | + | 0 | 1 | 1 | + | 1 | 0 | 1 | + | 1 | 1 | 0 | + ------------------------------ +Refer - https://www.geeksforgeeks.org/logic-gates-in-python/ +""" + + +def nand_gate(input_1: int, input_2: int) -> int: + """ + Calculate AND of the input values + >>> nand_gate(0, 0) + 1 + >>> nand_gate(0, 1) + 1 + >>> nand_gate(1, 0) + 1 + >>> nand_gate(1, 1) + 0 + """ + return int(not((input_1, input_2).count(0) == 0)) + + +def test_nand_gate() -> None: + """ + Tests the nand_gate function + """ + assert nand_gate(0, 0) == 1 + assert nand_gate(0, 1) == 1 + assert nand_gate(1, 0) == 1 + assert nand_gate(1, 1) == 0 + + +if __name__ == "__main__": + print(nand_gate(0, 0)) + print(nand_gate(0, 1)) + print(nand_gate(1, 0)) + print(nand_gate(1, 1)) \ No newline at end of file From 8ff99a4da70e0440a2ed844946162b50944a3f3a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 24 Oct 2022 11:48:04 +0000 Subject: [PATCH 04/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- boolean_algebra/or_gate.py | 8 ++++---- boolean_algebra/xnor_gate.py | 2 +- nand_gate.py | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/boolean_algebra/or_gate.py b/boolean_algebra/or_gate.py index 99af402771c4..aa7e6645e33f 100644 --- a/boolean_algebra/or_gate.py +++ b/boolean_algebra/or_gate.py @@ -40,7 +40,7 @@ def test_or_gate() -> None: if __name__ == "__main__": - print(or_gate(0,1)) - print(or_gate(1,0)) - print(or_gate(0,0)) - print(or_gate(1,1)) \ No newline at end of file + print(or_gate(0, 1)) + print(or_gate(1, 0)) + print(or_gate(0, 0)) + print(or_gate(1, 1)) diff --git a/boolean_algebra/xnor_gate.py b/boolean_algebra/xnor_gate.py index 5db0a8953224..9435b4489aa7 100644 --- a/boolean_algebra/xnor_gate.py +++ b/boolean_algebra/xnor_gate.py @@ -43,4 +43,4 @@ def test_xnor_gate() -> None: print(xnor_gate(0, 0)) print(xnor_gate(0, 1)) print(xnor_gate(1, 0)) - print(xnor_gate(1, 1)) \ No newline at end of file + print(xnor_gate(1, 1)) diff --git a/nand_gate.py b/nand_gate.py index 107c8ae99378..d0f0576bd1b2 100644 --- a/nand_gate.py +++ b/nand_gate.py @@ -26,7 +26,7 @@ def nand_gate(input_1: int, input_2: int) -> int: >>> nand_gate(1, 1) 0 """ - return int(not((input_1, input_2).count(0) == 0)) + return int(not ((input_1, input_2).count(0) == 0)) def test_nand_gate() -> None: @@ -43,4 +43,4 @@ def test_nand_gate() -> None: print(nand_gate(0, 0)) print(nand_gate(0, 1)) print(nand_gate(1, 0)) - print(nand_gate(1, 1)) \ No newline at end of file + print(nand_gate(1, 1)) From d2452bf728603a354111abfabb8eb95360a54afd Mon Sep 17 00:00:00 2001 From: Karthik S <73390717+karthiks2611@users.noreply.github.com> Date: Mon, 24 Oct 2022 17:23:53 +0530 Subject: [PATCH 05/16] Added Implementation of NAND gate --- boolean_algebra/nand_gate.py | 46 ++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 boolean_algebra/nand_gate.py diff --git a/boolean_algebra/nand_gate.py b/boolean_algebra/nand_gate.py new file mode 100644 index 000000000000..107c8ae99378 --- /dev/null +++ b/boolean_algebra/nand_gate.py @@ -0,0 +1,46 @@ +""" +A NAND Gate is a logic gate in boolean algebra which results to 0 (False) if both the +inputs are 1, and 1 (True) otherwise. It's similar to adding a NOT gate along with an AND gate. +Following is the truth table of a NAND Gate: + ------------------------------ + | Input 1 | Input 2 | Output | + ------------------------------ + | 0 | 0 | 1 | + | 0 | 1 | 1 | + | 1 | 0 | 1 | + | 1 | 1 | 0 | + ------------------------------ +Refer - https://www.geeksforgeeks.org/logic-gates-in-python/ +""" + + +def nand_gate(input_1: int, input_2: int) -> int: + """ + Calculate AND of the input values + >>> nand_gate(0, 0) + 1 + >>> nand_gate(0, 1) + 1 + >>> nand_gate(1, 0) + 1 + >>> nand_gate(1, 1) + 0 + """ + return int(not((input_1, input_2).count(0) == 0)) + + +def test_nand_gate() -> None: + """ + Tests the nand_gate function + """ + assert nand_gate(0, 0) == 1 + assert nand_gate(0, 1) == 1 + assert nand_gate(1, 0) == 1 + assert nand_gate(1, 1) == 0 + + +if __name__ == "__main__": + print(nand_gate(0, 0)) + print(nand_gate(0, 1)) + print(nand_gate(1, 0)) + print(nand_gate(1, 1)) \ No newline at end of file From 6833ec37d59d085a8d52a9248fe1396c0295a5e2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 24 Oct 2022 11:54:41 +0000 Subject: [PATCH 06/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- boolean_algebra/nand_gate.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/boolean_algebra/nand_gate.py b/boolean_algebra/nand_gate.py index 107c8ae99378..d0f0576bd1b2 100644 --- a/boolean_algebra/nand_gate.py +++ b/boolean_algebra/nand_gate.py @@ -26,7 +26,7 @@ def nand_gate(input_1: int, input_2: int) -> int: >>> nand_gate(1, 1) 0 """ - return int(not((input_1, input_2).count(0) == 0)) + return int(not ((input_1, input_2).count(0) == 0)) def test_nand_gate() -> None: @@ -43,4 +43,4 @@ def test_nand_gate() -> None: print(nand_gate(0, 0)) print(nand_gate(0, 1)) print(nand_gate(1, 0)) - print(nand_gate(1, 1)) \ No newline at end of file + print(nand_gate(1, 1)) From 108908bd81c489fede92fd7171674ed1804061e4 Mon Sep 17 00:00:00 2001 From: Karthik S <73390717+karthiks2611@users.noreply.github.com> Date: Mon, 24 Oct 2022 17:30:16 +0530 Subject: [PATCH 07/16] Updated nand_gate.py --- boolean_algebra/nand_gate.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/boolean_algebra/nand_gate.py b/boolean_algebra/nand_gate.py index d0f0576bd1b2..b20fae8bf6f0 100644 --- a/boolean_algebra/nand_gate.py +++ b/boolean_algebra/nand_gate.py @@ -1,6 +1,7 @@ """ A NAND Gate is a logic gate in boolean algebra which results to 0 (False) if both the -inputs are 1, and 1 (True) otherwise. It's similar to adding a NOT gate along with an AND gate. +inputs are 1, and 1 (True) otherwise. It's similar to adding +a NOT gate along with an AND gate. Following is the truth table of a NAND Gate: ------------------------------ | Input 1 | Input 2 | Output | From 90fe594d0704a3b706523d0b2779cca74e1a944b Mon Sep 17 00:00:00 2001 From: Karthik S <73390717+karthiks2611@users.noreply.github.com> Date: Mon, 24 Oct 2022 17:31:08 +0530 Subject: [PATCH 08/16] updated xnor_gate.py after some changes --- boolean_algebra/xnor_gate.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/boolean_algebra/xnor_gate.py b/boolean_algebra/xnor_gate.py index 9435b4489aa7..fc2b8bd9ae5b 100644 --- a/boolean_algebra/xnor_gate.py +++ b/boolean_algebra/xnor_gate.py @@ -1,6 +1,7 @@ """ A XNOR Gate is a logic gate in boolean algebra which results to 0 (False) if both the -inputs are different, and 1 (True), if the inputs are same. It's basically adding NOT gate to an XOR gate +inputs are different, and 1 (True), if the inputs are same. +It's similar to adding a NOT gate to an XOR gate Following is the truth table of a XNOR Gate: ------------------------------ | Input 1 | Input 2 | Output | @@ -43,4 +44,4 @@ def test_xnor_gate() -> None: print(xnor_gate(0, 0)) print(xnor_gate(0, 1)) print(xnor_gate(1, 0)) - print(xnor_gate(1, 1)) + print(xnor_gate(1, 1)) From cb6ce680af41afbfb49b0f0734c646f7ee6fdc9d Mon Sep 17 00:00:00 2001 From: Karthik S <73390717+karthiks2611@users.noreply.github.com> Date: Mon, 24 Oct 2022 17:40:35 +0530 Subject: [PATCH 09/16] Delete due to duplicate file --- nand_gate.py | 46 ---------------------------------------------- 1 file changed, 46 deletions(-) delete mode 100644 nand_gate.py diff --git a/nand_gate.py b/nand_gate.py deleted file mode 100644 index d0f0576bd1b2..000000000000 --- a/nand_gate.py +++ /dev/null @@ -1,46 +0,0 @@ -""" -A NAND Gate is a logic gate in boolean algebra which results to 0 (False) if both the -inputs are 1, and 1 (True) otherwise. It's similar to adding a NOT gate along with an AND gate. -Following is the truth table of a NAND Gate: - ------------------------------ - | Input 1 | Input 2 | Output | - ------------------------------ - | 0 | 0 | 1 | - | 0 | 1 | 1 | - | 1 | 0 | 1 | - | 1 | 1 | 0 | - ------------------------------ -Refer - https://www.geeksforgeeks.org/logic-gates-in-python/ -""" - - -def nand_gate(input_1: int, input_2: int) -> int: - """ - Calculate AND of the input values - >>> nand_gate(0, 0) - 1 - >>> nand_gate(0, 1) - 1 - >>> nand_gate(1, 0) - 1 - >>> nand_gate(1, 1) - 0 - """ - return int(not ((input_1, input_2).count(0) == 0)) - - -def test_nand_gate() -> None: - """ - Tests the nand_gate function - """ - assert nand_gate(0, 0) == 1 - assert nand_gate(0, 1) == 1 - assert nand_gate(1, 0) == 1 - assert nand_gate(1, 1) == 0 - - -if __name__ == "__main__": - print(nand_gate(0, 0)) - print(nand_gate(0, 1)) - print(nand_gate(1, 0)) - print(nand_gate(1, 1)) From a8f3d6494806fd29096fc4fe49ebea415d544342 Mon Sep 17 00:00:00 2001 From: Karthik S <73390717+karthiks2611@users.noreply.github.com> Date: Mon, 24 Oct 2022 17:41:52 +0530 Subject: [PATCH 10/16] Updated xnor_gate.py --- boolean_algebra/xnor_gate.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/boolean_algebra/xnor_gate.py b/boolean_algebra/xnor_gate.py index fc2b8bd9ae5b..45ab2700ec35 100644 --- a/boolean_algebra/xnor_gate.py +++ b/boolean_algebra/xnor_gate.py @@ -2,6 +2,7 @@ A XNOR Gate is a logic gate in boolean algebra which results to 0 (False) if both the inputs are different, and 1 (True), if the inputs are same. It's similar to adding a NOT gate to an XOR gate + Following is the truth table of a XNOR Gate: ------------------------------ | Input 1 | Input 2 | Output | @@ -32,7 +33,7 @@ def xnor_gate(input_1: int, input_2: int) -> int: def test_xnor_gate() -> None: """ - Tests the xor_gate function + Tests the xnor_gate function """ assert xnor_gate(0, 0) == 1 assert xnor_gate(0, 1) == 0 From f2028022b6f8f24aaaff9da4f0278dc81b40ab57 Mon Sep 17 00:00:00 2001 From: Karthik S <73390717+karthiks2611@users.noreply.github.com> Date: Mon, 24 Oct 2022 17:48:23 +0530 Subject: [PATCH 11/16] Added Implementation of NOT gate in python --- boolean_algebra/not_gate.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 boolean_algebra/not_gate.py diff --git a/boolean_algebra/not_gate.py b/boolean_algebra/not_gate.py new file mode 100644 index 000000000000..89b08822b409 --- /dev/null +++ b/boolean_algebra/not_gate.py @@ -0,0 +1,37 @@ +""" +A NOT Gate is a logic gate in boolean algebra which results to 0 (False) if the +input is high, and 1 (True) if the input is low. +Following is the truth table of a XOR Gate: + ------------------------------ + | Input | Output | + ------------------------------ + | 0 | 1 | + | 1 | 0 | + ------------------------------ +Refer - https://www.geeksforgeeks.org/logic-gates-in-python/ +""" + + +def not_gate(input_1: int) -> int: + """ + Calculate NOT of the input values + >>> not_gate(0) + 1 + >>> not_gate(1) + 0 + """ + + return 1 if input_1 == 0 else 0 + + +def test_not_gate() -> None: + """ + Tests the not_gate function + """ + assert not_gate(0) == 1 + assert not_gate(1) == 0 + + +if __name__ == "__main__": + print(not_gate(0)) + print(not_gate(1)) \ No newline at end of file From b54c3b3c37ce866846ef93564bb17af875f72fa9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 24 Oct 2022 12:20:03 +0000 Subject: [PATCH 12/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- boolean_algebra/not_gate.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/boolean_algebra/not_gate.py b/boolean_algebra/not_gate.py index 89b08822b409..b41da602d936 100644 --- a/boolean_algebra/not_gate.py +++ b/boolean_algebra/not_gate.py @@ -1,6 +1,6 @@ """ A NOT Gate is a logic gate in boolean algebra which results to 0 (False) if the -input is high, and 1 (True) if the input is low. +input is high, and 1 (True) if the input is low. Following is the truth table of a XOR Gate: ------------------------------ | Input | Output | @@ -20,7 +20,7 @@ def not_gate(input_1: int) -> int: >>> not_gate(1) 0 """ - + return 1 if input_1 == 0 else 0 @@ -34,4 +34,4 @@ def test_not_gate() -> None: if __name__ == "__main__": print(not_gate(0)) - print(not_gate(1)) \ No newline at end of file + print(not_gate(1)) From ef8218aebc84abfd594eb1d4c94902928e35e220 Mon Sep 17 00:00:00 2001 From: Karthik S <73390717+karthiks2611@users.noreply.github.com> Date: Mon, 24 Oct 2022 20:26:08 +0530 Subject: [PATCH 13/16] fixed a typo error --- boolean_algebra/nand_gate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boolean_algebra/nand_gate.py b/boolean_algebra/nand_gate.py index b20fae8bf6f0..0706517b7a9e 100644 --- a/boolean_algebra/nand_gate.py +++ b/boolean_algebra/nand_gate.py @@ -17,7 +17,7 @@ def nand_gate(input_1: int, input_2: int) -> int: """ - Calculate AND of the input values + Calculate NAND of the input values >>> nand_gate(0, 0) 1 >>> nand_gate(0, 1) From c3b5434412f05c7d8c448b73ccbe514551b6434d Mon Sep 17 00:00:00 2001 From: Karthik S <73390717+karthiks2611@users.noreply.github.com> Date: Mon, 24 Oct 2022 20:26:54 +0530 Subject: [PATCH 14/16] Updated to a new logic --- boolean_algebra/nand_gate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boolean_algebra/nand_gate.py b/boolean_algebra/nand_gate.py index 0706517b7a9e..c15f3a85c1ca 100644 --- a/boolean_algebra/nand_gate.py +++ b/boolean_algebra/nand_gate.py @@ -27,7 +27,7 @@ def nand_gate(input_1: int, input_2: int) -> int: >>> nand_gate(1, 1) 0 """ - return int(not ((input_1, input_2).count(0) == 0)) + return int(((input_1, input_2).count(0) != 0)) def test_nand_gate() -> None: From 71a839bf350dee1108df6d85b7342870bfe6ffe7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 24 Oct 2022 14:59:44 +0000 Subject: [PATCH 15/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- boolean_algebra/nand_gate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boolean_algebra/nand_gate.py b/boolean_algebra/nand_gate.py index c15f3a85c1ca..1c40dc37cb1c 100644 --- a/boolean_algebra/nand_gate.py +++ b/boolean_algebra/nand_gate.py @@ -27,7 +27,7 @@ def nand_gate(input_1: int, input_2: int) -> int: >>> nand_gate(1, 1) 0 """ - return int(((input_1, input_2).count(0) != 0)) + return int((input_1, input_2).count(0) != 0) def test_nand_gate() -> None: From 61d077346a397b2368a323181e47dac526ca909c Mon Sep 17 00:00:00 2001 From: Karthik S <73390717+karthiks2611@users.noreply.github.com> Date: Tue, 25 Oct 2022 00:26:03 +0530 Subject: [PATCH 16/16] Updated nand_gate.py file --- boolean_algebra/nand_gate.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/boolean_algebra/nand_gate.py b/boolean_algebra/nand_gate.py index 1c40dc37cb1c..ea3303d16b25 100644 --- a/boolean_algebra/nand_gate.py +++ b/boolean_algebra/nand_gate.py @@ -1,6 +1,6 @@ """ -A NAND Gate is a logic gate in boolean algebra which results to 0 (False) if both the -inputs are 1, and 1 (True) otherwise. It's similar to adding +A NAND Gate is a logic gate in boolean algebra which results to 0 (False) if both +the inputs are 1, and 1 (True) otherwise. It's similar to adding a NOT gate along with an AND gate. Following is the truth table of a NAND Gate: ------------------------------