From ae6809c5e01dbb4774a9f49333e03cf728965d3d Mon Sep 17 00:00:00 2001 From: thenuka99 Date: Sat, 15 Oct 2022 15:25:38 +0530 Subject: [PATCH 1/3] NAND Gate --- boolean_algebra/nandgate.py | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 boolean_algebra/nandgate.py diff --git a/boolean_algebra/nandgate.py b/boolean_algebra/nandgate.py new file mode 100644 index 000000000000..34d03ebf40a2 --- /dev/null +++ b/boolean_algebra/nandgate.py @@ -0,0 +1,44 @@ +""" A NAND Gate is a logic gate in boolean algebra which results to false(1) + if any of the input is 0, and True(1) if both the inputs are 1. + Following is the truth table of an NAND Gate: + | Input 1 | Input 2 | Output | + | 0 | 0 | 1 | + | 0 | 1 | 1 | + | 1 | 0 | 1 | + | 1 | 1 | 0 | +""" +"""Following is the code implementation of the NAND Gate""" + + +def nand_gate(input_1: int, input_2: int) -> int: + """ + >>> nand_gate(0, 0) + 1 + >>> nand_gate(0, 1) + 1 + >>> nand_gate(1, 0) + 1 + >>> nand_gate(1, 1) + 0 + """ + if (input_1 == input_2 == 1): + return 0 + else: + return 1 + +def main() -> None: + print("Truth Table of NAND Gate:") + print("| Input 1 |", " Input 2 |", " Output |") + print("| 0 |", " 0 | ", nand_gate(0, 0), " |") + print("| 0 |", " 1 | ", nand_gate(0, 1), " |") + print("| 1 |", " 0 | ", nand_gate(1, 0), " |") + print("| 1 |", " 1 | ", nand_gate(1, 1), " |") + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + main() +"""Code provided by Janith Herath""" +"""Reference: https://www.geeksforgeeks.org/logic-gates-in-python/""" From 6f0d79f17c4880d226a642249d78027ff50c15fa 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:01:31 +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/nandgate.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/boolean_algebra/nandgate.py b/boolean_algebra/nandgate.py index 34d03ebf40a2..1a65adc9b964 100644 --- a/boolean_algebra/nandgate.py +++ b/boolean_algebra/nandgate.py @@ -21,11 +21,12 @@ def nand_gate(input_1: int, input_2: int) -> int: >>> nand_gate(1, 1) 0 """ - if (input_1 == input_2 == 1): + if input_1 == input_2 == 1: return 0 else: return 1 + def main() -> None: print("Truth Table of NAND Gate:") print("| Input 1 |", " Input 2 |", " Output |") From 0ddada3a49f3351298c17dc938efc442fa5895e5 Mon Sep 17 00:00:00 2001 From: Janith Thenuka Herath Date: Sat, 15 Oct 2022 07:39:10 -0700 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Caeden --- boolean_algebra/nandgate.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/boolean_algebra/nandgate.py b/boolean_algebra/nandgate.py index 1a65adc9b964..0893b14585c6 100644 --- a/boolean_algebra/nandgate.py +++ b/boolean_algebra/nandgate.py @@ -1,13 +1,16 @@ -""" A NAND Gate is a logic gate in boolean algebra which results to false(1) - if any of the input is 0, and True(1) if both the inputs are 1. - Following is the truth table of an NAND Gate: +""" +A NAND Gate is a logic gate in boolean algebra which results to false(1) +if any of the input is 0, and True(1) if both the inputs are 1. +Following is the truth table of an NAND Gate: | Input 1 | Input 2 | Output | | 0 | 0 | 1 | | 0 | 1 | 1 | | 1 | 0 | 1 | | 1 | 1 | 0 | + + +Following is the code implementation of the NAND Gate """ -"""Following is the code implementation of the NAND Gate""" def nand_gate(input_1: int, input_2: int) -> int: