{ "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: Given a list of tuples representing ranges, condense the ranges. \n", "\n", "Example: [(2, 3), (3, 5), (7, 9), (8, 10)] -> [(2, 5), (7, 10)]\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 the tuples in sorted order?\n", " * No\n", "* Are the tuples ints?\n", " * Yes\n", "* Will all tuples have the first element less than the second?\n", " * Yes\n", "* Is there an upper bound on the input range?\n", " * No\n", "* Is the output a list of tuples?\n", " * Yes\n", "* Is the output a new array?\n", " * Yes\n", "* Can we assume the inputs are valid?\n", " * No, check for None\n", "* Can we assume this fits memory?\n", " * Yes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Test Cases\n", "\n", "
\n",
    "* None input -> TypeError\n",
    "* [] - []\n",
    "* [(2, 3), (7, 9)] -> [(2, 3), (7, 9)]\n",
    "* [(2, 3), (3, 5), (7, 9), (8, 10)] -> [(2, 5), (7, 10)]\n",
    "* [(2, 3), (3, 5), (7, 9), (8, 10), (1, 11)] -> [(1, 11)]\n",
    "* [(2, 3), (3, 8), (7, 9), (8, 10)] -> [(2, 10)]\n",
    "
" ] }, { "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 Solution(object):\n", "\n", " def merge_ranges(self, array):\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_merge_ranges.py\n", "from nose.tools import assert_equal, assert_raises\n", "\n", "\n", "class TestMergeRanges(object):\n", "\n", " def test_merge_ranges(self):\n", " solution = Solution()\n", " assert_raises(TypeError, solution.merge_ranges, None)\n", " assert_equal(solution.merge_ranges([]), [])\n", " array = [(2, 3), (7, 9)]\n", " expected = [(2, 3), (7, 9)]\n", " assert_equal(solution.merge_ranges(array), expected)\n", " array = [(2, 3), (3, 5), (7, 9), (8, 10)]\n", " expected = [(2, 5), (7, 10)]\n", " assert_equal(solution.merge_ranges(array), expected)\n", " array = [(2, 3), (3, 5), (7, 9), (8, 10), (1, 11)]\n", " expected = [(1, 11)]\n", " assert_equal(solution.merge_ranges(array), expected)\n", " print('Success: test_merge_ranges')\n", "\n", "\n", "def main():\n", " test = TestMergeRanges()\n", " test.test_merge_ranges()\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 }