Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6635879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:06:45+00:00 2026-05-25T23:06:45+00:00

My professor wrote this median function and I don’t understand it very well. Can

  • 0

My professor wrote this median function and I don’t understand it very well. Can someone please explain the part about i = len(list)/2 and median = avg() and the else statement?

def avg_list(numbers):  
    sum = 0 
    for num in numbers:
        sum += num

    avg = float(sum)/len(numbers)
    print avg

def median(list):            
    list.sort()
    if len(list)%2 == 0:
        #have to take avg of middle two
        i = len(list)/2
        median = avg()
    else:
        #find the middle (remembering that lists start at 0)
        i = len(list)/2
        median = list        
    return median

To add from an example I saw, for even list length:

def median(s):
    i = len(s)
    if not i%2:
        return (s[(i/2)-1]+s[i/2])/2.0
    return s[i/2]

This works very well but I don’t understand the last return s[i/2]?

For odd list length:

x = [1,2,5,2,3,763,234,23,1,234,21,3,2134,23,54]
median = sorted(x)[len(x)/2]

Since x has a list length of odd, wouldn’t the [len(x)/2] be a floating number index? I’m not getting this all the way? Any explanation better than mine is much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:06:45+00:00Added an answer on May 25, 2026 at 11:06 pm

    We’re missing some code here, but we can puzzle it out.

    The comments here are instructive. When we check:

        if len(list)%2 == 0:
    

    Then we’re checking to see if the list is of even length. If a list has an even number of members, then there is no true “middle” element, and so:

        #have to take avg of middle two
            i = len(list)/2
            median = avg()
    

    We assume that the avg() function is going to return the average of the two middle elements. Since you didn’t include a definition of an avg function, it’s possible that this is really supposed to be an avg_list function taking the middle two elements of the list.

    Now, if the list is of odd length, there is a middle element, and so:

        else:
            #find the middle (remembering that lists start at 0)
            i = len(list)/2
            median = list
    

    Now this looks kinda wrong to me too, but my guess is that the intention is that this should read:

    median = list[i]
    

    That would be us returning the middle element of the list. Since the list has been sorted, that middle element is the true median of the list.

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a professor that doesn't understand source control very well. He asked us
i have wrote the visitor pattern as follow but i don't understand what is
This is for a homework assignment, so I don't want the exact code, but
Preface: This question is about a project I am working on with a professor
This is something the professor showed us in his scripts. I have not used
(This is NOT a homework help question) I was asked by my professor to
This is part of a homework assignment. What we have to do is write
My professor defined this in the .h file void list_map(INTLIST* list, void (*f)(void *));
Here's a very basic question I have. In my professor's lecture slide, there is
During this semester my professor tried to convince us why is good to use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.