Ever wonder how your linter knows if you have matching brackets? Well it's time to think of a solution to that!
Given a set of brackets, [, {, (, create a function that determines if the brackets are well-formed (match) or not - even with bracket nesting. For example:
bracket('{}');
// => truebracket('{(');
// => falsebracket('{()}');
// => truebracket('{[)][]}');
// => falsebracket(']');
// => falsebracket('({[]}{[]})');
// => true- Copy this markdown and paste in your own, private gist
- In your private gist, fill out the questions below
- Submit by the due time as instructed in Zoom
- Given a string of 'brackets', check to see if each set of brackets has both an opening and closing bracket.
What assumptions will you make about this problem if you cannot ask any more clarifying questions? What are your reasons for making those assumptions?
- The input will always be a string containing ONLY brackets and not other characters such as numbers and letters.
- I assume this because I do not think they are testing my error handling in this problem.
- The output should be a boolean.
- Closing bracket occurs BEFORE a an opening bracket. FALSE
- If two types of opening brackets, expect the latter to close first. TRUE
- If only an opening bracket occurs without a closing bracket. FALSE
- Searching of Data
- Sorting of Data
- Pattern Recognition
- Build/Navigate a Grid
- Math
- Language API knowledge
- Optimization
- String into array
- CON: have to use extra function. PRO: can iterate over an array and not a string.
- Brake the initial STRING into an array.
- For each element in the array, check if it is an opening or closing bracket.
- If it is an closing bracket, and the opening bracket has NOT occurred yet STOP.
- If it is an opening bracket, check if the closing bracket occurs. If not STOP.
- If both the opening and closing bracket occur, and in that order, is there anything between them?
- If there IS something between them, check if the next character is an opening bracket that matches the penultimate character.
- Continue in this pattern until you have confirmed that ALL opening brackets have the necessary closing bracket.