We all know the age-old debate - should strings be length-prefixed or should they be null-terminated? Well, you've run into a blob of binary data and apparently whoever made it decided that the best solution would be a compromise and length-terminated their strings.
For the purposes of this challenge, a length-terminated sequence is a series of one or multiple integers such that the last item in the sequence is equal to the number of items in the sequence, and no prefix of the sequence is also a valid length-terminated sequence. For example, [1]
, [15, 2]
, and [82, 19, 42, 111, 11, 6]
are all valid length-terminated sequences. [15]
is not a valid length-terminated sequence (it hasn't been terminated), and neither is [15, 2, 3]
(it should have ended at the 2
).
Your task is to write a program or function that takes a list of integers, and outputs all the valid length-terminated sequences within that list. For example, for the input list [7, 6, 5, 4, 3, 2, 1]
, valid length-terminated sequences would be [1]
, [3, 2]
, [5, 4, 3]
and [7, 6, 5, 4]
.
- You must output all valid length-terminated sequences found in the input, in any order. The order may vary between runs of your program. You must output no length-terminated sequences not found within the input, and no sequences found within the input that aren't valid length-terminated sequences.
- If some length-terminated sequence occurs multiple times within the input, it should also be output multiple times. The sequence
[9, 8, 3]
occurs twice in the input[9, 8, 3, 9, 8, 3]
(once starting with the first number, and again starting at the fourth number) and should thus be output twice. - A number may be part of more than one length-terminated sequence. For example, the input
[9, 9, 2, 4]
contains both the length-terminated sequences[9, 9, 2, 4]
and[9, 2]
and they should both be output. - You may assume all numbers are between 0 and 127 (both inclusive).
- You may assume the input contains no more than 126 numbers.
- You may not make any further assumptions about the input, including the assumption that the input contains at least one valid length-terminated sequence.
- You may take input or produce output by any of the default methods. Input and output formats are flexible (within reason) and the input format need not be the same as the output format. However, the your input and output formats must not depend on the input, and must not vary between runs of your program.
- This is code-golf - make your code as small as possible.
Examples
[input] -> [output], [output], ...
[7, 6, 5, 4, 3, 2, 1] -> [7, 6, 5, 4], [5, 4, 3], [3, 2], [1]
[90, 80, 70] -> (no output)
[1, 2, 3, 4, 5] -> [1]
[100, 0, 2, 2, 2, 0, 0, 2, 4] -> [0, 2], [2, 2], [2, 2], [0, 0, 2, 4], [0, 2]
[] -> (no output)
[54, 68, 3, 54, 68, 3] -> [54, 68, 3], [54, 68, 3]
[4, 64, 115, 26, 20, 85, 118, 9, 109, 84, 64, 48, 75, 123, 99, 32, 35, 98, 14, 56, 30, 13, 33, 55, 119, 54, 19, 23, 112, 58, 79, 79, 45, 118, 45, 51, 91, 116, 7, 63, 98, 52, 37, 113, 64, 81, 99, 30, 83, 70] -> [85, 118, 9, 109, 84, 64, 48, 75, 123, 99, 32, 35, 98, 14], [118, 9, 109, 84, 64, 48, 75, 123, 99, 32, 35, 98, 14, 56, 30, 13, 33, 55, 119, 54, 19, 23, 112, 58, 79, 79, 45, 118, 45, 51, 91, 116, 7, 63, 98, 52, 37], [109, 84, 64, 48, 75, 123, 99, 32, 35, 98, 14, 56, 30, 13, 33, 55, 119, 54, 19], [84, 64, 48, 75, 123, 99, 32, 35, 98, 14, 56, 30, 13], [14, 56, 30, 13, 33, 55, 119, 54, 19, 23, 112, 58, 79, 79, 45, 118, 45, 51, 91, 116, 7, 63, 98, 52, 37, 113, 64, 81, 99, 30], [45, 118, 45, 51, 91, 116, 7]
9,9,2,4
example, the possible strings (9,9,2,4
and9,2
if we include the terminators) start in different locations, so if you know where the string starts you do get an unambiguous value - the2
won't terminate9,9,2
but will terminate9,2
. The input contains zero or more strings in unknown locations because scanning the entire memory for possible matches is exactly what the challenge is about. \$\endgroup\$9 9 2
at some memory location it will result in the memory pattern9 9 2 4
, and reading the memory location later will unambiguously give you back9 9 2
. The problem is that if you try to store9 9 2 4 9
at some location, that will result in the memory pattern9 9 2 4 9 6
, but reading it back will give you9 9 2
(and the last 3 numbers will be leaked memory) because the 4 in the data is mistaken for a terminator. \$\endgroup\$