| title | Process Student Marks | ||
|---|---|---|---|
| tags |
|
Write a function process_student_marks that processes student marks and performs the following operations:
- Read the number of students (
n) and their respective names and marks. - Compute the average mark (rounded to 2 decimal places).
- Determine the student(s) with the highest mark.
- Print the results:
- Each student's name and mark, sorted alphabetically by name.
- The average mark.
- The names of the top-performing students (alphabetically sorted in case of a tie).
The first line contains an integer n, the number of students.
The next n lines each contain a student's name and mark, separated by a space.
The output should include:
- Each student's name and mark in alphabetical order.
- The average mark (rounded to 2 decimal places).
- The names of the top-performing students.
<prefix>
def process_student_marks(n: int) -> str:
'''
Process student marks and generate results based on the given rules.
Arguments:
n: int - Number of students.
Returns:
str - Formatted string with student details, average mark, and top performers.
'''
</prefix>
<template>
<sol>
# Initialize dictionary to store student marks
student_marks = {}
# Read input and store marks
for _ in range(n):
name, mark = input().split()
mark = float(mark)
student_marks[name] = mark
# Compute average
average = round(sum(student_marks.values()) / n, 2)
# Find top performers
highest_mark = max(student_marks.values())
top_performers = sorted([name for name, mark in student_marks.items() if mark == highest_mark])
# Prepare result
result = []
for name in sorted(student_marks.keys()):
result.append(f"{name}: {student_marks[name]:.2f}")
result.append(f"average: {average:.2f}")
result.append(f"top_performers: {', '.join(top_performers)}")
return "\n".join(result)
</sol>
<los>pass</los>
</template>
<suffix>
n = int(input())
print(process_student_marks(n))
</suffix>
<suffix_invisible>
</suffix_invisible>3
Alice 85
Bob 90
Charlie 85
Alice: 85.00
Bob: 90.00
Charlie: 85.00
average: 86.67
top_performers: Bob
4
Daisy 78
Ella 95
Fiona 95
George 88
Daisy: 78.00
Ella: 95.00
Fiona: 95.00
George: 88.00
average: 89.00
top_performers: Ella, Fiona
2
Hank 75
Ivy 85
Hank: 75.00
Ivy: 85.00
average: 80.00
top_performers: Ivy
5
John 60
Jane 95
Jack 60
Jill 80
Jerry 95
Jack: 60.00
Jane: 95.00
Jerry: 95.00
Jill: 80.00
John: 60.00
average: 78.00
top_performers: Jane, Jerry
3
Kim 70
Liam 85
Mia 85
Kim: 70.00
Liam: 85.00
Mia: 85.00
average: 80.00
top_performers: Liam, Mia
4
Nancy 90
Oscar 85
Paul 85
Quincy 90
Nancy: 90.00
Oscar: 85.00
Paul: 85.00
Quincy: 90.00
average: 87.50
top_performers: Nancy, Quincy