Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Simplify this function utilizing less ors & ands?

Both p and s are tuples with values like (1,9), (4,7), (5,6), etc.
The function checks if a node is close to another node to find the best path.

def prox(p,s):
    if (p[0]==s[0]+1 and p[1]==s[1]) or (p[0]==s[0]-1 and p[1]==s[1]) or (p[1]==s[1]+1 and p[0]==s[0])  or (p[1]==s[1]-1 and p[0]==s[0]) or (p[0]+1==s[0] and p[1]==s[1])  or (p[0]-1==s[0] and p[1]==s[1]) or (p[1]+1==s[1] and p[0]==s[0]) or (p[1]-1==s[1]  and p[0]==s[0]):
        return True
    else:
        return False

It works fine but I feel like there’s a better way to write it

I didn’t try much else because I’m new to Python and even in other languages, because I never used tuples before.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Let’s do this a little at a time. You’re new to Python, so if you don’t understand all the tools used here, that’s fine! I’d recommend picking the most concise code that you personally still find readable at this point. I’ve provided links to learn more about the builtins and language constructs used.

First of all, you’re using this classic antipattern:

if boolean_condition:
    return True
else:
    return False

boolean_condition is already True or False, so anytime you see this structure, you can simplify it to just:

return boolean_condition

That already gets us down to:

def prox(p, s):
    return (p[0]==s[0]+1 and p[1]==s[1]) or (p[0]==s[0]-1 and p[1]==s[1]) or (p[1]==s[1]+1 and p[0]==s[0])  or (p[1]==s[1]-1 and p[0]==s[0]) or (p[0]+1==s[0] and p[1]==s[1])  or (p[0]-1==s[0] and p[1]==s[1]) or (p[1]+1==s[1] and p[0]==s[0]) or (p[1]-1==s[1]  and p[0]==s[0])

Now, you want to check if the distance between the two nodes is exactly 1, correct? So either the x or y coordinates need to be one apart, but not both. That means that if you subtract the two xs and take the absolute value of that difference, then do the same for the ys, then add the two differences, it should equal 1.

We could start by writing this as a for loop and zip to iterate through both tuples together, adding up the absolute value of differences:

def prox(p, s):
    total_difference = 0
    for first, second in zip(p, s):
        total_difference += abs(first - second)
    return total_difference == 1

This can be condensed using Python’s built-in sum function and a generator expression:

def prox(p, s):
    return sum(abs(first - second) for first, second in zip(p, s)) == 1

Bonus: this same function generalizes to three (or any number of) dimensions. Probably not useful in this case, but neat.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading