Your task is to implement a function; that will return the top articles from the given list. The top articles are those that have the highest number of views. The returned list should have the same order as the given list. The function should return a list of the given size. If the given list is smaller than the given size, it should return all the articles. If the given list is empty, it should return an empty list.
Starting code:
class TopArticlesGenerator(
private val articles: List<ArticleStatistics>,
) {
fun topArticles(n: Int): List<ArticleStatistics> = TODO()
}
data class ArticleStatistics(
val title: String,
val views: Long,
)
This problem can either be solved in the below playground or you can clone kotlin-exercises project and solve it locally. In the project, you can find code template for this exercise in functional/collections/TopArticles.kt. You can find there starting code, example usage and unit tests.
Hint: The trick to be able to sort by views and then return the original order is to use withIndex function.
Once you are done with the exercise, you can check your solution here.
Playground
import junit.framework.TestCase.assertEquals
import org.junit.Test
class TopArticlesGenerator(
private val articles: List<ArticleStatistics>,
) {
fun topArticles(n: Int): List<ArticleStatistics> = TODO()
}
data class ArticleStatistics(
val title: String,
val views: Long,
)
fun main() {
val generator = TopArticlesGenerator(
listOf(
ArticleStatistics("Article 1", 400),
ArticleStatistics("Article 2", 100),
ArticleStatistics("Article 3", 200),
ArticleStatistics("Article 4", 300),
ArticleStatistics("Article 5", 500),
ArticleStatistics("Article 6", 0),
)
)
val topArticles = generator.topArticles(3)
topArticles.onEach { println(it) }
// ArticleStatistics(title=Article 1, views=400)
// ArticleStatistics(title=Article 4, views=300)
// ArticleStatistics(title=Article 5, views=500)
}
class TopArticlesTest {
@Test
fun `Top articles are returned in the correct order`() {
val articles = listOf(
ArticleStatistics("Article 1", 400),
ArticleStatistics("Article 2", 100),
ArticleStatistics("Article 3", 200),
ArticleStatistics("Article 4", 300),
ArticleStatistics("Article 5", 500),
ArticleStatistics("Article 6", 0),
)
val generator = TopArticlesGenerator(articles)
val topArticles = generator.topArticles(100)
assertEquals(articles, topArticles)
}
@Test
fun `Only n top articles are kept`() {
val articles = listOf(
ArticleStatistics("Article 1", 400),
ArticleStatistics("Article 2", 100),
ArticleStatistics("Article 3", 200),
ArticleStatistics("Article 4", 300),
ArticleStatistics("Article 5", 500),
ArticleStatistics("Article 6", 0),
)
val generator = TopArticlesGenerator(articles)
val topArticles = generator.topArticles(3)
assertEquals(articles.slice(listOf(0, 3, 4)), topArticles)
}
}
Marcin Moskala is a highly experienced developer and Kotlin instructor as the founder of Kt. Academy, an official JetBrains partner specializing in Kotlin training, Google Developers Expert, known for his significant contributions to the Kotlin community. Moskala is the author of several widely recognized books, including "Effective Kotlin," "Kotlin Coroutines," "Functional Kotlin," "Advanced Kotlin," "Kotlin Essentials," and "Android Development with Kotlin."
Beyond his literary achievements, Moskala is the author of the largest Medium publication dedicated to Kotlin. As a respected speaker, he has been invited to share his insights at numerous programming conferences, including events such as Droidcon and the prestigious Kotlin Conf, the premier conference dedicated to the Kotlin programming language.