ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
def bracket_ok(s):
    pairs = {')': '(', ']': '[', '}': '{'}
    st = []
    for ch in s:
        if ch in '([{':
            st.append(ch)
        elif not st or st.pop() != pairs[ch]:
            return False
    return not st
print(bracket_ok('{[()]}'), bracket_ok('([)]'), bracket_ok('(('))
  1. A. True False True
  2. B. True False False
  3. C. True True False
  4. D. False False False

Correct answer: B. True False False

A stack of open brackets must match each closing bracket. '{[()]}' is balanced. '([)]' fails because ')' meets '[' on top. '((' ends with an open bracket left on the stack.

More Python Programming questions

Exams that ask this topic