Collections
Collection Grouping
Count short and long words with a small loop.
grouping
Grouping is often just counting values that belong to each category.
Collection Grouping
CollectionGrouping.kt
Replay: real traced execution (multi-file project)
fun main() {
val cutoff = 4
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 = 3
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 = 5
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")
}
cutoff ← 4, words ← [ant, bird, cloud], shortCount ← 0, longCount ← 0
1fun main() {2 val cutoff→ 4 = 4 //@cutoff=3, 53 val words→ [ant, bird, cloud] = listOf("ant", "bird", "cloud")4 var shortCount→ 0 = 05 var longCount→ 0 = 0for (word in words)
pass 1 of 37for (wordant in words[ant, bird, cloud]) {8 if (word.length <= cutoff) {All 3 passes — pass 1 is the card above pass wordword.lengthcutoffshortCountlongCount1 ant 3 4 0 → 1 — 2 bird 4 4 1 → 2 — 3 cloud — — — 0 → 1 shortCount ← 1
pass 1 of 27for (word in words) {8 if (word.length3 <= cutoff4) {9 shortCount→ 1 += 110 } else {values this stepantwordshortCount ← 2
pass 2 of 27for (word in words) {8 if (word.length4 <= cutoff4) {9 shortCount→ 2 += 110 } else {values this stepbirdwordlongCount ← 1
9 shortCount += 110} else {11 longCount→ 1 += 112}println("short=$shortCount")
15 println("short=$shortCount2")16 println("long=$longCount1")17}outputshort=2 long=1
cutoff ← 3, words ← [ant, bird, cloud], shortCount ← 0, longCount ← 0
1fun main() {2 val cutoff→ 3 = 33 val words→ [ant, bird, cloud] = listOf("ant", "bird", "cloud")4 var shortCount→ 0 = 05 var longCount→ 0 = 0for (word in words)
pass 1 of 37for (wordant in words[ant, bird, cloud]) {8 if (word.length <= cutoff) {All 3 passes — pass 1 is the card above pass wordword.lengthcutoffshortCountlongCount1 ant 3 3 0 → 1 — 2 bird — — — 0 → 1 3 cloud — — — 1 → 2 shortCount ← 1
7for (word in words) {8 if (word.length3 <= cutoff3) {9 shortCount→ 1 += 110 } else {values this stepantwordlongCount ← 1
pass 1 of 29 shortCount += 110} else {11 longCount→ 1 += 112}longCount ← 2
pass 2 of 29 shortCount += 110} else {11 longCount→ 2 += 112}println("short=$shortCount")
15 println("short=$shortCount1")16 println("long=$longCount2")17}outputshort=1 long=2
cutoff ← 5, words ← [ant, bird, cloud], shortCount ← 0, longCount ← 0
1fun main() {2 val cutoff→ 5 = 53 val words→ [ant, bird, cloud] = listOf("ant", "bird", "cloud")4 var shortCount→ 0 = 05 var longCount→ 0 = 0for (word in words)
pass 1 of 37for (wordant in words[ant, bird, cloud]) {8 if (word.length <= cutoff) {All 3 passes — pass 1 is the card above pass word1 ant 2 bird 3 cloud shortCount ← 1
pass 1 of 37for (word in words) {8 if (word.length3 <= cutoff5) {9 shortCount→ 1 += 110 } else {All 3 passes — pass 1 is the card above pass word.lengthwordshortCount1 3 ant 0 → 1 2 4 bird 1 → 2 3 5 cloud 2 → 3 println("short=$shortCount")
15 println("short=$shortCount3")16 println("long=$longCount0")17}outputshort=3 long=0
Count Short and Long
cutoffis4.- The loop checks
ant,bird, andcloud. - Lengths at or below
4increaseshortCount. - Longer words increase
longCount. | Word | Length | Group | | --- | --- | --- | |ant|3| short | |bird|4| short | |cloud|5| long |
Exercise: CollectionGrouping.kt
Loop through words, count short and long groups, and print both counts