Kotlin Collections Challenges (Part I)

Alfonso García Santiago
4 min readApr 29, 2020

--

… with only one line of code.

It is a practice-oriented article.

Each section has an explanation and data. All that you need to resolve it.

After that, you can access to my solution (it is not unique and very possibly it is not the best… just my solution) in two ways:

  • Screen capture
  • Link to the correspond Kotlin Playground exercise

To solve these challenges (with only one line of code) you need to master:

generateSequence()map {}mapIndexed {}groupingBy {}fold() {}filter {}zipWithNext {}max()maxBy()

Good Luck!

And don’t forget to enjoy it, please!

1. Fibonacci series

Golden shape (golden ratio)
Photo by Giulia May on Unsplash

Problem:

Generate a Fibonacci series with only one line of Kotlin code.

Solution:

Screenshot with my solution and the output

Run the code in Kotlin Playground

2. Basketball match scoreboard

Two teams playing a basketball match (blue and yellow)
Photo by Nik Shuliahin on Unsplash

Problem:

Two teams play a basketball match. Given the list of <player, team, points> for each player in the match, we have to get the match scoreboard with only one line of code.

Data:

enum class Team {
YELLOW, BLUE
}
data class PlayerGame (
val player : String,
val team : Team,
val points : Int
)
val match = listOf(
PlayerGame("13", Team.YELLOW, 12),
PlayerGame("7", Team.YELLOW, 7),
PlayerGame("3", Team.YELLOW, 20),
PlayerGame("22", Team.YELLOW, 16),
PlayerGame("33", Team.YELLOW, 4),
PlayerGame("45", Team.YELLOW, 26),
PlayerGame("8", Team.BLUE, 17),
PlayerGame("3", Team.BLUE, 15),
PlayerGame("21", Team.BLUE, 16),
PlayerGame("30", Team.BLUE, 12),
PlayerGame("45", Team.BLUE, 24)
)

Solution:

Screenshot with my solution and the output

Run the code in Kotlin Playground

3. Step by step

A person walking with sneakers
Photo by Arek Adeoye on Unsplash

Problem:

We have an array where each element is the number of steps taken until that moment (given by the position in the array).

How to get the maximum number of steps in a minute with one line of code?

Data:

val steps = listOf (
0, 71, 140, 212, 283, 358, 430, 501, 575, 640, 708, 780
)

Solution:

Screenshot with my solution and the output

Run the code in Kotlin Playground

4. Rallying Race Stages

A car in a rallying race (Toyota)
Photo by Maxime Agnelli on Unsplash

Problem:

A rallying race has 4 stages. At the end of each stage, a measure is taken. This measure has time accumulated in seconds and distance accumulated in meters. Besides this measure, we have the initial measure at the starting point with (0,0).

How to get the fastest stage with only one line of code?

Data:

data class Measure (
val number : Int,
val secondsAcc : Int,
val metersAcc : Int
)
val measures = mutableListOf (
Measure(0, 0, 0),
Measure(1, 302, 8090),
Measure(2, 689, 17655),
Measure(3, 1204, 32655),
Measure(4, 1425, 39884)
)

Solution:

Screenshot with my solution and the output

Stage number 4 is the stage from measure 3 to measure 4 (see diagram above)

Run the code in Kotlin Playground

5. Cumulative Elevation Gain (Total Ascent)

A group of cyclists in a professional race (the race’s leader is among them)
Photo by Simon Connellan on Unsplash

Problem:

Cumulative Elevation Gain or Total Ascent is:

the sum of every gain in elevation throughout an entire trip

We have an array with elevations in meters along the route. To simplify, suppose we do a relatively short cycling stage (2.5 km). Every 100 meters traveled we have one elevation measure, so we have 25 measures.

How to obtain the cumulative gain elevation in meters with only one line of code?

Data

val altitudes = listOf(
800, 805, 804, 800, 803, 806, 809, 805, 800, 798, 796, 799,
803, 805, 808, 812, 815, 816, 819, 822, 826, 830, 832, 835, 837
)

Solution

Screenshot with my solution and the output

I hope you’ve found this article enjoyable and useful!Any doubt, question or observation? Please, leave a comment!

--

--