15
1
Your mission, should you choose to accept it, is to add the minimum number of parentheses, braces, and brackets to make a given string (containing only parentheses, braces, and brackets) have correct brace matching. Ties of symbols added must be broken by having the maximum distance between paired braces. You must return only one correct answer that matches these two rules; Further ties, should they exist, may be broken any way you see fit.
Examples:
input output
// Empty String is a legal input
[ [] // Boring example
[()] [()] // Do nothing if there's nothing to be done
({{ ({{}}) // NOT (){}{} (0 + 0 + 0). Maximum distance is 4 + 2 + 0, ({{}})
[([{])]} {[([{}])]} // NOT [([])]{[([])]} or similar
You may write a program or function, receives the input via STDIN as a string argument to your function, which returns the output as a string or prints it to STDOUT (or closest alternative). You may optionally include a single trailing newline in the output.
You may assume the input string consists only of the following 6 characters (or lack thereof): [](){}
(You do not need to support <>
)
This is code-golf, shortest program wins. Standard loopholes are banned, of course.
Did you mean to repeat the title directly underneath the actual title, or repeat the tag directly above the actual tags? Just asking in case you copy pasted from Sandbox and forgot to remove them. – Rainbolt – 2015-05-27T20:40:07.303
@Rainbolt The former no (sandbox), the latter yes – durron597 – 2015-05-27T20:47:15.807
1@AlexA. I can see how they're different in minor ways, but I think they're too similar to be considered separate questions. – NinjaBearMonkey – 2015-05-27T22:22:00.183
Fair enough. It's certainly not cut-and-dry, and I won't pursue getting it closed if others decide not to. – NinjaBearMonkey – 2015-05-27T22:29:27.490
I would consider it different enough. Voted to reopen. – nderscore – 2015-05-28T02:39:20.273
Will there be adjacent top-level groups (such as
{[()]}[()]
)? – HyperNeutrino – 2016-04-11T00:27:34.420@AlexL. there can be, if that is the shortest answer with maximum distance. Note that in most cases it won't be, because of the maximum distance rule, but not all cases. For example
{[()]}[()
would result in{[()]}[()]
. – durron597 – 2016-04-11T17:57:08.043