Problem 275624 · easy · Level 02 Linear Data Structures

Her Strongest Subject

z-scores · standard deviation · sorting with a key · dictionaries

Mina's raw marks are highest in art, but art is marked generously and every student scores high there. Her tutor wants to know in which subject she stands out most compared with her class.

For each subject, measure her mark by z = (mark - mean) / sd, where mean and sd are the mean and the population standard deviation (divide by n) of the marks of the whole class in that subject (her own mark is one of them). If every student in a class got the same mark, the standard deviation is 0 and her z in that subject is 0.0.

Write rank_subjects(marks, classes) where marks maps each subject to Mina's mark and classes maps each subject to the list of all marks in that class. Return a list of (subject, z) pairs sorted from her strongest subject to her weakest (largest z first). Subjects with equal z are listed in alphabetical order.

Examples

Input:  marks = {"physics": 64, "art": 88, "french": 71}
        classes = {"physics": [48, 64, 52, 58, 45, 61, 50, 56],
                   "art":     [90, 85, 88, 92, 86, 95, 84, 80],
                   "french":  [71, 65, 80, 74, 62, 77, 69, 70]}
Output: [("physics", 1.577771118422333), ("art", 0.11180339887498948), ("french", 0.0)]
Explanation: in physics the mean is 54.25 and the standard deviation about 6.18,
so her 64 is about 1.58 standard deviations above the mean. Her 88 in art is only
just above that class's mean of 87.5, and her 71 in French is exactly the mean.

Constraints

  • 1 <= len(marks) <= 50; classes has the same subjects as marks
  • every class list has between 1 and 500 marks and contains her mark
  • marks are whole numbers from 0 to 100
  • floats are compared with a tolerance of 1e-6

Goals

  • Describe a mark by how many standard deviations it lies above its class mean
  • Compare results from groups with different averages and spreads
  • Sort by a computed value with a tie-break
Starting Python…