Joris PZ
12/02/2018, 8:49 AMDay 2 - Inventory Management System
?Joris PZ
12/02/2018, 8:51 AMJVM: 15 ms
NodeJS: 115ms
Native: 620ms
I was pretty pleased with this extension functions to count the character frequencies in a string:
fun String.letterMap() = this.groupingBy { it }.eachCount()
keturn
12/02/2018, 9:05 AMeachCount
is built-in there's not much left on part one 😀keturn
12/02/2018, 9:07 AMketurn
12/02/2018, 9:07 AMGerard Klijs
12/02/2018, 9:12 AMJoris PZ
12/02/2018, 9:13 AMandyb
12/02/2018, 9:14 AMfun String.hammingDistance(other: String) = this.zip(other).count { it.first != it.second }
keturn
12/02/2018, 9:26 AMandyb
12/02/2018, 9:29 AMkarelpeeters
12/02/2018, 2:04 PMtodd.ginsberg
12/02/2018, 2:16 PMstkent
12/02/2018, 2:17 PMtodd.ginsberg
12/02/2018, 2:19 PMstkent
12/02/2018, 2:20 PMtodd.ginsberg
12/02/2018, 2:23 PMtodd.ginsberg
12/02/2018, 2:23 PMkarelpeeters
12/02/2018, 2:24 PMstkent
12/02/2018, 2:24 PMany
for that part, but it tripped me first toostkent
12/02/2018, 2:25 PMinput.count { id -> id.charCounts().any { it.value == 2 } }
where charCounts
is the obvious extensionkarelpeeters
12/02/2018, 2:25 PMfoo.any { it == x }
is just x in foo
, right?stkent
12/02/2018, 2:26 PMstkent
12/02/2018, 2:26 PMstkent
12/02/2018, 2:26 PMin
a lotstkent
12/02/2018, 2:26 PMtodd.ginsberg
12/02/2018, 2:26 PMtodd.ginsberg
12/02/2018, 2:27 PMkarelpeeters
12/02/2018, 2:27 PMtodd.ginsberg
12/02/2018, 2:30 PMtodd.ginsberg
12/02/2018, 2:30 PMprivate fun String.findLetterPairs(): Pair<Boolean, Boolean> {
val byChar = this.groupingBy { it }.eachCount()
return Pair(
byChar.any { it.value == 2 },
byChar.any { it.value == 3 }
)
}
todd.ginsberg
12/02/2018, 2:30 PMketurn
12/02/2018, 7:56 PMit.value ==
is in the stdlib as https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-map/contains-value.htmlkarelpeeters
12/02/2018, 7:57 PMketurn
12/02/2018, 7:58 PM