【问题标题】:Writing a tuples function that permutes all possible n-tuples编写一个置换所有可能的 n 元组的元组函数
【发布时间】:2025-11-24 20:10:02
【问题描述】:

我正在开发一个元组函数,它接受集合和参数 n。该参数指定生成的向量应具有的索引数。然后该函数置换集合中所有可能的元素的 n 元组。

到目前为止,我一直在尝试组合来自 tuples.core 和 math.combinatoris 的函数, 即元组和排列。

  (defn Tuples [& args]
        (combo/permutations (tuple args))) 

示例)

输入:(0,1) n=3

输出:[[0,0,0] [0,0,1] [0,1,0] [1,0,0] [0,1,1] [1,1,0] [1 ,0,1] [1,1,1]]

【问题讨论】:

  • 不清楚你在这里要求什么。您可以添加更多上下文吗?例如,样本输入和期望的输出。

标签: clojure clojure-contrib


【解决方案1】:

你要找的是clojure.math.combinatorics/selections:

(require '[clojure.math.combinatorics :as c])

(c/selections [0 1] 3)
;=> ((0 0 0) (0 0 1) (0 1 0) (0 1 1) (1 0 0) (1 0 1) (1 1 0) (1 1 1))

【讨论】:

    最近更新 更多