{ "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 magic index in an array, where array[i] = i.\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", "* Is the array sorted?\n", " * Yes\n", "* Are the elements in the array distinct?\n", " * No\n", "* Does a magic index always exist?\n", " * No\n", "* If there is no magic index, do we just return -1?\n", " * Yes\n", "* Are negative values allowed in the array?\n", " * Yes\n", "* If there are multiple magic values, what do we return?\n", " * Return the left-most one\n", "* Can we assume this fits memory?\n", " * Yes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Test Cases\n", "\n", "* None input -> -1\n", "* Empty array -> -1\n", "\n", "
\n", "a[i] -4 -2 2 6 6 6 6 10\n", " i 0 1 2 3 4 5 6 7\n", "\n", "\n", "Result: 2\n", "\n", "
\n", "a[i] -4 -2 1 6 6 6 6 10\n", " i 0 1 2 3 4 5 6 7\n", "\n", "\n", "Result: 6\n", "\n", "
\n", "a[i] -4 -2 1 6 6 6 7 10\n", " i 0 1 2 3 4 5 6 7\n", "\n", "\n", "Result: -1" ] }, { "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 MagicIndex(object):\n", "\n", " def find_magic_index(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_find_magic_index.py\n", "from nose.tools import assert_equal\n", "\n", "\n", "class TestFindMagicIndex(object):\n", "\n", " def test_find_magic_index(self):\n", " magic_index = MagicIndex()\n", " assert_equal(magic_index.find_magic_index(None), -1)\n", " assert_equal(magic_index.find_magic_index([]), -1)\n", " array = [-4, -2, 2, 6, 6, 6, 6, 10]\n", " assert_equal(magic_index.find_magic_index(array), 2)\n", " array = [-4, -2, 1, 6, 6, 6, 6, 10]\n", " assert_equal(magic_index.find_magic_index(array), 6)\n", " array = [-4, -2, 1, 6, 6, 6, 7, 10]\n", " assert_equal(magic_index.find_magic_index(array), -1)\n", " print('Success: test_find_magic')\n", "\n", "\n", "def main():\n", " test = TestFindMagicIndex()\n", " test.test_find_magic_index()\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 }