# Name: ... # CSE 163 # Autumn 2026 # Programming Practice 4 # Fill in the two classes and one function below so that they match the # behavior described in each docstring. You can check your work by running # this file before submitting. # Do not change the class, function names, or the number of # parameters. Keep the main-method pattern at the bottom of the file. # It may seem like a lot of functions to complete at first, but many of # them are simply getter and setter methods! from typing import Any # ~~~ Begin Problem 1 ~~~ class Song: """ Represents a song in a music library. A Song has a title, an artist, and a count of how many times it has been played. """ def __init__(self, title: str, artist: str, plays: int = 0) -> None: """ Initializes a new Song with the given title and artist. If plays is not given, the play count starts at 0. """ # TODO: remove 'pass' and implement this method! pass def get_title(self) -> str: """ Returns the title of this song. """ # TODO: remove 'pass' and implement this method! pass def get_artist(self) -> str: """ Returns the artist of this song. """ # TODO: remove 'pass' and implement this method! pass def get_plays(self) -> int: """ Returns how many times this song has been played. """ # TODO: remove 'pass' and implement this method! pass def play(self) -> None: """ Plays this song once, increasing its play count by 1. """ # TODO: remove 'pass' and implement this method! pass def __str__(self) -> str: """ Returns a str representation of this song in the format 'TITLE by ARTIST (N plays)' where N is the play count. For example, a song with title 'Groupby Groove' and artist 'The DataFrames' that has been played twice returns 'Groupby Groove by The DataFrames (2 plays)'. """ # TODO: remove 'pass' and implement this method! pass def __eq__(self, other: Any) -> bool: """ Returns True if this song and other are the same song: they have the same title and the same artist. The play counts do NOT need to match for two songs to be considered equal. Do not assume that 'other' is a Song """ # TODO: remove 'pass' and implement this method! pass # ~~~ End Problem 1 ~~~ def test_song(): """ Tests the Song class. """ song = Song('Groupby Groove', 'The DataFrames') assert 'Groupby Groove' == song.get_title() assert 'The DataFrames' == song.get_artist() assert 0 == song.get_plays() song.play() song.play() assert 2 == song.get_plays() assert 'Groupby Groove by The DataFrames (2 plays)' == str(song) song2 = Song('Groupby Groove', 'The DataFrames', 5) assert song == song2 assert song is not song2 song3 = Song('Groupby Groove', 'The Sorted Keys') assert song != song3 # ~~~ Begin Problem 2 ~~~ class Playlist: """ Represents a playlist that keeps track of a list of Songs. """ def __init__(self) -> None: """ Initializes a new, empty Playlist. The field storing the songs should start out as the empty list. """ # TODO: remove 'pass' and implement this method! pass def add_song(self, song: Song) -> None: """ Takes a Song and adds it to the end of this playlist. """ # TODO: remove 'pass' and implement this method! pass def total_plays(self) -> int: """ Returns the total number of plays across all songs in this playlist. Returns 0 if the playlist is empty. """ # TODO: remove 'pass' and implement this method! pass def play_all(self) -> None: """ Plays every song in this playlist once, in order, by calling each Song's play() method. """ # TODO: remove 'pass' and implement this method! pass # ~~~ End Problem 2 ~~~ def test_playlist(): """ Tests the Playlist class. """ playlist = Playlist() assert 0 == playlist.total_plays() song1 = Song('Lambda Lullaby', 'The Sorted Keys', 3) song2 = Song('NaN of Your Business', 'Missing Values') playlist.add_song(song1) playlist.add_song(song2) assert 3 == playlist.total_plays() playlist.play_all() assert 5 == playlist.total_plays() assert 4 == song1.get_plays() # ~~~ Begin Problem 3 ~~~ def most_played(songs: list[Song]) -> list[Song]: """ Takes a list of Songs and returns a new list containing the same Songs sorted from most plays to fewest plays. Does not change the order of the list that was passed in. Hint: use the sorted function with a key. """ # TODO: remove 'pass' and implement this function! pass # ~~~ End Problem 3 ~~~ def test_most_played(): """ Tests the most_played function. """ songs = [ Song('Import Antics', 'The Modules', 2), Song('Groupby Groove', 'The DataFrames', 10), Song('Lambda Lullaby', 'The Sorted Keys', 5), ] result = most_played(songs) result_titles = [song.get_title() for song in result] expected = ['Groupby Groove', 'Lambda Lullaby', 'Import Antics'] assert expected == result_titles original_titles = [song.get_title() for song in songs] expected_original = ['Import Antics', 'Groupby Groove', 'Lambda Lullaby'] assert expected_original == original_titles def main(): test_song() test_playlist() test_most_played() print("All tests passed!") if __name__ == '__main__': main()