This notebook was prepared by Rishi Rajasekaran. Source and license info is available on Github.

Challenge Notebook

Problem: Find all valid combinations of n-pairs of parentheses.

Constraints

  • Is the input an integer representing the number of pairs?
    • Yes
  • Can we assume the inputs are valid?
    • No
  • Is the output a list of valid combinations?
    • Yes
  • Should the output have duplicates?
    • No
  • Can we assume this fits memory?
    • Yes

Test Cases

* None -> Exception
* Negative -> Exception
* 0 -> []
* 1 -> ['()']
* 2 -> ['(())', '()()']
* 3 -> ['((()))', '(()())', '(())()', '()(())', '()()()']

Algorithm

Refer to the Solution Notebook. If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start.

Code

In [ ]:
class Parentheses(object):

    def find_pair(self, num_pairs):
        # TODO: implement me
        pass

Unit Test

In [ ]:
# %load test_n_pairs_parentheses.py
import unittest


class TestPairParentheses(unittest.TestCase):

    def test_pair_parentheses(self):
        parentheses = Parentheses()
        self.assertRaises(TypeError, parentheses.find_pair, None)
        self.assertRaises(ValueError, parentheses.find_pair, -1)
        self.assertEqual(parentheses.find_pair(0), [])
        self.assertEqual(parentheses.find_pair(1), ['()'])
        self.assertEqual(parentheses.find_pair(2), ['(())',
                                                '()()'])
        self.assertEqual(parentheses.find_pair(3), ['((()))',
                                                '(()())',
                                                '(())()',
                                                '()(())',
                                                '()()()'])
        print('Success: test_pair_parentheses')


def main():
    test = TestPairParentheses()
    test.test_pair_parentheses()


if __name__ == '__main__':
    main()

Solution Notebook

Review the Solution Notebook for a discussion on algorithms and code solutions.