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('(('))- A. True False True
- B. True False False
- C. True True False
- 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
- What is printed? python import sys data = 'first line nsecondn' it = iter(data.splitlines()) print(repr(next(it)), repr(next(it).strip())…
- What is printed? python from collections import deque q = deque() for ch in 'abcd': q.append(ch) if len(q) > 2: q.popleft()…
- What is printed? python a = [1, 2, 3, 4, 5, 6, 7, 8] print(a[::3], a[2::-1], a[-3::2])
- What is printed? python x = [i for i in range(3)] g = (i for i in range(3)) print(x == list(g), list(g), len(x))
- What is printed? python print(sorted(['b10', 'b9', 'a100'], key=lambda s: (s[0], int(s[1:]))))
- What is printed? python print(round(3.14159, 3), round(12345, -2), round(0.5), round(1.5), round(2.5, 0))