Click here to Skip to main content
15,908,634 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
using System;
using System.Collections.Generic;
using System.Linq;
 
namespace ConsoleApplication31
{
  class Program
  {
    static void Main(string[] args)
    {
      int[] test = new int[] { 1, 2, 4, 6 };
      int threshold = 10;
      foreach (var item in CombinationIndexes(test).Where(ii => ii.Sum(ix => test[ix]) >= threshold))
      {
        Console.WriteLine(string.Join(",", item.Select(i => i.ToString())));
      }
      Console.ReadLine();
    }
 
    const int BitsInUlong = 8 * sizeof(ulong);
    static IEnumerable<IEnumerable<int>> CombinationIndexes<T>(IList<T> collection)
    {
      if (collection.Count > BitsInUlong)
        throw new ArgumentOutOfRangeException("collection", "collection is too large");
      ulong count = (~(ulong)0) >> (BitsInUlong - collection.Count);
      for (ulong bits = 0; bits <= count; bits++)
      {
        yield return BitNumbers(bits);
      }
    }
 
    static IEnumerable<int> BitNumbers(ulong bits)
    {
      if (bits == 0)
        yield break;
      ulong mask = 1;
      for (int i = 0; i < BitsInUlong; ++i)
      {
        if ((bits & mask) != 0)
        {
          yield return i;
        }
        mask <<= 1;
      }
    }
  }
}


Hi
Matt T Heffron
when I convert your code to vb.net create error in line :
yield Return BitNumbers(bits)

the error is : yield is not declared
Posted
Comments
Patrice T 29-Aug-15 7:07am    
This don't look like VB code !
Do you know this language ? what is supposed to be "yield" ?

1 solution

Don't post this under Quick Answers - if you got the code from an article, then there is a "Add a Comment or Question" button at the bottom of that article, which causes an email to be sent to the author. They are then alerted that you wish to speak to them.
Posting this here relies on them "dropping by" and realising it is for them.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900