Skip to content

Commit 3ea7efd

Browse files
committed
A function that tests a list for symmetries
1 parent c15c589 commit 3ea7efd

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package de.ronny_h.extensions
2+
3+
fun List<Int>.isSymmetrical(): Boolean {
4+
for (i in 0..<size / 2) {
5+
if (this[i] != this[lastIndex - i]) return false
6+
}
7+
return true
8+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package de.ronny_h.extensions
2+
3+
import org.assertj.core.api.Assertions.assertThat
4+
import org.junit.jupiter.api.Test
5+
6+
class SymmetriesTest {
7+
8+
@Test
9+
fun `an empty list is symmetrical`() {
10+
assertThat(listOf<Int>().isSymmetrical()).isTrue()
11+
}
12+
13+
@Test
14+
fun `a symmetrical list is symmetrical`() {
15+
assertThat(listOf(1, 2, 3, 2, 1).isSymmetrical()).isTrue()
16+
}
17+
18+
@Test
19+
fun `a asymmetrical list is not symmetrical`() {
20+
assertThat(listOf(4, 2, 3, 2, 1).isSymmetrical()).isFalse()
21+
}
22+
}

0 commit comments

Comments
 (0)