13,250,043 members (65,071 online)

#### Stats

443.3K views
206 bookmarked
Posted 13 May 2008

# Permutations, Combinations, and Variations using C# Generics

, 23 May 2008
Discusses the six major types of combinatorial collections, with examples and formulas for counting. Expands with a C# Generics-based set of classes for enumerating each meta-collection.
 CombinatoricsSample TextSumProblemSample bin Debug Release Combinatorics Facet.ico obj Debug Refactor TempPE Release TempPE Properties TextSumProblemSample.csproj.user TextSumProblemSample.exe ```using System; using System.Collections.Generic; using System.Text; using Facet.Combinatorics; namespace TextSumProblemSample { /// /// Simple wrapper around a text sum problem, e.g. FOUR + FIVE = NINE. /// public class TextSumProblem { /// /// Event indicating that another variation has been tried on the solution, includes total number that will be tried. /// public event ProgressDelegate OnProgress; public delegate void ProgressDelegate(object sender, int current, int total); /// /// Event indicating that a solution to the problem has been found, includes a dictionary indicating the solution characters and values. /// public event AnswerDelegate OnSolution; public delegate void AnswerDelegate(object sender, Dictionary solution); /// /// Create and solve a problem of numeric substitution, e.g. FOUR + FIVE = NINE. /// /// /// First, the problem is turned into FOUR + FIVE - NINE = 0 with coeffiicents /// on each letter. I.e.: /// (1000F + 100O + 10U + R) + (1000F + 100I + 10V + E) - (1000N + 100I + 10N + E) = 0 /// => 2000F + 100O + 10U + R + 0I + 10V + 0E + 1010N = 0 /// Each variation is then generated and the simplified problem is checked for satisifiability. /// Events are raised as appropriate. /// public void Solve(string op1, string op2, string sum) { int[] ints = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; characters.Clear(); cofs.Clear(); AddCofs(op1, 1); AddCofs(op2, 1); AddCofs(sum, -1); Variations variations = new Variations(ints, cofs.Count); int count = (int)variations.Count; int current = 0; foreach(IList variation in variations) { if(OnProgress != null) { OnProgress(this, current, count); } if(Satisfies(variation) == true) { if(OnSolution != null) { Dictionary solution = new Dictionary(); for(int i = 0; i < variation.Count; ++i) { solution.Add(characters[i], variation[i]); } OnSolution(this, solution); } } ++current; } } /// /// Determine if the given variation satisisfies the problem. /// private bool Satisfies(IList variation) { int result = 0; for(int i = 0; i < variation.Count; ++i) { char c = characters[i]; int cof = cofs[i]; int var = variation[i]; result += cof * var; } return result == 0; } private void AddCofs(string s, int factor) { for(int i = 0; i < s.Length; ++i) { char c = s[i]; int digitFactor = factor * (int)Math.Pow(10, s.Length - i); if(characters.Contains(c) == true) { cofs[characters.IndexOf(c)] += digitFactor; } else { characters.Add(c); cofs.Add(digitFactor); } } } private List characters = new List(); private List cofs = new List(); } } ```

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.

## Share

 Team Leader Zuuse Pty Ltd Australia
I have been a professional software developer for twenty years, starting with C++ and migrated to C#. While I have transitioned into full time management, writing code is still my passion. As I don't write code for work very often, I have had the opportunity to apply my programming skills as a hobby where I have recently authored two Windows 8 store apps. First, an Asteroids tribute game, 'Roid Rage and most recently Shared Whiteboard (which does what it says).

I make a habit of contributing production code to every project I run. Most notably, I have recently run teams to build The Navigator for The Advertiser newspaper and Street Lights Out for SA Power Networks.

## You may also be interested in...

 Pro Pro