Skip to content

Commit bd49061

Browse files
authored
Add function for AND gate (#7593)
1 parent e2a83b3 commit bd49061

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

boolean_algebra/and_gate.py

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

0 commit comments

Comments
 (0)