mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-07 18:08:01 +00:00
172 lines
4.3 KiB
Python
172 lines
4.3 KiB
Python
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<small><i>This notebook was prepared by [Rishi Rajasekaran](https://github.com/rishihot55). Source and license info is available on [Github](https://github.com/donnemartin/interactive-coding-challenges).</i></small>"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Challenge 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)\n",
|
|
"* [Solution Notebook](#Solution-Notebook)"
|
|
]
|
|
},
|
|
{
|
|
"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",
|
|
"Refer to the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/n_pairs_parentheses/n_pairs_parentheses_solution.ipynb). If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Code"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": true
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"def parentheses_util(no_left, no_right, pair_string, result):\n",
|
|
" # TODO: implement parentheses pairing here\n",
|
|
" pass\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"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Unit Test"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# %load test_n_pairs_parentheses.py\n",
|
|
"from nose.tools import assert_equal\n",
|
|
"\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",
|
|
" '()()']))\n",
|
|
" assert_equal(solution(3), set(['((()))', \n",
|
|
" '(()())', \n",
|
|
" '(())()', \n",
|
|
" '()(())', \n",
|
|
" '()()()']))\n",
|
|
" print('Success: test_pair_parentheses')\n",
|
|
"\n",
|
|
"\n",
|
|
"def main():\n",
|
|
" test = TestPairParentheses()\n",
|
|
" test.test_pair_parentheses(pair_parentheses)\n",
|
|
"\n",
|
|
"\n",
|
|
"if __name__ == '__main__':\n",
|
|
" main()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Solution Notebook\n",
|
|
"\n",
|
|
"Review the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/n_pairs_parentheses/n_pairs_parentheses_solution.ipynb) for a discussion on algorithms and code solutions."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": true
|
|
},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"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.10"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 0
|
|
}
|