Java:生成幂集

发布于 2024-08-10 02:54:21 字数 458 浏览 5 评论 0原文

这可能是与语言无关的/有用的答案可能只是在伪代码中。

我有一个程序想在一系列输入下进行测试。该程序需要一组文件,其中一个被指定为根。我想使用所有可能的文件子集运行该程序。 (包含相同文件但具有不同根的两个子集被认为是不同的。)

这是一个相同的示例。假设我有文件 A、B 和 C。我想用以下内容进行测试:

{A}, root = A
{B}, root = B
{C}, root = C
{A B}, root = A
{A B}, root = B
{B C}, root = B
{B C}, root = C
{A C}, root = A
{A C}, root = C
{A B C}, root = A
{A B C}, root = B
{A B C}, root = C

等等。我相信这将是动力组。

给定一个充满文件的目录,在 Java 中生成这个集合的最佳方法是什么?

This could be language agnostic/helpful answers could just be in pseudo-code.

I have a program that I would like to test under a range of inputs. This program takes a set of files, one of which is designated as the root. I want to run the program with all possible subsets of files. (Two subsets, containing the same files, but with different roots, are considered to be different.)

Here's a same example. Say I have files A, B, and C. I would want to test with:

{A}, root = A
{B}, root = B
{C}, root = C
{A B}, root = A
{A B}, root = B
{B C}, root = B
{B C}, root = C
{A C}, root = A
{A C}, root = C
{A B C}, root = A
{A B C}, root = B
{A B C}, root = C

and so on. I believe this would be the powerset.

What is the best way to generate this set in Java, given a directory full of files?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜声 2024-08-17 02:54:21

您说的是 Java,但请看一下:使用 C# 的排列、组合和变体泛型

You said Java, but please take a look on this: Permutations, Combinations, and Variations using C# Generics.

楠木可依 2024-08-17 02:54:21

下面是对所有可能的 mixes 进行测试的递归方法的伪代码。最大子集优先:

allofthem = set(listallfiles(thedir))

function trythemall(someset):
  if someset is empty: return
  for entry in someset:
    dotest(someset, root=entry)
  for entry in someset:
    trythemall(someset - set([entry]))

trythemall(allofthem)

当然,如果您首先想要最小的子集,那么重组并不难。

Here's pseudocode for a recursive approach to doing tests on all possible mixes.largest-subsets-first:

allofthem = set(listallfiles(thedir))

function trythemall(someset):
  if someset is empty: return
  for entry in someset:
    dotest(someset, root=entry)
  for entry in someset:
    trythemall(someset - set([entry]))

trythemall(allofthem)

Not hard to reorg if you want smallest subsets first, of course.

安人多梦 2024-08-17 02:54:21

这就是你所追求的(伪代码)吗?

set = new List()
foreach (file in dir) {
    set.add(file)
    foreach (entry in set) {
        do-test(set, entry)
    }
}

这将构建一个集合,然后将该集合和集合中的每个条目传递给 do-test 方法。

Is this what you are after (psuedocode)?

set = new List()
foreach (file in dir) {
    set.add(file)
    foreach (entry in set) {
        do-test(set, entry)
    }
}

This would build a set, then pass the set and each entry in the set to a do-test method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文