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

How to compare integers with accuracy in Python?

I have a tuple for color. For example, (218, 174, 84). The task is to increase each red, green and blue value by some increment and then compare it with an accuracy 1. How can I do this in Pythonic way? Do you know best practices?

Color: (218, 174, 84)
Increment: 5

For red value 222, 223, 224 is legal. Green: 178, 179, 180, Blue: 88, 89, 90.

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 :

def valid_color(orig_color, new_color, increment):
    return all(c1 + increment - 1 <= c2 <= c1 + increment + 1 for c1, c2 in zip(orig_color, new_color))

Use zip() to pair up the components of the original color and the color you’re comparing with. Then use comparison operators to test that each component is valid.

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