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