8.2 - Music stats

Problem

A record company needs a program to analyse their artists.

The program should:

  • Store the artists, songs and song lengths in parallel arrays
  • Use a function to count the number of songs by searched artist
  • Use a function to calculate the average song length by searched artist
  • Use a procedure to display
    • the artist
    • the number of songs by the artist
    • the name and length of each song
    • The average song length in minutes and seconds

Example run

Enter artist: Luna Ray 

Luna Ray has 2 songs. 
Starlight 210s
Falling Sky 198s

The average song length is 3m 24s

Arrays

artists     = ["Luna Ray", "Echo Storm", "DJ Solaris", "Luna Ray", "The Night Owls", "DJ Solaris"]
songs       = ["Starlight", "Crash Wave", "Neon Dreams", "Falling Sky", "Midnight Drive", "Solar Flare"]
songlengths = [210,         185,          240,           198,           225,              260]

 

1 Count songs for specified artist IN artists
searched_artist
OUT song_count
2 Calculate average song length for specified artist IN songlengths
song_count
artists
searched_artist
OUT average_song_length
3 Display results IN artists
searched_artist, songs
songlengths
song_count
average_song_length
OUT
3.1 Display searched_artist and song_count
3.2 For i from 0 to length of artists – 1
3.3     if artists[i] = searched_artist then
3.4         displays songs[i] and song_lengths[i]
3.5     end if
3.6 end for
3.6 display blank line
3.7 set minutes to int(average_song_length/60)
3.8 set seconds to modulus of average_song_length / 60
3.9 display avergae song length in minutes and seconds

 

 

# Main
artists     = ["Luna Ray", "Echo Storm", "DJ Solaris", "Luna Ray", "The Night Owls", "DJ Solaris"]
songs       = ["Starlight", "Crash Wave", "Neon Dreams", "Falling Sky", "Midnight Drive", "Solar Flare"]
songlengths = [210,         185,          240,           198,           225,              260]

searched_artist = input("Enter name of artist: ")

song_count = counts_songs(artists, searched_artist)

average_song_length = calculate_avg_song_length(songlengths, song_count, artists, searched_artist)

display_results(artists, searched_artist, songs, songlengths, song_count, average_song_length)
def counts_songs(artists, searched_artist):
    song_count = 0
    for i in range(len(artists)):
        if artists[i] == searched_artist:
            song_count = song_count + 1
    return song_count

def calculate_avg_song_length(songlengths, song_count, artists, searched_artist):
    total_time = 0
    for i in range(len(artists)):
        if artists[i] == searched_artist:
            total_time = total_time + songlengths[i]
    average_song_length = total_time / song_count
    return average_song_length

def display_results(artists, searched_artist, songs, songlengths, song_count, average_song_length):
    print(searched_artist, "has", song_count, "songs.")

    for i in range(len(artists)):
        if artists[i] == searched_artist:
            print(songs[i], str(songlengths[i]) + "s")

    print()

    minutes = int(average_song_length/60)
    seconds = int(average_song_length % 60)
    print("The average song length is",  str(minutes) + "m", str(seconds) + "s")

# Main
artists     = ["Luna Ray", "Echo Storm", "DJ Solaris", "Luna Ray", "The Night Owls", "DJ Solaris"]
songs       = ["Starlight", "Crash Wave", "Neon Dreams", "Falling Sky", "Midnight Drive", "Solar Flare"]
songlengths = [210,         185,          240,           198,           225,              260]

searched_artist = input("Enter name of artist: ")

song_count = counts_songs(artists, searched_artist)

average_song_length = calculate_avg_song_length(songlengths, song_count, artists, searched_artist)

display_results(artists, searched_artist, songs, songlengths, song_count, average_song_length)
# ------------------------------------------------------------
# FUNCTION counts_songs(artists, searched_artist)
# Counts how many songs in the list belong to the searched artist.
# Returns the number of songs by that artist.
# ------------------------------------------------------------
def counts_songs(artists, searched_artist):
    song_count = 0
    for i in range(len(artists)):
        if artists[i] == searched_artist:
            song_count = song_count + 1
    return song_count


# ------------------------------------------------------------
# FUNCTION calculate_avg_song_length(songlengths, song_count, artists, searched_artist)
# Calculates the average song length for the searched artist.
# Adds up the lengths of all songs belonging to the artist,
# then divides by the total number of songs.
# Returns the average song length in seconds.
# ------------------------------------------------------------
def calculate_avg_song_length(songlengths, song_count, artists, searched_artist):
    total_time = 0
    for i in range(len(artists)):
        if artists[i] == searched_artist:
            total_time = total_time + songlengths[i]
    average_song_length = total_time / song_count
    return average_song_length


# ------------------------------------------------------------
# PROCEDURE display_results(artists, searched_artist, songs, songlengths, song_count, average_song_length)
# Displays the results for the searched artist.
# Prints:
#   - how many songs the artist has
#   - the titles and lengths of each song
#   - the average song length formatted as minutes and seconds
# ------------------------------------------------------------
def display_results(artists, searched_artist, songs, songlengths, song_count, average_song_length):
    print(searched_artist, "has", song_count, "songs.")

    for i in range(len(artists)):
        if artists[i] == searched_artist:
            print(songs[i], str(songlengths[i]) + "s")

    print()

    # Convert total average song length in seconds into minutes
    # int() is used to drop any decimal part, leaving whole minutes
    minutes = int(average_song_length / 60)

    # Use modulus % to find leftover seconds after taking full minutes
    # int() ensures the result is stored as a whole number of seconds
    seconds = int(average_song_length % 60)

    print("The average song length is", str(minutes) + "m", str(seconds) + "s")


# -------------------------
# Main Program
# -------------------------

# List of artists for each song
artists     = ["Luna Ray", "Echo Storm", "DJ Solaris", "Luna Ray", "The Night Owls", "DJ Solaris"]

# Matching list of song titles
songs       = ["Starlight", "Crash Wave", "Neon Dreams", "Falling Sky", "Midnight Drive", "Solar Flare"]

# Matching list of song lengths in seconds
songlengths = [210, 185, 240, 198, 225, 260]

# Ask the user which artist to search for
searched_artist = input("Enter name of artist: ")

# Count how many songs belong to the searched artist
song_count = counts_songs(artists, searched_artist)

# Calculate the average song length for that artist
average_song_length = calculate_avg_song_length(songlengths, song_count, artists, searched_artist)

# Display the results: total songs, list of songs with lengths, and average duration (in minutes and seconds)
display_results(artists, searched_artist, songs, songlengths, song_count, average_song_length)

Extension

Update the program so that it will display each song in minutes and seconds.

Enter artist: Luna Ray 

Luna Ray has 2 songs. 
Starlight 3m 30s 
Falling Sky 3m 18s 

The average song length is 3m 24s

Target

Write a function that uses parameters

Demonstrate parallel arrays with a shared index