From f6afb9458865a37acc0fde1b48bdf6d0acf884d7 Mon Sep 17 00:00:00 2001 From: Arjit Arora <42044030+arjitarora26@users.noreply.github.com> Date: Mon, 24 Oct 2022 11:47:02 +0530 Subject: [PATCH 1/7] Add function for xor gate --- boolean_algebra/xor_gate.py | 55 +++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 boolean_algebra/xor_gate.py diff --git a/boolean_algebra/xor_gate.py b/boolean_algebra/xor_gate.py new file mode 100644 index 000000000000..3e5bfc19f070 --- /dev/null +++ b/boolean_algebra/xor_gate.py @@ -0,0 +1,55 @@ +""" +A XOR Gate is a logic gate in boolean algebra which results to True(1) +if only one of the two inputs is 1, and False(1) if even number +of inputs are 1. +Following is the truth table of a XOR Gate: + ------------------------------ + | Input 1 | Input 2 | Output | + ------------------------------ + | 0 | 0 | 0 | + | 0 | 1 | 1 | + | 1 | 0 | 1 | + | 1 | 1 | 0 | + ------------------------------ + +Refer - https://www.geeksforgeeks.org/logic-gates-in-python/ + +""" + + +def xor_gate(input_1: int, input_2: int) -> int: + """ + >>> xor_gate(0, 0) + 0 + >>> xor_gate(0, 1) + 1 + >>> xor_gate(1, 0) + 1 + >>> xor_gate(1, 1) + 0 + """ + num_ones = 0 + + if input_1 != 0: + num_ones += 1 + + if input_2 != 0: + num_ones += 1 + + return int(num_ones % 2 != 0) + + +def main() -> None: + print("Truth Table of XOR Gate:") + print("| Input 1 | Input 2 | Output |") + print(f"| 0 | 0 | {xor_gate(0, 0)} |") + print(f"| 0 | 1 | {xor_gate(0, 1)} |") + print(f"| 1 | 0 | {xor_gate(1, 0)} |") + print(f"| 1 | 1 | {xor_gate(1, 1)} |") + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + main() From 859364e28eb2aa69ae8be0d4b7d756ddc93ceb27 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 06:18:47 +0000 Subject: [PATCH 2/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- boolean_algebra/xor_gate.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/boolean_algebra/xor_gate.py b/boolean_algebra/xor_gate.py index 3e5bfc19f070..e28444543498 100644 --- a/boolean_algebra/xor_gate.py +++ b/boolean_algebra/xor_gate.py @@ -32,10 +32,10 @@ def xor_gate(input_1: int, input_2: int) -> int: if input_1 != 0: num_ones += 1 - + if input_2 != 0: num_ones += 1 - + return int(num_ones % 2 != 0) From c26e8f4c01ad74f345ae445b9f5991677bfdcf94 Mon Sep 17 00:00:00 2001 From: Arjit Arora <42044030+arjitarora26@users.noreply.github.com> Date: Mon, 24 Oct 2022 11:55:27 +0530 Subject: [PATCH 3/7] Add test case for xor functions --- boolean_algebra/xor_gate.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/boolean_algebra/xor_gate.py b/boolean_algebra/xor_gate.py index e28444543498..07d2105e7796 100644 --- a/boolean_algebra/xor_gate.py +++ b/boolean_algebra/xor_gate.py @@ -1,6 +1,6 @@ """ A XOR Gate is a logic gate in boolean algebra which results to True(1) -if only one of the two inputs is 1, and False(1) if even number +if only one of the two inputs is 1, and False(0) if even number of inputs are 1. Following is the truth table of a XOR Gate: ------------------------------ @@ -13,12 +13,13 @@ ------------------------------ Refer - https://www.geeksforgeeks.org/logic-gates-in-python/ - """ def xor_gate(input_1: int, input_2: int) -> int: """ + calculate xor of the input values + >>> xor_gate(0, 0) 0 >>> xor_gate(0, 1) @@ -39,17 +40,16 @@ def xor_gate(input_1: int, input_2: int) -> int: return int(num_ones % 2 != 0) -def main() -> None: - print("Truth Table of XOR Gate:") - print("| Input 1 | Input 2 | Output |") - print(f"| 0 | 0 | {xor_gate(0, 0)} |") - print(f"| 0 | 1 | {xor_gate(0, 1)} |") - print(f"| 1 | 0 | {xor_gate(1, 0)} |") - print(f"| 1 | 1 | {xor_gate(1, 1)} |") +def test_xor_gate() -> None: + """ + Tests the xor_gate function + """ + assert xor_gate(0, 0) == 0 + assert xor_gate(0, 1) == 1 + assert xor_gate(1, 0) == 1 + assert xor_gate(1, 1) == 0 if __name__ == "__main__": - import doctest - - doctest.testmod() - main() + print(xor_gate(0, 0)) + print(xor_gate(0, 1)) From 56bf6653a344313ee0e249533607ba9e3b826b97 Mon Sep 17 00:00:00 2001 From: Arjit Arora <42044030+arjitarora26@users.noreply.github.com> Date: Mon, 24 Oct 2022 12:37:29 +0530 Subject: [PATCH 4/7] Update boolean_algebra/xor_gate.py Co-authored-by: Christian Clauss --- boolean_algebra/xor_gate.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/boolean_algebra/xor_gate.py b/boolean_algebra/xor_gate.py index 07d2105e7796..208469f5cc84 100644 --- a/boolean_algebra/xor_gate.py +++ b/boolean_algebra/xor_gate.py @@ -1,7 +1,6 @@ """ -A XOR Gate is a logic gate in boolean algebra which results to True(1) -if only one of the two inputs is 1, and False(0) if even number -of inputs are 1. +A XOR Gate is a logic gate in boolean algebra which results to 1 (True) if only one of the two inputs is 1, and +0 (False) if an even number of inputs are 1. Following is the truth table of a XOR Gate: ------------------------------ | Input 1 | Input 2 | Output | From a898a382957c6dcdb2d3f886f456dee4fe249c68 Mon Sep 17 00:00:00 2001 From: Arjit Arora <42044030+arjitarora26@users.noreply.github.com> Date: Mon, 24 Oct 2022 12:37:48 +0530 Subject: [PATCH 5/7] Update boolean_algebra/xor_gate.py Co-authored-by: Christian Clauss --- boolean_algebra/xor_gate.py | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/boolean_algebra/xor_gate.py b/boolean_algebra/xor_gate.py index 208469f5cc84..1ff7183f84e0 100644 --- a/boolean_algebra/xor_gate.py +++ b/boolean_algebra/xor_gate.py @@ -28,15 +28,7 @@ def xor_gate(input_1: int, input_2: int) -> int: >>> xor_gate(1, 1) 0 """ - num_ones = 0 - - if input_1 != 0: - num_ones += 1 - - if input_2 != 0: - num_ones += 1 - - return int(num_ones % 2 != 0) + return (input_1, input_2).count(0) % 2 def test_xor_gate() -> None: From 841c421309e7f3fb245177072c2de8c6a1194283 Mon Sep 17 00:00:00 2001 From: Arjit Arora <42044030+arjitarora26@users.noreply.github.com> Date: Mon, 24 Oct 2022 12:41:45 +0530 Subject: [PATCH 6/7] Split long comment line into two lines --- boolean_algebra/xor_gate.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/boolean_algebra/xor_gate.py b/boolean_algebra/xor_gate.py index 1ff7183f84e0..f36cce243365 100644 --- a/boolean_algebra/xor_gate.py +++ b/boolean_algebra/xor_gate.py @@ -1,6 +1,7 @@ """ -A XOR Gate is a logic gate in boolean algebra which results to 1 (True) if only one of the two inputs is 1, and -0 (False) if an even number of inputs are 1. +A XOR Gate is a logic gate in boolean algebra which results to +1 (True) if only one of the two inputs is 1, and 0 (False) if +an even number of inputs are 1. Following is the truth table of a XOR Gate: ------------------------------ | Input 1 | Input 2 | Output | From e77e4dd5dad1257d6b0d40cde9411f195ff13411 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 24 Oct 2022 09:13:38 +0200 Subject: [PATCH 7/7] 88 characters per line --- boolean_algebra/xor_gate.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/boolean_algebra/xor_gate.py b/boolean_algebra/xor_gate.py index f36cce243365..db4f5b45c3c6 100644 --- a/boolean_algebra/xor_gate.py +++ b/boolean_algebra/xor_gate.py @@ -1,7 +1,6 @@ """ -A XOR Gate is a logic gate in boolean algebra which results to -1 (True) if only one of the two inputs is 1, and 0 (False) if -an even number of inputs are 1. +A XOR Gate is a logic gate in boolean algebra which results to 1 (True) if only one of +the two inputs is 1, and 0 (False) if an even number of inputs are 1. Following is the truth table of a XOR Gate: ------------------------------ | Input 1 | Input 2 | Output |