{ "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 the method draw_line(screen, width, x1, x2) where screen is a list of bytes, width is divisible by 8, and x1, x2 are absolute pixel positions.\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", "* Can we assume the inputs are valid?\n", " * No\n", "* For the output, do we set corresponding bits in screen?\n", " * Yes\n", "* Can we assume this fits memory?\n", " * Yes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Test Cases\n", "\n", "* Invalid inputs -> Exception\n", " * screen is empty\n", " * width = 0\n", " * any input param is None\n", " * x1 or x2 is out of bounds\n", "* General case for len(screen) = 20, width = 32:\n", " * x1 = 2, x2 = 6\n", " * screen[0] = int('00111110', base=2)\n", " * x1 = 68, x2 = 80\n", " * screen[8], int('00001111', base=2)\n", " * screen[9], int('11111111', base=2)\n", " * screen[10], int('10000000', base=2)" ] }, { "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 BitsScreen(object):\n", "\n", " def draw_line(self, screen, width, x1, x2):\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_draw_line.py\n", "from nose.tools import assert_equal\n", "\n", "\n", "class TestBitsScreen(object):\n", "\n", " def test_draw_line(self):\n", " bits_screen = BitsScreen()\n", " screen = []\n", " for _ in range(20):\n", " screen.append(int('00000000', base=2))\n", " bits_screen.draw_line(screen, width=32, x1=68, x2=80)\n", " assert_equal(screen[8], int('00001111', base=2))\n", " assert_equal(screen[9], int('11111111', base=2))\n", " assert_equal(screen[10], int('10000000', base=2))\n", " bits_screen.draw_line(screen, width=32, x1=2, x2=6)\n", " assert_equal(screen[0], int('00111110', base=2))\n", " bits_screen.draw_line(screen, width=32, x1=10, x2=13)\n", " assert_equal(screen[1], int('00111100', base=2))\n", " print('Success: test_draw_line')\n", "\n", "\n", "def main():\n", " test = TestBitsScreen()\n", " test.test_draw_line()\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 }