article banner

Exercise: Top articles

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

Example usage:

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)

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