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 flag a given value based on a range rule

I’ve flags, each range of value has a flag.
for ex :

  • value = 0 is D
  • value > 0 and < 0.2 is C
  • value >=0.2 and <=0.8 is B
  • value > 0.8 is A

flag = ["A", "B", "C", "D"]

def get_flag(value) : 
            if value == 0: return "D"
            elif value > 0.8: return "A"
            elif value <=0.8 and value >= 0.2: return "B"  
            else: return "C"

i think this implementation is annoying and not algorthmically pretty to see, any suggestions so i can get the correct index in python, i thought about modulo and div but values are floats between 0 and 1.

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 :

You don’t need elif and else here, since return ends the execution of the function (so that lines after a return are not executed if the preceding return is triggered):

def get_flag(value) : 
    if value == 0: return "D"
    if value < 0.2: return "C"
    if value <= 0.8: return "B"
    return "A"
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