Count values on each side of a threshold.

counting-groups Grouping can start with simple counters. Each loop pass updates the count for one group.

Counting Groups

threshold
CountingGroups.scala
Replay: real traced execution (multi-file project)
object Main {
  def main(args: Array[String]): Unit = {
    val threshold = 3
    val values = List(1, 2, 3, 4, 5)
    var small = 0
    var large = 0

    for (value <- values) {
      if (value <= threshold) {
        small = small + 1
      } else {
        large = large + 1
      }
    }

    println("threshold=" + threshold)
    println("small=" + small)
    println("large=" + large)
  }
}
object Main {
  def main(args: Array[String]): Unit = {
    val threshold = 2
    val values = List(1, 2, 3, 4, 5)
    var small = 0
    var large = 0

    for (value <- values) {
      if (value <= threshold) {
        small = small + 1
      } else {
        large = large + 1
      }
    }

    println("threshold=" + threshold)
    println("small=" + small)
    println("large=" + large)
  }
}
object Main {
  def main(args: Array[String]): Unit = {
    val threshold = 4
    val values = List(1, 2, 3, 4, 5)
    var small = 0
    var large = 0

    for (value <- values) {
      if (value <= threshold) {
        small = small + 1
      } else {
        large = large + 1
      }
    }

    println("threshold=" + threshold)
    println("small=" + small)
    println("large=" + large)
  }
}
  1. threshold ← 3, values ← List(1, 2, 3, 4, 5), small ← 0, large ← 0

    1object Main {2  def main(args: Array[String]): Unit = {3    val threshold→ 3 = 3 //@threshold=2, 44    val values→ List(1, 2, 3, 4, 5) = List(1, 2, 3, 4, 5)5    var small→ 0 = 06    var large→ 0 = 078    for (value <- values) {
  2. for (value <- values)

    pass 1 of 5
    8for (value1 <- valuesList(1, 2, 3, 4, 5)) {9  if (value <= threshold) {
    All 5 passes — pass 1 is the card above
    passvaluelarge
    11
    22
    33
    440 1
    551 2
  3. small ← 1

    pass 1 of 3
    8for (value <- values) {9  if (value1 <= threshold3) {10    small→ 1 = small + 111  } else {12    large = large + 1
    All 3 passes — pass 1 is the card above
    passvaluesmalllarge
    110 1
    221 2
    332 30 1
  4. large ← 1

    pass 1 of 2
    10    small = small + 111  } else {12    large→ 1 = large + 113  }14}
  5. large ← 2

    pass 2 of 2
    10    small = small + 111  } else {12    large→ 2 = large + 113  }14}
  6. println("threshold=" + threshold)

    16    println("threshold=" + threshold3)17    println("small=" + small3)18    println("large=" + large2)19  }20}
    outputthreshold=3
    small=3
    large=2
  1. threshold ← 2, values ← List(1, 2, 3, 4, 5), small ← 0, large ← 0

    1object Main {2  def main(args: Array[String]): Unit = {3    val threshold→ 2 = 24    val values→ List(1, 2, 3, 4, 5) = List(1, 2, 3, 4, 5)5    var small→ 0 = 06    var large→ 0 = 078    for (value <- values) {
  2. for (value <- values)

    pass 1 of 5
    8for (value1 <- valuesList(1, 2, 3, 4, 5)) {9  if (value <= threshold) {
    All 5 passes — pass 1 is the card above
    passvaluethresholdsmall
    1120 1
    2221 2
    33
    44
    55
  3. small ← 1

    pass 1 of 2
    8for (value <- values) {9  if (value1 <= threshold2) {10    small→ 1 = small + 111  } else {12    large = large + 1
  4. small ← 2

    pass 2 of 2
    8for (value <- values) {9  if (value2 <= threshold2) {10    small→ 2 = small + 111  } else {12    large = large + 1
  5. large ← 1

    pass 1 of 3
    10    small = small + 111  } else {12    large→ 1 = large + 113  }14}
    All 3 passes — pass 1 is the card above
    passlarge
    10 1
    21 2
    32 3
  6. println("threshold=" + threshold)

    16    println("threshold=" + threshold2)17    println("small=" + small2)18    println("large=" + large3)19  }20}
    outputthreshold=2
    small=2
    large=3
  1. threshold ← 4, values ← List(1, 2, 3, 4, 5), small ← 0, large ← 0

    1object Main {2  def main(args: Array[String]): Unit = {3    val threshold→ 4 = 44    val values→ List(1, 2, 3, 4, 5) = List(1, 2, 3, 4, 5)5    var small→ 0 = 06    var large→ 0 = 078    for (value <- values) {
  2. for (value <- values)

    pass 1 of 5
    8for (value1 <- valuesList(1, 2, 3, 4, 5)) {9  if (value <= threshold) {
    All 5 passes — pass 1 is the card above
    passvaluelarge
    11
    22
    33
    44
    550 1
  3. small ← 1

    pass 1 of 4
    8for (value <- values) {9  if (value1 <= threshold4) {10    small→ 1 = small + 111  } else {12    large = large + 1
    All 4 passes — pass 1 is the card above
    passvaluesmalllarge
    110 1
    221 2
    332 3
    443 40 1
  4. large ← 1

    10    small = small + 111  } else {12    large→ 1 = large + 113  }14}
  5. println("threshold=" + threshold)

    16    println("threshold=" + threshold4)17    println("small=" + small4)18    println("large=" + large1)19  }20}
    outputthreshold=4
    small=4
    large=1

Count Small and Large

  1. threshold is 3.
  2. The loop checks values 1 through 5.
  3. Values at or below 3 increase small.
  4. Values above 3 increase large. | Value | Group | Running counts | | --- | --- | --- | | 1 | small | small=1, large=0 | | 2 | small | small=2, large=0 | | 3 | small | small=3, large=0 | | 4 | large | small=3, large=1 | | 5 | large | small=3, large=2 |

Exercise: CountingGroups.scala

Loop through numbers, count values at or below a threshold, and count the rest separately