Clean algorithm to generate all sets of the kind (0) to (0,1,2,3,4,5,6,7,8,9)

j.i.h.

Basically, I'd like a set of sets that contains from (0..9), then (0, 1..9), (1, 2..9)..(8,9), and so on and so forth up until (0,1,2,3,4,5,6,7,8,9). I know this can be accomplished by nesting for loops in the manner below, but I'm wondering if there's a neater way of doing it?

Preferably something that could be accomplished within C#, but I'm interested in any algorithm.

for (int i = 0; i < max; i++) {
    yield {i};
    for (int j = i + 1; j < max; j++) {
        yield {i, j};
        for (int k = j + 1; k < max; k++) {
            yield {i, j, k};
            for (int l = k + 1; l < max; l++) {
                yield {i, j, k, l};
                for (int m = l + 1; m < max; m++) {
                    yield {i, j, k, l, m};
                    // And so on and so forth
                }
            }
        }
    }
}
Dennis_E

I wrote this a while ago. It uses a Stack. It's generic, so it can be used for other sequences as well.

static IEnumerable<T[]> CombinationsAnyLength<T>(params T[] values)
{
    Stack<int> stack = new Stack<int>(values.Length);
    int i = 0;
    while (stack.Count > 0 || i < values.Length) {
        if (i < values.Length) {
            stack.Push(i++);
            int c = stack.Count;
            T[] result = new T[c];
            foreach (var index in stack) result[--c] = values[index];
            yield return result;
        } else {
            i = stack.Pop() + 1;
            if (stack.Count > 0) i = stack.Pop() + 1;
        }
    }
}

CombinationsAnyLength(1, 2, 3, 4) outputs:

1 12 123 1234 124 13 134 14 2 23 234 24 3 34 4

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Cの{0,1,2,3,4,5,6,7,8,9}以外の条件

分類Dev

種類(0)から(0,1,2,3,4,5,6,7,8,9)のすべてのセットを生成するクリーンなアルゴリズム

分類Dev

Algorithm to generate every combination of n elements split into k sets

分類Dev

配列の要素をa [0]、a [1] + a [2]、a [3] + a [4] + a [5]、a [6] + a [7] + a [8]として追加します+ a [9] ...など

分類Dev

What is a good algorithm to check whether or not a number exist in multiple sets without searching them all?

分類Dev

generate 0 byte pdf with droidtext

分類Dev

which kind of array can give to paste0

分類Dev

In GLSL integers are all 0

分類Dev

Finding elements that appear in all sets in a list of sets

分類Dev

How to generate {0} json string by PHP?

分類Dev

Generate a matrix without 0 values in a random way

分類Dev

Algorithm to find if two sets of sets of numbers are isomorphic or not (under permutation)

分類Dev

Generate all combinations of 2 lists (game playing)

分類Dev

Cannot find symbol on clean generate release

分類Dev

Best algorithm for converting real number between 0 and 1 to index

分類Dev

Reducing time complexity of Knapsack 0~1 while using DP algorithm

分類Dev

1で始まり0で終わるintループを設計するにはどうすればよいですか(1,2,3,4,5,6,7,8,9,0)

分類Dev

`mdadm --create --assume-clean`でRAID0を拡張しますか?

分類Dev

Generate all combinations

分類Dev

ClassNotFoundException:com.auth0.jwt.algorithms.Algorithm

分類Dev

SChannel、SEC_E_ALGORITHM_MISMATCH(0x80090331)

分類Dev

Generate a random number and add 0 before it to ensure fixed number of digits

分類Dev

アンダースコアまたはlazy.jsマップ(0,1,2,3,4)+(1,2,3,4,5)->(1,3,5,7,9)

分類Dev

Generate all possible sequential strings from list using Python 3

分類Dev

php function return 0 or no return at all

分類Dev

All the rows expect 0th Row

分類Dev

How to `find` all files and folders with 0** permissions?

分類Dev

0と4-9(4、5、6、7、8、9)を含む文字列の正規表現パターン

分類Dev

部分名(sets / 3/0)は有効なRuby識別子ではありません

Related 関連記事

  1. 1

    Cの{0,1,2,3,4,5,6,7,8,9}以外の条件

  2. 2

    種類(0)から(0,1,2,3,4,5,6,7,8,9)のすべてのセットを生成するクリーンなアルゴリズム

  3. 3

    Algorithm to generate every combination of n elements split into k sets

  4. 4

    配列の要素をa [0]、a [1] + a [2]、a [3] + a [4] + a [5]、a [6] + a [7] + a [8]として追加します+ a [9] ...など

  5. 5

    What is a good algorithm to check whether or not a number exist in multiple sets without searching them all?

  6. 6

    generate 0 byte pdf with droidtext

  7. 7

    which kind of array can give to paste0

  8. 8

    In GLSL integers are all 0

  9. 9

    Finding elements that appear in all sets in a list of sets

  10. 10

    How to generate {0} json string by PHP?

  11. 11

    Generate a matrix without 0 values in a random way

  12. 12

    Algorithm to find if two sets of sets of numbers are isomorphic or not (under permutation)

  13. 13

    Generate all combinations of 2 lists (game playing)

  14. 14

    Cannot find symbol on clean generate release

  15. 15

    Best algorithm for converting real number between 0 and 1 to index

  16. 16

    Reducing time complexity of Knapsack 0~1 while using DP algorithm

  17. 17

    1で始まり0で終わるintループを設計するにはどうすればよいですか(1,2,3,4,5,6,7,8,9,0)

  18. 18

    `mdadm --create --assume-clean`でRAID0を拡張しますか?

  19. 19

    Generate all combinations

  20. 20

    ClassNotFoundException:com.auth0.jwt.algorithms.Algorithm

  21. 21

    SChannel、SEC_E_ALGORITHM_MISMATCH(0x80090331)

  22. 22

    Generate a random number and add 0 before it to ensure fixed number of digits

  23. 23

    アンダースコアまたはlazy.jsマップ(0,1,2,3,4)+(1,2,3,4,5)->(1,3,5,7,9)

  24. 24

    Generate all possible sequential strings from list using Python 3

  25. 25

    php function return 0 or no return at all

  26. 26

    All the rows expect 0th Row

  27. 27

    How to `find` all files and folders with 0** permissions?

  28. 28

    0と4-9(4、5、6、7、8、9)を含む文字列の正規表現パターン

  29. 29

    部分名(sets / 3/0)は有効なRuby識別子ではありません

ホットタグ

アーカイブ