From 7e349354df72df943d27b654c9076bc53147d037 Mon Sep 17 00:00:00 2001 From: Kuldeep Borkar Jr Date: Sat, 15 Oct 2022 16:00:29 +0530 Subject: [PATCH 1/9] Added logic gates xor and xnor in Boolean Algebra --- boolean_algebra/xnorgate.py | 37 +++++++++++++++++++++++++++++++++++++ boolean_algebra/xorgate.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 boolean_algebra/xnorgate.py create mode 100644 boolean_algebra/xorgate.py diff --git a/boolean_algebra/xnorgate.py b/boolean_algebra/xnorgate.py new file mode 100644 index 000000000000..678ab1d42900 --- /dev/null +++ b/boolean_algebra/xnorgate.py @@ -0,0 +1,37 @@ +""" A XNOR Gate is a logic gate in boolean algebra which results to false(0) + if both the inputs are different and results to true(1) if both the inputs are same. + Following is the truth table of an XNOR Gate: + | Input 1 | Input 2 | Output | + | 0 | 0 | 1 | + | 0 | 1 | 0 | + | 1 | 0 | 0 | + | 1 | 1 | 1 | +""" +"""Following is the code implementation of the XNOR Gate""" + + +def xnor_gate(input_1: int, input_2: int) -> int: + """ + >>> xnor_gate(0, 0) + 1 + >>> xnor_gate(0, 1) + 0 + >>> xnor_gate(1, 0) + 0 + >>> xnor_gate(1, 1) + 1 + """ + return int((not(input_1 != input_2))) + +def main() -> None: + print("Truth Table of XNOR Gate:") + print("| Input 1 |", " Input 2 |", " Output |") + print("| 0 |", " 0 | ", xnor_gate(0, 0), " |") + print("| 0 |", " 1 | ", xnor_gate(0, 1), " |") + print("| 1 |", " 0 | ", xnor_gate(1, 0), " |") + print("| 1 |", " 1 | ", xnor_gate(1, 1), " |") + +if __name__ == "__main__": + import doctest + doctest.testmod() + main() \ No newline at end of file diff --git a/boolean_algebra/xorgate.py b/boolean_algebra/xorgate.py new file mode 100644 index 000000000000..233614f30ed7 --- /dev/null +++ b/boolean_algebra/xorgate.py @@ -0,0 +1,37 @@ +""" A XOR Gate is a logic gate in boolean algebra which results to true(1) + only when the two input values are different, and false(0) if the inputs are equal. + Following is the truth table of an XOR Gate: + | Input 1 | Input 2 | Output | + | 0 | 0 | 0 | + | 0 | 1 | 1 | + | 1 | 0 | 1 | + | 1 | 1 | 0 | +""" +"""Following is the code implementation of the XOR Gate""" + + +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 + """ + return int(input_1 != input_2) + +def main() -> None: + print("Truth Table of XOR Gate:") + print("| Input 1 |", " Input 2 |", " Output |") + print("| 0 |", " 0 | ", xor_gate(0, 0), " |") + print("| 0 |", " 1 | ", xor_gate(0, 1), " |") + print("| 1 |", " 0 | ", xor_gate(1, 0), " |") + print("| 1 |", " 1 | ", xor_gate(1, 1), " |") + +if __name__ == "__main__": + import doctest + doctest.testmod() + main() \ No newline at end of file From c5f12c2a3650a11558c1981befd6ce62f01d1ed6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 15 Oct 2022 10:41:13 +0000 Subject: [PATCH 2/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- boolean_algebra/xnorgate.py | 7 +++++-- boolean_algebra/xorgate.py | 5 ++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/boolean_algebra/xnorgate.py b/boolean_algebra/xnorgate.py index 678ab1d42900..1abf09c88e13 100644 --- a/boolean_algebra/xnorgate.py +++ b/boolean_algebra/xnorgate.py @@ -21,7 +21,8 @@ def xnor_gate(input_1: int, input_2: int) -> int: >>> xnor_gate(1, 1) 1 """ - return int((not(input_1 != input_2))) + return int(not (input_1 != input_2)) + def main() -> None: print("Truth Table of XNOR Gate:") @@ -31,7 +32,9 @@ def main() -> None: print("| 1 |", " 0 | ", xnor_gate(1, 0), " |") print("| 1 |", " 1 | ", xnor_gate(1, 1), " |") + if __name__ == "__main__": import doctest + doctest.testmod() - main() \ No newline at end of file + main() diff --git a/boolean_algebra/xorgate.py b/boolean_algebra/xorgate.py index 233614f30ed7..38918f7119c6 100644 --- a/boolean_algebra/xorgate.py +++ b/boolean_algebra/xorgate.py @@ -23,6 +23,7 @@ def xor_gate(input_1: int, input_2: int) -> int: """ return int(input_1 != input_2) + def main() -> None: print("Truth Table of XOR Gate:") print("| Input 1 |", " Input 2 |", " Output |") @@ -31,7 +32,9 @@ def main() -> None: print("| 1 |", " 0 | ", xor_gate(1, 0), " |") print("| 1 |", " 1 | ", xor_gate(1, 1), " |") + if __name__ == "__main__": import doctest + doctest.testmod() - main() \ No newline at end of file + main() From 98a4c2487814cdfe0822526e05c4e63ff6aef7d0 Mon Sep 17 00:00:00 2001 From: Caeden Date: Sat, 15 Oct 2022 13:58:09 +0100 Subject: [PATCH 3/9] feat: Binary tree node sum (#7020) (#7162) * feat: Binary tree node sum (#7020) * feat: Sum of all nodes in binary tree explanation (#7020) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update data_structures/binary_tree/binary_tree_node_sum.py Co-authored-by: Christian Clauss * refactor: Change replace method with `__iter__` overriding (#7020) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss --- DIRECTORY.md | 1 + .../binary_tree/binary_tree_node_sum.py | 76 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 data_structures/binary_tree/binary_tree_node_sum.py diff --git a/DIRECTORY.md b/DIRECTORY.md index 239dafa65f2b..92bed9cb4c6e 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -154,6 +154,7 @@ * [Binary Search Tree](data_structures/binary_tree/binary_search_tree.py) * [Binary Search Tree Recursive](data_structures/binary_tree/binary_search_tree_recursive.py) * [Binary Tree Mirror](data_structures/binary_tree/binary_tree_mirror.py) + * [Binary Tree Node Sum](data_structures/binary_tree/binary_tree_node_sum.py) * [Binary Tree Traversals](data_structures/binary_tree/binary_tree_traversals.py) * [Fenwick Tree](data_structures/binary_tree/fenwick_tree.py) * [Inorder Tree Traversal 2022](data_structures/binary_tree/inorder_tree_traversal_2022.py) diff --git a/data_structures/binary_tree/binary_tree_node_sum.py b/data_structures/binary_tree/binary_tree_node_sum.py new file mode 100644 index 000000000000..5a13e74e3c9f --- /dev/null +++ b/data_structures/binary_tree/binary_tree_node_sum.py @@ -0,0 +1,76 @@ +""" +Sum of all nodes in a binary tree. + +Python implementation: + O(n) time complexity - Recurses through :meth:`depth_first_search` + with each element. + O(n) space complexity - At any point in time maximum number of stack + frames that could be in memory is `n` +""" + + +from __future__ import annotations + +from collections.abc import Iterator + + +class Node: + """ + A Node has a value variable and pointers to Nodes to its left and right. + """ + + def __init__(self, value: int) -> None: + self.value = value + self.left: Node | None = None + self.right: Node | None = None + + +class BinaryTreeNodeSum: + r""" + The below tree looks like this + 10 + / \ + 5 -3 + / / \ + 12 8 0 + + >>> tree = Node(10) + >>> sum(BinaryTreeNodeSum(tree)) + 10 + + >>> tree.left = Node(5) + >>> sum(BinaryTreeNodeSum(tree)) + 15 + + >>> tree.right = Node(-3) + >>> sum(BinaryTreeNodeSum(tree)) + 12 + + >>> tree.left.left = Node(12) + >>> sum(BinaryTreeNodeSum(tree)) + 24 + + >>> tree.right.left = Node(8) + >>> tree.right.right = Node(0) + >>> sum(BinaryTreeNodeSum(tree)) + 32 + """ + + def __init__(self, tree: Node) -> None: + self.tree = tree + + def depth_first_search(self, node: Node | None) -> int: + if node is None: + return 0 + return node.value + ( + self.depth_first_search(node.left) + self.depth_first_search(node.right) + ) + + def __iter__(self) -> Iterator[int]: + yield self.depth_first_search(self.tree) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 0bf4d60ed08c64afa1e8f1e17944712d4cb475b1 Mon Sep 17 00:00:00 2001 From: Kuldeep Borkar <74557588+KuldeepBorkar@users.noreply.github.com> Date: Sat, 15 Oct 2022 19:54:39 +0530 Subject: [PATCH 4/9] Update boolean_algebra/xnorgate.py Co-authored-by: Caeden --- boolean_algebra/xnorgate.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/boolean_algebra/xnorgate.py b/boolean_algebra/xnorgate.py index 1abf09c88e13..d7c2b0d6e580 100644 --- a/boolean_algebra/xnorgate.py +++ b/boolean_algebra/xnorgate.py @@ -1,4 +1,5 @@ -""" A XNOR Gate is a logic gate in boolean algebra which results to false(0) +""" +A XNOR Gate is a logic gate in boolean algebra which results to false(0) if both the inputs are different and results to true(1) if both the inputs are same. Following is the truth table of an XNOR Gate: | Input 1 | Input 2 | Output | @@ -6,8 +7,9 @@ | 0 | 1 | 0 | | 1 | 0 | 0 | | 1 | 1 | 1 | + +Following is the code implementation of the XNOR Gate """ -"""Following is the code implementation of the XNOR Gate""" def xnor_gate(input_1: int, input_2: int) -> int: From 67fc89b6b02d89011b93ad1a530c210a98c1ab44 Mon Sep 17 00:00:00 2001 From: Kuldeep Borkar <74557588+KuldeepBorkar@users.noreply.github.com> Date: Sat, 15 Oct 2022 19:54:57 +0530 Subject: [PATCH 5/9] Update boolean_algebra/xorgate.py Co-authored-by: Caeden --- boolean_algebra/xorgate.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/boolean_algebra/xorgate.py b/boolean_algebra/xorgate.py index 38918f7119c6..62e27a89224a 100644 --- a/boolean_algebra/xorgate.py +++ b/boolean_algebra/xorgate.py @@ -1,4 +1,5 @@ -""" A XOR Gate is a logic gate in boolean algebra which results to true(1) +""" +A XOR Gate is a logic gate in boolean algebra which results to true(1) only when the two input values are different, and false(0) if the inputs are equal. Following is the truth table of an XOR Gate: | Input 1 | Input 2 | Output | @@ -6,8 +7,9 @@ | 0 | 1 | 1 | | 1 | 0 | 1 | | 1 | 1 | 0 | + +Following is the code implementation of the XOR Gate """ -"""Following is the code implementation of the XOR Gate""" def xor_gate(input_1: int, input_2: int) -> int: From cb24b3939a357de8ef47f9de194c3a5d1692df29 Mon Sep 17 00:00:00 2001 From: Kuldeep Borkar Jr Date: Sat, 15 Oct 2022 16:00:29 +0530 Subject: [PATCH 6/9] Added logic gates xor and xnor in Boolean Algebra --- boolean_algebra/xnorgate.py | 37 +++++++++++++++++++++++++++++++++++++ boolean_algebra/xorgate.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 boolean_algebra/xnorgate.py create mode 100644 boolean_algebra/xorgate.py diff --git a/boolean_algebra/xnorgate.py b/boolean_algebra/xnorgate.py new file mode 100644 index 000000000000..678ab1d42900 --- /dev/null +++ b/boolean_algebra/xnorgate.py @@ -0,0 +1,37 @@ +""" A XNOR Gate is a logic gate in boolean algebra which results to false(0) + if both the inputs are different and results to true(1) if both the inputs are same. + Following is the truth table of an XNOR Gate: + | Input 1 | Input 2 | Output | + | 0 | 0 | 1 | + | 0 | 1 | 0 | + | 1 | 0 | 0 | + | 1 | 1 | 1 | +""" +"""Following is the code implementation of the XNOR Gate""" + + +def xnor_gate(input_1: int, input_2: int) -> int: + """ + >>> xnor_gate(0, 0) + 1 + >>> xnor_gate(0, 1) + 0 + >>> xnor_gate(1, 0) + 0 + >>> xnor_gate(1, 1) + 1 + """ + return int((not(input_1 != input_2))) + +def main() -> None: + print("Truth Table of XNOR Gate:") + print("| Input 1 |", " Input 2 |", " Output |") + print("| 0 |", " 0 | ", xnor_gate(0, 0), " |") + print("| 0 |", " 1 | ", xnor_gate(0, 1), " |") + print("| 1 |", " 0 | ", xnor_gate(1, 0), " |") + print("| 1 |", " 1 | ", xnor_gate(1, 1), " |") + +if __name__ == "__main__": + import doctest + doctest.testmod() + main() \ No newline at end of file diff --git a/boolean_algebra/xorgate.py b/boolean_algebra/xorgate.py new file mode 100644 index 000000000000..233614f30ed7 --- /dev/null +++ b/boolean_algebra/xorgate.py @@ -0,0 +1,37 @@ +""" A XOR Gate is a logic gate in boolean algebra which results to true(1) + only when the two input values are different, and false(0) if the inputs are equal. + Following is the truth table of an XOR Gate: + | Input 1 | Input 2 | Output | + | 0 | 0 | 0 | + | 0 | 1 | 1 | + | 1 | 0 | 1 | + | 1 | 1 | 0 | +""" +"""Following is the code implementation of the XOR Gate""" + + +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 + """ + return int(input_1 != input_2) + +def main() -> None: + print("Truth Table of XOR Gate:") + print("| Input 1 |", " Input 2 |", " Output |") + print("| 0 |", " 0 | ", xor_gate(0, 0), " |") + print("| 0 |", " 1 | ", xor_gate(0, 1), " |") + print("| 1 |", " 0 | ", xor_gate(1, 0), " |") + print("| 1 |", " 1 | ", xor_gate(1, 1), " |") + +if __name__ == "__main__": + import doctest + doctest.testmod() + main() \ No newline at end of file From 0e12f0223cbaecd4f2c5f6bd10055e91d3a3c20e Mon Sep 17 00:00:00 2001 From: Kuldeep Borkar Jr Date: Sat, 15 Oct 2022 20:45:23 +0530 Subject: [PATCH 7/9] Updated with some suggested formatting --- boolean_algebra/xnorgate.py | 21 +++++++++++++-------- boolean_algebra/xorgate.py | 19 ++++++++++++------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/boolean_algebra/xnorgate.py b/boolean_algebra/xnorgate.py index 678ab1d42900..ca3c1a01b018 100644 --- a/boolean_algebra/xnorgate.py +++ b/boolean_algebra/xnorgate.py @@ -1,4 +1,5 @@ -""" A XNOR Gate is a logic gate in boolean algebra which results to false(0) +""" +A XNOR Gate is a logic gate in boolean algebra which results to false(0) if both the inputs are different and results to true(1) if both the inputs are same. Following is the truth table of an XNOR Gate: | Input 1 | Input 2 | Output | @@ -6,8 +7,9 @@ | 0 | 1 | 0 | | 1 | 0 | 0 | | 1 | 1 | 1 | + +Following is the code implementation of the XNOR Gate """ -"""Following is the code implementation of the XNOR Gate""" def xnor_gate(input_1: int, input_2: int) -> int: @@ -21,17 +23,20 @@ def xnor_gate(input_1: int, input_2: int) -> int: >>> xnor_gate(1, 1) 1 """ - return int((not(input_1 != input_2))) + return int(input_1 == input_2) + def main() -> None: print("Truth Table of XNOR Gate:") print("| Input 1 |", " Input 2 |", " Output |") - print("| 0 |", " 0 | ", xnor_gate(0, 0), " |") - print("| 0 |", " 1 | ", xnor_gate(0, 1), " |") - print("| 1 |", " 0 | ", xnor_gate(1, 0), " |") - print("| 1 |", " 1 | ", xnor_gate(1, 1), " |") + print(f"| 0 | 0 | {xnor_gate(0,0)} |") + print(f"| 0 | 1 | {xnor_gate(0,1)} |") + print(f"| 1 | 0 | {xnor_gate(1,0)} |") + print(f"| 1 | 1 | {xnor_gate(1,1)} |") + if __name__ == "__main__": import doctest + doctest.testmod() - main() \ No newline at end of file + main() diff --git a/boolean_algebra/xorgate.py b/boolean_algebra/xorgate.py index 233614f30ed7..ac45adfea6b2 100644 --- a/boolean_algebra/xorgate.py +++ b/boolean_algebra/xorgate.py @@ -1,4 +1,5 @@ -""" A XOR Gate is a logic gate in boolean algebra which results to true(1) +""" +A XOR Gate is a logic gate in boolean algebra which results to true(1) only when the two input values are different, and false(0) if the inputs are equal. Following is the truth table of an XOR Gate: | Input 1 | Input 2 | Output | @@ -6,8 +7,9 @@ | 0 | 1 | 1 | | 1 | 0 | 1 | | 1 | 1 | 0 | + +Following is the code implementation of the XOR Gate """ -"""Following is the code implementation of the XOR Gate""" def xor_gate(input_1: int, input_2: int) -> int: @@ -23,15 +25,18 @@ def xor_gate(input_1: int, input_2: int) -> int: """ return int(input_1 != input_2) + def main() -> None: print("Truth Table of XOR Gate:") print("| Input 1 |", " Input 2 |", " Output |") - print("| 0 |", " 0 | ", xor_gate(0, 0), " |") - print("| 0 |", " 1 | ", xor_gate(0, 1), " |") - print("| 1 |", " 0 | ", xor_gate(1, 0), " |") - print("| 1 |", " 1 | ", xor_gate(1, 1), " |") + 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() \ No newline at end of file + main() From 1b0514c7d15de23eaa2e3432e40bf735652aca49 Mon Sep 17 00:00:00 2001 From: Kuldeep Borkar Jr Date: Sat, 15 Oct 2022 22:34:49 +0530 Subject: [PATCH 8/9] Revert "feat: Binary tree node sum (#7020) (#7162)" This reverts commit 98a4c2487814cdfe0822526e05c4e63ff6aef7d0. --- DIRECTORY.md | 1 - .../binary_tree/binary_tree_node_sum.py | 76 ------------------- 2 files changed, 77 deletions(-) delete mode 100644 data_structures/binary_tree/binary_tree_node_sum.py diff --git a/DIRECTORY.md b/DIRECTORY.md index 92bed9cb4c6e..239dafa65f2b 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -154,7 +154,6 @@ * [Binary Search Tree](data_structures/binary_tree/binary_search_tree.py) * [Binary Search Tree Recursive](data_structures/binary_tree/binary_search_tree_recursive.py) * [Binary Tree Mirror](data_structures/binary_tree/binary_tree_mirror.py) - * [Binary Tree Node Sum](data_structures/binary_tree/binary_tree_node_sum.py) * [Binary Tree Traversals](data_structures/binary_tree/binary_tree_traversals.py) * [Fenwick Tree](data_structures/binary_tree/fenwick_tree.py) * [Inorder Tree Traversal 2022](data_structures/binary_tree/inorder_tree_traversal_2022.py) diff --git a/data_structures/binary_tree/binary_tree_node_sum.py b/data_structures/binary_tree/binary_tree_node_sum.py deleted file mode 100644 index 5a13e74e3c9f..000000000000 --- a/data_structures/binary_tree/binary_tree_node_sum.py +++ /dev/null @@ -1,76 +0,0 @@ -""" -Sum of all nodes in a binary tree. - -Python implementation: - O(n) time complexity - Recurses through :meth:`depth_first_search` - with each element. - O(n) space complexity - At any point in time maximum number of stack - frames that could be in memory is `n` -""" - - -from __future__ import annotations - -from collections.abc import Iterator - - -class Node: - """ - A Node has a value variable and pointers to Nodes to its left and right. - """ - - def __init__(self, value: int) -> None: - self.value = value - self.left: Node | None = None - self.right: Node | None = None - - -class BinaryTreeNodeSum: - r""" - The below tree looks like this - 10 - / \ - 5 -3 - / / \ - 12 8 0 - - >>> tree = Node(10) - >>> sum(BinaryTreeNodeSum(tree)) - 10 - - >>> tree.left = Node(5) - >>> sum(BinaryTreeNodeSum(tree)) - 15 - - >>> tree.right = Node(-3) - >>> sum(BinaryTreeNodeSum(tree)) - 12 - - >>> tree.left.left = Node(12) - >>> sum(BinaryTreeNodeSum(tree)) - 24 - - >>> tree.right.left = Node(8) - >>> tree.right.right = Node(0) - >>> sum(BinaryTreeNodeSum(tree)) - 32 - """ - - def __init__(self, tree: Node) -> None: - self.tree = tree - - def depth_first_search(self, node: Node | None) -> int: - if node is None: - return 0 - return node.value + ( - self.depth_first_search(node.left) + self.depth_first_search(node.right) - ) - - def __iter__(self) -> Iterator[int]: - yield self.depth_first_search(self.tree) - - -if __name__ == "__main__": - import doctest - - doctest.testmod() From caecd3a72b0bd26e0dbe43e0624e796c00af29a4 Mon Sep 17 00:00:00 2001 From: Kuldeep Borkar <74557588+KuldeepBorkar@users.noreply.github.com> Date: Sat, 15 Oct 2022 22:39:08 +0530 Subject: [PATCH 9/9] Update boolean_algebra/xorgate.py Co-authored-by: Caeden --- boolean_algebra/xorgate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boolean_algebra/xorgate.py b/boolean_algebra/xorgate.py index ac45adfea6b2..b6681260b07f 100644 --- a/boolean_algebra/xorgate.py +++ b/boolean_algebra/xorgate.py @@ -28,7 +28,7 @@ def xor_gate(input_1: int, input_2: int) -> int: def main() -> None: print("Truth Table of XOR Gate:") - print("| Input 1 |", " Input 2 |", " Output |") + 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)} |")