I am given a set of sizes L and I want to generate each sorted subset of size k. It would be great if your solution is in scala, but maybe I can translate it myself.
An example for L = 6 and k = 3 should give.
1, 2, 3
1, 2, 4
1, 2, 5
1, 2, 6
1, 3, 4
1, 3, 5
1, 3, 6
1, 4, 5
1, 4, 6
1, 5, 6
2, 3, 4
2, 3, 5
2, 3, 6
2, 4, 5
2, 4, 6
2, 5, 6
3, 4, 5
3, 4, 6
3, 5, 6
4, 5, 6
My scala attempt was something like this:
object Util { def main(args : Array[String]) : Unit = { starts(6,3,1) } def starts(L: Int, num: Int, level: Int) : List[List[Int]] = { if( num == 0 ) { return List() }else{ (level to (L-num+1)).map( o => o :: starts(L,num-1,level+1)) } } }
I hope you help me.