Kotlin Collections Challenges (Part I)

  • Screen capture
  • Link to the correspond Kotlin Playground exercise
generateSequence()map {}mapIndexed {}groupingBy {}fold() {}filter {}zipWithNext {}max()maxBy()

1. Fibonacci series

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

2. Basketball match scoreboard

Two teams playing a basketball match (blue and yellow)
Photo by Nik Shuliahin on Unsplash
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)
)

3. Step by step

A person walking with sneakers
Photo by Arek Adeoye on Unsplash
val steps = listOf (
0, 71, 140, 212, 283, 358, 430, 501, 575, 640, 708, 780
)

4. Rallying Race Stages

A car in a rallying race (Toyota)
Photo by Maxime Agnelli on Unsplash
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)
)

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

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.

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
)
To be continued…
I hope you’ve found this article enjoyable and useful!Any doubt, question or observation? Please, leave a comment!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store