Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python playlist solution, how to finish is_repeating_playlist function?

class Song:
    def __init__(self, name):
        self.name = name
        self.next = None

    def next_song(self, song):
        self.next = song 

    def is_repeating_playlist(self):
        """
        :returns: (bool) True if the playlist is repeating, False if not.
        """
        return None

first = Song("Hello")
second = Song("Eye of the tiger")

first.next_song(second);
second.next_song(first);
like image 853
Ilya Alexandrov Avatar asked Dec 24 '22 06:12

Ilya Alexandrov


2 Answers

class Song:
def __init__(self, name):
    self.name = name
    self.next = None

def next_song(self, song):
    self.next = song 

def is_repeating_playlist(self):
    """
    :returns: (bool) True if the playlist is repeating, False if not.
    """
    songs = set()
    next_song = self
    while next_song:
        if next_song.name in songs:
            return True
        else:
            songs.add(next_song.name)
            next_song = next_song.next or None

    return False

first = Song("Anam Nesis - Contemplare")
second = Song("Petre Inspirescu - Anima")
third = Song("VOLK - Cântul Ielelor")

first.next_song(second);
second.next_song(third);


print(first.is_repeating_playlist())

Please make sure you use set() for increased speed.

The function "is_repeating_playlist" works in two steps:

  • Iterates thru the (next) songs
  • If it finds the next song in the already added songs list, then the list is repeating
like image 101
Sergiu Vlad Avatar answered Dec 25 '22 20:12

Sergiu Vlad


This is a linked list which is a very thoroughly examined data structure in computer science. You want to detect if your linked list has a loop.

Here is an answer adapted from https://www.geeksforgeeks.org/detect-loop-in-a-linked-list/

Because I heavily used that site (and because I wrote the function), you shouldn't hand this in as yours if it is homework. Instead, find more info on linked list and create your own solution.

class Song:
    def __init__(self, name):
        self.name = name
        self.next = None

    def next_song(self, song):
        self.next = song

Because I didn't use self, I made this a staticmethod.

    @staticmethod
    def is_repeating_playlist(first_song): 
        """
        :returns: (bool) True if the playlist is repeating, False if not.
        """
        songs_in_playlist = set()
        current_song = first_song
        while(current_song):
            if current_song.name in songs_in_playlist: # if we already saw this song
                return True
            songs_in_playlist.add(current_song.name)
            current_song = current_song.next
        return False # no repeats found

Now let's try it out:

first = Song("Hello")
second = Song("Eye of the tiger")

first.next_song(second);
second.next_song(first);

print(Song.is_repeating_playlist(first)) 

True

And let's check one that doesn't repeat

first = Song("Hello")
second = Song("Eye of the tiger")
third = Song("We Will Rock You")
first.next_song(second);
second.next_song(third);

print(Song.is_repeating_playlist(first))

False

like image 42
Zev Avatar answered Dec 25 '22 19:12

Zev