Skip to content

Instantly share code, notes, and snippets.

@Luro02
Created November 6, 2020 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Luro02/16b2f2943adc9e4743f4d9a0a737cca7 to your computer and use it in GitHub Desktop.
Save Luro02/16b2f2943adc9e4743f4d9a0a737cca7 to your computer and use it in GitHub Desktop.
#!/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