Skip to content

Commit 6dbe4c2

Browse files
committed
An extension function to get the ones digit of a Long value
1 parent c284755 commit 6dbe4c2

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package de.ronny_h.aoc.extensions
22

3+
import kotlin.math.abs
4+
35
/**
46
* Counts the number of digits of the given Long number.
57
*/
@@ -14,3 +16,8 @@ fun Long.digitCount(): Int {
1416
}
1517
return count
1618
}
19+
20+
/**
21+
* Returns the ones digit (the rightmost digit) of the given Long number.
22+
*/
23+
fun Long.onesDigit(): Int = abs(this % 10).toInt()

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,20 @@ class DigitsTest : StringSpec({
1313
number.digitCount() shouldBe digits
1414
}
1515
}
16+
17+
"The ones digit is returned" {
18+
forAll(
19+
row(0, 0),
20+
row(123L, 3),
21+
row(15887950, 0),
22+
row(16495136, 6),
23+
row(527345, 5),
24+
row(-123L, 3),
25+
row(-15887950, 0),
26+
row(-16495136, 6),
27+
row(-527345, 5),
28+
) { number, ones ->
29+
number.onesDigit() shouldBe ones
30+
}
31+
}
1632
})

0 commit comments

Comments
 (0)