Skip to content

Instantly share code, notes, and snippets.

@Luro02
Created Nov 6, 2020
Embed
What would you like to do?
#!/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