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
  •  | 
  •  

Pergunta

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"

Foi útil?

Solução

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

For n = 3 produces:

[
  "1",
  "1 2",
  "1 2 3"
]
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top