Median - pmp


#1



Works in Spyder but not in here? It returns 4 instead of 4.5 when given [4,5,5,4] however in Spyder it returns 4.5.

Any help would be greatly appreciated !


def median(imp):
    
    imp.sort()
    midindex2=[]
    length = len(imp)
    
    if length%2 != 0:
        midindex = int(length-1)/2
        med = imp[midindex]
        return med
    else:
        i = int(length/2)
        i = i-1
        midindex2.append(i)
        t=i+1
        midindex2.append(t)
        
        ans = (imp[midindex2[0]]+imp[midindex2[1]])/2
              
    return ans


#2

in python3, a change was made in division behavior:

# python3
print 9 / 2 # 4.5
# python2
print 9 / 2 # 4

the number was no longer floored/rounded down. To overcome this problem in python2, use a float in the division


#3

Excellent, thank you!


#4

#5

your spyder seems to run python3, good to hear you solved it :slight_smile: