Object-oriented programming · 24 / 26 Exercise Medium

Iterable playlist

Objective

If a class defines __iter__, __len__ and __contains__, its objects behave like collections: they are traversed with for, measured with len() and queried with in.

Instructions

  • Define Playlist with an internal list of songs (title, seconds), a method add(title, seconds) and the special methods __len__, __iter__ and __contains__ (it searches by title).
  • Add total_duration(), which adds the seconds of all the songs.
  • Read n and then n lines title seconds, and finally a title to look for.
  • Go through the list with for and show 1. title (m:ss), with the duration as minutes and seconds (two digits for the seconds).
  • Show Songs: n, Duration: m:ss and Found: Yes.
Input
3
alba 185
mar 240
sol 65
mar
Output
1. alba (3:05)
2. mar (4:00)
3. sol (1:05)
Songs: 3
Duration: 8:10
Found: Yes

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise