Question
Why is my median incorrect?
Answer
Finding the median of a list of numbers can be especially difficult if it’s an even number of numbers. To ensure that you’re actually working with the values you think you are, it can be very helpful to print
them all out at each step. For example, when you sort your list, print your_sorted_list
to see if it worked. When you calculate the midpoint, print your_midpoint
to see if it’s the value at the middle index, or if it’s the middle index.
Half the battle is knowing what you’re working with at each step of the way. There are so many approaches to this particular problem that it’s not very meaningful to try and guess what could be wrong in your particular implementation. However, it will always be helpful to print your code out in steps, as that’s a major part of debugging!
Once you know what your values actually store, then you can proceed to make necessary changes with everything we’ve learned so far.