Dividir um array em blocos
Crie uma função que divida um array de inteiros em blocos consecutivos de tamanho n, em que n é o tamanho solicitado de cada bloco.
Exemplos
Program.Chunkify(new[] { 2, 3, 4, 5 }, 2) // new[] { new[] { 2, 3 }, new[] { 4, 5 } }
Program.Chunkify(new[] { 2, 3, 4, 5, 6 }, 2) // new[] { new[] { 2, 3 }, new[] { 4, 5 }, new[] { 6 } }
Program.Chunkify(new[] { 2, 3, 4, 5, 6, 7 }, 3) // new[] { new[] { 2, 3, 4 }, new[] { 5, 6, 7 } }
Program.Chunkify(new[] { 2, 3, 4, 5, 6, 7 }, 1) // new[] { new[] { 2 }, new[] { 3 }, new[] { 4 }, new[] { 5 }, new[] { 6 }, new[] { 7 } }
Program.Chunkify(new[] { 2, 3, 4, 5, 6, 7 }, 7) // new[] { new[] { 2, 3, 4, 5, 6, 7 } }Observações
- Mantenha o bloco final mesmo quando ele tiver menos de
nvalores. - Os números inteiros de entrada sempre terão apenas um dígito.