这个序列生成问题的名称是什么?有什么意见吗?

发布于 2024-08-24 10:38:40 字数 763 浏览 5 评论 0原文

我需要迭代由数字数组定义的有序序列 ai, i = 1 ..n,其中n是每个序列元素的长度,每个ai< /sub> 指定输出序列中位置 i 处可能值的最大数量。

示例:

  • a = {10,10,10}
    序列:000001002、...999000< /code> 到 999)

  • a = (2,3,2}
    序列:000001010011020021、100101110111120 , 121

(注意:我不仅需要打印序列,还需要迭代其元素,其中每个元素都是一个数组,例如 {1,2,1}。

)我实现了这个,想问一下大家有什么意见吗?也许这是一个已知问题(名称?),并且已经有可用的代码,或者它简化为其他一些众所周知的问题?它确实与排列问题有相似之处。

I need to iterate over an ordered sequence that is defined by an array of numbers ai, i = 1..n, where n is the length of each sequence element, and each ai specifies the max number of possible values at position i in the output sequence.

Example:

  • a = {10,10,10}
    Sequence: 000, 001, 002, ... 999 (the decimal numbers from 000 to 999)

  • a = (2,3,2}
    Sequence: 000, 001, 010, 011, 020, 021, 100, 101, 110, 111, 120, 121

(Note: I don't just need to print the sequence, but I need to iterate over its elements, where each element is an array, e.g. {1,2,1}.)

Before I implement this, I wanted to ask if anyone has any comments? Maybe this is a known problem (name?), and there is already code available, or it reduces to some other well-known problem? It does have similarities to the permutation problem.

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

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

发布评论

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

评论(3

巴黎夜雨 2024-08-31 10:38:40

这是一个笛卡尔积

在Python中, itertools.product 产生这样的元组列表。

>>> import itertools
>>> list(itertools.product(*map(range, [2, 3, 2])))
[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (0, 2, 0), (0, 2, 1),
 (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1), (1, 2, 0), (1, 2, 1)]

在其他语言中,使用嵌套循环创建笛卡尔积很容易:

for (int i = 0; i < 2; i++)
    for (int j = 0; j < 3; j++)
        for (int k = 0; k < 2; k++)
            cout << i << j << k << endl;

This is a Cartesian product.

In Python, itertools.product produces such lists of tuples.

>>> import itertools
>>> list(itertools.product(*map(range, [2, 3, 2])))
[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (0, 2, 0), (0, 2, 1),
 (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1), (1, 2, 0), (1, 2, 1)]

In other languages, it's easy to create a Cartesian product using nested loops:

for (int i = 0; i < 2; i++)
    for (int j = 0; j < 3; j++)
        for (int k = 0; k < 2; k++)
            cout << i << j << k << endl;
遗忘曾经 2024-08-31 10:38:40

序列的元素按字典顺序列出。另请参阅(另一个不同但相关的问题)。

The elements of the sequences are listed in the lexographic order. Also see this (a different but related problem).

带上头具痛哭 2024-08-31 10:38:40

如果您使用 C# (linq) 并且笛卡尔积中的因子数是静态的,那么它也可以非常优雅地表达

    static void Main(string[] args)
    {
        IEnumerable<int> firstFactor = Enumerable.Range(0, 2);
        IEnumerable<int> secondFactor = Enumerable.Range(0, 3);

        var cartesianProd =
             from first in firstFactor
             from second in secondFactor
             select new { First = first, Second = second };

        foreach (var tuple in cartesianProd)
        {
            Console.WriteLine("({0}, {1})", tuple.First, tuple.Second);
        }
        Console.ReadLine();
    } 

If you are using C# (linq) and the number of factors in the cartesian product is static, then it can also be quite elgantly expressed

    static void Main(string[] args)
    {
        IEnumerable<int> firstFactor = Enumerable.Range(0, 2);
        IEnumerable<int> secondFactor = Enumerable.Range(0, 3);

        var cartesianProd =
             from first in firstFactor
             from second in secondFactor
             select new { First = first, Second = second };

        foreach (var tuple in cartesianProd)
        {
            Console.WriteLine("({0}, {1})", tuple.First, tuple.Second);
        }
        Console.ReadLine();
    } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文