Skip to content

Commit 1351410

Browse files
committed
Generalize isSymmetrical() to a list of arbitrary type
The only condition is that the list elements' type implements equals() which is provided on `Any` level.
1 parent 507c22d commit 1351410

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/main/kotlin/de/ronny_h/aoc/extensions/Symmetries.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package de.ronny_h.aoc.extensions
22

3-
fun List<Int>.isSymmetrical(): Boolean {
3+
fun <T> List<T>.isSymmetrical(): Boolean {
44
for (i in 0..<size / 2) {
55
if (this[i] != this[lastIndex - i]) return false
66
}

src/test/kotlin/de/ronny_h/aoc/extensions/SymmetriesTest.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ class SymmetriesTest : StringSpec({
1313
listOf(1, 2, 3, 2, 1).isSymmetrical() shouldBe true
1414
}
1515

16-
"a asymmetrical list is not symmetrical" {
16+
"a symmetrical list of Strings is symmetrical" {
17+
listOf("a", "b", "c", "b", "a").isSymmetrical() shouldBe true
18+
}
19+
20+
"an asymmetrical list is not symmetrical" {
1721
listOf(4, 2, 3, 2, 1).isSymmetrical() shouldBe false
1822
}
1923
})

0 commit comments

Comments
 (0)