How to find the marks between >25th percentile and <75th percentile in the increasing order of marks

Consider the marks list of class students given in two lists
Students = [‘student1’,‘student2’,‘student3’,‘student4’,‘student5’,‘student6’,‘student7’,‘student8’,‘student9’,‘student10’]
Marks = [45, 78, 12, 14, 48, 43, 45, 98, 35, 80]
from the above two lists the Student[0] got Marks[0], Student[1] got Marks[1] and so on.

Who got marks between >25th percentile <75th percentile, in the increasing order of marks.

Ex 1:
Students=[‘student1’,‘student2’,‘student3’,‘student4’,‘student5’,‘student6’,‘student7’,‘student8’,‘student9’,‘student10’]
Marks = [45, 78, 12, 14, 48, 43, 47, 98, 35, 80]

output:
student9 35
student6 43
student1 45
student7 47
student5 48

Welcome, @d_p_reddy2004. This looks a lot like a school assignment. Have you started? What have you got so far that you can show us?

def display_dash_board(students, marks):
    #zip the two lists, create a new sorted list based on zip 
    if(len(students)!=0 and len(marks)!=0):
        #zipping
        zipped = zip(students,marks)
        #convert to list
        zipped = list(zipped)
        top_5_students = sorted(zipped,key=lambda x :x[1],reverse=True)
        # printing result 
        print("a.")
        for i in range(5):
            print(top_5_students[i])
        print("\n")
        print("b.")
      
        #median
        dictionaryZipped = dict(zip(students,marks))
        n= len(dictionaryZipped)
        print(IQR(dictionaryZipped,n))
    
def median(sortedDict,l,r):
    n=r-l+1
    n=(n+1)//2 -1
    return n+1
    
def IQR(dictionaryZipped,n):
    sortedDict = sorted(dictionaryZipped,key=lambda x:x[0])
    print(sortedDict)
    # Index of median of entire data 
    mid_index = median(sortedDict, 0, n) 
    print(mid_index)
    # Median of first half 
    Q1 = sortedDict[median(sortedDict, 0, mid_index)] 
    Q1Value=sortedDict.get(Q1)
    print(type(Q1))
    print(Q1Value)
    # Median of second half 
    Q3 = sortedDict[median(sortedDict, mid_index + 1, n)] 
    print(type(Q3))
    print(Q3)
    # IQR calculation 
    return (int(Q3) - int(Q1)) 

could anyone tell me whats wrong in the code.

Well, let’s start with a few basics.

def median(sortedDict,l,r):
    n=r-l+1
    n=(n+1)//2 -1
    return n+1

What is the purpose of passing sortedDict to this function? It’s never used. Either incorporate the len line, and drop the l and the r:

def median(sortedDict):
    n= len(sortedDict)
    n=r-l+1
    n=(n+1)//2 -1
    return n+1

or only have the l and r.

def median(l,r):
    n=r-l+1
    n=(n+1)//2 -1
    return n+1

Or have the function ACTUALLY return the median value, instead of the median index value:

def median(sortedDict,l,r):
    n=r-l+1
    n=(n+1)//2 -1
    return sortedDict[n+1]

Please note, that this is NOT a median calculation. Or a median index calculation. If l = 0, and r = dictionary.length = 6 (for example), then this function will return 4, but it should return either 2 or 3:

[0 1 2 3 4 5] <-- an index representation of your dictionary. Length = 6.
         ^ This is not the median.

My idea is that, if I find the median on a sorted data. I can split the data into two equal parts. Now, I am taking the values from 25th percentile to 75th percentile. For which I have written the code above. Since I am considering a sorted and zipped list, I am not sure how we can calculate the median on the sorted zipped list and print the data in the below format
output:
student9 35
student6 43
student1 45
student7 47
student5 48

Your code does not find the median.

Be careful there. Are you trying to find the mean or the median? They are not the same thing.
The mean of your data is 43.6
The median is 45.

If I have a sorted list of 5 things, how do you define the index of the median, mathematically?

You know the following information:
The first element is at index 0.
The list has a length of 5.
You can calculate any other information you need. Like perhaps the index of the last element. That may be important.

import statistics

def display_dash_board(students, marks):
    #zip the two lists, create a new sorted list based on zip 
    if(len(students)!=0 and len(marks)!=0):
        #zipping
        zipped = zip(students,marks)
        #convert to list
        zipped = list(zipped)
        least_5_students = sorted(zipped,key=lambda x :x[1]) 
       n= len(least_5_students)
       IQR(least_5_students,n)

def IQR(sortedDict,n):
   
    numbers = [sortedDict[key] for key in sortedDict]
    median_ = statistics.median(numbers,0,len(sortedDict))
    print(median_)

****************
<ipython-input-34-8b765d36dfc8> in <listcomp>(.0)
     40 def IQR(sortedDict,n):
     41 
---> 42     numbers = [sortedDict[key] for key in sortedDict]
     43     median_ = statistics.median(numbers,0,len(sortedDict))
     44     print(median_)

TypeError: list indices must be integers or slices, not tuple

Is there a way to fix this.

I mean, if you’re allowed to use premade libraries then

import scipy.stats
scipy.stats.iqr(sortedDict)

and your problem is solved.

sortedDict[key] for key in sortedDict
should be
v for v in sortedDict.values()

Yes, But I am not allowed to use premade libraries.

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