{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Workbook for Chapter 5 of Explorations in Computing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Section 5.2: Implementing Binary Search" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `bsearch` " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Enter the definition of `bsearch` (Figure 5.6) in the code cell below. You can either type the definition shown in the figure, or copy and paste the code from `bsearch.py`.\n", "\n", "After the definition is complete type ⌃⏎ to execute the cell to make the function available in this notebook." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Exercises " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T8. To see what the `print_bsearch_brackets` helper does, import it into your interactive session:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T9. Define the same list of strings used in tests in the previous section:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T10. Call the helper, telling it the lower boundary is 1, the midpoint is 3, and the upper boundary is 5:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T11. Call `print_bsearch_brackets` again, this time passing it the actual values of the boundary points that will be used on the first iteration of the search:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T12. Call `bsearch` (the function you defined above) a few times to make sure it is working:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T13. Edit the function definition to uncomment the call to the helper function. Type ⌃⏎ to execute the cell so the notebook uses the updated definition." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T14. Repeat the searches above. In each case notice how the search region changes on each iteration." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T15. Try a search that fails:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "♦ Create some large test lists and try some successful searches:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "♦ The number of iterations (which you can figure out by counting the number of lines printed) should vary from 1 to 5. Is that what you see?" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "♦ Do some unsuccessful searches:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "♦ These searches should always make five iterations. Can you explain why?" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [] } ], "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.4.2" } }, "nbformat": 4, "nbformat_minor": 0 }