-
-
Save roman-on/3849b5f3f5bc10295dec9a19f1a9b76f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
The function accepts as a path parameter to the file (string). | |
The file represents a playlist of songs and has a fixed format built from the song name, performer name (singer / band) and song length, separated by semicolon (;) without spaces. | |
An example of an input file called songs.txt: | |
Tudo Bom; Static and Ben El Tavori; 5:13; | |
In Gotta Feeling; The Black Eyed Peas; 4: 05; | |
Instrumental, Unknown 4:15; | |
Paradise; Coldplay; 4:23; | |
Where is the love ?: The Black Eyed Peas; 4:13; | |
The function returns a Tafel where: | |
• The first organ is a string that represents the longest song name in the file (meaning the longest song, assume all lengths are different). | |
• The second organ is a number that represents the number of songs the file contains. | |
• The third organ is a string that represents the name of the operation that appears in the file the largest number of times (assume there is only one). | |
An example of running the my_mp3_playlist function on the songs.txt file | |
>>> print (my_mp3_playlist (r "c: \ my_files \ songs.txt")) | |
("Tudo Bom", 5, "The Black Eyed Peas") | |
""" | |
def my_mp3_playlist(file_path): | |
a = [] # New list variable | |
b = [] # New list variable | |
e = [] # New list variable | |
f = "" # New string variable | |
with open (file_path, "r") as file_path: # Open the file and closes automatically | |
read_all_line = file_path.read() # Printing all the lines from the file | |
splited = read_all_line.split("\n") # Converting from multiple lines into one line by (split("\n")) and making one long list | |
for element in splited: # For every element in the seperated list | |
a.append(element.split(";")) # Spliting every element in the list into seperated lists by .split(";") and adding the new list to a new variable list "a" | |
for num in range(0, len(a)-1): # Running the number in range of the length of my lists (5 lists) in my one big list | |
b.append(a[num][2][0]) # Adding all the [0] number from the time length of every song into a new variable list ['5', '4', '4', '4'] | |
d = int(max(b)) # The biggest number in the list above will be saved in variabale "d" (in our case this is number 5) | |
if max(b) in a[num][2][0]: # If the max number inside this list | |
c = a[num][0] # If true save the name of the song | |
for num1 in range(0, len(a)): # Running from 0 to the number of length of the variable "a" list which is 5 in my case | |
length = len(a[num1][1]) # Running thru the length of every band names | |
e.append(length) # Adding every length of the band names in to a new variable "e" | |
for num2 in range(len(e)): # Running thru the length of "e" which is 5 in my case | |
if e.count(e[num2]) > 1 and a[num2][1] not in f: # If the counted numbers in "e" list bigger than 1 and this name not in the list "f" already so it's true | |
f += (a[num2][1]) # Adding the name of the artist that apears the most in the whole list "a" | |
z = c,d,f # Adding all the variables with their results as a tuple to variable "z" | |
return z # Returning the result | |
def main(): | |
my_mp3_playlist (r"C:\.....") | |
if __name__ == "__main__": | |
main() | |
################ | |
""" | |
INPUT: | |
print (my_mp3_playlist (r"C:\.....")) | |
OUTPUT: | |
("Tudo Bom", 5, "The Black Eyed Peas") | |
""" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment