Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Boolean Algebra by grutte_pier
# mapping of mathematical names to boolean operations
OPERATION_NAMES = {"conjunction": lambda x, y: x and y,
"disjunction": lambda x, y: x or y,
"implication": lambda x, y: not x or y,
"exclusive": lambda x, y: x != y,
"equivalence": lambda x, y: x == y
}
def boolean(x, y, operation):
# Use the global mapping to execute the operation, then
# transform it into either a one or zero again
return 1 if OPERATION_NAMES[operation](x, y) else 0
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert boolean(1, 0, "conjunction") == 0, "and"
assert boolean(1, 0, "disjunction") == 1, "or"
assert boolean(1, 1, "implication") == 1, "material"
assert boolean(0, 1, "exclusive") == 1, "xor"
assert boolean(0, 1, "equivalence") == 0, "same?"
Sept. 7, 2014