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

File tree

4 files changed

+178
-0
lines changed

4 files changed

+178
-0
lines changed

boolean_algebra/nand_gate.py

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

boolean_algebra/not_gate.py

+37
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

+46
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

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
A XNOR Gate is a logic gate in boolean algebra which results to 0 (False) if both the
3+
inputs are different, and 1 (True), if the inputs are same.
4+
It's similar to adding a NOT gate to an XOR gate
5+
6+
Following is the truth table of a XNOR Gate:
7+
------------------------------
8+
| Input 1 | Input 2 | Output |
9+
------------------------------
10+
| 0 | 0 | 1 |
11+
| 0 | 1 | 0 |
12+
| 1 | 0 | 0 |
13+
| 1 | 1 | 1 |
14+
------------------------------
15+
Refer - https://www.geeksforgeeks.org/logic-gates-in-python/
16+
"""
17+
18+
19+
def xnor_gate(input_1: int, input_2: int) -> int:
20+
"""
21+
Calculate XOR of the input values
22+
>>> xnor_gate(0, 0)
23+
1
24+
>>> xnor_gate(0, 1)
25+
0
26+
>>> xnor_gate(1, 0)
27+
0
28+
>>> xnor_gate(1, 1)
29+
1
30+
"""
31+
return 1 if input_1 == input_2 else 0
32+
33+
34+
def test_xnor_gate() -> None:
35+
"""
36+
Tests the xnor_gate function
37+
"""
38+
assert xnor_gate(0, 0) == 1
39+
assert xnor_gate(0, 1) == 0
40+
assert xnor_gate(1, 0) == 0
41+
assert xnor_gate(1, 1) == 1
42+
43+
44+
if __name__ == "__main__":
45+
print(xnor_gate(0, 0))
46+
print(xnor_gate(0, 1))
47+
print(xnor_gate(1, 0))
48+
print(xnor_gate(1, 1))

0 commit comments

Comments
 (0)