Practice Cleaning and Preparing Data in Python

Screen Link: https://app.dataquest.io/m/1000351/cleaning-and-preparing-data-in-python-practice-problems/3/fixing-a-music-playlist

My Code:

def format_songs(songs):
    formatted_songs = []
    for song in songs:
        lst = song.split(' ')
        number = lst[0]
        formatted_song = song.replace(number, number.zfill(2))
        formatted_songs.append(formatted_song)
    return formatted_songs

print(format_songs(test_songs) == expected_answer)

What I expected to happen: To format the playlist and output “True” if the format matches the expected_answer

What actually happened: The code output True

Your function returned an incorrect answer.
2 Likes

Hi @obatemitope,

Your code is creating the expected answer. Where it failed is on this part of the instruction:

Your function will also be tested on hidden test cases to ensure correctness. Your needs to work for any number of songs.

Your code will not work if the total number of songs are 100 or 1000. Let’s say, the hidden test case has 1000 songs. Does .zfill(2) work? No.

Here is how you can make sure that it works for all hidden test cases:

def format_songs(songs):
    max_digits = len(str(len(songs)))
    formatted_songs = []
    for song in songs:
        lst = song.split(' ')
        number = lst[0]
        formatted_song = song.replace(number, number.zfill(max_digits))
        formatted_songs.append(formatted_song)
    return formatted_songs

Hope this helps :slightly_smiling_face:

Best,
Sahil

3 Likes

Hello @Sahil,

What about my solution?

Even if the hidden test case leads to 1000 songs, my code logic is designed to handle the first 2 indexes of the string. The minimum integer will be 10, which will lead to any single-digit integer to add ‘0’ before it:

    def format_songs(songs):
        result = []
        for song in songs:
            if int(song[:2]) < 10:
                song = '0' + song
                result.append(song)
            else:
                result.append(song)
            
        return result

Would be happy to know if there are flaws to my code or this is just a bug

Thanks!

2 Likes

The only issue is that, it will not satisfy this instruction:

You must format the song names by adding zeros to the left of the names so that each song number has the same number of digits as the number of songs.

This implies that, if there are 1000 songs, the first song should have 3 zeroes:
0001

Your code is only adding a single 0. Therefore, the approach is incorrect (based on the instructions).

Best,
Sahil

Hello there,

I am looking at the solution and I don’t get what this line mean:

number_digits = len(str(len(songs)))

I guess my question here is, how does this end up giving us only 2 digits?

1 Like

Hi @awelhady,

Welcome to the community!

test_songs = ['1 - Thoughts Of Time', '2 - Passion Of The Night', '3 - Amused By Her Strength', '4 - Get My Friends', '5 - Broken Life', '6 - Super Song', '7 - Darling, Remember The Times', "8 - Baby, You are Strange And I Like It", '9 - She Said I Will Try', '10 - In Love With You', '12 - Hat Of The South', '13 - Friends Of My Dreams', '14 - Dream His Luck']

The above list contains 13 songs. After converting it to string str(len(songs)), we will get '13'. If we use the len() function on a string we will get the number of characters in the string. In this case, '13' contains 2 characters, hence we got 2 as the number of digits.

Best,
Sahil

3 Likes

Hi, could you check why my following program still being given “incorrect answer in your functions”?

def formatt2_songs(songs):
        maxdigits=len(str(len(songs)))
        print(maxdigits)
        # temp=[song.split(' - ')[0].zfill(maxdigits) for song in songs]
        songs=list(map(lambda x:x.split(' - ')[0].zfill(maxdigits)+' - '+x.split('-')[1], songs))
        return songs
    formatted_songs = formatt2_songs(test_songs)
    for song in formatted_songs:
        print(song)

And I tested it and even it works for 100+ song list:
001 - Thoughts Of Time
002 - Passion Of The Night
003 - Amused By Her Strength
004 - Get My Friends
005 - Broken Life
006 - Super Song
007 - Darling, Remember The Times
008 - Baby, You are Strange And I Like It
009 - She Said I Will Try
010 - In Love With You
012 - Hat Of The South
013 - Friends Of My Dreams
014 - Dream His Luck
001 - Thoughts Of Time
002 - Passion Of The Night
003 - Amused By Her Strength
004 - Get My Friends
005 - Broken Life
006 - Super Song
007 - Darling, Remember The Times
008 - Baby, You are Strange And I Like It
009 - She Said I Will Try
010 - In Love With You
012 - Hat Of The South
013 - Friends Of My Dreams
014 - Dream His Luck
001 - Thoughts Of Time

1 Like

Hi @shi.aaron,

Welcome to our community! Your code is correct. If there are any print statements after the following line:
print(format_songs(test_songs) == expected_answer)

Please remove it and try again. If it still didn’t work, please send us the complete code you have used.

Best,
Sahil

I am having trouble with this problem also. I made a solution which will add the correct number of zeros, but when I submit it I get: “Your code doesn’t seem to have the correct side-effects. Please re-check the instructions and your code.”

I’m confused by the other answers posted here. I started the practice problems after the first lesson, Cleaning and Preparing Data, in the Python for Data Science:Intermediate course. I don’t know what zfill is. I used the code that I had learned so far and got something that appears to work, even when I change the numbers in the test songs to be much larger. Can you help me understand what is wrong?

#create a function to add a 0 to the front of each number less than ten.
def format_songs (songs):
    formatted_song = []
    number_digits = []
    #isolate the number portion of the string to number_digits list
    #store as int so can find max later.
    for song in songs:
        num = song.split ("-")
        num = num[0]
        num_as_int = int(num)
        number_digits.append (num_as_int)
    #calculate the max number, convert to a string and find the length. 
    #this number will equal the number of digits in the number. 
    x = str(max(number_digits))
    max_digits = len(x)
    #test to see calculation is correct
    print("max number is:", x,"max digits:", max_digits)
    #isolate the number portion of the string and check the length of each
    for song in songs:
        num = song.split ("-")
        num = int(num[0])
        num = str(num)
        digits_in_num = len(num)
        #the number of zeros to be added is equal to the max digits minus
        #the number of digits in the number. 
        multiplier = max_digits - digits_in_num
        formatted_song.append ("0"*multiplier+song)
    return (formatted_song)