将整数列表(IEnumerable <int>)转换为特定长度的元组列表(IEnumerable <IEnumerable <int >>)

时间:2019-12-23 10:06:23

标签: c# algorithm statistics

我尝试解决下一个练习:

输入:带有count >= 1;个正整数k

的整数列表

输出: 该整数的所有可能的元组,其长度k

例如

输入{1, 2}; k = 4

输出:

{
 {1, 1, 1, 1},
 {1, 1, 1, 2},
 {1, 1, 2, 1},
 {1, 1, 2, 2},
 {1, 2, 1, 1},
 {1, 2, 1, 2},
 {1, 2, 2, 1},
 {1, 2, 2, 2},
 {2, 1, 1, 1},
 {2, 1, 1, 2},
 {2, 1, 2, 1},
 {2, 1, 2, 2},
 {2, 2, 1, 1},
 {2, 2, 1, 2},
 {2, 2, 2, 1},
 {2, 2, 2, 2}
}

我试图创建一个包含k 输入列表副本,然后使用Combinations array

public static IEnumerable<IEnumerable<T>> Combinations<T>(
  this IEnumerable<T> elements, 
  int k)
{
    return k == 0 
        ? new[] { new T[0] } 
        : elements.SelectMany((e, i) => elements
              .Skip(i + 1)
              .Combinations(k - 1)
              .Select(c => (new[] { e }).Concat(c)));
}

但是k > 9花费的时间太长了。是否有一种算法可以在短时间内解决此问题?

1 个答案:

答案 0 :(得分:5)

让我们摆脱递归并拥有512项:

代码:

//TODO: you may want to declare it as IEnumerable<T[]> Combinations<T> 
public static IEnumerable<IEnumerable<T>> Combinations<T>(
  this IEnumerable<T> elements, int k) {

  if (null == elements)
    throw new ArgumentNullException(nameof(elements));
  else if (k < 0)
    throw new ArgumentOutOfRangeException(nameof(k));

  T[] alphabet = elements.ToArray();

  // Special cases
  if (alphabet.Length <= 0)
    yield break;
  else if (k == 0)
    yield break;

  int[] indexes = new int[k];

  do {
    yield return indexes
      .Select(i => alphabet[i])
      .ToArray();

    for (int i = indexes.Length - 1; i >= 0; --i)
      if (indexes[i] >= alphabet.Length - 1)
        indexes[i] = 0;
      else {
        indexes[i] += 1;

        break;
      }
  }
  while (!indexes.All(index => index == 0));
}

演示:

string report = string.Join(Environment.NewLine, Combinations(new int[] { 1, 2}, 9)
  .Select(line => string.Join(", ", line)));

Console.Write(report);

结果:({512条记录)

1, 1, 1, 1, 1, 1, 1, 1, 1
1, 1, 1, 1, 1, 1, 1, 1, 2
1, 1, 1, 1, 1, 1, 1, 2, 1
1, 1, 1, 1, 1, 1, 1, 2, 2
1, 1, 1, 1, 1, 1, 2, 1, 1
1, 1, 1, 1, 1, 1, 2, 1, 2
1, 1, 1, 1, 1, 1, 2, 2, 1
1, 1, 1, 1, 1, 1, 2, 2, 2
1, 1, 1, 1, 1, 2, 1, 1, 1
...
2, 2, 2, 2, 2, 2, 1, 2, 1
2, 2, 2, 2, 2, 2, 1, 2, 2
2, 2, 2, 2, 2, 2, 2, 1, 1
2, 2, 2, 2, 2, 2, 2, 1, 2
2, 2, 2, 2, 2, 2, 2, 2, 1
2, 2, 2, 2, 2, 2, 2, 2, 2

让我们生成所有2**20 == 1048576个项目(k = 20),即生成超过{em> 1百万个大小为20的数组:

  Stopwatch sw = new Stopwatch();

  sw.Start();  

  int count = Combinations(new int[] { 1, 2 }, 20).Count();

  sw.Stop();

  Console.Write($"{count.ToString()} items at {sw.ElapsedMilliseconds:f0} milliseconds");

结果:

  1048576 items at 469 milliseconds