c# - Generate number sequences with LINQ -
i try write linq statement returns me possible combinations of numbers (i need test , inspired article of eric lippert). method's prototype call looks like:
ienumerable<collection<int>> allsequences( int start, int end, int size );
the rules are:
- all returned collections have length of
size
- number values within collection have increase
- every number between
start
,end
should used
so calling allsequences( 1, 5, 3 )
should result in 10 collections, each of size 3:
1 2 3 1 2 4 1 2 5 1 3 4 1 3 5 1 4 5 2 3 4 2 3 5 2 4 5 3 4 5
now, somehow i'd see pure linq solution. able write non linq solution on own, please put no effort solution without linq.
tries far ended @ point have join number result of recursive call of method - like:
return in enumerable.range( start, end - size + 1 ) select buildcollection(i, allsequences( i, end, size -1));
but can't manage implement buildcollection()
on linq base - or skip method call. can me here?
think i've got it.
ienumerable<list<int>> allsequences(int start, int end, int size) { if (size == 0) return enumerable.repeat<list<int>>(new list<int>(), 1); return in enumerable.range(start, end - size - start + 2) seq in allsequences(i + 1, end, size - 1) select new list<int>{i}.concat(seq).tolist(); }
Comments
Post a Comment