clojure:子集的排列?(clojure: permutations of subsets?)
我是clojure的新手,正在寻找一个函数来生成子集的排列:
=> (find-subsets 1 #{1 2 3 4}) (#{1} #{2} #{3} #{4}) => (find-subsets 2 #{1 2 3 4}) (#{1 2} #{1 3} #{1 4} #{2 3} #{2 4} #{3 4}) => (find-subsets 3 #{1 2 3 4}) (#{1 2 3} #{1 3 4} #{2 3 4})这样的事情存在吗? 如果没有,是否有一个很好的,干净的,惯用的方式来编写功能?
I'm new to clojure, looking for a function to generate permutations of subsets:
=> (find-subsets 1 #{1 2 3 4}) (#{1} #{2} #{3} #{4}) => (find-subsets 2 #{1 2 3 4}) (#{1 2} #{1 3} #{1 4} #{2 3} #{2 4} #{3 4}) => (find-subsets 3 #{1 2 3 4}) (#{1 2 3} #{1 3 4} #{2 3 4})Does such a thing exist? If not, is there a nice, clean, idiomatic way to code the function?
最满意答案
看一下组合学 。 它满足你的需求:
; all the unique ways of taking n different elements from items (clojure.math.combinatorics/combinations [1 2 3] 2) ;;=> ((1 2) (1 3) (2 3))如果因为使用集合而不是矢量而抱怨,只需在调用combinations之前将其转换为带矢量的矢量。
Take a look at combinatorics. It does what you need:
; all the unique ways of taking n different elements from items (clojure.math.combinatorics/combinations [1 2 3] 2) ;;=> ((1 2) (1 3) (2 3))If it complains because you use a set instead of a vector, just convert to vector with vec before calling combinations.
更多推荐
发布评论