Finding sequential Numbers?

If the input is sorted, and you really want to avoid a foreach loop, you can use.

If the input is sorted, and you really want to avoid a foreach loop, you can use: list. Select((value,index)=>new {value,index}). GroupBy(x=>x.

Value-x. Index,x=>x. Value).

Select(g=>g.AsEnumerable()) One could also write a general helper method: public static IEnumerable> SplitBetween(this IEnumerable sequence, Func predicate) { T previous=default(T); List list=new List(); int index=0; foreach(T current in sequence) { if((index>0)&&predicate(previous,current)) { yield return list.ToArray(); list.Clear(); } list. Add(current); previous=current; index++; } if(list. Count>0) yield return list.ToArray(); } And then use it with list.

SplitBetween((previous,current) => previous+1! = current).

1 And you may as well provide the code to sort the list as it's trivial. – Yuck Dec 2 at 14:13 @Yuck No that wouldn't work. I think for 1, 4, 2, 3 the expected output is (1) (4) (2,3) and not (1,2,3,4) as sorting would return.

– CodeInChaos Dec 2 at 14:14 1 Excelent, except the zeros in the return... where they come from? – Bart Dec 2 at 14:23 1 Then add a Select(group=>new {group.First(),group.Count()}). In your examples you listed the elements in each group, so I thought you were interested in them.

– CodeInChaos Dec 2 at 14:37 1 @Bart The capacity is larger than the size of these collections. If you check the count it is small enough to exclude the trailing 0s. That's just like List has an underlying array that's larger than than the size of the list due to its size doubling growth strategy.

The only reason why you see those 0s is because you peek at private data with your debugger. If enumerate the sequences you won't get them. – CodeInChaos Dec 27 at 11:30.

I don't think that this is very "elegant", but here is my suggestion, hopefully it helps you: var list = new List { 1, 3, 4, 5, 7, 12, 13, 14, 15, 20, 22, 24, 28, 29, 30 }; int offset = 0; int sequence = 0; do { int offset1 = offset; var subList = list. Skip(offset). TakeWhile((item, index) => (index == 0) || (item == (listoffset1 + index - 1 + 1))).ToList(); offset += subList.Count(); sequence++; Debug.

WriteLine("Sequence {0} from {1} amount {2} ({3})", sequence, subList0, subList.Count(), string. Join(" ", subList)); } while (offset.

Int n = 12;//your number int x = list. IndexOf(n); var result = list. Skip(x).

TakeWhile((value, index) => value - index == n).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions