Skip to content

Commit bcd486f

Browse files
committed
pow(power) and digit(n) functions on Int
1 parent 6da6572 commit bcd486f

File tree

5 files changed

+52
-3
lines changed

5 files changed

+52
-3
lines changed

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

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

3-
import java.lang.Math.pow
3+
import kotlin.math.pow
44

55

66
/**
@@ -46,7 +46,7 @@ fun <E> permutationsOf(list: List<E>): Sequence<List<E>> = sequence {
4646
fun <E> allSublistsOf(list: List<E>): Sequence<List<E>> = sequence {
4747
// count a binary number with list.size digits from 0...0 to 1...1
4848
var number = 0
49-
while (number < pow(2.0, list.size.toDouble())) {
49+
while (number < 2.0.pow(list.size.toDouble())) {
5050
val sublist = ArrayList<E>(list.size)
5151
val binary = number.toString(2).reversed()
5252
// take the list elements at indices of the 1 bits

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,18 @@ fun Long.digitCount(): Int {
1818
}
1919

2020
/**
21-
* Returns the ones digit (the rightmost digit) of the given Long number.
21+
* @return The ones digit (the rightmost digit) of the given Long number.
2222
*/
2323
fun Long.onesDigit(): Int = abs(this % 10).toInt()
2424

25+
/**
26+
* @return The [n]th digit of the given Int number.
27+
*/
28+
fun Int.digit(n: Int): Int {
29+
require(n > 0) { "n must be greater than zero, but was: $n" }
30+
return abs(this / 10.pow(n - 1) % 10)
31+
}
32+
2533
/**
2634
* Converts a digit character to a boolean value.
2735
* @return `true` if this is '1', `false` if this is '0'.

src/main/kotlin/de/ronny_h/aoc/extensions/numbers/IntegralNumbers.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,15 @@ fun Long.toIntChecked(): Int {
2525
}
2626
return this.toInt()
2727
}
28+
29+
/**
30+
* @return This Int to the power of [power].
31+
*/
32+
fun Int.pow(power: Int): Int {
33+
if (power == 0) return 1
34+
var result = this
35+
repeat(power - 1) {
36+
result *= this
37+
}
38+
return result
39+
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ class DigitsTest : StringSpec({
3131
}
3232
}
3333

34+
"The nth digit is returned" {
35+
forAll(
36+
row(0, 1, 0),
37+
row(0, 2, 0),
38+
row(54321, 1, 1),
39+
row(54321, 2, 2),
40+
row(54321, 3, 3),
41+
row(54321, 4, 4),
42+
row(54321, 5, 5),
43+
row(54321, 6, 0),
44+
) { number, n, digit ->
45+
number.digit(n) shouldBe digit
46+
}
47+
}
48+
3449
"toBoolean converts a char digit to Boolean" {
3550
'0'.toBoolean() shouldBe false
3651
'1'.toBoolean() shouldBe true

src/test/kotlin/de/ronny_h/aoc/extensions/numbers/IntegralNumbersTest.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,18 @@ class IntegralNumbersTest : StringSpec({
7676
}
7777
}
7878
}
79+
80+
"The power of Ints" {
81+
forAll(
82+
row(0, 0, 1),
83+
row(1, 1, 1),
84+
row(1, 2, 1),
85+
row(10, 0, 1),
86+
row(10, 1, 10),
87+
row(10, 2, 100),
88+
row(10, 5, 100000),
89+
) { number, power, result ->
90+
number.pow(power) shouldBe result
91+
}
92+
}
7993
})

0 commit comments

Comments
 (0)