Count short and long words with a small loop.

grouping Grouping is often just counting values that belong to each category.

Collection Grouping

cutoff
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")
}
  1. 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 = 0
  2. for (word in words)

    pass 1 of 3
    7for (wordant in words[ant, bird, cloud]) {8    if (word.length <= cutoff) {
    All 3 passes — pass 1 is the card above
    passwordword.lengthcutoffshortCountlongCount
    1ant340 1
    2bird441 2
    3cloud0 1
  3. shortCount ← 1

    pass 1 of 2
    7for (word in words) {8    if (word.length3 <= cutoff4) {9        shortCount→ 1 += 110    } else {
    values this stepantword
  4. shortCount ← 2

    pass 2 of 2
    7for (word in words) {8    if (word.length4 <= cutoff4) {9        shortCount→ 2 += 110    } else {
    values this stepbirdword
  5. longCount ← 1

    9    shortCount += 110} else {11    longCount→ 1 += 112}
  6. println("short=$shortCount")

    15    println("short=$shortCount2")16    println("long=$longCount1")17}
    outputshort=2
    long=1
  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 = 0
  2. for (word in words)

    pass 1 of 3
    7for (wordant in words[ant, bird, cloud]) {8    if (word.length <= cutoff) {
    All 3 passes — pass 1 is the card above
    passwordword.lengthcutoffshortCountlongCount
    1ant330 1
    2bird0 1
    3cloud1 2
  3. shortCount ← 1

    7for (word in words) {8    if (word.length3 <= cutoff3) {9        shortCount→ 1 += 110    } else {
    values this stepantword
  4. longCount ← 1

    pass 1 of 2
    9    shortCount += 110} else {11    longCount→ 1 += 112}
  5. longCount ← 2

    pass 2 of 2
    9    shortCount += 110} else {11    longCount→ 2 += 112}
  6. println("short=$shortCount")

    15    println("short=$shortCount1")16    println("long=$longCount2")17}
    outputshort=1
    long=2
  1. 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 = 0
  2. for (word in words)

    pass 1 of 3
    7for (wordant in words[ant, bird, cloud]) {8    if (word.length <= cutoff) {
    All 3 passes — pass 1 is the card above
    password
    1ant
    2bird
    3cloud
  3. shortCount ← 1

    pass 1 of 3
    7for (word in words) {8    if (word.length3 <= cutoff5) {9        shortCount→ 1 += 110    } else {
    All 3 passes — pass 1 is the card above
    password.lengthwordshortCount
    13ant0 1
    24bird1 2
    35cloud2 3
  4. println("short=$shortCount")

    15    println("short=$shortCount3")16    println("long=$longCount0")17}
    outputshort=3
    long=0

Count Short and Long

  1. cutoff is 4.
  2. The loop checks ant, bird, and cloud.
  3. Lengths at or below 4 increase shortCount.
  4. 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