Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Create a Media Library Composed of Albums and Tracks

Model a small media library using composition: tracks belong to albums, and albums belong to a library.

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

Problem statement

Build a small object model for a media library using composition (Album contains Track objects; MediaLibrary contains Album objects). Implement three classes: Track, Album, and MediaLibrary. Required behavior: - Track(title, duration_sec): represents a single track with a title and duration in seconds. - Album(title, artist, tracks=None): can be created with an optional list of Track objects. It should support adding tracks, listing track titles, and computing the album's total duration in seconds. - MediaLibrary(albums=None): can be created with an optional list of Album objects. It should support adding albums, computing the library's total duration (sum of all album durations), and finding tracks by a case-insensitive substring match in their title. The search method should return a list of matching track titles preserving the album and track order. Implement methods: - Album.add_track(track) - Album.total_duration() - Album.track_titles() - MediaLibrary.add_album(album) - MediaLibrary.total_duration() - MediaLibrary.find_tracks_by_title(substring) Follow composition: albums should store Track instances, and the library should store Album instances. Keep behavior predictable and simple (no external libraries).

Task

Design Track, Album, and MediaLibrary classes using composition. Implement methods to add items, compute durations, and search tracks.

Examples

Album total duration

Input

Album("Hits","DJ",[Track("One",120),Track("Two",150)]).total_duration()

Output

270

Two tracks of 120 and 150 seconds sum to 270.

Input format

You will instantiate classes and call methods. Each test is a single Python expression that should evaluate to the described value.

Output format

Return values (numbers, lists of strings, etc.). The test harness compares str(value) to the expected string.

Constraints

Do not use external libraries. Preserve the order of albums and tracks when returning lists. Search must be case-insensitive and match substrings.

Samples

Sample 1

Input

MediaLibrary([Album("A","X",[Track("a",30)]),Album("B","Y",[Track("b",40),Track("c",50)])]).total_duration()

Output

120

Total duration is 30 + 40 + 50 = 120 seconds.