Scala 函数获取大小为 k 的所有排序子集
我得到了一组大小为 L 的集合,并且想要生成大小为 k 的每个已排序子集。 如果你的解决方案是用 scala 编写的,那就太好了,但也许我可以自己翻译。
L = 6 和 k = 3 运行的示例应该会产生结果。
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
我的 scala 尝试是这样的:
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 am given a set of size L and want to generate every sorted subset of size k.
Would be great if your solution is in scala but maybe I am able to translate by myself.
An example run for L = 6 and k = 3 should yield.
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:
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 can help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您所需要的只是
结果:
按要求。
All you need is
Results:
as required.
你可以从那开始
You could start from that
如果您实际上有块而不是条目,那么您需要知道每个块的长度。您所显示的是条目(或者等效地,长度为 1 的块)。
首先,请注意我们必须有
L>=k
。其次,请注意,如果第一个条目是i
,则剩余的可能性是i+f(Li,k-1)
,其中f
是产生条目的内容,并假设+
分布在集合中。最后,我们注意到,带有为每个元素生成序列的函数的flatMap
会将结果解压缩为单个序列。现在我们已经拥有了需要知道的一切:
如果您实际上指的是块,那么我们需要知道块有多长。分析是相同的,只是我们需要跳过块大小,而不是只跳过一个值:(
列出了块的起始条目)。
If you actually have blocks and not entries then you need to know how long each block is. What you've shown is for entries (or, equivalently, blocks of length 1).
First, note that we must have
L>=k
. Second, note that if the first entry isi
, the remaining possibilities arei+f(L-i,k-1)
, wheref
is what produces the entries, and assuming that+
distributes across collections. Finally, we note that aflatMap
with a function that produces sequences for every element will unpack the result into a single sequence.Now that we have everything we need to know:
If you did in fact mean blocks, then we need to know how long the blocks are. The analysis is the same, except instead of skipping only one value, we need to skip the block size:
(the starting entry of the block is listed).