-1
def popular_genre(musics):
    """
    Function that finds the most popular genre
    :return: its name or "none" if all the songs are from different genres
    """
    highest_frequency = 0

    for song_no_1 in musics:
        frequency = 0
        for song_no_2 in musics:
            if song_no_1.get_genre() == song_no_2.get_genre():
                frequency += 1
            if frequency > highest_frequency:
                highest_frequency = frequency
                tmp_genre = song_no_1.get_genre()
    if highest_frequency == 1:
        tmp_genre = "none"
    return tmp_genre

So I want this function to show the most popular genre. musics is a list of songs. I have 3 songs already and when I use this function like this popular_genre(musics) nothing happens. Why?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • Does this answer your question? [Python- find the item with maximum occurrences in a list](https://stackoverflow.com/questions/6987285/python-find-the-item-with-maximum-occurrences-in-a-list) – Tomerikoo Aug 18 '20 at 07:04
  • what you get_genre() method is returning ? And in the list music is there any way to extract genre also ? As per your comment its just list of songs. – rahul rai Aug 18 '20 at 07:05

2 Answers2

3

I can suggest you a different approach instead of this. As you are going to iterate over all songs, you can create a dictionary where you can add a genre of songs and increment its value every time a song has that genre.

def popular_genre(musics):
    """
    Function that finds the most popular genre
    :return: its name or "none" if all the songs are from different genres
    """
    #highest_frequency = 0
    genres = {}
    for song in musics:
      if song.get_genre() not in genres.keys():
        genres[song.get_genre()] = 1
      else :
        genres[song.get_genre()] += 1
    highest = max(genres,key=genres.get)
    return (highest,genres[highest])

try and confirm if this works

  • 1
    As this is a coding Q&A, a short code snippet would be very helpful to support your answer, but anyway yes - that is the best approach – Tomerikoo Aug 18 '20 at 07:10
  • @Tomerikoo added code. you can suggest improvements. –  Aug 18 '20 at 07:19
  • 1
    Just a few: instead of doing the check if the genre is already in the dict you can either use a [`defaultdict`](https://docs.python.org/3/library/collections.html#collections.defaultdict) or more simply a [`Counter`](https://docs.python.org/3/library/collections.html#collections.Counter) (all your loop would come down to a simple `genres = Counter(musics)`). To get the highest one you can do `genre, count = max(genres.items(), key=lambda item: item[1])` – Tomerikoo Aug 18 '20 at 07:47
0

As you confirmed your get_genre() method is returning genre of song. then we can use below approach:

highest_frequency = 0
 genre_with_highest_freq = 'none'
 genreDict= {}
    for song in musics:
        genre = song.get_genre()
        if genre in genreDict:
            genreDict[genre] = genreDict.get(genre)+1
        else:
            genreDict[genre] = 1
    #Now we can iterate through this dictionary to get genre with highest occurrence
    for genre in genreDict:
       if genreDict[genre] genreDict[genre]>highest_frequency :
          highest_frequency = 
          genre_with_highest_freq = genre
    return genre_with_highest_freq 
rahul rai
  • 2,260
  • 1
  • 7
  • 17