If I have a sequence IEnumerable<T>
(not numbers, just T
):
[ a, b, c, d ]
How to return sort of Pascal's or Floyd's triangle:
a
ab
abc
abcd
so it would be IEnumerable<IEnumerable<T>>
?
Wondering whether there a way to achieve this elegantly using LINQ rather than implement manually using loops.
This should work:
var seq = new List<string> { "a", "b", "c", "d" };
var pascal = seq.Select(a => seq.Take(seq.IndexOf(a) +1 ).ToList());
edit:
var seq = new List<string> { "a", "b", "c", "d" };
var pascal = seq.Select((a,i) => seq.Take(i+1).ToList());