Skip to main content
deleted 1755 characters in body
Source Link
code
  • 531
  • 2
  • 4

The Missing MatchThe Missing Match

You are given a string of unknown length that contains a varied amount of (, {, [ and their respective closing parentheses (in this context all referred to as braces).

With one exception, all the braces are all balanced, meaning an opening one has a corresponding closing one somewhere afterwards in the string.
The braces may not be "interpolated" with each other, i.e. opening brace b which is inside opening brace a must close before brace a closes, so ([)], {[(]}), and ({[(])}())[()] are invalid, but ([]), {[()]}, and ({[()])}()[()] are valid.

Challenge

The task is, inside the string there is a single unbalanced brace, i.e. an opening brace that does not have a closing one. You must insert the closing brace where it is the farthest distance apart from the opening brace, but not interpolated with other braces, then return the modified (or new) string.

Test cases

^ added to point out where a brace was added.

"("                        --> "()"
                                 ^
"[(]"                      --> "[()]"
                                  ^
"{[({}()([[[{}]]])]{[]}}"  --> "{[({}()([[[{}]]]))]{[]}}"
                                                 ^
"[[[]]"                    --> "[[[]]]"
                                     ^
"{[[]([(()){[[{}]()]})]}"  --> "{[[]([(()){[[{}]()]}])]}"
                                                    ^

Scoring

This is , so shortest code wins.

Meta

  • Is my definition of "brace interpolation" easy to understand and clear?
  • Is there something in the challenge that I didn't consider?
  • Is this too complicated? If so how can I dumb it down?

The Missing Match

You are given a string of unknown length that contains a varied amount of (, {, [ and their respective closing parentheses (in this context all referred to as braces).

With one exception, all the braces are all balanced, meaning an opening one has a corresponding closing one somewhere afterwards in the string.
The braces may not be "interpolated" with each other, i.e. opening brace b which is inside opening brace a must close before brace a closes, so ([)], {[(]}), and ({[(])}())[()] are invalid, but ([]), {[()]}, and ({[()])}()[()] are valid.

Challenge

The task is, inside the string there is a single unbalanced brace, i.e. an opening brace that does not have a closing one. You must insert the closing brace where it is the farthest distance apart from the opening brace, but not interpolated with other braces, then return the modified (or new) string.

Test cases

^ added to point out where a brace was added.

"("                        --> "()"
                                 ^
"[(]"                      --> "[()]"
                                  ^
"{[({}()([[[{}]]])]{[]}}"  --> "{[({}()([[[{}]]]))]{[]}}"
                                                 ^
"[[[]]"                    --> "[[[]]]"
                                     ^
"{[[]([(()){[[{}]()]})]}"  --> "{[[]([(()){[[{}]()]}])]}"
                                                    ^

Scoring

This is , so shortest code wins.

Meta

  • Is my definition of "brace interpolation" easy to understand and clear?
  • Is there something in the challenge that I didn't consider?
  • Is this too complicated? If so how can I dumb it down?
added 27 characters in body
Source Link
code
  • 531
  • 2
  • 4

You are given a string of unknown length that contains a varied amount of (, {, [ and their respective closing parentheses (in this context all referred to as braces), and their respective closing parentheses. The

With one exception, all the braces are all balanced, meaning an opening one has a corresponding closing one somewhere afterwards in the string. 
The braces may not be "interpolated" with each other, i.e. opening brace b which is inside opening brace a must close before brace a closes, so ([)], {[(]}), and ({[(])}())[()] are invalid, but ([]), {[()]}, and ({[()])}()[()] are valid.

"("                        --> "()"
                                 ^
"[(]"                      --> "[()]"
                                  ^
"{[({}()([[[{}]]])]{[]}}"  --> "{[({}()([[[{}]]]))]{[]}}"
                                                 ^
"[[[]]"                    --> "[[[]]]"
                                     ^
"{[[]([(()){[[{}]()]}])]}"  --> "{[[]([(()){[[{}]()]}])]}"
                                                    ^

You are given a string of unknown length that contains a varied amount of (, {, [ (in this context all referred to as braces), and their respective closing parentheses. The braces are all balanced, meaning an opening one has a corresponding closing one somewhere afterwards in the string. The braces may not be "interpolated" with each other, i.e. opening brace b which is inside opening brace a must close before brace a closes, so ([)], {[(]}), and ({[(])}())[()] are invalid, but ([]), {[()]}, and ({[()])}()[()] are valid.

"("                        --> "()"
                                 ^
"[(]"                      --> "[()]"
                                  ^
"{[({}()([[[{}]]])]{[]}}"  --> "{[({}()([[[{}]]]))]{[]}}"
                                                 ^
"[[[]]"                    --> "[[[]]]"
                                     ^
"{[[]([(()){[[{}]()]}])]}" --> "{[[]([(()){[[{}]()]}])]}"
                                                    ^

You are given a string of unknown length that contains a varied amount of (, {, [ and their respective closing parentheses (in this context all referred to as braces).

With one exception, all the braces are all balanced, meaning an opening one has a corresponding closing one somewhere afterwards in the string. 
The braces may not be "interpolated" with each other, i.e. opening brace b which is inside opening brace a must close before brace a closes, so ([)], {[(]}), and ({[(])}())[()] are invalid, but ([]), {[()]}, and ({[()])}()[()] are valid.

"("                        --> "()"
                                 ^
"[(]"                      --> "[()]"
                                  ^
"{[({}()([[[{}]]])]{[]}}"  --> "{[({}()([[[{}]]]))]{[]}}"
                                                 ^
"[[[]]"                    --> "[[[]]]"
                                     ^
"{[[]([(()){[[{}]()]})]}"  --> "{[[]([(()){[[{}]()]}])]}"
                                                    ^
added 150 characters in body
Source Link
code
  • 531
  • 2
  • 4
"("                        --> "()"
                                 ^
"[(]"                      --> "[()]"
                                  ^
"{[({}()([[[{}]]])]{[]}}"  --> "{[({}()([[[{}]]]))]{[]}}"
                                                 ^
"[[[]]"                    --> "[[[]]]"
                                     ^
"{[[]([(()){[[{}]()]}])]}" --> "{[[]([(()){[[{}]()]}])]}"
                                                    ^

Meta

  • Is my definition of "brace interpolation" easy to understand and clear?
  • Is there something in the challenge that I didn't consider?
  • Is this too complicated? If so how can I dumb it down?
"("                       --> "()"
                                ^
"[(]"                     --> "[()]"
                                 ^
"{[({}()([[[{}]]])]{[]}}" --> "{[({}()([[[{}]]]))]{[]}}"
                                                ^
"[[[]]"                   --> "[[[]]]"
                                    ^
"("                        --> "()"
                                 ^
"[(]"                      --> "[()]"
                                  ^
"{[({}()([[[{}]]])]{[]}}"  --> "{[({}()([[[{}]]]))]{[]}}"
                                                 ^
"[[[]]"                    --> "[[[]]]"
                                     ^
"{[[]([(()){[[{}]()]}])]}" --> "{[[]([(()){[[{}]()]}])]}"
                                                    ^

Meta

  • Is my definition of "brace interpolation" easy to understand and clear?
  • Is there something in the challenge that I didn't consider?
  • Is this too complicated? If so how can I dumb it down?
added 112 characters in body
Source Link
code
  • 531
  • 2
  • 4
Loading
Source Link
code
  • 531
  • 2
  • 4
Loading