Average Grades per Student
Problem statement
You are given two parallel arrays, names and grades. Student names[i] has the integer subject grades in grades[i].
Compute the arithmetic average grade for every student: the sum of that student's grades divided by the number of grades.
The result is a mapping from each student name to that student's average. Return this mapping as an array averages, where averages[i] is the value associated with names[i]. Preserve the input order; do not sort students or combine grade lists. For example, pairing names[i] with averages[i] reconstructs the name-to-average dictionary.
Return the numerical averages without rounding them to integers or a fixed number of decimal places. Each result is accepted when its relative or absolute error is at most 10^-6.
Function
averageGrades(names: String[], grades: int[][]) → double[]Examples
Example 1
names = ["Alice","Bob","Charlie"]grades = [[85,90,88],[70,75,80],[65,70,75]]return = [87.66666666666667,75,70]Alice's grades sum to 263, so her average is 263 / 3. Bob's average is 225 / 3 = 75, and Charlie's is 210 / 3 = 70. The returned values correspond to Alice, Bob, and Charlie in that order.
Example 2
names = ["Zoe","Amy"]grades = [[0,100],[20,40,80]]return = [50,46.666666666666664]Zoe has average 100 / 2 = 50, and Amy has average 140 / 3. The grade lists have different lengths. Preserve the input order even though Amy comes first alphabetically.
Example 3
names = ["Solo"]grades = [[0]]return = [0]A student with one grade has that grade as the average, including when the grade is 0.
Constraints
1 <= names.length = grades.length <= 500.- Every name is a nonempty string of at most
50characters, and all names are distinct. Names are case-sensitive. - Every
grades[i]contains at least one integer. 0 <= grades[i][j] <= 100.- The total number of grades across all students is at most
5000.