Files
interactive-coding-challenges/recursion_dynamic/n_pairs_parentheses/n_pairs_parentheses_solution.ipynb

176 lines
4.9 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<small><i>This notebook was prepared by [Rishi Rajasekaran](https://github.com/rishihot55). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges).</i></small>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Solution Notebook"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem: Print all valid combinations of n-pairs of parentheses\n",
"\n",
"* [Constraints](#Constraints)\n",
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)\n",
"* [Unit Test](#Unit-Test)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Constraints\n",
"* None"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"* 0 -> ' '\n",
"* 1 -> ()\n",
"* 2 -> (()), ()()\n",
"* 3 -> ((())), (()()), (())(), ()(()), ()()()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Algorithm\n",
"\n",
"Let `l` and `r` denote the number of left and right parentheses remaining at any given point. \n",
"The algorithm makes use of the following conditions applied recursively:\n",
"* Left braces can be inserted any time, as long as we do not exhaust them i.e. `l > 0`.\n",
"* Right braces can be inserted, as long as the number of right braces remaining is greater than the left braces remaining i.e. `r > l`. Violation of the aforementioned condition produces an unbalanced string of parentheses.\n",
"* If both left and right braces have been exhausted i.e. `l = 0 and r = 0`, then the resultant string produced is balanced.\n",
"\n",
"The algorithm can be rephrased as:\n",
"* Base case: `l = 0 and r = 0`\n",
" - Add the string generated to the result set\n",
"* Case 1: `l > 0`\n",
" - Add a left parenthesis to the parentheses string.\n",
" - Call parentheses_util(l - 1, r, new_string, result_set)\n",
"* Case 2: `r > l`\n",
" - Add a right parenthesis to the parentheses string.\n",
" - Call parentheses_util(l, r - 1, new_string, result_set)\n",
"\n",
"Complexity:\n",
"* Time: `O(4^n/n^(3/2))`. See [Catalan numbers](https://en.wikipedia.org/wiki/Catalan_number#Applications_in_combinatorics)\n",
"* Space complexity: `O(n)` (Due to the implicit call stack storing a maximum of 2n function calls)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def parentheses_util(no_left, no_right, pair_string, result):\n",
" if no_left == 0 and no_right == 0:\n",
" result.add(pair_string)\n",
" else:\n",
" if no_left > 0:\n",
" parentheses_util(no_left - 1, no_right, pair_string + '(', result)\n",
" if no_right > no_left:\n",
" parentheses_util(no_left, no_right - 1, pair_string + ')', result)\n",
"\n",
"\n",
"def pair_parentheses(n):\n",
" result_set = set()\n",
" if n == 0:\n",
" return result_set\n",
" parentheses_util(n, n, '', result_set)\n",
" return result_set\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Unit Test"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Success: test_pair_parentheses\n"
]
}
],
"source": [
"# %load test_n_pairs_parentheses.py\n",
"from nose.tools import assert_equal\n",
"\n",
"class TestPairParentheses(object):\n",
" \n",
" def test_pair_parentheses(self, solution):\n",
" assert_equal(solution(0), set([]))\n",
" assert_equal(solution(1), set(['()']))\n",
" assert_equal(solution(2), set(['(())', '()()']))\n",
" assert_equal(solution(3), set(['((()))','(()())', '(())()', '()(())', '()()()']))\n",
" print('Success: test_pair_parentheses')\n",
"\n",
"def main():\n",
" test = TestPairParentheses()\n",
" test.test_pair_parentheses(pair_parentheses)\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 0
}