Collections
Counting Groups
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
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)
}
}
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) {for (value <- values)
pass 1 of 58for (value1 <- valuesList(1, 2, 3, 4, 5)) {9 if (value <= threshold) {All 5 passes — pass 1 is the card above pass valuelarge1 1 — 2 2 — 3 3 — 4 4 0 → 1 5 5 1 → 2 small ← 1
pass 1 of 38for (value <- values) {9 if (value1 <= threshold3) {10 small→ 1 = small + 111 } else {12 large = large + 1All 3 passes — pass 1 is the card above pass valuesmalllarge1 1 0 → 1 — 2 2 1 → 2 — 3 3 2 → 3 0 → 1 large ← 1
pass 1 of 210 small = small + 111 } else {12 large→ 1 = large + 113 }14}large ← 2
pass 2 of 210 small = small + 111 } else {12 large→ 2 = large + 113 }14}println("threshold=" + threshold)
16 println("threshold=" + threshold3)17 println("small=" + small3)18 println("large=" + large2)19 }20}outputthreshold=3 small=3 large=2
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) {for (value <- values)
pass 1 of 58for (value1 <- valuesList(1, 2, 3, 4, 5)) {9 if (value <= threshold) {All 5 passes — pass 1 is the card above pass valuethresholdsmall1 1 2 0 → 1 2 2 2 1 → 2 3 3 — — 4 4 — — 5 5 — — small ← 1
pass 1 of 28for (value <- values) {9 if (value1 <= threshold2) {10 small→ 1 = small + 111 } else {12 large = large + 1small ← 2
pass 2 of 28for (value <- values) {9 if (value2 <= threshold2) {10 small→ 2 = small + 111 } else {12 large = large + 1large ← 1
pass 1 of 310 small = small + 111 } else {12 large→ 1 = large + 113 }14}All 3 passes — pass 1 is the card above pass large1 0 → 1 2 1 → 2 3 2 → 3 println("threshold=" + threshold)
16 println("threshold=" + threshold2)17 println("small=" + small2)18 println("large=" + large3)19 }20}outputthreshold=2 small=2 large=3
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) {for (value <- values)
pass 1 of 58for (value1 <- valuesList(1, 2, 3, 4, 5)) {9 if (value <= threshold) {All 5 passes — pass 1 is the card above pass valuelarge1 1 — 2 2 — 3 3 — 4 4 — 5 5 0 → 1 small ← 1
pass 1 of 48for (value <- values) {9 if (value1 <= threshold4) {10 small→ 1 = small + 111 } else {12 large = large + 1All 4 passes — pass 1 is the card above pass valuesmalllarge1 1 0 → 1 — 2 2 1 → 2 — 3 3 2 → 3 — 4 4 3 → 4 0 → 1 large ← 1
10 small = small + 111 } else {12 large→ 1 = large + 113 }14}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
thresholdis3.- The loop checks values
1through5. - Values at or below
3increasesmall. - Values above
3increaselarge. | 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