Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Boolean Algebra by rojeeer
OPERATION_NAMES = (
"conjunction", "disjunction", "implication", "exclusive", "equivalence")
def boolean(x, y, operation):
# conjunction (x & y) = (x*y)
if operation == OPERATION_NAMES[0]:
return x * y
# disjunction (x | y) = (x + y)
elif operation == OPERATION_NAMES[1]:
return (x+y)>0
# implication (x -> y) = (!x | y) = (x+1)%2 + y
elif operation == OPERATION_NAMES[2]:
return ((x + 1) % 2 + y)>0
# exclusive (x XOR y) = (!x & y) | (x & !y)
# = ((x+1)%2)*y + x*((y+1)%2)
elif operation == OPERATION_NAMES[3]:
return (((x + 1) % 2) * y + x * ((y + 1) % 2))>0
# equivalence (x=y) = (x & y) | (!x & !y)
elif operation == OPERATION_NAMES[4]:
return (x * y + ((x + 1) % 2) * ((y + 1) % 2))>0
Dec. 4, 2015