University of Arizona, Department of Computer Science

CSc 120: Balanced Delimiters (Stacks)

Expected Behavior

Write a function is_balanced(symbols), that takes a string symbols and returns True if symbols is balanced with respect to the pairs of delimiters "[]", "{}" and "()" and False otherwise. The string argument symbols will consist only of the delimiter characters.

Programming Requirements

Your solution must use a stack for auxillary storage and must use the process shown in lecture to determine if the argument string of symbols is balanced.

Solutions that do not use a stack will not get credit.

The Stack class is shown below for reference:

class Stack:
    def __init__(self):
        self._items = []

    def push(self, item):
        self._items.append(item)
    
    def pop(self):
        return self._items.pop()

    def is_empty(self):
        return self._items == []
    
    def __str__(self):
        return str(self._items) 

Examples

  1. is_balanced('{}[][]()')
    return value: True

  2. is_balanced('({)}')
    return value: False

  3. is_balanced('[[{{(())}}]]')
    return value: True

  4. is_balanced('((()}))')
    return value: False