Created
November 6, 2020 09:08
-
-
Save Luro02/16b2f2943adc9e4743f4d9a0a737cca7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# | |
# Written for Python 3.7 | |
from __future__ import annotations | |
from typing import Callable | |
def and_x(a: bool, b: bool) -> bool: | |
return a and b | |
def or_x(a: bool, b: bool) -> bool: | |
return a or b | |
def not_x(a: bool) -> bool: | |
return not a | |
def xor_x(a: bool, b: bool) -> bool: | |
return a ^ b | |
def nand_x(a: bool, b: bool) -> bool: | |
return not and_x(a, b) | |
def print_truth_table(f: Callable[[bool, bool], bool]) -> None: | |
print("_" * 25) | |
print("| A | B | |") | |
print("-" * 25) | |
for (left, right) in ((True, True), (True, False), (False, True), (False, False)): | |
print(f"| {'W' if left else 'F':4}| {'W' if right else 'F':4}| {'W' if f(left, right) else 'F':4}|") | |
print("-" * 25) | |
def xor(left: bool, right: bool) -> bool: | |
return not_x(or_x(and_x(left, right), not_x(or_x(left, right)))) | |
print_truth_table(and_x) | |
print("\n") | |
print_truth_table(xor) | |
print("\n") | |
print_truth_table(nand_x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment