15. Median - Help, can't go through


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/2/5?curriculum_id=4f89dab3d788890003000096


Oops, try again. median([4, 5, 5, 4]) returned 4 instead of 4.5


return the correct median


def median(numeros):
    numeros.sort()
    len_numeros = len(numeros)
    if len_numeros%2 == 0:
        medeven = ((len_numeros)/2)
        mediana = (numeros[int(medeven)] + numeros[int(medeven-1)]) /2
    elif len_numeros == 1:
        mediana = 1
    else:
        medianaindex = ((len_numeros-1)/2)
        mediana = numeros[int(medianaindex)]
    return mediana
print (median(numeros))


#2

medeven = int(len_numeros / 2)
if len_numeros % 2 == 0:
    mediana = float(numeros[medeven] + numeros[medeven-1]) / 2
else:
    mediana = numeros[medeven]

#3

Make shure you have at least one operand of your division in 'float' type. This automatically force result to be a floating number.

 mediana = (numeros[int(medeven)] + numeros[int(medeven-1)]) /2.0

#4

My code looks similar, can you please explain why it isn't working even though I have included float for the result?

def median(numbers):
    
    sortlist = sorted(numbers)
    
    if len(sortlist) % 2 == 1:
        y = len(sortlist) / 2
        x = int(y)
        return sortlist[x]
    else:
        a = len(sortlist) / 2
        return float((sortlist[a-1] + sortlist[a]) / 2)
        
        
print median([4,4,5,5])

The error is the same, with same example [4,5,5,4] results with 4 instead of 4.5


#5

Cast to float an operand, not result.
c = a / float(b)


#6

Converting the numerator to a float is not a good practice. It is a counting number and should rightly be left as one, rather than massaging it into a float.

The numerator is where the float belongs.

return float(sortlist[a-1] + sortlist[a]) / 2

Good point, as long as we cast the number that is most likely to be a float, not the counting number. Implicit casting is a dangerous road to go down if we want our code to be readable and make sense. Massaging working values never ends well.


#7

Thank you for your help, however can you please explain why it doesn't work when I leave the number "2" as a float too?


#8

Because it is massaging an already internally defined result, which may already be an integer in Python 2, so it stays an integer value, sans any decimal fraction, even while we have cast it as a float. As @systemcoder68263 alluded, cast one of numerator or denomiator, not the quotient.

For this reason, I harp on the retention of any counting numbers in the division. Those values or expressions that do not contain a variable are treated as their own type. An expression that contains a variable may be type cast arbitrarily without disrupting the logic and semantic flow of the program for a reader.

Maybe it's just me. I cringe when I see division by 2.0 or anything point zero. It's just too artificial. I'm glad that Python 3 got away from that and just returns floats, period, without our massaging them. It is now an imperative to declare integers. Finally we get to see the function when it literally applies.


#9

Thank you @mtf for important notice! It really does make sense! I want to know more about readeble code, could you suggest a book or other information sources,please?


#10

If I had an Amazon affiliation I could proably come up with a few... Truth be told, all you need is online. Read the documentation.


#11

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.