Generate all combinations for a list of strings

前端 未结 4 904
情歌与酒
情歌与酒 2020-12-05 08:14

I want to generate a list of all possible combinations of a list of strings (it\'s actually a list of objects, but for simplicity we\'ll use strings). I need this list so th

相关标签:
4条回答
  • 2020-12-05 08:57

    You can make in manually, using the fact that n-bit binary number naturally corresponds to a subset of n-element set.

    private IEnumerable<int> constructSetFromBits(int i)
    {
        for (int n = 0; i != 0; i /= 2, n++)
        {
            if ((i & 1) != 0)
                yield return n;
        }
    }
    
    List<string> allValues = new List<string>()
            { "A1", "A2", "A3", "B1", "B2", "C1" };
    
    private IEnumerable<List<string>> produceEnumeration()
    {
        for (int i = 0; i < (1 << allValues.Count); i++)
        {
            yield return
                constructSetFromBits(i).Select(n => allValues[n]).ToList();
        }
    }
    
    public List<List<string>> produceList()
    {
        return produceEnumeration().ToList();
    }
    
    0 讨论(0)
  • 2020-12-05 09:00

    If you want all variations, have a look at this project to see how it's implemented.

    http://www.codeproject.com/Articles/26050/Permutations-Combinations-and-Variations-using-C-G

    But you can use it since it's open source under CPOL.

    For example:

    var allValues = new List<string>() { "A1", "A2", "A3", "B1", "B2", "C1" };
    List<String> result = new List<String>();
    var indices = Enumerable.Range(1, allValues.Count);
    foreach (int lowerIndex in indices)
    {
        var partVariations = new Facet.Combinatorics.Variations<String>(allValues, lowerIndex);
        result.AddRange(partVariations.Select(p => String.Join(" ", p)));
    }
    
    var length = result.Count;  // 1956
    
    0 讨论(0)
  • 2020-12-05 09:00

    Simillar kind of task is achived in the below post:

    Listing all permutations of a string/integer

    Hope this help.

    0 讨论(0)
  • 2020-12-05 09:01

    One more recursive solution. From AllCombinations in below code, you will get all possible combinations. Logic:

    1. Starting with one element.
    2. Generate all possible combinations with it.
    3. Move to next element and begin with step 2 again.

    Code:

    public class Combination<T>
    {
        private IEnumerable<T> list { get; set; }
        private int length;
        private List<IEnumerable<T>> _allCombination;
    
        public Combination(IEnumerable<T> _list)
        {
            list = _list;
            length = _list.Count();
    
            _allCombination = new List<IEnumerable<T>>();
        }
    
        public IEnumerable<IEnumerable<T>> AllCombinations
        {
            get
            {
                GenerateCombination(default(int), Enumerable.Empty<T>());
    
                return _allCombination;
            }
        }
    
        private void GenerateCombination(int position, IEnumerable<T> previousCombination)
        {
            for (int i = position; i < length; i++)
            {
                var currentCombination = new List<T>();
                currentCombination.AddRange(previousCombination);
                currentCombination.Add(list.ElementAt(i));
    
                _allCombination.Add(currentCombination);
    
                GenerateCombination(i + 1, currentCombination);
    
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题