File tree Expand file tree Collapse file tree 5 files changed +52
-3
lines changed
main/kotlin/de/ronny_h/aoc/extensions
test/kotlin/de/ronny_h/aoc/extensions/numbers Expand file tree Collapse file tree 5 files changed +52
-3
lines changed Original file line number Diff line number Diff line change 11package 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 {
4646fun <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
Original file line number Diff line number Diff 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 */
2323fun 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'.
Original file line number Diff line number Diff 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+ }
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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})
You can’t perform that action at this time.
0 commit comments