Why should you golf in Haskell? Find out here. In that answer, Zgarb defines a task:
Let's define a function
f
that splits a list at the second occurrence of the first element, e.g.[0,2,2,3,0,1,0,1] -> ([0,2,2,3],[0,1,0,1])
:
Alright then, lets!
You are to take a non-empty list consisting of digits, and output a pair of lists, clearly distinguished, such that the output is the input split before the second occurrence of the first element.
For example, you may output two strings of digits with a non-digit separator. The separator you choose between the elements of each pair and the pairs themselves must be distinct (e.g. space and newline), consistent and not contain digits.
The output must only contain 2 elements however. You may not output empty lists in the output. If you choose to use newlines as a separator between pairs, you may not have a leading newline. You may have trailing newlines no matter what, and you may have trailing whitespace, so long as its sensible.
You may assume:
- The input will be provided in any convenient method, and you may output in any convenient method
- The input will either be a list of digits, or a string
- The input will only contain the integers
0
though to9
- The first element of the input will always occur at least twice
- The input will always have 3 or more elements in it
Additionally, this is code-golf, so the shortest code in bytes wins
Test cases
[1, 1, 9] -> [[1], [1, 9]]
[4, 9, 4] -> [[4, 9], [4]]
[5, 7, 5, 5] -> [[5, 7], [5, 5]]
[8, 8, 0, 7] -> [[8], [8, 0, 7]]
[7, 1, 5, 7, 4, 2] -> [[7, 1, 5], [7, 4, 2]]
[0, 6, 9, 1, 1, 0, 2] -> [[0, 6, 9, 1, 1], [0, 2]]
[2, 9, 3, 2, 4, 2, 5, 9] -> [[2, 9, 3], [2, 4, 2, 5, 9]]
[0, 2, 2, 3, 0, 1, 0, 1] -> [[0, 2, 2, 3], [0, 1, 0, 1]]
[2, 7, 4, 6, 2, 6, 6, 4, 8, 2] -> [[2, 7, 4, 6], [2, 6, 6, 4, 8, 2]]
[8, 2, 2, 7, 5, 4, 7, 0, 8, 0, 7] -> [[8, 2, 2, 7, 5, 4, 7, 0], [8, 0, 7]]
[8, 7, 8, 9, 4, 2, 9, 4, 5, 7, 5, 1, 9] -> [[8, 7], [8, 9, 4, 2, 9, 4, 5, 7, 5, 1, 9]]
[3, 8, 1, 1, 7, 3, 6, 9, 7, 1, 4, 3, 4] -> [[3, 8, 1, 1, 7], [3, 6, 9, 7, 1, 4, 3, 4]]
[4, 7, 0, 5, 6, 5, 0, 1, 7, 8, 7, 8, 4, 1] -> [[4, 7, 0, 5, 6, 5, 0, 1, 7, 8, 7, 8], [4, 1]]
[2, 1, 8, 0, 3, 2, 2, 5, 7, 9, 4, 3, 5, 1, 9, 6, 9] -> [[2, 1, 8, 0, 3], [2, 2, 5, 7, 9, 4, 3, 5, 1, 9, 6, 9]]
[1, 1, 4, 1, 2, 5, 5, 3, 3, 4, 3, 2, 0, 8, 6, 0, 3] -> [[1], [1, 4, 1, 2, 5, 5, 3, 3, 4, 3, 2, 0, 8, 6, 0, 3]]
[4, 3, 5, 2, 2, 0, 6, 4, 8, 6, 6, 6, 7, 3, 4, 8, 7, 6] -> [[4, 3, 5, 2, 2, 0, 6], [4, 8, 6, 6, 6, 7, 3, 4, 8, 7, 6]]
-1
as a "separator" shouldn't break the challenge (e.g.[0,2,2,3,0,1,0,1] -> [0,2,2,3,-1,0,1,0,1]
), so you're welcome to use that format. Otherwise, I don't see how you could use that language here :( \$\endgroup\$