/// <summary> /// Returns the number of unique paths along the lattice lines of a 2d grid, /// being able to only move down or to the right at each point. /// </summary> /// <param name="width">Width of the 2d grid.</param> /// <param name="height">Height of the 2d grid.</param> /// <returns>The number of paths through the lattice points of the grid.</returns> public static long NumLatticePaths2DDownAndRight(int width, int height) { var toCheck = new Dictionary<Grid2D, long>(); toCheck.Add(new Grid2D {Width = width, Height = height}, 1); var pathLength = 0L; while (toCheck.Count != 0) { var gridInfo = toCheck.First(); var grid = gridInfo.Key; var count = gridInfo.Value; toCheck.Remove(grid); if (grid.Width == 1) { pathLength += count * (grid.Height + 1); } else if (grid.Height == 1) { pathLength += count * (grid.Width + 1); } else { var subGrid1 = new Grid2D {Width = grid.Width, Height = grid.Height - 1}; var subGrid2 = new Grid2D {Width = grid.Width - 1, Height = grid.Height}; toCheck.AddOrIncrement(subGrid1, count, count); toCheck.AddOrIncrement(subGrid2, count, count); } } return pathLength; }
public void DictionaryExt_Test005_AddTwoDuplicateDifferenctCase_CaseInsensitiveDictionary() { Dictionary<string, int> dictionary = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase); dictionary.AddOrIncrement(__keyYellow); dictionary.AddOrIncrement(__keyYellow.ToLower()); Assert.AreEqual<int>(2, dictionary[__keyYellow]); }
public void DictionaryExt_Test003_AddTwoDuplicate() { Dictionary<string, int> dictionary = new Dictionary<string, int>(); dictionary.AddOrIncrement(__keyYellow); dictionary.AddOrIncrement(__keyYellow); Assert.AreEqual<int>(2, dictionary[__keyYellow]); }
public void DictionaryExt_Test004_AddTwoDuplicateDifferenctCase() { Dictionary<string, int> dictionary = new Dictionary<string, int>(); dictionary.AddOrIncrement(__keyYellow); dictionary.AddOrIncrement(__keyYellow.ToLower()); Assert.AreEqual<int>(1, dictionary[__keyYellow]); Assert.AreEqual<int>(1, dictionary[__keyYellow.ToLower()]); }
public void DictionaryExt_Test002_AddTwoUnique() { Dictionary<string, int> dictionary = new Dictionary<string, int>(); dictionary.AddOrIncrement(__keyYellow); dictionary.AddOrIncrement(__keyGreen); Assert.AreEqual<int>(1, dictionary[__keyYellow]); Assert.AreEqual<int>(1, dictionary[__keyGreen]); }
public void ProcessText() { Dictionary<string, int> sorted = new Dictionary<string, int>(); string[] words = this.TextToSplit.RemovePunctuation().Split(new char[] { ' ' }); foreach (var word in words) if (word.Trim() != string.Empty) sorted.AddOrIncrement(word.Trim()); this.Breakdown = sorted; }
public static void Test_WordCount(string[] args) { Console.WriteLine("Please enter data to split:\n"); var data = Console.ReadLine(); Dictionary<string, int> sorted = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase); string[] words = data.RemovePunctuation().Split(new char[] { ' ' }); foreach (var word in words) if (word.Trim() != string.Empty) sorted.AddOrIncrement(word.Trim()); foreach (var entry in sorted) Console.WriteLine("{0} - {1}", entry.Key, entry.Value); }