Search code examples
c#linqenumerable

What is shortest in code terms way for given N to output strings "1", "1 2"... "1 2 ... N"?


What is shortest in code terms way for given N (int) to output List<strings> out; containing strings "1", "1 2"... "1 2 ... N"? For N == 3 out would contain "1"; "1 2"; "1 2 3"


Solution

  • Enumerable.Range(1, n)
              .Select(i => String.Join(" ", Enumerable.Range(1, i)))
              .ToList();
    

    For n = 3 produces:

    [
      "1",
      "1 2",
      "1 2 3"
    ]