i tasked my roommate with this as practice since he's just getting started with programming, he came up with this:
fun getCombinations(onesRequired: Int, digitsRequired: Int, currentSolution: String = "", solutions: MutableList<String> = mutableListOf()): MutableList<String> {
if (digitsRequired < onesRequired) {
return solutions //no solution possible from here
}
if(onesRequired == 0) {
var y = currentSolution
for(x in 1..digitsRequired) {
y += "0" //append missing 0's
}
solutions.add(y)
return solutions
}
bitarray(onesRequired - 1, digitsRequired - 1, currentSolution + "1", solutions)
bitarray(onesRequired - 0, digitsRequired - 1, currentSolution + "0", solutions)
return solutions
}
fun main() {
val combinations = getCombinations(3,5)
combinations.forEach {
foo(it[0]=='1', it[1]=='1', it[2]=='1', it[3]=='1', it[4]=='1')
}
}
not a perfect solution, but it does the job. also lets me use it more generalized for n out of m instead of being locked to 3 out of 5