Created
October 18, 2016 14:07
-
-
Save dkilmer/aa75533afa8889f022040e813a180861 to your computer and use it in GitHub Desktop.
Truth-table of 2 aggregated booleans
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.math.BigDecimal | |
import java.math.RoundingMode | |
class Crosstab2d(val colLabel: String, val rowLabel: String) { | |
var tot: Int = 0 | |
val grid = arrayOf( | |
intArrayOf(0, 0), | |
intArrayOf(0, 0) | |
) | |
fun aggregate(colVal: Boolean, rowVal: Boolean) { | |
val col = if (colVal) 1 else 0 | |
val row = if (rowVal) 1 else 0 | |
grid[row][col] += 1 | |
tot += 1 | |
} | |
fun print() { | |
val pcts = arrayOf( | |
arrayOf( | |
((BigDecimal("${grid[0][0]}.0000") / BigDecimal("$tot.0000")) * BigDecimal("100.00")).setScale(2, RoundingMode.HALF_EVEN), | |
((BigDecimal("${grid[0][1]}.0000") / BigDecimal("$tot.0000")) * BigDecimal("100.00")).setScale(2, RoundingMode.HALF_EVEN) | |
), | |
arrayOf( | |
((BigDecimal("${grid[1][0]}.0000") / BigDecimal("$tot.0000")) * BigDecimal("100.00")).setScale(2, RoundingMode.HALF_EVEN), | |
((BigDecimal("${grid[1][1]}.0000") / BigDecimal("$tot.0000")) * BigDecimal("100.00")).setScale(2, RoundingMode.HALF_EVEN) | |
) | |
) | |
val rowLen = rowLabel.length+2 | |
val rowBar = "-".repeat(rowLen) | |
val rowSpc = " ".repeat(rowLen) | |
println("$rowSpc | $colLabel") | |
println("$rowSpc | 0 | 1 |") | |
println("$rowBar---|----------|----------|") | |
println("$rowSpc 0 | ${String.format("%8d", grid[0][0])} | ${String.format("%8d", grid[0][1])} |") | |
println("$rowSpc | ${pcts[0][0].toString().padStart(7)}% | ${pcts[0][1].toString().padStart(7)}% |") | |
println("- $rowLabel --|----------|----------|") | |
println("$rowSpc 1 | ${String.format("%8d", grid[1][0])} | ${String.format("%8d", grid[1][1])} |") | |
println("$rowSpc | ${pcts[1][0].toString().padStart(7)}% | ${pcts[1][1].toString().padStart(7)}% |") | |
println() | |
println("total: $tot") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment