3
Students=['student1','student2','student3','student4','student5','student6','student7','student8','student9','student10'] 
Marks = [45, 78, 12, 14, 48, 43, 47, 98, 35, 80]


def display_dash_board(students, marks):

    dictionary = dict(zip(Students,Marks))

    # write code for computing top top 5 students
    print("Top 5 Students are :\n\n")
    for key, value in sorted(dictionary.items(), key=lambda item: item[1],reverse=True)[:5]:
          print("%s: %s" % (key, value))

    # write code for computing top least 5 students
    print("\n\n Top Least 5 Students are : \n\n")
    for key, value in sorted(dictionary.items(), key=lambda item: item[1])[:5]:
          print("%s: %s" % (key, value))    

    # write code for students within 25 to 75 percentile
    print("\n\n Students in 25-75 percentile range are : \n\n")

For Calculating percentile use:-

 max = max_mark

 min = min_mark

 diff = max - min

 pre_25 = diff*0.25

 pre_75 = diff*0.75

display_dash_board(Students, Marks)

AKX
  • 152,115
  • 15
  • 115
  • 172
  • Looks like a job for `pandas` to me. But I assume the assignment does not allow external libraries. :) – timgeb May 25 '20 at 07:32
  • 2
    @timgeb It absolutely doesn't need Pandas. It needs a single list comprehension for filtering. – AKX May 25 '20 at 07:32
  • @AKX if the percentiles are already computed. – timgeb May 25 '20 at 07:34
  • @timgeb ... for which there is code in OP's question. – AKX May 25 '20 at 07:34
  • @AKX yes, so? I just said I would do the whole problem with `pandas`. But I realize this is probably homework so my approach is not applicable. – timgeb May 25 '20 at 07:35
  • you can follow [this thread](https://stackoverflow.com/questions/2374640/how-do-i-calculate-percentiles-with-python-numpy) for several possible approaches. – hack3r-0m May 25 '20 at 07:37
  • @hack3r_0m OP's post already has the math for percentile, and this doesn't involve Numpy. – AKX May 25 '20 at 07:38

2 Answers2

6

Given the hints for performing percentile calculation,

max_mark = max(marks)
min_mark = min(marks)
diff = max_mark - min_mark
pre_25 = diff * 0.25
pre_75 = diff * 0.75

you can then filter your dictionary to only the scores matching these limits,

within_25_75 = {
   name: score 
   for (name, score) 
   in dictionary.items() 
   if pre_25 <= score <= pre_75
}

and print them as you've done before.

AKX
  • 152,115
  • 15
  • 115
  • 172
  • I want to find out percentile with below procedure only max = max_mark min = min_mark diff = max - min pre_25 = diff*0.25 pre_75 = diff*0.75 – pravin kamble May 25 '20 at 08:55
  • That is the same procedure as here, only with the correct variables. – AKX May 25 '20 at 09:34
0

One liner. Because 1/4 is 25th percentile and 3/4 is 75th percentile.

import math
[print(k[0], k[1]) for k in
 list(sorted(dictionary.items(), key=lambda item: item[1]))[math.ceil(len(Marks) / 4):math.floor(3 * len(Marks) / 4)]]

#Output
student6 43
student1 45
student7 47
student5 48

Explanation: 25th-75th percentile means, print the values between 1/4th length of the sorted list to 3/4th length of the sorted list.

Notice, that we also use floor and ceil. This is to ensure the values are strictly between 25th and 75th percentile only

Abhishek J
  • 2,386
  • 2
  • 21
  • 22
  • Would you be able to easily read that one-liner after you've written it once? – AKX May 25 '20 at 07:43
  • Thats right. But if you add a comment just above it. It explains what happens in the code. And then it gives you benefits as having fewer lines of code to manage and lesser complexity. Anyways its just another flavour for those who like writing short code(fewer lines) and code golf.. – Abhishek J May 25 '20 at 07:45
  • Thanks for suggestion but i want find out only by given percentile procedure without using pandas and numpy – pravin kamble May 25 '20 at 08:57
  • @pravinkamble I have not used either of them. The code is using pure python and importing the inbuilt math library. – Abhishek J May 25 '20 at 09:01