Created
July 31, 2021 16:08
-
-
Save maxsei/b930f61294ac2663b99da7578da1ec50 to your computer and use it in GitHub Desktop.
This python code demonstrates distributive law of boolean logic
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
# coding: utf-8 | |
from itertools import product | |
def logic(x, y, z): | |
""" | |
( | |
X OR (y AND z) = (x OR y) AND (x OR z) | |
AND | |
X AND (y OR z) = (x AND y) OR (x AND z) | |
) | |
""" | |
a = x or (y and z) | |
b = (x or y) and (x or z) | |
c = x and (y or z) | |
d = (x and y) or (x and z) | |
return (a == b) and (c == d) | |
inputs = list(product([0, 1], repeat=3)) | |
print(*inputs, sep="\n") | |
""" | |
>>> (0, 0, 0) | |
>>> (0, 0, 1) | |
>>> (0, 1, 0) | |
>>> (0, 1, 1) | |
>>> (1, 0, 0) | |
>>> (1, 0, 1) | |
>>> (1, 1, 0) | |
>>> (1, 1, 1) | |
""" | |
for xx in inputs: | |
if logic(*xx): | |
continue | |
raise ValueError("not true!") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment