-
Notifications
You must be signed in to change notification settings - Fork 92
/
P28.kt
61 lines (55 loc) · 1.53 KB
/
P28.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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() })
)
}
}