Git Product home page Git Product logo

brackets_ass's Introduction

Checking brackets in code

Your friend is making a text editor for programmers. He is currently working on a feature that will find errors in the usage of different types of brackets.

Code can contain any brackets from the set []{}(), where the opening brackets are [,{ and (, and the closing brackets corresponding to them are ],}, and ).

For efficiency, the text editor should not only inform the user that there is an error in the usage of brackets, but also point to the exact place in the code with the problematic bracket. First priority is to find the first unmatched closing bracket which either doesn’t have an opening bracket before it, like ] in ](), or closes the wrong opening bracket, like } in ()[}. If there are no such mistakes, then it should point to the first unmatched opening bracket without the corresponding closing bracket after it, like ( in {}([]. If there are no mistakes, text editor should inform the user that the usage of brackets is correct.

Apart from the brackets, code can contain big and small latin letters, digits and punctuation marks.

More formally, all brackets in the code should be divided into pairs of matching brackets, such that in each pair the opening bracket goes before the closing bracket, and for any two pairs of brackets either one of them is nested inside another one as in (foo[bar]) or they are separate as in f(a,b)-g[c]. The bracket [ corresponds to the bracket ], { corresponds to }, and ( corresponds to ).

Input Format

Input contains one string S, which consists of big and small latin letters, digits, punctuation marks and brackets from the set []{}().

Constraints

The length of S is at least 1 and at most 105.

Output Format

If the code in S uses brackets correctly, output Success. Otherwise, output the 1-based index of the first unmatched closing bracket, and if there are no unmatched closing brackets, output the 1-based index of the first unmatched opening bracket.

Sample 1

Input:
[]
Output:
Success

Sample 2

Input:
[()]
Output:
Success

Sample 3

Input:
{[}
Output:
3

Sample 4

Input:
foo(bar[i);
Output:
10

Tests

To test your program, you can run provided bash script. You may need to change file permissions in order to be able to run it:

chmod a+x run_tests.sh

After that, run:

./run_tests.sh inputs outputs

Folder inputs contains 54 sample files for testing brackets, and folder outputs contains the expected output for each file. You can compare your output to the desired output, and fix your program until both of them match.

brackets_ass's People

Contributors

mgbarsky avatar artoria2e5 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.