Click here to Skip to main content
15,895,084 members
Articles / Programming Languages / C#

A C# Combinations Iterator

Rate me:
Please Sign up or sign in to vote.
4.17/5 (5 votes)
16 Nov 2009CPOL2 min read 38.5K   345   10  
An iterator over all combinations of (m) elements from a sequence of (n) elements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Combinations
{
    class Program
    {
        static void Main(string[] args)
        {
            string line, s;
            int count;
            do
            {
                Console.WriteLine("Select Demo:");
                Console.WriteLine("1 - Combinations over integers.");
                Console.WriteLine("2 - Combinations over a string.");
                Console.WriteLine("3 - Combinations and permutations.");
                Console.WriteLine("q - Quit.");
                line = Console.ReadLine();
                if (line == "q") break;
                switch (line)
                {
                    case "1":
                        CombinationsOverIntegers();
                        break;
                    case "2":
                        if (!InputParameters(out s, out count))
                            continue;
                        CombinationsOverString(s, count);
                        break;
                    case "3":
                        if (!InputParameters(out s, out count))
                            continue;
                        CombinationsPermutations(s, count);
                        break;
                }
            } while (line != "q");
        }

        private static bool InputParameters(out string s, out int count)
        {
            Console.WriteLine("Enter string:");
            s = Console.ReadLine();
            Console.WriteLine("How may characters to choose?");
            if (!int.TryParse(Console.ReadLine(), out count)) return false;
            return true;
        }

        private static void CombinationsPermutations(string s, int count)
        {
            foreach (var comb in Iterator.Combinations(s.ToCharArray().ToList(), count))
            {
                foreach (var perm in Iterator.Permutations(comb, count))
                {
                    string r = new string(perm.Take(count).ToArray());
                    Console.Write("{0,-8}", r);
                }
            }
            Console.WriteLine();
        }

        private static void CombinationsOverString(string s, int count)
        {
            foreach (var comb in Iterator.Combinations(s.ToCharArray().ToList(), count))
            {
                string r = new string(comb.Take(count).ToArray());
                Console.Write("{0,-8}", r);
            }
            Console.WriteLine();
        }

        private static void CombinationsOverIntegers()
        {
            List<int> l = new List<int>() { 1, 2, 3, 4, 5, 6 };
            int count = 4;
            foreach (var comb in Iterator.Combinations(l, count))
            {
                string s = comb.Take(count).Aggregate<int, string>("", (x, y) => x + y);
                Console.Write("{0,-8}", s);
            }
            Console.WriteLine();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer (Senior)
Israel Israel
Software developer since 1984, currently specializing in C# and .NET.

Comments and Discussions