Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 实现一个函数是_repeating _playlist,该函数在播放列表重复时返回true,在播放列表不重复时返回false_Python - Fatal编程技术网

Python 实现一个函数是_repeating _playlist,该函数在播放列表重复时返回true,在播放列表不重复时返回false

Python 实现一个函数是_repeating _playlist,该函数在播放列表重复时返回true,在播放列表不重复时返回false,python,Python,我正试图通过testdome.com的所有python测试 问题6要求您: 如果任何歌曲包含对播放列表中先前歌曲的引用,则播放列表被视为重复播放列表。否则,播放列表将以指向None的最后一首歌曲结束 实现一个函数是_repeating _playlist,该函数在播放列表重复时有效地返回true,否则返回false 例如,以下代码打印为True,因为两首歌曲都指向对方 first = Song("Hello") second = Song("Eye of the tiger") first.n

我正试图通过testdome.com的所有python测试

问题6要求您:

如果任何歌曲包含对播放列表中先前歌曲的引用,则播放列表被视为重复播放列表。否则,播放列表将以指向None的最后一首歌曲结束

实现一个函数是_repeating _playlist,该函数在播放列表重复时有效地返回true,否则返回false

例如,以下代码打印为True,因为两首歌曲都指向对方

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

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

print(first.is_repeating_playlist())
我写了下面的代码来检查一首歌的下一首歌是否指向了一首早期的歌,这首歌是否有效,但是测试显示我没有通过3/4部分。我希望有人能解释原因:

class Song:
    songs = []
    repeats = []

    def __init__(self, name):

        self.name = name
        self.songs.append(self.name)
        self.next = None

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

        try:
            self.repeats.append(song.name)

        except(AttributeError):
            self.repeats.append("invalid song")

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

        for i in Song.songs:
            if repeats == True:
                break

            for j in Song.repeats:
                if i == j:
                    print(i,j)
                    print(Song.repeats.index(j), Song.songs.index(i))
                    if Song.repeats.index(i) > Song.songs.index(j):
                        repeats = True
                        break

        return repeats


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

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

print(first.is_repeating_playlist())
如果输入类似second.next_song的值,代码将按预期返回True,如果输入类似second.next_song的值,则返回False


但很明显,人们对我的期望更高?

只要用一套,看看你是否能看到任何东西两次

def is_repeating_playlist(self):
    """
    :returns: (bool) True if the playlist is repeating, False if not.
    """
    seen = set()
    target = self
    while 1:
        if not target:
            # end of list no cycles found
            return False
        if target in seen:
            # this target has already appeared, so it repeats
            return True
        seen.add(target)
        target = target.next

    return None

这些测试描述了它们是什么。你试过匹配的案例吗?