Compare commits

...

3 Commits

View File

@ -83,28 +83,42 @@ case class TopSongs(songs: List[Song] = List()) {
TopSongs(song :: songs) TopSongs(song :: songs)
} }
def printSongs(): Unit = { def printSongs(): Unit = {
songs.foreach(song => { songs.map(song => {
val title = song.title val title = song.title
val singer = song.singer val singer = song.singer
val producers = song.producer.map(_.create()).mkString(", ") val producers = song.producer.map(_.create()).mkString(", ")
val streak = song.rank._1.streak.getOrElse("no") val streak = song.rank._1.streak.getOrElse("no")
val pos = song.rank._2.pos.getOrElse("NA") val pos = song.rank._2.pos.getOrElse("NA")
if (singer.exists(a => { singer match {
a.isInstanceOf[God] case s if s.exists(_.person.isInstanceOf[God]) => println(s"$title by God ${singer.map(_.person.name).mkString(", ")} spent $streak weeks on the charts on Pos. $pos")
})) { case _ => println(
println(s"$title by God ${singer.map(_.person.name).mkString(", ")} spent $streak weeks on the charts on Pos. $pos")
} else {
println(
s"$title by ${singer.map(_.person.name).mkString(", ")}. " + s"$title by ${singer.map(_.person.name).mkString(", ")}. " +
s"$producers this song that " + s"$producers this song that " +
s"spent $streak weeks " + s"spent $streak weeks " +
s"on the charts on Pos. $pos" s"on the charts on Pos. $pos"
) )
} }
}) })
} }
// This function counts the number of songs by each artist
// It uses flatMap to extract the artist names from the songs
// Then it groups the songs by artist name and counts the occurrences
// Finally, it returns a map with the artist names as keys and the counts as values
def countSongsByArtist(songs: List[Song]): Map[String, Int] = {
songs.flatMap(_.singer.map(_.person.name))
.groupBy(identity)
.view.mapValues(_.size)
.toMap
}
// This function returns the top N songs by weeks on chart
// It sorts the songs by the streak value in descending order and takes the top N songs
def topNSongsByWeeks(songs: List[Song], n: Int): List[Song] = {
songs.sortBy(song => -song.rank._1.streak.getOrElse(0)).take(n)
}
} }
@main def main(): Unit = @main def main(): Unit =
@ -141,3 +155,26 @@ case class TopSongs(songs: List[Song] = List()) {
// print the songs // print the songs
topSongs.printSongs() topSongs.printSongs()
println("----------");
// print the number of songs by artist
val songsByArtist = topSongs.countSongsByArtist(topSongs.songs)
println("Number of songs by artist:")
songsByArtist.foreach {
case (artist, count) => println(s"$artist: $count")
}
println("----------");
// print the top N songs by weeks on chart
val topNSongs = topSongs.topNSongsByWeeks(topSongs.songs, 5)
println("Top N songs by weeks on chart:")
topNSongs.foreach { song =>
println(s"${song.title} by ${song.singer.map(_.person.name).mkString(", ")} - ${song.rank._1.streak.getOrElse(0)} weeks")
}