static void Test(IList <string> needles, string haystack, string expected) { var results = Needles.Find(needles, haystack); var spans = results.Select(r => r.Needle >= 0 ? needles[r.Needle] : haystack.Substring(r.Start, -r.Needle)); var actual = string.Join(",", spans); if (actual != expected) { Console.WriteLine($"FAILED - actual={actual}; expected={expected}"); } }
static void Main() { // Example of how to use it: var needles = new[] { "aystb", "sta" }; var haystack = "haystack"; foreach (var result in Needles.Find(needles, haystack)) { if (result.Needle >= 0) { Console.WriteLine("match: " + needles[result.Needle]); } else { Console.WriteLine("skip: " + haystack.Substring(result.Start, -result.Needle)); } } // Regression tests: Test(new[] { "aystb", "sta" }, "haystack", "hay,sta,ck"); Test(new[] { "aac" }, "123aaac456", "123a,aac,456"); Test(new[] { "abc", "c" }, "1abc2", "1,abc,2"); Test(new[] { "abc", "a" }, "1abc2", "1,a,bc2"); }
private void CellBtn_Click(object sender, RoutedEventArgs e) { Cell cell = (Cell)((Button)sender).Tag; //SelectedNeedles.Reverse(); //Search needles list to see if the selected cell a needle, if it is... if (Needles.Find(x => x.coord.CompareTo(cell.coord) == 1) != null) { if (SelectedNeedles.Count > 0) { Cell lastSelected = SelectedNeedles.Last(); int correctIndex = Needles.FindLastIndex(x => x.value.CompareTo(lastSelected.value) == 0); //If repeats are valid then the next value CAN be the same as the last. if (settings.allowRepeats && lastSelected.value.CompareTo(cell.value) == 0) { AddSelectedCell(cell); } //If the last value is less than the next else if (lastSelected.value.CompareTo(cell.value) == -1) { //if the selected cell is greater than the previous but is higher than the correct value then we failed if ((!settings.allowRepeats && cell.value.CompareTo(Needles[correctIndex + 1].value) == 1) || //Or if repeats are allowed, if the selected cell is higher than the correct cell value or if the selected... //..value is correct but not all of the previous value has been selected then we have failed (settings.allowRepeats && (cell.value.CompareTo(Needles[correctIndex + 1].value) == 1 || (cell.value.CompareTo(Needles[correctIndex + 1].value) == 0 && SelectedNeedles.FindAll(x => x.value == lastSelected.value).Count < Needles.FindAll(x => x.value == lastSelected.value).Count)))) { MessageBox.Show("Failed"); Window.GetWindow(this).Close(); return; } AddSelectedCell(cell); } else { MessageBox.Show("FAILED"); Window.GetWindow(this).Close(); return; } } else { //If the selected cell is not the first index of the Needles list (sorted by ascending value order and by coordinates) then its not in numerical order. if ((settings.allowRepeats && Needles.FindIndex(x => x.value == cell.value) != 0) || (!settings.allowRepeats && Needles.FindIndex(x => x.coord.CompareTo(cell.coord) == 1) != 0)) { if (Needles.FindIndex(x => x.value == cell.value) != 0) { MessageBox.Show("Failed"); Window.GetWindow(this).Close(); return; } } //Handle some of the difficult settings here. If you're playing above easy, the first selection will hide the rest. if (settings.difficulty > GameSettings.Difficulty.EASY) { foreach (Cell c in Needles) { c.cellType = Cell.CellType.HAYSTACK; } } AddSelectedCell(cell); } cell.num.IsEnabled = false; if (SelectedNeedles.Count == Needles.Count) { MessageBox.Show("Winner winner chicken dinner!"); Window.GetWindow(this).Close(); return; } } else { MessageBox.Show("FAiled"); Window.GetWindow(this).Close(); return; } }