15/15 median, need help with the code


#1



Replace this line with your code.


#2

Hi!

Although it's best if you post what you have so far, so we can figure out the best way to help your understanding, I am more than happy to walk you through this problem. I will give you the framework for the code, but I want you to figure out the more thoughtful parts.

That will ensure that in the future, you'll have a better shot of figuring out problems without a hitch.

Let's think about the problem from a high-level perspective. Finding the median of a list of numbers requires a few steps, mathematically.


First we need to sort the list, because the median is the middle number of a sorted sequence of numbers.

From a programming perspective, we already have our first task: create a function that accepts a sequence of numbers as a parameter and sort it!

We start with:

def median(numbers): #where numbers is a sequence of values

The most appropriate data structure for this problem would be a list. So numbers holds an arbitrary list of values.

We need to sort this list to find the median. Python has a sorted() function which accepts any iterable and returns a new iterable sorted from least to greatest.

Added to our program:

def median(numbers):
    numbers = sorted(numbers) #here I just reassigned numbers to its sorted version

Now, we're ready to find the median of the list. To do this, we must first figure out if our list contains an even number of values or an odd number - this will affect how we calculate the median.

We can easily figure this out using the modulo (%) operator and the len() function - think about how you would design a function to test whether a number is even or odd, and replace the number with the length of the list!


For an even length list, we need to get the two middle values of the list and average them together (this implies using a float - i.e. 2.0 versus just 2 - so we can get a decimal, if need be!)

By default, Python returns the index of the latter of two middle values in an even-length list when a list is divided in half. In other words, for list = [1, 2, 3, 4, 5, 6], the function len(list)/2 returns the index 3, which corresponds to the value 4. To get the index of the other value, we can simply subtract one from this index.

Let's set a variable x equal to the index of the latter middle value, to make it easier to work with.

So far, we have:

def median(numbers):
    numbers = sorted(numbers)
    if #even:
        x = len(numbers)/2
        return #average of the two middle values - use a float when you divide!

We can handle the case of an odd length list with our else statement. Here, we can just use the len() function because it will return the index of the middle value of an odd length list if we divide it in half.

All together:

def median(numbers):
    numbers = sorted(numbers)
    if #even:
        x = len(numbers)/2
        return #average of the two middle values - use a float when you divide!
    else:
        return #middle value

Remember, you need to use the index of the middle values to return and calculate them correctly, using the list[index] format. We want to return the values, not the indexes.

Hope this helps your understanding!

Let me know if you have any more questions.


#3

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