1
1
"""
2
- A NOR Gate is a logic gate in boolean algebra which results to false(0)
3
- if any of the input is 1, and True(1) if both the inputs are 0.
2
+ A NOR Gate is a logic gate in boolean algebra which results in false(0) if any of the
3
+ inputs is 1, and True(1) if all inputs are 0.
4
4
Following is the truth table of a NOR Gate:
5
- | Input 1 | Input 2 | Output |
6
- | 0 | 0 | 1 |
7
- | 0 | 1 | 0 |
8
- | 1 | 0 | 0 |
9
- | 1 | 1 | 0 |
5
+ Truth Table of NOR Gate:
6
+ | Input 1 | Input 2 | Output |
7
+ | 0 | 0 | 1 |
8
+ | 0 | 1 | 0 |
9
+ | 1 | 0 | 0 |
10
+ | 1 | 1 | 0 |
10
11
11
- Following is the code implementation of the NOR Gate
12
+ Code provided by Akshaj Vishwanathan
13
+ https://www.geeksforgeeks.org/logic-gates-in-python
12
14
"""
15
+ from collections .abc import Callable
13
16
14
17
15
18
def nor_gate (input_1 : int , input_2 : int ) -> int :
@@ -30,19 +33,35 @@ def nor_gate(input_1: int, input_2: int) -> int:
30
33
return int (input_1 == input_2 == 0 )
31
34
32
35
33
- def main () -> None :
34
- print ("Truth Table of NOR Gate:" )
35
- print ("| Input 1 | Input 2 | Output |" )
36
- print (f"| 0 | 0 | { nor_gate (0 , 0 )} |" )
37
- print (f"| 0 | 1 | { nor_gate (0 , 1 )} |" )
38
- print (f"| 1 | 0 | { nor_gate (1 , 0 )} |" )
39
- print (f"| 1 | 1 | { nor_gate (1 , 1 )} |" )
36
+ def truth_table (func : Callable ) -> str :
37
+ """
38
+ >>> print(truth_table(nor_gate))
39
+ Truth Table of NOR Gate:
40
+ | Input 1 | Input 2 | Output |
41
+ | 0 | 0 | 1 |
42
+ | 0 | 1 | 0 |
43
+ | 1 | 0 | 0 |
44
+ | 1 | 1 | 0 |
45
+ """
46
+
47
+ def make_table_row (items : list | tuple ) -> str :
48
+ """
49
+ >>> make_table_row(("One", "Two", "Three"))
50
+ '| One | Two | Three |'
51
+ """
52
+ return f"| { ' | ' .join (f'{ item :^8} ' for item in items )} |"
53
+
54
+ return "\n " .join (
55
+ (
56
+ "Truth Table of NOR Gate:" ,
57
+ make_table_row (("Input 1" , "Input 2" , "Output" )),
58
+ * [make_table_row ((i , j , func (i , j ))) for i in (0 , 1 ) for j in (0 , 1 )],
59
+ )
60
+ )
40
61
41
62
42
63
if __name__ == "__main__" :
43
64
import doctest
44
65
45
66
doctest .testmod ()
46
- main ()
47
- """Code provided by Akshaj Vishwanathan"""
48
- """Reference: https://www.geeksforgeeks.org/logic-gates-in-python/"""
67
+ print (truth_table (nor_gate ))
0 commit comments