PRACTICE MAKES PERFECT median


#1

i try this code in codecademy but this is not give a proper answer.
then i tried in repl.it and trinklet it show correct and give correct answer.
codecademy has bug.

def median(alist):
  alist.sort()
  print (alist)
  num=len(alist)
  if num>1:
    if num%2!=0:
      new_num=num+1
      median_no=int(new_num/2)
      value=alist[median_no]
      return value
    else:
      num_1=num/2
      new_num2=alist[int(num_1-1)]
      new_num=alist[int(num_1)]
      value=(new_num2+new_num)/2
      return value
  else:
    return alist[num-1]






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

#2

no, division behavior in python2 and python3 is different:

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

#3

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