35
3
Given some finite list, return a list of all its prefixes, including an empty list, in ascending order of their length.
(Basically implementing the Haskell function inits
.)
Details
- The input list contains numbers (or another type if more convenient).
- The output must be a list of lists.
- The submission can, but does not have to be a function, any default I/O can be used.
- There is a CW answer for all trivial solutions.
Example
[] -> [[]]
[42] -> [[],[42]]
[1,2,3,4] -> [[], [1], [1,2], [1,2,3], [1,2,3,4]]
[4,3,2,1] -> [[], [4], [4,3], [4,3,2], [4,3,2,1]]
If a language does not define any types except for characters, can I take input as a string and separate the input by newlines, in the case of a full program? – NieDzejkob – 2018-12-08T20:37:50.810
@NieDzejkob I'm not sure what consensus there is for this case, but the Brainfuck answer seems to do something like that. – flawr – 2018-12-08T21:18:44.210
Can we expect the list to be null-terminated? – None – 2018-12-09T10:02:30.527
It's especially common in C/C++, main use being strings. – None – 2018-12-13T14:46:34.987
@Rogem If it is that common I think allowing it is reasonable. – flawr – 2018-12-13T14:49:41.310