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}"); } }
private static void Main(string[] args) { // Combinatorics.GenerateCombinationsWithRepetition(new[] { 1, 2, 3, 4, 5 }, 3); // Console.WriteLine(Recursion.Fibonacci(1000)); // EightQueensProblem.Solve(); // LabyrinthPaths.Solve(); // Combinatorics.SwapPermutations(new[] { 3, 3, 2, 1, 3, 3 }); // TowerOfHanoi.Solve(3); // ConnectedAreas.Solve(new []{new []{' ', ' ', ' ', '*', ' ', ' ', ' '}}); // Words.Solve("nopqrstuvw"); Needles.Solve(new int[] { 3, 5, 11, 0, 0, 0, 12, 12, 0, 0, 0, 12, 12, 70, 71, 0, 90, 123, 140, 150, 166, 190, 0 }, new int[] { 5, 13, 90, 1, 70, 75, 7, 188, 12 }); //int[] arr = MergeSort<int>.Sort(new[] { 3, 2, 1 }); //Console.WriteLine(string.Join(" ", arr)); // Console.WriteLine(BinarySearch<int>.Find(new[] { 4, 7, 1, 2, 3, -3 }, -3)); }
/// <summary> /// If you need to find the same set of needles in many haystacks, it's more efficient /// to compile the needles first using this method. /// </summary> /// <param name="needles">The needles to search for</param> /// <returns>A compiled needle-searcher, able to search future haystacks</returns> public static Needles Compile(IList <string> needles) { if (needles == null) { throw new ArgumentNullException(nameof(needles)); } var n = new Needles(); n.needles = needles; for (int ni = 0; ni < needles.Count; ni++) { var needle = needles[ni]; if (string.IsNullOrEmpty(needle)) { throw new ArgumentNullException(nameof(needles)); } if (needle.Length == 1) { n.match1 = Tuple.Create(ni, needle[0]); } else if (needle.Length == 2) { n.match2 = Tuple.Create(ni, needle[0], needle[1]); } else if (needle.Length == 3) { n.match3 = Tuple.Create(ni, needle[0], needle[1], needle[2]); } else { long three = needle[0] | ((long)needle[1] << 16) | ((long)needle[2] << 32); if (!n.matchN.ContainsKey(three)) { n.matchN[three] = new List <int>(); } n.matchN[three].Add(ni); } } return(n); }
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 InitializeGrid() { //TODO: Random rand = new Random(DateTime.Now.Millisecond + DateTime.Now.Second * 1337); //Determine our needle cordinates before hand. List <Coordinate> needleCordinates = new List <Coordinate>(); List <int> randomValues = new List <int>(); for (int i = 0; i < settings.numOfNeedles; i++) { Coordinate buffer = new Coordinate(rand.Next(1, 6), rand.Next(1, 9)); while (needleCordinates.Find(x => x.CompareTo(buffer) == 1) != null) { buffer = new Coordinate(rand.Next(1, 6), rand.Next(1, 9)); } needleCordinates.Add(buffer); if (settings.allowRepeats) { randomValues.Add(rand.Next(1, 10)); } } if (!settings.allowRepeats) { for (int i = 1; i < 10; i++) { randomValues.Add(i); } randomValues.Shuffle(); randomValues = randomValues.GetRange(0, settings.numOfNeedles); } for (int row = 1; row < 6; row++) { for (int col = 1; col < 9; col++) { //Initialize a new Cell object with the random value and the current row,col Cell cell = new Cell(randomValues.Count > 0 ? randomValues.Last() : 0); cell.coord = new Coordinate(row, col); if (needleCordinates.Find(x => x.CompareTo(cell.coord) == 1) != null) { cell.cellType = Cell.CellType.NEEDLE; if (randomValues.Count > 0) { randomValues.RemoveAt(randomValues.Count - 1); } } else { cell.cellType = Cell.CellType.HAYSTACK; } //Add the cell to its respected list. if (cell.cellType == Cell.CellType.HAYSTACK) { if (settings.difficulty == GameSettings.Difficulty.EASY || settings.difficulty == GameSettings.Difficulty.NORMAL) { continue; } Haystack.Add(cell); } else { Needles.Add(cell); } cell.num.Click += CellBtn_Click; cell.num.Tag = cell; //Add the Cell to the GameGrid UI. Grid.SetRow(cell, row - 1); Grid.SetColumn(cell, col - 1); this.grid.Children.Add(cell); } } //Sort the Needles Needles.Sort(delegate(Cell x, Cell y) { if (x == null && y == null) { return(0); } else if (x.value == y.value) { return(0); } else if (x == null) { return(-1); } else if (y == null) { return(1); } else { int retVal = x.value.CompareTo(y.value); if (retVal == 0) { return(x.coord.CompareTo(y.coord)); } else { return(retVal); } } }); }
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; } }