Lesson guide
What this Python exercise practices
Model a Playlist with Track Components is a beginner practice lesson that focuses on python practice, coding exercises, test feedback. It is designed to be solved in about 8 minutes with examples, starter code, and test feedback.
Prerequisites
- Python basics
- Variables
- Reading simple prompts
Difficulty and time
- Level
- Beginner
- Estimated time
- 8 minutes
Related public exercises
Summary
Build a Playlist class composed of Track objects and implement common playlist behaviors using composition.
Problem statement
You will model a simple music playlist using composition. Implement two classes: Track and Playlist. A Track represents a single song with a title, artist, duration (in seconds), and optional album. A Playlist contains Track instances and provides operations to add and remove tracks, compute durations, and search by artist. Required Playlist methods: - add_track(track): add a Track to the end of the playlist. - total_duration(): return the total duration in seconds (int) of all tracks in the playlist. - track_titles(): return a list of track titles in playlist order. - find_by_artist(artist): return a list of titles of tracks by the given artist, preserving playlist order. - remove_track(title): remove the first track matching title; return True if removed, False if not found. - average_duration(): return the average track duration as a float rounded to 2 decimals; return 0.0 for an empty playlist. Design the classes so Playlist stores Track objects (composition) rather than copying track data around.
Task
Practice design using composition: create Track and Playlist classes and implement methods that operate on composed Track instances.
Examples
Add tracks and get total duration
Input
p = create_playlist([('Song A','Artist 1',210), ('Song B','Artist 2',180)]) p.total_duration()
Output
390
Explanation
Two tracks of 210 and 180 seconds sum to 390.
Input format
Functions and methods will be called directly in tests (e.g., create_playlist([...]).total_duration()).
Output format
Return values: ints, lists of strings, booleans, or floats (converted to string by the test harness).
Constraints
Do not use external libraries. Preserve playlist order for title lists and searches. average_duration must return 0.0 when the playlist has no tracks.
Samples
Sample input 0
p = create_playlist([('One','X',100),('Two','Y',200)]) p.track_titles()
Sample output 0
['One', 'Two']
Explanation 0
track_titles returns a list of titles in the order they were added.
AI assistant
Ask me anything!
Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.
Chat history is temporary and will not be saved.
Free preview includes 1 Teach Theory response and 1 AI hint on each of the first 3 lessons in this module.