0

Been stuck on this the last while and for the life of me can't find what's wrong. So the exercise in question is:

Write a function called median that takes a list as an input and returns the median value of the list. For example: median([1,1,2]) should return 1.

I'm doing this on codeacadamy and it keeps telling me the answer it's getting is 4 (should be 4.5 as the test list there using to check my code is [4,5,5,4]). To make things even weirder for me, I tried the code on a console IDE on the cscircles website (like how its feels) and that says its fine.

def median(y):
    x = sorted(y)
    x_len = len(x)
    if x_len % 2 == 0:
        start = x_len // 2
        median = (x[start-1] + x[start]) / 2
        return median
    else:
        start = x_len // 2
        median = x[start]
        return median
OneCricketeer
  • 126,858
  • 14
  • 92
  • 185
  • If your output is not guaranteed to be monotonic, to find median you need to sort list beforehand. Also, integer division in Python 2 (as mentioned by other people). – Łukasz Rogalski Nov 04 '16 at 18:47
  • Possible integer division. Try adding from __future__ import division at the top of the file – wrdeman Nov 04 '16 at 18:47
  • By the way [finding the median of a list](https://stackoverflow.com/questions/24101524/finding-median-of-list-in-python?rq=1) has already been asked – OneCricketeer Nov 04 '16 at 18:51

4 Answers4

2

Open a Python 2 REPL and try the following:

>>> (4+5)/2
4

You can force float division by using 2.0.

>>> (4+5)/2.0
4.5

In Python 3 the first example would produce 4.5, which would account for the difference between CodeCademy and the other site.

You can also see if adding the following import makes your code work on CodeCademy:

from __future__ import division
Michael Kohl
  • 63,285
  • 10
  • 129
  • 152
  • The reason they get different results from different online systems is likely due to some of them using Python 3, in which "real" division is the default. It appears OP has learned Python 3 as well, due to their use of the `//` operator. – L3viathan Nov 04 '16 at 18:48
  • Yes, I had just updated my answer while your comment appeared to clarify that. – Michael Kohl Nov 04 '16 at 18:49
  • @L3viathan `//` works in many earlier versions of Python as well, the only thing that changed is the default behavior of `/`. You can change that easily in 2.7 with `from __future__ import division`. This confusion is exactly why Python 3 changed things. – Mark Ransom Nov 04 '16 at 18:52
  • @L3viathan yes i did a course a while back learning python 3 and now im just speeding through this one for python 2 even though as far as i can tell the differences are minor. – Mark Mcgrath Nov 04 '16 at 18:56
  • @MarkMcgrath so just use `from __future__ import division` in your code, it is always the correct way to do it in Python 2. – Antti Haapala Nov 04 '16 at 18:56
  • @MarkRansom I know it _works_, but if you write Python 2 code exclusively you usually don't use it. – L3viathan Nov 04 '16 at 19:18
1

Cast as a float first. float((x[start-1] + x[start]))/2

Albert Rothman
  • 658
  • 7
  • 25
  • aha bang on. it was converting to an int so it rounded down to 4. got it! thanks very much for the help. that one was annoying the hell out me as it should have been easy for me. – Mark Mcgrath Nov 04 '16 at 18:51
0

just a minor mistake

def median(y):
 x = sorted(y)
 x_len = len(x)
 if x_len % 2 == 0:
  start = x_len // 2
  median = (x[start-1] + x[start]) / 2.0
  return median
else:
 start = x_len // 2
 median = x[start]
 return median

division of two int is always a int. you should change one part as float

mukesh kudi
  • 709
  • 5
  • 18
0
def median(a):
    ordered = sorted(a)
    length = len(a)
    return float((ordered[length/2] + ordered[-(length+1)/2]))/2
Vivek Srinivasan
  • 1,759
  • 1
  • 14
  • 15