Balanced Brackets Checker

Step 2 - Balanced Brackets Checker

A classic real use of a stack: checking whether brackets match. Scan the string; push every opener and, on each closer, pop and confirm it matches.

Implement is_balanced(s) so it returns True when ()[]{} are correctly nested and closed, and False otherwise. Reuse the Stack from Step 1. Ignore any non-bracket characters. An empty string is balanced.

Hints
Starter code
class Stack:
    def __init__(self):
        self._data = []

    def push(self, x):
        self._data.append(x)

    def pop(self):
        if not self._data:
            return None
        return self._data.pop()

    def peek(self):
        if not self._data:
            return None
        return self._data[-1]

    def is_empty(self):
        return len(self._data) == 0

    def size(self):
        return len(self._data)


def is_balanced(s):
    """Return True if brackets ()[]{} in s are balanced, else False."""
    # TODO: use a Stack; push openers, match closers with the top opener
    pass
Write it on a laptop

The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.