jeudi 18 novembre 2021

Write swapped conditions with if-else efficiently (python)

I was trying to code conditions with 2 objects, which their conditions could be swapped. Looks something like:

# pseudo code
if (obj_a meets condition_a and obj_b meets condition_b) or 
   (obj_a meets condition_b and obj_b meets condition_a): return True
else: return False

But this seems very hard-coded, if I had more objects and more conditions, that's gonna be a bunch of identical lines with swapped conditions. How do I manage to code this efficiently? for example:

# objs are Capitalized, conditions have a trailing tail for easier reading. (pseudo code)
if (A.a_ and B.b_ and C.c_) or
   (A.b_ and B.c_ and C.a_) or
   (A.c_ and B.a_ and C.b_): return True
else: return False

Aucun commentaire:

Enregistrer un commentaire