Median


#1



Hi,
I'm not going to lie. If I do not know the solution I check on google what to do and going through different solutions to try understand it.
Here is the one I cannot figure it out.
How is lst[n//2] finding a median with a odd list of numbers.
What does lst[n//2] exactly do?. Please advise


numbers = [4, 9, 10, 13, 15, 16, 17]

def median(lst):
    lst = sorted(lst)
    n = len(lst)
    if n < 1:
            return None
    if n % 2 == 1:
            return lst[n//2]
print median(numbers)


#2

n is the length, for odd lists the length divided by two is the middle index

// is floor division (rounds down to integer) rather then normal division

You can do the math, for odd lists length divided by two gives middle index, this index we can then use to retrieve middle value (median)


#3

This is great. Thank you for explanations.
I did not know it will always return the middle index.


#4

well, you can do the math:

1 // 2 # 0
3 // 2 # 1
5 // 2 # 2

and so on. if the length is 3, the indexes are 0, 1 and 2 so 1 is the middle index

we can use the same logic for even lists, length divided by two gives right middle index, so length didivided by two gives the left middle index (we need the two middle values for even lists)


#5

sum(lst[n//2-1:n//2+1])/2.0

I understand this will find two middle numbers and divide their sum by two and returns a float number from division?.


#6

yep, it will


#7

Thank you. Things become so much easier to understand with explanations.


#8

honestly, this is bad. You should see how far you can come, then come to the forum, and ask for a step in the right direction

Just going through the different solutions mean you won't be able to solve the problems yourself

problem solving is the fundamental thing of programming, coding (what this course up until this point) is just a small bit of programming


#9

When I run the following code in idle, it returns 4.5:
x =[4,5,5,4]
def median(x):
x.sort()
print(x)
if len(x)%2 == 0:
print('even')
num1 = ((len(x)/2)-1)
num2 = ((len(x)/2))
even_median = (x[int(num1)] + x[int(num2)])/2
return even_median

else:
    print('odd')
    return x[int((len(x)-1)/2)]

CodeAcademy, however, gives me the following error message:

"Oops, try again. median([4, 5, 5, 4]) returned 4 instead of 4.5"

What gives?


#10

the different version of python, in python3 integer division doesn't floor (round down) anymore

to overcome this problem in the lesson, the division should involve a float


#11

What version of Python does CodeAcademy use?


#12

import sys
print sys.version

its 2.7.3, but you can run the above code to be sure


#13

Error : Oops, try again. median([4, 5, 5, 4]) returned 5.0 instead of 4.5
What's the mistake?

def median(dup):
    sorted(dup)
    length = len(dup)
    if length % 2 != 0:
        return dup[(length-1)/2]
    else :
        return (dup[(length/2)] + dup[(length/2) - 1]) / 2.0

#14

you don't sort the list. Well, you do, but then you don't store the sorted list anywhere


#15

It shows an error.
Traceback (most recent call last):
File "python", line 19, in
TypeError: full_time_wage() takes exactly 2 arguments (1 given)

help me to debug the code.

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

class Employee(object):
    """Models real-life employees!"""
    def __init__(self, employee_name):
        self.employee_name = employee_name

    def calculate_wage(self, hours):
        self.hours = hours
        return hours * 20.00

# Add your code below!
class PartTimeEmployee(Employee):
    def calculate_wage(self, hours):
        self.hours = hours
        return hours * 12.00
    def full_time_wage(self, hours):
            return super(PartTimeEmployee, self).calculate_wage()
        
milton = PartTimeEmployee("Elic")
print milton.full_time_wage()

#16

here:

print milton.full_time_wage()

how many hours did milton work? For how many hours should he get paid? You need to specify this number as argument


#17

Still i am getting error.

Traceback (most recent call last):
File "python", line 19, in
File "python", line 16, in full_time_wage
TypeError: calculate_wage() takes exactly 2 arguments (1 given)


#18

can i see an updated version of your code? You did give a argument (number of hours worked) to the method call, right?


#19

Yes.

class Employee(object):
    """Models real-life employees!"""
    def __init__(self, employee_name):
        self.employee_name = employee_name

    def calculate_wage(self, hours):
        self.hours = hours
        return hours * 20.00

# Add your code below!
class PartTimeEmployee(Employee):
    def calculate_wage(self, hours):
        self.hours = hours
        return hours * 12.00
    def full_time_wage(self, hours):
            return super(PartTimeEmployee, self).calculate_wage()
        
milton = PartTimeEmployee("Elic")
print milton.full_time_wage(10)

#20

and now the exercise url, because this topic isn't anywhere the exercise you are at