Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Model a Classroom Composed of Students and Subjects

Design a small object model for a classroom using composition: Students belong to a Classroom and each student has grades in Subjects.

Python practice15 minComposition & Real-World ModelingIntermediateLast updated April 11, 2026

Problem statement

Create a small object-oriented model for a classroom. You will implement two main classes: Student and Classroom. A Student stores their name and a record of grades per subject (each subject may have multiple grades). A Classroom manages a set of subjects and a collection of Student instances. Implement methods to add subjects and students, assign grades to students for a particular subject, compute the average grade for a subject, determine the top student for a subject, and compute the overall class average across all grades. Required behaviors: - Student should allow adding grades for a subject and reporting grades/averages for a subject. - Classroom should maintain a list of subjects and students. It should allow adding subjects and students, assigning grades (student name + subject + score), computing the class average for a subject, finding the top student for a subject, and computing the overall class average across all grades in the classroom. Return values: - Methods that compute averages or find top students should return None when there is no data for the requested subject. (The test harness treats None as an empty string for comparison.)

Task

Implement Student and Classroom classes. Use composition to manage students and subjects, and write methods to assign grades and compute averages and top students.

Examples

Compute average for a subject

Input

c = build_sample_classroom(); c.average_grade('Math')

Output

85.0

The sample classroom has three Math grades: 90 (Alice), 80 (Bob), 85 (Charlie). The average is (90+80+85)/3 = 85.0.

Input format

You will instantiate classes and call their methods. Example tests call helper functions like build_sample_classroom() to obtain a Classroom instance.

Output format

Return values or None (treated as empty string by the harness). Numeric averages should be returned as floats.

Constraints

Do not use external libraries. Subject names and student names are case-sensitive strings. Grades are numeric values (int or float). Methods should return None when no grades are available for a subject.

Samples

Sample 1

Input

build_sample_classroom().top_student('Science')

Output

Bob

In the sample classroom, Bob has the highest Science grade (90) compared to Alice (80).