简体   繁体   English

Java:生成电源集

[英]Java: Generate a Powerset

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,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

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? 给定一个充满文件的目录,用Java生成此集合的最佳方法是什么?

您说过Java,但是请看一下: 使用C#泛型的置换,组合和变体

Here's pseudocode for a recursive approach to doing tests on all possible mixes.largest-subsets-first: 这是用于对所有可能的混合进行测试的递归方法的伪代码.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. 当然,如果您首先需要最小的子集,则不难进行重组。

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. 这将建立一个集合,然后将该集合和集合中的每个条目传递给do-test方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM