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

StackOverflow https://stackoverflow.com/questions/20661698

  •  19-09-2022
  •  | 
  •  

Вопрос

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"

Это было полезно?

Решение

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

For n = 3 produces:

[
  "1",
  "1 2",
  "1 2 3"
]
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top