>>> 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)
| 35 | |
| 36 | |
| 37 | def 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 | |
| 64 | if __name__ == "__main__": |
no test coverage detected