DWITE '07 R2 #4 - All Is Balanced

View as PDF

Submit solution

Points: 5
Time limit: 2.0s
Memory limit: 64M

Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig
DWITE Online Computer Programming Contest, November 2007, Problem 4

Parenthesis are important characters in programming -- they define order of operations and organize information. That is, as long as they are properly balanced. Parenthesis are balanced when opening and closing brackets are match with one another, and are themselves nested within balanced parenthesis.

This is perhaps best illustrated in examples.

  • () - balanced: opening is matched with a closing
  • (() - not balanced: one of the opening brackets has no match
  • ([)] - not balanced: the contents inside of ( ) are not balanced

The input will contain 5 line, each no more than 255 characters long. Valid characters are any of the three parenthesis types: (), [], {}, and any alphanumeric characters: a-z0-9.

The output will contain 5 lines, each stating either "balanced" or "not balanced", for the supplied expressions. Use the exact string output, all lower case, without quotes.

All of the characters but the parenthesis could effectively be ignored. Keep in mind the different types of parenthesis -- an opening bracket needs to be matched with a closing bracket of the same type.

Sample Input

abc
([{a}b]c)
)(
([)]
{abc]

Sample Output

balanced
balanced
not balanced
not balanced
not balanced

Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0) Problem Resource: DWITE


Comments


  • -3
    Arihan10  commented on April 20, 2019, 7:52 a.m.

    Are only specific types of brackets permitted for this problem? If so, what are they? (i.e (), [], etc.)


    • -3
      Robloxian  commented on April 20, 2019, 10:38 a.m.

      The input will contain 5 line, each no more than 255 characters long. Valid characters are any of the three parenthesis types: (), [], {}, and any alphanumeric characters: a-z0-9. The output will contain 5 lines, each stating either "balanced" or "not balanced", for the supplied expressions. Use the exact string output, all lower case, without quotes. All of the characters but the parenthesis could effectively be ignored. Keep in mind the different types of parenthesis -- an opening bracket needs to be matched with a closing bracket of the same type.


      • 0
        Arihan10  commented on April 21, 2019, 8:03 a.m.

        Oh sorry. I didn't see that (I don't know how). Thanks & sorry again.