title: Implement a function is_repeating_playlist that, efficiently with respect to time used, returns true if a playlist is repeating or false if it is not
body: <p>I am trying to pass al the python tests from testdome.com (<a href="https://www.testdome.com/d/python-interview-questions/9" rel="nofollow noreferrer">https://www.testdome.com/d/python-interview-questions/9</a>)</p>
  
  <p>Question 6 requires you to: </p>
  
  <p>A playlist is considered a repeating playlist if any of the songs contain a reference to a previous song in the playlist. Otherwise, the playlist will end with the last song which points to None.</p>
  
  <p>Implement a function is_repeating_playlist that, efficiently with respect to time used, returns true if a playlist is repeating or false if it is not.</p>
  
  <p>For example, the following code prints "True" as both songs point to each other.</p>
  
  <pre><code>first = Song("Hello")
  second = Song("Eye of the tiger")
  
  first.next_song(second);
  second.next_song(first);
  
  print(first.is_repeating_playlist())
  </code></pre>
  
  <p>I wrote the following code to check if a song's "next song" points to an earlier song and this works, but the test says I failed 3/4 parts. I am hoping someone can explain why: </p>
  
  <pre><code>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) &gt; 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())
  </code></pre>
  
  <p>The code returns True as expected and False if you enter something like  second.next_song("random nonsense);</p>
  
  <p>But clearly more was expected of me?</p>