mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-08 18:38:03 +00:00
Rework n pairs parens challenge (#145)
This commit is contained in:
@@ -18,7 +18,7 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Problem: Print all valid combinations of n-pairs of parentheses.\n",
|
||||
"## Problem: Find all valid combinations of n-pairs of parentheses.\n",
|
||||
"\n",
|
||||
"* [Constraints](#Constraints)\n",
|
||||
"* [Test Cases](#Test-Cases)\n",
|
||||
@@ -33,7 +33,17 @@
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Constraints\n",
|
||||
"* None"
|
||||
"\n",
|
||||
"* Is the input an integer representing the number of pairs?\n",
|
||||
" * Yes\n",
|
||||
"* Can we assume the inputs are valid?\n",
|
||||
" * No\n",
|
||||
"* Is the output a list of valid combinations?\n",
|
||||
" * Yes\n",
|
||||
"* Should the output have duplicates?\n",
|
||||
" * No\n",
|
||||
"* Can we assume this fits memory?\n",
|
||||
" * Yes"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -42,10 +52,14 @@
|
||||
"source": [
|
||||
"## Test Cases\n",
|
||||
"\n",
|
||||
"<pre>\n",
|
||||
"* None -> Exception\n",
|
||||
"* Negative -> Exception\n",
|
||||
"* 0 -> []\n",
|
||||
"* 1 -> [()]\n",
|
||||
"* 2 -> [(()), ()()]\n",
|
||||
"* 3 -> [((())), (()()), (())(), ()(()), ()()()]"
|
||||
"* 1 -> ['()']\n",
|
||||
"* 2 -> ['(())', '()()']\n",
|
||||
"* 3 -> ['((()))', '(()())', '(())()', '()(())', '()()()']\n",
|
||||
"</pre>"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -72,17 +86,11 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def parentheses_util(no_left, no_right, pair_string, result):\n",
|
||||
" # TODO: implement parentheses pairing here\n",
|
||||
" pass\n",
|
||||
"class Parentheses(object):\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"
|
||||
" def find_pair(self, num_pairs):\n",
|
||||
" # TODO: implement me\n",
|
||||
" pass"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -101,27 +109,30 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# %load test_n_pairs_parentheses.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"from nose.tools import assert_equal, assert_raises\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",
|
||||
" def test_pair_parentheses(self):\n",
|
||||
" parentheses = Parentheses()\n",
|
||||
" assert_raises(TypeError, parentheses.find_pair, None)\n",
|
||||
" assert_raises(ValueError, parentheses.find_pair, -1)\n",
|
||||
" assert_equal(parentheses.find_pair(0), [])\n",
|
||||
" assert_equal(parentheses.find_pair(1), ['()'])\n",
|
||||
" assert_equal(parentheses.find_pair(2), ['(())',\n",
|
||||
" '()()'])\n",
|
||||
" assert_equal(parentheses.find_pair(3), ['((()))',\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",
|
||||
" test.test_pair_parentheses()\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"if __name__ == '__main__':\n",
|
||||
@@ -136,34 +147,25 @@
|
||||
"\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",
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python2"
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 2
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.10"
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
Reference in New Issue
Block a user