This notebook was prepared by Rishi Rajasekaran. Source and license info is available on Github.
* None -> Exception * Negative -> Exception * 0 -> [] * 1 -> ['()'] * 2 -> ['(())', '()()'] * 3 -> ['((()))', '(()())', '(())()', '()(())', '()()()']
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.
class Parentheses(object):
def find_pair(self, num_pairs):
# TODO: implement me
pass
# %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()
Review the Solution Notebook for a discussion on algorithms and code solutions.