python播放列表解决方案,如何完成is_repeating_playlist函数?

时间:2018-07-03 03:47:09

标签: python algorithm

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);

7 个答案:

答案 0 :(得分:4)

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())

请确保使用 set()提高速度。

函数“ is_repeating_playlist”分两个步骤工作:

  • 遍历(下一首)歌曲
  • 如果它在已添加的歌曲列表中找到下一首歌曲,则该列表正在重复

答案 1 :(得分:2)

我只是一个新手。。但是请检查一下。看起来好像运行良好。

def is_repeating_playlist(self):
    n = self
    x = set()
    while n is not None:
        x.add(n)
        n = n.next
        if n in x:
            return True
    return False

答案 2 :(得分:1)

这是一个链接列表,它是计算机科学中经过非常仔细研究的数据结构。您要检测链表中是否有循环。

以下是根据https://www.geeksforgeeks.org/detect-loop-in-a-linked-list/

改编而成的答案

因为我大量使用了该站点(并且因为我编写了该函数),所以如果要写作业,则不应将其作为您的工作。而是在链接列表上找到more info并创建您自己的解决方案。

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

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

因为我没有使用self,所以我将其设为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

现在让我们尝试一下:

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

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

print(Song.is_repeating_playlist(first)) 
  

让我们检查一个不再重复的地方

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))
  

错误

答案 3 :(得分:0)

我认为我的解决方案比已经发布的答案的性能要高一些。我没有在我的解决方案中使用“in”关键字来避免每次迭代遍历我的歌曲集,我只使用集的长度。使用 len() 实际上不需要遍历整个列表,它实际上以 O(1) 恒定时间运行!这是我的参考:Cost of len() function

def is_repeating_playlist(self):
    song_set = {self}
    songs_traversed = 1
    current_song = self
    while True:
        next = current_song.next
        if next == None:
            return False
        if len(song_set) != songs_traversed:
            return True
        song_set.add(next)
        current_song = next
        songs_traversed += 1

答案 4 :(得分:0)

我是用递归函数做的。虽然我必须在满足条件时分解函数,就像我调用的递归函数一样多。

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):
    first = self.name
    def recursion(self,first):
        if self.next == None:
            return False

        while not first == self.next.next.name:
            recursion(self.next, first)
            break
        print('break out')
        return True

    return recursion(self,first)


first = Song("Hello")
second = Song("Eye of the tiger")
third = Song("mamma mia!")
fourth = Song("lion king")

first.next_song(second)
second.next_song(third)
third.next_song(fourth)
fourth.next_song(first)

print(first.is_repeating_playlist())

答案 5 :(得分:0)

简单明了

def is_repeating_playlist(self):
    songs = set()
    while self.next:
        if self.next.name in songs:
            return True
        else:
            songs.add(self.next.name)
            self.next = self.next.next or None

    return False

答案 6 :(得分:-1)

您应该使用一个队列,其中最后一个元素链接到第一个元素。要实施此更新,请使用self.previous

然后您的队列将看起来像(为了清楚起见增加了第三列):

first = Song("Hello")
second = Song("Eye of the tiger")
third = Song("We are the champions")

first.next_song(second)
first.prev_song(third)

second.next_song(third)
second.prev_song(first)

third.next_song(first)
third.prev_song(second)