Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
match..case solution in Clear category for Boolean Algebra by mu_py
OPERATION_NAMES = (
"conjunction",
"disjunction",
"implication",
"exclusive",
"equivalence",
)
def boolean(x: bool, y: bool, operation: str) -> bool:
match operation:
case 'conjunction': return x and y
case 'disjunction': return x or y
case 'implication': return x != 1 or y==1
case 'exclusive': return x != y
case 'equivalence': return x == y
case _: raise KeyError(f'unknown operation <{operation}>') # just in case ;-)
print("Example:")
print(boolean(1, 0, "conjunction"))
assert boolean(0, 0, "conjunction") == 0
assert boolean(0, 1, "conjunction") == 0
assert boolean(1, 0, "conjunction") == 0
assert boolean(1, 1, "conjunction") == 1
assert boolean(0, 0, "disjunction") == 0
assert boolean(0, 1, "disjunction") == 1
assert boolean(1, 0, "disjunction") == 1
assert boolean(1, 1, "disjunction") == 1
print("The mission is done! Click 'Check Solution' to earn rewards!")
#check the exception:
#print(boolean(1, 1, "malfunction"))
Oct. 22, 2022
Comments: