17
2
You are a project manager. One day, one of your programmers went insane (not your fault) and took all the expressions in the codebase and added random brackets to them, before quitting on the spot, ranting about your incompetence (also not your fault). This would be an easy fix, however, for some reason you aren't using revision control (totally not your fault). And for some reason, none of the other programmers want to go through every expression to fix up the mismatched brackets (by the way, that is not your fault). Programmers these days, you think to yourself. You will have to do it yourself. The horror! Such tasks were supposed to be beneath you...
The input will be a single line, which will contain a number of left brackets (( [ {
) and right brackets () ] }
). It also may, but not always, contain comments (/* */
) and string literals (" "
or ' '
) and various numbers, letters, or symbols.
There will be at least one bracket (outside of a comment or string literal) that does not have a corrosponding opposite (outside of a comment or string literal). For example, an errant }
without a {
prior. Another example: a (
which does not have a )
afterwards. Your program will replace with a space the minimum number of brackets required to make the brackets match up.
Examples:
(4 + (2 + 3))]
==> (4 + (2 + 3))
(the square bracket at the end)
][][[]]
==> [][[]]
(the square bracket at the start)
("Hel(o!"))
==> ("Hel(o!")
(the parenthesis at the end)
( /* )]*/
==> /* )]*/
(the parenthesis at the start)
{()]
==> ()
(the curly bracket and the square bracket)
- Input can be taken from whichever way is most convenient (STDIN, command line argument, reading from a file, etc.)
- If there is more than one way to resolve the mismatch with the same number of removals, either is acceptable.
- There will be only mismatches in brackets. String literals and comments will always be correctly formed.
- The title comes from this SO thread
- There will never be any quotes in comments, quotes in quotes, comments in comments, or comments in quotes.
This is code golf, so minimum number of bytes wins. Ask questions in comments if the specification is not clear.
Whoops, our edits kind of collided there. :P Everything should be fixed now. – Doorknob – 2014-08-19T22:22:16.553
@Doorknob Thanks for that, by the way. Didn't know how to stop SE from wiping the spaces. – absinthe – 2014-08-19T22:22:47.130
Do we have to handle escaping stuff in string literals (e.g.
("foo (\") bar")
)? – Doorknob – 2014-08-19T22:30:22.657@Doorknob To simplify things, I'm just going to say there won't be any double quotes ever inside a string literal in the input. – absinthe – 2014-08-19T22:34:10.207
Similarly, can there be quotes in a comment / a
/*
inside quotes / etc.? For examplethis is "a tricky /* input" /* it is "very tricky */
– Doorknob – 2014-08-19T22:38:27.187@Doorknob How about this. Quotes override comments. So in your example, everything between
a
andis
is a quote, and the/*
and"
inside that just become part of the quote. This leaves the*/
on the outside as a mismatched quote, which won't every happen according to the bullet point. – absinthe – 2014-08-19T22:41:52.347Why do quotes override comments, but comments not override quotes? That seems somewhat inconsistent. It would make more sense if either a.) There will never be quotes in comments / comments in quotes, or b.) There can be both quotes in comments and comments in quotes. – Doorknob – 2014-08-19T22:43:32.843
@Doorknob Yeah, I suppose you're right. We'll go with the former option then, so any input where there are quotes in comments or comments in quotes will never happen. I'll edit to reflect that. – absinthe – 2014-08-19T22:44:52.313
@Doorknob In most languages isn't the deal with strings comments just that what ever starts first continues until the ending delimiter is encountered? – Martin Ender – 2014-08-19T23:01:46.777
@MartinBüttner Yes, which would be option B in my comment. But option A is simpler for the challenge. – Doorknob – 2014-08-19T23:42:10.453
Is the correct output for
{{(})
{(})
or{ }
? – QuadmasterXLII – 2014-08-20T18:04:10.330@QuadmasterXLII The correct output would be
{(})
(based on my definition above), but I'm inclined to let anything go for these edge cases, since they're so confusing for everybody involved :/ – absinthe – 2014-08-20T22:28:27.2471I would argue that the correct output for
{{(})
should be{ }
or equivalent, since the opening scenario implies that the code was working to begin with, and{(})
counts as mismatched brackets in every programming language I know (i.e. "causes stasis"??). But, then, I already wrote an answer, so I'm biased. – DLosc – 2014-08-21T22:48:17.233@DLosc, that is indeed the syntactically correct answer. However, I wanted to highlight the incompetence of the project manager in the scenario. – absinthe – 2014-08-21T22:50:16.860
3I see. Guess I'm just not incompetent enough. ;) – DLosc – 2014-08-21T23:00:46.230