{ "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: Implement an algorithm to have a robot move from the upper left corner to the bottom right corner of a grid.\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 there restrictions to how the robot moves?\n", " * The robot can only move right and down\n", "* Are some cells off limits?\n", " * Yes\n", "* Is this a rectangular grid? i.e. the grid is not jagged?\n", " * Yes\n", "* Will there always be a valid way for the robot to get to the bottom right?\n", " * No, return None\n", "* Can we assume the inputs are valid?\n", " * No\n", "* Can we assume this fits memory?\n", " * Yes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Test Cases\n", "\n", "
\n", "o = valid cell\n", "x = invalid cell\n", "\n", " 0 1 2 3\n", "0 o o o o\n", "1 o x o o\n", "2 o o x o\n", "3 x o o o\n", "4 o o x o\n", "5 o o o x\n", "6 o x o x\n", "7 o x o o\n", "\n", "\n", "* General case\n", "\n", "```\n", "expected = [(0, 0), (1, 0), (2, 0),\n", " (2, 1), (3, 1), (4, 1),\n", " (5, 1), (5, 2), (6, 2), \n", " (7, 2), (7, 3)]\n", "```\n", "\n", "* No valid path: In above example, row 7 col 2 is also invalid -> None\n", "* None input -> None\n", "* Empty matrix -> None" ] }, { "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 Grid(object):\n", "\n", " def find_path(self, matrix):\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_grid_path.py\n", "from nose.tools import assert_equal\n", "\n", "\n", "class TestGridPath(object):\n", "\n", " def test_grid_path(self):\n", " grid = Grid()\n", " assert_equal(grid.find_path(None), None)\n", " assert_equal(grid.find_path([[]]), None)\n", " max_rows = 8\n", " max_cols = 4\n", " matrix = [[1] * max_cols for _ in range(max_rows)]\n", " matrix[1][1] = 0\n", " matrix[2][2] = 0\n", " matrix[3][0] = 0\n", " matrix[4][2] = 0\n", " matrix[5][3] = 0\n", " matrix[6][1] = 0\n", " matrix[6][3] = 0\n", " matrix[7][1] = 0\n", " result = grid.find_path(matrix)\n", " expected = [(0, 0), (1, 0), (2, 0),\n", " (2, 1), (3, 1), (4, 1),\n", " (5, 1), (5, 2), (6, 2), \n", " (7, 2), (7, 3)]\n", " assert_equal(result, expected)\n", " matrix[7][2] = 0\n", " result = grid.find_path(matrix)\n", " assert_equal(result, None)\n", " print('Success: test_grid_path')\n", "\n", "\n", "def main():\n", " test = TestGridPath()\n", " test.test_grid_path()\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 }