MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / truth_table

Function truth_table

boolean_algebra/nor_gate.py:37–61  ·  view source on GitHub ↗

>>> print(truth_table(nor_gate)) Truth Table of NOR Gate: | Input 1 | Input 2 | Output | | 0 | 0 | 1 | | 0 | 1 | 0 | | 1 | 0 | 0 | | 1 | 1 | 0 |

(func: Callable)

Source from the content-addressed store, hash-verified

35
36
37def truth_table(func: Callable) -> str:
38 """
39 >>> print(truth_table(nor_gate))
40 Truth Table of NOR Gate:
41 | Input 1 | Input 2 | Output |
42 | 0 | 0 | 1 |
43 | 0 | 1 | 0 |
44 | 1 | 0 | 0 |
45 | 1 | 1 | 0 |
46 """
47
48 def make_table_row(items: list | tuple) -> str:
49 """
50 >>> make_table_row(("One", "Two", "Three"))
51 '| One | Two | Three |'
52 """
53 return f"| {' | '.join(f'{item:^8}' for item in items)} |"
54
55 return "\n".join(
56 (
57 "Truth Table of NOR Gate:",
58 make_table_row(("Input 1", "Input 2", "Output")),
59 *[make_table_row((i, j, func(i, j))) for i in (0, 1) for j in (0, 1)],
60 )
61 )
62
63
64if __name__ == "__main__":

Callers 1

nor_gate.pyFile · 0.85

Calls 2

make_table_rowFunction · 0.85
funcFunction · 0.85

Tested by

no test coverage detected