August 26, 2024
P30 - Sorting a list of lists according to length of sublists (2)
a) We suppose that a list contains elements that are lists themselves. The objective is to sort elements of the list according to their length. E.g. short lists first, longer lists later, or vice versa. Example:
> lengthSort(listOf("abc".toList(), "de".toList(), "fgh".toList(), "de".toList(), "ijkl".toList(), "mn".toList(), "o".toList()))
[[o], [d, e], [d, e], [m, n], [a, b, c], [f, g, h], [i, j, k, l]]
b) Again, we suppose that a list contains elements that are lists themselves. But this time the objective is to sort elements according to their length frequency; i.e. lists with rare lengths are placed first, others with more frequent lengths come later. Example:
> lengthFreqSort(listOf("abc".toList(), "de".toList(), "fgh".toList(), "de".toList(), "ijkl".toList(), "mn".toList(), "o".toList()))
[[i, j, k, l], [o], [a, b, c], [f, g, h], [d, e], [d, e], [m, n]]
Note that in the above example, the first two lists in the result have length 4 and 1 and both lengths appear just once. The third and fourth lists have length 3 and there are two list of this length. Finally, the last three lists have length 2. This is the most frequent length.
kotlin
package org.kotlin99.lists
import com.natpryce.hamkrest.assertion.assertThat
import com.natpryce.hamkrest.equalTo
import org.junit.Test
fun <T> lengthSort(listOfLists: List<List<T>>): List<List<T>> = listOfLists.sortedBy { it.size }
fun <T> lengthFreqSort(listOfLists: List<List<T>>): List<List<T>> {
val groupedLists = listOfLists.groupBy { it.size }
return listOfLists.sortedBy { groupedLists.getValue(it.size).size }
}
class P28Test {
@Test fun `a) sort elements of the list according to their length`() {
assertThat(
lengthSort(listOf(
"abc",
"de",
"fgh",
"de",
"ijkl",
"mn",
"o"
).map { it.toList() }),
equalTo(listOf(
"o",
"de",
"de",
"mn",
"abc",
"fgh",
"ijkl"
).map { it.toList() })
)
}
@Test fun `b) sort elements according to their length frequency`() {
assertThat(
lengthFreqSort(listOf(
"abc",
"de",
"fgh",
"de",
"ijkl",
"mn",
"o"
).map { it.toList() }),
equalTo(listOf(
"ijkl",
"o",
"abc",
"fgh",
"de",
"de",
"mn"
).map { it.toList() })
)
}
}