public void Generator_ThreadSafe_TaskReaders() { var generator = new CombinationGenerator(StandardKeypad); var threadCount = 1000; var batchSize = 100; var tasks = Enumerable.Range(1, threadCount) .Select(_ => Task.Factory.StartNew(() => generator.Generate(TEST_PHONE_NUMBER).Take(batchSize).ToList()) ) .ToArray(); Task.WaitAll(tasks); var sequentialGenerator = new CombinationGenerator(StandardKeypad); var sequence = sequentialGenerator.Generate(TEST_PHONE_NUMBER).Take(threadCount * batchSize).ToList(); for (var i = 0; i < batchSize; i++) { foreach (var task in tasks) { Assert.AreEqual(sequence[i], task.Result[i]); } } }
public void Generator_NoCombinationPossible_ReturnsNull() { var generator = new CombinationGenerator(StandardKeypad); var test = generator.Generate("0000000000").FirstOrDefault(); Assert.IsNull(test); }
public void Generator_InvalidPhoneNumber_ReturnsNull() { var generator = new CombinationGenerator(StandardKeypad); var test = generator.Generate("(ABC)").FirstOrDefault(); Assert.IsNull(test); }
public void GenerateRightCombinationsForNEqualsThree() { CombinationGenerator generator = new CombinationGenerator(); var result = generator.Generate(25, 3); Assert.Equal(25, result.Count); Assert.Equal(new int[] { }, result[0]); Assert.Equal(new[] { 1 }, result[1]); Assert.Equal(new[] { 2 }, result[2]); Assert.Equal(new[] { 3 }, result[3]); Assert.Equal(new[] { 1, 1 }, result[4]); Assert.Equal(new[] { 1, 2 }, result[5]); Assert.Equal(new[] { 1, 3 }, result[6]); Assert.Equal(new[] { 2, 2 }, result[7]); Assert.Equal(new[] { 2, 3 }, result[8]); Assert.Equal(new[] { 3, 3 }, result[9]); Assert.Equal(new[] { 1, 1, 1 }, result[10]); Assert.Equal(new[] { 1, 1, 2 }, result[11]); Assert.Equal(new[] { 1, 1, 3 }, result[12]); Assert.Equal(new[] { 1, 2, 2 }, result[13]); Assert.Equal(new[] { 1, 2, 3 }, result[14]); Assert.Equal(new[] { 1, 3, 3 }, result[15]); Assert.Equal(new[] { 2, 2, 2 }, result[16]); Assert.Equal(new[] { 2, 2, 3 }, result[17]); Assert.Equal(new[] { 2, 3, 3 }, result[18]); Assert.Equal(new[] { 3, 3, 3 }, result[19]); }
public IList <string> LetterCasePermutation(string S) { List <string> list = new List <string>(); CombinationGenerator.Generate( S.Length, ptr => IsLetter(S[ptr]) ? 2 : 1, (ptr, index) => index, flags => { StringBuilder sb = new StringBuilder(S.Length); for (int i = 0; i < flags.Length; i++) { char c = S[i]; if (IsLower(c)) { sb.Append(flags[i] == 0 ? c : ToUpper(c)); } else if (IsUpper(c)) { sb.Append(flags[i] == 0 ? ToLower(c) : c); } else { sb.Append(c); } } list.Add(sb.ToString()); } ); return(list); }
public void GenerateReturnsRightNumberOfCombinations() { CombinationGenerator generator = new CombinationGenerator(); var result = generator.Generate(112, 2); Assert.Equal(112, result.Count); }
public void Generator_IsIEnumerable() { var generator = new CombinationGenerator(StandardKeypad); var combinations = generator.Generate(TEST_PHONE_NUMBER).Take(10).ToList(); Assert.AreEqual(10, combinations.Count()); combinations.ForEach(Console.WriteLine); }
public void Generator_SecondToLastSymbolChanges_OK() { var phoneNumber = "3015559860"; var generator = new CombinationGenerator(StandardKeypad); var nextNumbers = generator.Generate(phoneNumber).Take(2).ToArray(); Assert.AreEqual(nextNumbers[0], "30155598M0"); Assert.AreEqual(nextNumbers[1], "30155598N0"); }
public void Generator_LastSymbolChanges_OK() { var phoneNumber = "3015559876"; var generator = new CombinationGenerator(StandardKeypad); var combinations = generator.Generate(phoneNumber).Take(3).ToArray(); Assert.AreEqual(combinations[0], "301555987M"); Assert.AreEqual(combinations[1], "301555987N"); Assert.AreEqual(combinations[2], "301555987O"); }
public void Generator_LastTwoSymbolsChange_OK() { var phoneNumber = "301555987O"; var generator = new CombinationGenerator(StandardKeypad); var combinations = generator.Generate(phoneNumber).Take(5).ToArray(); Assert.AreEqual(combinations[0], "30155598P6"); Assert.AreEqual(combinations[1], "30155598PM"); Assert.AreEqual(combinations[2], "30155598PN"); Assert.AreEqual(combinations[3], "30155598PO"); }
public void Generator_AllCombinations_Verified() { var phoneNumber = "2"; var generator = new CombinationGenerator(StandardKeypad); var nextNumbers = generator.Generate(phoneNumber).Take(1000).ToArray(); Assert.AreEqual(3, nextNumbers.Length); Assert.AreEqual("A", nextNumbers[0]); Assert.AreEqual("B", nextNumbers[1]); Assert.AreEqual("C", nextNumbers[2]); }
public string NextClosestTime(string time) { int hh = Int32.Parse(time.Substring(0, 1)); int hl = Int32.Parse(time.Substring(1, 1)); int mh = Int32.Parse(time.Substring(3, 1)); int ml = Int32.Parse(time.Substring(4, 1)); int i_time = hh * 1000 + hl * 100 + mh * 10 + ml; HashSet <int> digits_hs = new HashSet <int>(); digits_hs.Add(hh); digits_hs.Add(hl); digits_hs.Add(mh); digits_hs.Add(ml); int[] digits = digits_hs.ToArray(); int min_time = i_time + 10000; CombinationGenerator.Generate(4, digits, a => { if (a[0] > 2) { return; } if (a[2] > 5) { return; } int v_h = a[0] * 10 + a[1]; int v_m = a[2] * 10 + a[3]; if (v_h <= 23 && v_m <= 59) { // Console.WriteLine("{0}{1}:{2}{3}", a[0], a[1], a[2], a[3]); int v_time = v_h * 100 + v_m; if (v_time != i_time) { if (v_time < i_time) { v_time += 10000; } min_time = Math.Min(v_time, min_time); } } }); min_time %= 10000; return(String.Format("{0:00}:{1:00}", min_time / 100, min_time % 100)); }
public IList <string> AddOperators(string num, int target) { if (num.Length == 0) { return(Array.Empty <string>()); } else if (num.Length == 1) { int n = Int32.Parse(num); if (n == target) { return(new[] { num }); } else { return(Array.Empty <string>()); } } Func <int, int, long?>[] op_func = { (a, b) => a + b, (a, b) => a - b, (a, b) => a * b, (a, b) => a > 0 ? (int?)a * 10 + b : null }; string[] op_char = { "+", "-", "*", "" }; int[] nums = new int[num.Length]; for (int i = 0; i < num.Length; i++) { nums[i] = Int32.Parse(num.Substring(i, 1)); } List <string> list = new List <string>(); CombinationGenerator.Generate(nums.Length - 1, new[] { 10, 11, 22, 33 }, ops => { Stack <int> op_stack = new Stack <int>(); Queue <int> n_output = new Queue <int>(); for (int i = 0; i < ops.Length; i++) { n_output.Enqueue(nums[i]); while (op_stack.TryPeek(out int op) && op / 10 >= ops[i] / 10) { n_output.Enqueue(op_stack.Pop()); } op_stack.Push(ops[i]); }
public string[] Expand(string s) { IList <IList <char> > map = new List <IList <char> >(); List <char> chars = null; foreach (char c in s) { if (c == '{') { chars = new List <char>(); } else if (c == '}') { map.Add(chars.ToArray()); chars = null; } else if (c == ',') { // NOP } else { if (chars != null) { chars.Add(c); } else { map.Add(new[] { c }); } } } List <string> list = new List <string>(); CombinationGenerator.Generate( map.Count, map, items => list.Add(new string(items)) ); list.Sort(); return(list.ToArray()); }
public void Generator_ThreadSafe_ParallelReaders() { var generator = new CombinationGenerator(StandardKeypad); var threadIdByNumber = new ConcurrentDictionary <string, int>(); generator .Generate(TEST_PHONE_NUMBER) .Take(100) .AsParallel() .ForAll(n => threadIdByNumber.TryAdd(n, Thread.CurrentThread.ManagedThreadId)); Assert.AreEqual(100, threadIdByNumber.Count()); foreach (var kvp in threadIdByNumber.OrderBy(x => x.Key)) { Console.WriteLine($"{kvp.Key} - {kvp.Value}"); } }
public void GenerateRightCombinationsForNEqualsTwo() { CombinationGenerator generator = new CombinationGenerator(); var result = generator.Generate(12, 2); Assert.Equal(12, result.Count); Assert.Equal(new int[] { }, result[0]); Assert.Equal(new[] { 1 }, result[1]); Assert.Equal(new[] { 2 }, result[2]); Assert.Equal(new[] { 1, 1 }, result[3]); Assert.Equal(new[] { 1, 2 }, result[4]); Assert.Equal(new[] { 2, 2 }, result[5]); Assert.Equal(new[] { 1, 1, 1 }, result[6]); Assert.Equal(new[] { 1, 1, 2 }, result[7]); Assert.Equal(new[] { 1, 2, 2 }, result[8]); Assert.Equal(new[] { 2, 2, 2 }, result[9]); Assert.Equal(new[] { 1, 1, 1, 1 }, result[10]); Assert.Equal(new[] { 1, 1, 1, 2 }, result[11]); }
public IList <IList <int> > Subsets(int[] nums) { IList <IList <int> > list = new List <IList <int> >(); CombinationGenerator.Generate(nums.Length, new[] { false, true }, flags => { List <int> items = new List <int>(); for (int index = 0; index < flags.Length; index++) { if (flags[index]) { items.Add(nums[index]); } } list.Add(items); }); return(list); }
// TODO: Optimize static ICollection <Tree> FindAllSubTreesWithSum(int sum) { var results = new List <Tree>(); foreach (int root in FindAllNodes()) { var currentQueue = new Queue <Tuple <Tree, int> >(); currentQueue.Enqueue(new Tuple <Tree, int>( new Tree(true) { new KeyValuePair <int, ICollection <int> >(root, new int[0]) }, root )); while (currentQueue.Count != 0) { var current = currentQueue.Dequeue(); if (current.Item2 > sum) { continue; } if (current.Item2 == sum) { results.Add(current.Item1); } // TODO foreach (var nextNode in CombinationGenerator.Generate()) { var nextTree = current.Item1.Clone(); } } } return(results); }
public void Generator_Generate_RequiresInputhoneNumber() { var generator = new CombinationGenerator(StandardKeypad); generator.Generate(null).ToList(); }