Add longest increasing subseq challenge

This commit is contained in:
Donne Martin
2017-03-28 05:02:00 -04:00
parent c9510e71b6
commit f6abe150e3
4 changed files with 422 additions and 0 deletions

View File

@@ -0,0 +1,169 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Challenge Notebook"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem: Find the longest increasing subsequence.\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",
"\n",
"* Are duplicates possible?\n",
" * Yes\n",
"* Can we assume the inputs are integers?\n",
" * Yes\n",
"* Can we assume the inputs are valid?\n",
" * No\n",
"* Do we expect the result to be an array of the longest increasing subsequence?\n",
" * Yes\n",
"* Can we assume this fits memory?\n",
" * Yes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"* None -> Exception\n",
"* [] -> []\n",
"* [3, 4, -1, 0, 6, 2, 3] -> [-1, 0, 2, 3]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
"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."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"class Subsequence(object):\n",
"\n",
" def longest_inc_subseq(self, seq):\n",
" # TODO: Implement me\n",
" pass"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Unit Test"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**The following unit test is expected to fail until you solve the challenge.**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# %load test_longest_increasing_subseq.py\n",
"from nose.tools import assert_equal, assert_raises\n",
"\n",
"\n",
"class TestLongestIncreasingSubseq(object):\n",
"\n",
" def test_longest_increasing_subseq(self):\n",
" subseq = Subsequence()\n",
" assert_raises(TypeError, subseq.longest_inc_subseq, None)\n",
" assert_equal(subseq.longest_inc_subseq([]), [])\n",
" seq = [3, 4, -1, 0, 6, 2, 3]\n",
" expected = [-1, 0, 2, 3]\n",
" assert_equal(subseq.longest_inc_subseq(seq), expected)\n",
" print('Success: test_longest_increasing_subseq')\n",
"\n",
"\n",
"def main():\n",
" test = TestLongestIncreasingSubseq()\n",
" test.test_longest_increasing_subseq()\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Solution Notebook\n",
"\n",
"Review the [Solution Notebook]() for a discussion on algorithms and code solutions."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}