Skip to content

Commit 1069767

Browse files
incendiumSleeplessByte
authored andcommitted
Run automated migrations for latest Kotlin version and clean up some additional warnings by hand.
1 parent 84b3802 commit 1069767

File tree

43 files changed

+97
-89
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+97
-89
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
object Acronym {
22
fun generate(phrase: String) = Regex("[A-Z]+[a-z]*|[a-z]+")
33
.findAll(phrase.replace("'", ""))
4-
.map { it.value.first().toUpperCase() }
4+
.map { it.value.first().uppercaseChar() }
55
.joinToString("")
66
}

exercises/practice/affine-cipher/.meta/src/reference/kotlin/AffineCipher.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ object AffineCipher {
2626
require(inv != 1) { "Key A and alphabet size must be coprime!" }
2727

2828
return text
29-
.toLowerCase()
29+
.lowercase()
3030
.mapNotNull { c ->
3131
when {
3232
c.isDigit() -> c

exercises/practice/all-your-base/.meta/src/reference/kotlin/BaseConverter.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import kotlin.math.floor
2+
import kotlin.math.pow
3+
14
class BaseConverter(originalBase: Int, originalDigits: IntArray) {
25

36
companion object {
4-
private val MINIMUM_VALID_BASE = 2
5-
private val INVALID_BASE_ERROR_MESSAGE = "Bases must be at least 2."
7+
private const val MINIMUM_VALID_BASE = 2
8+
private const val INVALID_BASE_ERROR_MESSAGE = "Bases must be at least 2."
69
}
710

811
private val numeral: Int
@@ -25,11 +28,11 @@ class BaseConverter(originalBase: Int, originalDigits: IntArray) {
2528
var remainder = numeral
2629

2730
for (currentExponent in largestExponent downTo 0) {
28-
val coefficient = Math.floor(remainder / Math.pow(newBase.toDouble(), currentExponent.toDouble())).toInt()
31+
val coefficient = floor(remainder / newBase.toDouble().pow(currentExponent.toDouble())).toInt()
2932

3033
result[largestExponent - currentExponent] = coefficient
3134

32-
remainder -= (coefficient * Math.pow(newBase.toDouble(), currentExponent.toDouble())).toInt()
35+
remainder -= (coefficient * newBase.toDouble().pow(currentExponent.toDouble())).toInt()
3336
}
3437

3538
return result
@@ -38,9 +41,9 @@ class BaseConverter(originalBase: Int, originalDigits: IntArray) {
3841
private fun computeNumeral(originalBase: Int, originalDigits: IntArray): Int {
3942
val largestExponent = originalDigits.size - 1
4043

41-
val result = (largestExponent downTo 0).sumBy { exponent ->
44+
val result = (largestExponent downTo 0).sumOf { exponent ->
4245
(originalDigits[largestExponent - exponent]
43-
* Math.pow(originalBase.toDouble(), exponent.toDouble())).toInt()
46+
* originalBase.toDouble().pow(exponent.toDouble())).toInt()
4447
}
4548

4649
return result
@@ -49,7 +52,7 @@ class BaseConverter(originalBase: Int, originalDigits: IntArray) {
4952
private fun computeLargestExponentForBase(newBase: Int): Int {
5053
var result = 0
5154

52-
while (Math.pow(newBase.toDouble(), (result + 1).toDouble()) < numeral) {
55+
while (newBase.toDouble().pow((result + 1).toDouble()) < numeral) {
5356
result += 1
5457
}
5558

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Anagram(private val word: String) {
22

33
fun match(anagrams: Collection<String>) =
4-
anagrams.filter { containSameChars(it.toLowerCase()) }
4+
anagrams.filter { containSameChars(it.lowercase()) }
55
.filterNot { it.equals(word, ignoreCase = true) }
66
.toSet()
77

88
private fun containSameChars(candidate: String) =
9-
candidate.toLowerCase().toCharArray().sorted() == word.toLowerCase().toCharArray().sorted()
9+
candidate.lowercase().toCharArray().sorted() == word.lowercase().toCharArray().sorted()
1010

1111
}

exercises/practice/armstrong-numbers/.meta/src/reference/kotlin/ArmstrongNumber.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ object ArmstrongNumber {
66
val str = input.toString()
77
val inputLength = str.length
88
val calculated = str
9-
.map { Character.getNumericValue(it) } // digit character to its numeric (not ASCII!) value
10-
.map { BigInteger.valueOf(it.toLong()).pow(inputLength).toLong() } // calculate digit^inputLength
11-
.sum()
9+
.map { Character.getNumericValue(it) } // digit character to its numeric (not ASCII!) value
10+
.sumOf { BigInteger.valueOf(it.toLong()).pow(inputLength).toLong() } // calculate digit^inputLength
1211

1312
return input == calculated
1413
}

exercises/practice/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
object Atbash {
2-
private val GROUP_SIZE = 5
2+
private const val GROUP_SIZE = 5
33

44
fun encode(s: String): String = cipherSubstitution(s).mapIndexed { index, char -> char + groupFinalizer(index) }.joinToString("").trimEnd()
55

66
fun decode(s: String): String = cipherSubstitution(s)
77

8-
private fun cipherSubstitution(s: String): String = s.fold("", { accum, char -> accum + substitute(char) })
8+
private fun cipherSubstitution(s: String): String = s.fold("") { accum, char -> accum + substitute(char) }
99

1010
private fun substitute(c: Char): String {
1111
return when {
1212
c.isDigit() -> c.toString()
13-
c.isLetter() -> ('a' + ('z' - c.toLowerCase())).toString()
13+
c.isLetter() -> ('a' + ('z' - c.lowercaseChar())).toString()
1414
else -> ""
1515
}
1616
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
object Binary {
22
fun toDecimal(s: String): Int {
3-
return s.fold(0, { accum, char ->
3+
return s.fold(0) { accum, char ->
44
when (char) {
55
'0' -> accum * 2
66
'1' -> (accum * 2) + 1
77
else -> return 0
88
}
9-
})
9+
}
1010
}
1111
}

exercises/practice/bob/.meta/src/reference/kotlin/Bob.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ object Bob {
1616
private fun isQuestion(input: String) = input.endsWith("?")
1717

1818
private fun isShout(input: String): Boolean {
19-
val isOnlyUppercase = input == input.toUpperCase()
19+
val isOnlyUppercase = input == input.uppercase()
2020
val hasLetter = input.contains(Regex("[A-Z]"))
2121

2222
return hasLetter && isOnlyUppercase

exercises/practice/change/.meta/src/reference/kotlin/ChangeCalculator.kt

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,17 @@ class ChangeCalculator(coins: List<Int>) {
66
require(grandTotal >= 0) { "Negative totals are not allowed." }
77

88
val minimalCoinsMap = mutableMapOf<Int, List<Int>?>()
9-
minimalCoinsMap.put(0, ArrayList())
9+
minimalCoinsMap[0] = ArrayList()
1010

1111
for (total in 1..grandTotal) {
1212
val minimalCoins = sortedCoins
13-
.filter { it <= total }
14-
.mapNotNull { coin ->
15-
val minimalRemainderCoins = minimalCoinsMap[total - coin]
16-
if (minimalRemainderCoins != null) prepend(coin, minimalRemainderCoins) else null
17-
}
18-
.sortedBy { it.size }
19-
.firstOrNull()
20-
21-
minimalCoinsMap.put(total, minimalCoins)
13+
.filter { it <= total }
14+
.mapNotNull { coin ->
15+
val minimalRemainderCoins = minimalCoinsMap[total - coin]
16+
if (minimalRemainderCoins != null) prepend(coin, minimalRemainderCoins) else null
17+
}.minByOrNull { it.size }
18+
19+
minimalCoinsMap[total] = minimalCoins
2220
}
2321

2422
return minimalCoinsMap[grandTotal] ?: throw IllegalArgumentException(

exercises/practice/clock/.meta/src/reference/kotlin/Clock.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
data class Clock(private var hours: Int, private var minutes: Int) {
22

33
companion object {
4-
private val MINUTES_IN_AN_HOUR = 60
5-
private val HOURS_IN_A_DAY = 24
4+
private const val MINUTES_IN_AN_HOUR = 60
5+
private const val HOURS_IN_A_DAY = 24
66
}
77

88
init {

0 commit comments

Comments
 (0)