I want to use boolean algebra instead of several conditionals, for example
def boo(x,y):
if x==3 and y==1: return 4
if x==3 and y==4: return 1
if x==4 and y==1: return 2
if x==4 and y==2: return 1
if x==5 and y==1: return 3
what I want to do is
def simple(x,y):
return x#y
and there are total 12 equations, I just want to directly return (x#y) where # is a boolean operator. I did this to a smaller problem where I luckily found out a relation. I want to do the same in this case also, how do I proceed with it?
Does this have any performance gains, because its not going through several if conditionals?
Is this normal practice?
sample:
x y output
1 2 3
1 3 2
1 4 5
1 5 4
here a simple bitwise xor gate will do
def(x,y): return x^y
I don’t know whether trying to find a terser expression of the above logic would lead to more readable code; probably not. But you can rework the logic as-is to a more mathematical formulation:
Another option is to use a dictionary:
If you know that all values will match the specified cases, you can write
[(x, y)]instead of.get((x, y), None).