"sorted" doesn't work for me in the median function


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/2/5?curriculum_id=4f89dab3d788890003000096


The "sorted" built in function does not work inside my median function, but "sorted" works just fine for a simple example outside of function.


Both uses of "sorted" as seen in code below should print a sorted list but use in median does not.


def median(x):
    sorted(x)
    return x
x = [5,4,1,2,3]
print "median result", median(x)
y = [6,3,5,2,1]
print "simple example result", sorted(y)
# example results
# median result [5, 4, 1, 2, 3]
# simple example result [1, 2, 3, 5, 6]


#2

Nothing results from this since no assignment is made.

n = sorted(x)

To do an in-place sort use,

x.sort()

Returning before anything has been evaluated? Where is the median?


#3

Thanks for the response. I thought that I had tried "n = sorted(x)" before without success. But, it works for me now. Now it is on to computation of the median.


#4

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