Create the shortest program/function/whatever that splits an inputted string along un-nested commas. A comma is considered nested if it is either within parentheses, brackets, or braces.
Input and output
Output should be a list or a string joined with linebreaks. The input may contain any characters. All testcases will have valid/balanced parentheses, brackets, and braces. There will also not be two adjacent commas, nor will there be commas at the start or end. Also, all inputs will be valid, printable ASCII characters from ' ' to '~' (no tabs or linebreaks).
Test cases:
"asd,cds,fcd"->["asd","cds","fcd"]
"(1,2,3)"->["(1,2,3)"]
"(1),{[2,3],4}"->["(1)","{[2,3],4}"]
"(1),[2]{3}"->["(1)","[2]{3}"]
"(1),((2,3),4)"->["(1)","((2,3),4)"]
"(1),(4,(2,3))"->["(1)","(4,(2,3))"]
Example program:
function f(code){
code = code.split(',');
let newCode = [];
let last = "";
for (part of code){
if (last.split("(").length == last.split(")").length && last.split("[").length == last.split("]").length && last.split("{").length == last.split("}").length){
last = part;
newCode.push(part);
}
else{
last += "," + part;
newCode[newCode.length - 1] = last;
}
}
return newCode;
}
<input id=in oninput="document.getElementById('out').innerText=f(this.value).join('\n')">
<p id=out></p>
"A,[B](C)" -> ["A","[B][C]"]
a valid test case? Or are balanced groups guaranteed to be separated by a comma? \$\endgroup\$"(1),((2,3),4)"->["(1)","((2,3),4)"]
(makes my naive answer fail ... I need another coffee). \$\endgroup\$