mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-06 09:28:02 +00:00
Added n-pairs parentheses challenge
This commit is contained in:
@@ -0,0 +1,162 @@
|
||||
{
|
||||
"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",
|
||||
"* If no_of_left == 0 and no_of right == 0, add pair_string to result\n",
|
||||
"* Else \n",
|
||||
" * If (no_left > 0), parentheses_util(no_left - 1, no_right, pair_string + '(', result)\n",
|
||||
" * If (no_right > no_left), parentheses_util(no_left, no_right - 1, pair_string + ')', result)\n",
|
||||
"\n",
|
||||
"Complexity:\n",
|
||||
"* Time: O(4^n/n^(3/2)) "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"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": 6,
|
||||
"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
|
||||
}
|
||||
Reference in New Issue
Block a user