Given an n
x m
array of boolean:
[[true, true, false],
[false, true, true],
[false, true, true]]
what is a simple way that can return "how many true are there in that column?"
the result should be
[1, 3, 2]
Use transpose to get an array where each subarray represents a column and then map each column to the number of true
s in it:
arr.transpose.map {|subarr| subarr.count(true) }
Here's a version with inject that should run on 1.8.6 without any dependencies:
arr.transpose.map {|subarr| subarr.inject(0) {|s,x| x ? s+1 : s} }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With