gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:05:25 33 4
gpt4 key购买 nike

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

最佳答案

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”函数分两步工作:

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

关于python播放列表解决方案,如何完成is_repeating_playlist函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51146399/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com