Collections
Collection Grouping
Count short and long words with a small loop.
Collection Grouping
CollectionGrouping.kt
fun main() {
val cutoff =
val words = listOf("ant", "bird", "cloud")
var shortCount = 0
var longCount = 0
for (word in words) {
if (word.length <= cutoff) {
shortCount += 1
} else {
longCount += 1
}
}
println("short=$shortCount")
println("long=$longCount")
}
fun main() {
val cutoff =
val words = listOf("ant", "bird", "cloud")
var shortCount = 0
var longCount = 0
for (word in words) {
if (word.length <= cutoff) {
shortCount += 1
} else {
longCount += 1
}
}
println("short=$shortCount")
println("long=$longCount")
}
fun main() {
val cutoff =
val words = listOf("ant", "bird", "cloud")
var shortCount = 0
var longCount = 0
for (word in words) {
if (word.length <= cutoff) {
shortCount += 1
} else {
longCount += 1
}
}
println("short=$shortCount")
println("long=$longCount")
}
grouping
Grouping is often just counting values that belong to each category.