Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b91c54e

Browse files
Merge pull request #137 from lironmiz/unit9-Ex9.3.1
Create unit9_ex9.3.1.py
2 parents 362a031 + c846da7 commit b91c54e

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

‎unit9_ex9.3.1.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# exercise 9.3.1 from unit 9
2+
'''
3+
Write a function called my_mp3_playlist defined as follows:
4+
5+
def my_mp3_playlist(file_path):
6+
The function accepts as a parameter a path to the file (string).
7+
The file represents a playlist of songs and has a fixed format consisting of the name of the song, the name of the performer (singer/band) and the length of the song, separated by a semicolon (;) without spaces.
8+
9+
An example of an input file called songs.txt:
10+
11+
Tudo Bom; Static and Ben El Tavori; 5:13;
12+
I Gotta Feeling; The Black Eyed Peas; 4:05;
13+
Instrumental; Unknown; 4:15;
14+
Paradise; Coldplay; 4:23;
15+
Where is the love?; The Black Eyed Peas; 4:13;
16+
The function returns a tuple in which:
17+
18+
The first member is a string representing the name of the longest song in the file
19+
(it means the longest song, assume all lengths are different).
20+
The second member is a number representing the number of songs the file contains.
21+
The third member is a string representing the name of the operation that appears in
22+
the file the largest number of times (assume there is only one).
23+
An example of running the my_mp3_playlist function on the songs.txt file
24+
>>> print(my_mp3_playlist(r"c:\my_files\songs.txt"))
25+
("Tudo Bom", 5, "The black Eyed Peas")
26+
'''
27+
def my_mp3_playlist(file_path):
28+
longest_song = ""
29+
longest_song_length = 0
30+
song_count = 0
31+
most_common_artist = ""
32+
artist_counts = {}
33+
34+
with open(file_path, 'r') as f:
35+
for line in f:
36+
song, artist, length = line.strip().split(';')
37+
length_parts = length.split(':')
38+
length_minutes = int(length_parts[0])
39+
length_seconds = int(length_parts[1])
40+
total_length = length_minutes * 60 + length_seconds
41+
42+
if total_length > longest_song_length:
43+
longest_song = song
44+
longest_song_length = total_length
45+
46+
if artist in artist_counts:
47+
artist_counts[artist] += 1
48+
else:
49+
artist_counts[artist] = 1
50+
51+
song_count += 1
52+
53+
most_common_artist = max(artist_counts, key=artist_counts.get)
54+
55+
return (longest_song, song_count, most_common_artist)
56+
57+
def main():
58+
result = my_mp3_playlist(r"c:\my_files\songs.txt")
59+
print(result)
60+
61+
if __name__ == "__main__":
62+
main()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /