LastExceed
10/13/2019, 7:39 AMPaul Woitaschek
10/13/2019, 10:10 AMAnil Gorthy
10/13/2019, 11:18 AMboolean
- builder pattern reduces the burden on the developer and prevents confusion when the order of the parameters is importantPaul Woitaschek
10/13/2019, 12:34 PMAnil Gorthy
10/13/2019, 1:42 PMkarelpeeters
10/13/2019, 4:51 PMfun testFoo() {
fun Int.has(index: Int) = ((this shr index) and 1) != 0
for (i in 0 until (2 shl 4))
if (Integer.bitCount(i) == 3)
foo(i.has(0), i.has(1), i.has(2), i.has(3), i.has(4))
}
fun foo(a: Boolean, b: Boolean, c: Boolean, d: Boolean, e: Boolean) = println("$a, $b, $c, $d, $e")
I don't know if that's the cleanest way though 😕LastExceed
10/13/2019, 4:54 PMLastExceed
10/13/2019, 5:06 PMfun 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 5karelpeeters
10/13/2019, 5:12 PMLastExceed
10/13/2019, 5:12 PM