17
In Elixir, (linked) lists are in the format [head | tail]
where head can be anything and tail is a list of the rest of the list, and []
- the empty list - is the only exception to this.
Lists can also be written like [1, 2, 3]
which is equivalent to [1 | [2 | [3 | []]]]
Your task is to convert a list as described. The input will always be a valid list (in Elixir) containing only numbers matching the regex \[(\d+(, ?\d+)*)?\]
. You may take the input with (one space after each comma) or without spaces. The output may be with (one space before and after each |
) or without spaces.
For inputs with leading zeroes you may output either without the zeroes or with.
Input must be taken as a string (if writing a function), as does output.
Examples
[] -> []
[5] -> [5 | []]
[1, 7] -> [1 | [7 | []]]
[4, 4, 4] -> [4 | [4 | [4 | []]]]
[10, 333] -> [10 | [333 | []]]
related, not a duplicate as this in part involves adding mode ]
to the end. Additionally, the Haskell answer here is quite different to the one there.
5
-1 from me. Cumbersome IO formats are discouraged. If the input is a list, let us take it as a list instead of having 90% of our code just parsing the input
– Jo King – 2018-04-30T23:04:10.6271very similar, but I wouldn't quite call it a dupe. – Post Rock Garf Hunter – 2018-05-01T00:40:43.897
@JoKing Discouraged, not banned :) – Conor O'Brien – 2018-05-01T01:20:38.470
2Do we have to support leading 0s? They fit the regex. – Jo King – 2018-05-01T02:04:29.023
1Why does the output need a space before and after a
|
(if we include spaces)? Elixer seems to parse any number of spaces, including none – Jo King – 2018-05-01T02:26:27.7502
Possible duplicate of Sugar Free Syntax
– NoOneIsHere – 2018-05-01T02:35:58.2475@JoKing I'd argue that here the challenge itself is about converting between two specific formats, so parsing the input is a fundamental part of the challenge and not something extra added to it. P.S. I've realized only now what your nickname really is xD – Leo – 2018-05-01T04:43:01.150
1Why do we want to take input as string , and same for output. That is 14 bytes worth in JavaScript just converting back and forth. – Muhammad Salman – 2018-05-01T05:14:59.433
2@MuhammadSalman: the challenge is tagged as "parsing", so converting from/to a string is a substantial part of it with intent. – nimi – 2018-05-01T09:52:03.970