Added Implementation of NAND, OR ,XNOR and NOT gates in python (#7596) · zinating/algorithms-python@103c9e0 · GitHub
Skip to content

Commit 103c9e0

Browse files
Added Implementation of NAND, OR ,XNOR and NOT gates in python (TheAlgorithms#7596)
* Added Implementation for XNOR gate * Added Implementation for OR gate * Added implementation of NAND gate * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Added Implementation of NAND gate * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Updated nand_gate.py * updated xnor_gate.py after some changes * Delete due to duplicate file * Updated xnor_gate.py * Added Implementation of NOT gate in python * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fixed a typo error * Updated to a new logic * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Updated nand_gate.py file Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 4508423 commit 103c9e0

4 files changed

Lines changed: 178 additions & 0 deletions

File tree

boolean_algebra/nand_gate.py

Lines changed: 47 additions & 0 deletions

boolean_algebra/not_gate.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
A NOT Gate is a logic gate in boolean algebra which results to 0 (False) if the
3+
input is high, and 1 (True) if the input is low.
4+
Following is the truth table of a XOR Gate:
5+
------------------------------
6+
| Input | Output |
7+
------------------------------
8+
| 0 | 1 |
9+
| 1 | 0 |
10+
------------------------------
11+
Refer - https://www.geeksforgeeks.org/logic-gates-in-python/
12+
"""
13+
14+
15+
def not_gate(input_1: int) -> int:
16+
"""
17+
Calculate NOT of the input values
18+
>>> not_gate(0)
19+
1
20+
>>> not_gate(1)
21+
0
22+
"""
23+
24+
return 1 if input_1 == 0 else 0
25+
26+
27+
def test_not_gate() -> None:
28+
"""
29+
Tests the not_gate function
30+
"""
31+
assert not_gate(0) == 1
32+
assert not_gate(1) == 0
33+
34+
35+
if __name__ == "__main__":
36+
print(not_gate(0))
37+
print(not_gate(1))

boolean_algebra/or_gate.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
An OR Gate is a logic gate in boolean algebra which results to 0 (False) if both the
3+
inputs are 0, and 1 (True) otherwise.
4+
Following is the truth table of an AND Gate:
5+
------------------------------
6+
| Input 1 | Input 2 | Output |
7+
------------------------------
8+
| 0 | 0 | 0 |
9+
| 0 | 1 | 1 |
10+
| 1 | 0 | 1 |
11+
| 1 | 1 | 1 |
12+
------------------------------
13+
Refer - https://www.geeksforgeeks.org/logic-gates-in-python/
14+
"""
15+
16+
17+
def or_gate(input_1: int, input_2: int) -> int:
18+
"""
19+
Calculate OR of the input values
20+
>>> or_gate(0, 0)
21+
0
22+
>>> or_gate(0, 1)
23+
1
24+
>>> or_gate(1, 0)
25+
1
26+
>>> or_gate(1, 1)
27+
1
28+
"""
29+
return int((input_1, input_2).count(1) != 0)
30+
31+
32+
def test_or_gate() -> None:
33+
"""
34+
Tests the or_gate function
35+
"""
36+
assert or_gate(0, 0) == 0
37+
assert or_gate(0, 1) == 1
38+
assert or_gate(1, 0) == 1
39+
assert or_gate(1, 1) == 1
40+
41+
42+
if __name__ == "__main__":
43+
print(or_gate(0, 1))
44+
print(or_gate(1, 0))
45+
print(or_gate(0, 0))
46+
print(or_gate(1, 1))

boolean_algebra/xnor_gate.py

Lines changed: 48 additions & 0 deletions

0 commit comments

Comments
 (0)