I have debugged this code several times, it gives correct result in IDE but fails on this complier. Please help me out in this code


#1

def median(arg):
s=0
k=0
median1=0
ran=len(arg)/2
odd=len(arg)
arg.sort()

if len(arg)%2==0 and len(arg)!=0:
    #print ("The indices are ",(ran,ran+1))
    s=arg[int(ran)-1]
    #print ("S is ",s)
    k=arg[int(ran)]
    #print ("K is ",k)

    median1=(s+k)/2
    #print median1

else:
     if len(arg)!=0 and len(arg)%2!=0:
              s=arg[int(odd)-1]
              print ("s value is ",s)
              median1=s

return median1

print (median([4,5,5,4]))


#2

W.r.t your code..

median1=(s+k)/2

we need a precise value(float) of median,
In python 2.x

/ opertaor
if both operands are integer we will have integer result.
to have float value we need to make one of the operand a float.

Now this line of code..
s=arg[int(odd)-1]

why odd? use arg ?
Due to / we don't even need to subtract one from it now.

After you debug your code,You can delete few unnecessary variables that are just idle there and do nothing.


#3

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