// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections.Generic; namespace E { public class Program { public static List Find1(List lst, int nmatch) { List res = new List(); for(int i = 0; i < lst.Count - nmatch + 1; i++) { bool good = true; for(int j = 1; j < nmatch; j++) { if(lst[i+j] != lst[i+j-1] + 1) { good = false; } } if(good) { res.Add(lst[i]); } } return res; } private static bool Good(List lst, int ix, int nmatch) { if(nmatch <= 1) { return true; } else { return (lst[ix]+1 == lst[ix+1]) && Good(lst, ix+1, nmatch-1); } } public static List Find2(List lst, int nmatch) { List res = new List(); for(int i = 0; i < lst.Count - nmatch + 1; i++) { if(Good(lst, i, nmatch)) { res.Add(lst[i]); } } return res; } public static void Test(List lst, int nmatch) { foreach(int v in Find1(lst, nmatch)) { Console.Write(" " + v); } Console.WriteLine(); foreach(int v in Find2(lst, nmatch)) { Console.Write(" " + v); } Console.WriteLine(); } public static void Main(string[] args) { Test(new List() {1, 2, 6, 7, 8, 10}, 2); Test(new List() {4, 5, 6, 7, 8, 10, 11, 12, 19, 20}, 3); } } }