15. Median


#1

I’m having a bit of trouble finding the median for a list of an odd amount of numbers. Please help.

def median(number_list):
  
  number_list = sorted(number_list)
  if len(number_list) % 2 != 0:
# Odd amount of numbers
    result = number_list[len(number_list) / 2]
  else:
# Even amount of numbers 
    result = number_list[(len(number_list) / 2) + (len(number_list) / 2 - 1) / 2]
  return result

print median([1, 3, 5, 6, 9, 10])

median([4, 5, 5, 4]) returned 5 instead of 4.5


#2

the problem is here:

result = number_list[(len(number_list) / 2) + (len(number_list) / 2 - 1) / 2]

you will need to get left middle and right middle value from number_list first, then add these two numbers together and divide by two. Don’t forget order operations, parentheses might be needed


#3

I have the left middle and right middle and i’m adding them together and dividing them by 2 while following order of operations, but it’s still not giving me the correct median.

def median(number_list):
# Right and left median numbers for even amount of numbers
  right_side = number_list[(len(number_list) / 2)]
  left_side = number_list[((len(number_list) / 2) - 1)]                        
  number_list = sorted(number_list)
  if len(number_list) % 2 != 0:
# Odd amount of numbers
    result = number_list[len(number_list) / 2]
  else:
# Even amount of numbers 
    result = (right_side + left_side) / 2 
  return result

print median([1, 3, 5, 6, 9, 10])

median([4, 5, 5, 4]) returned 5 instead of 4.5


#4

there’s nothing stopping you from adding prints in your code to print out everything you’re doing and see where things go wrong and therefore what you need to adjust

though, rather than printing out everything, it would be more efficient to print things out roughly in the middle of the process to figure out if the mistake has been made at that point which tells you where to continue looking, either before that point, or after
but then again not much is being done so writing out a full account of what your program does isn’t particularly crazy


#5

I found the problems. I defined the right and left side before I did sorted(number_list), and I needed to divide by the float 2.0 instead of just the integer 2.

"""Takes a list of numbers and returns the median"""

def median(number_list):
# Right and left median numbers for even amount of numbers
  print "Before sort: " + str(number_list)
  number_list = sorted(number_list)
  print "After sort " + str(number_list)
  right_side = number_list[(len(number_list) / 2)]
  left_side = number_list[((len(number_list) / 2) - 1)]
  if len(number_list) % 2 != 0:
    print "Odd amount of numbers"
# Odd amount of numbers
    result = number_list[len(number_list) / 2]
  else:
    print "Even amount of numbers"
# Even amount of numbers 
    print "Left Median " + str(left_side)
    print "right Median " + str(right_side)
    result = (right_side + left_side) / 2.0 
  return result

print median([5, 10, 9, 1, 6, 3])

median([5, 10, 9, 1, 6, 3]) returns 5.5


#6

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