{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Workbook for Chapter 5 of Explorations in Computing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Section 5.7: Recursive Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Execute these cells to initialize the notebook:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from PythonLabs.RecursionLab import *" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%matplotlib tk" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `rsearch` " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Enter the definitions of `bsearch` and `rsearch` from Figure 5.12 in the code cell below. You can either type the definitions shown in the figure, or copy and paste the code from `rsearch.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": [ "♦ Make a sorted list of random numbers and try a couple searches that succeed:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "♦ Repeat the command above, except search for an item not in the list." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "♦ If you want to watch the progress of the search as it runs, add this line right after `rsearch` has computed the value of `mid`:\n", "
\n",
"print_bsearch_brackets(a,lower,mid,upper)\n",
"\n",
"(`print_bsearch_brackets` is defined in RecursionLab, which you already imported with the first code cell in this notebook).\n",
"Type ⌃⏎ to put the changes into effect and repeat the experiments."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### `qsort` "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Enter the definitions of `qsort` and its helper function `qs` from Figure 5.13 in the code cell below. You can either type the definitions shown in the figure, or copy and paste the code from `qsort.py`.\n",
"\n",
"After the definition is complete type ⌃⏎ to execute the cell to make the function available in this notebook."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"♦ Create a list `a` with the numbers 30, 49, 65, 20, 11, 6, 4, 19:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"♦ Since 30 is the first item in the list it will be used as the pivot. A call to `partition` will move the 30 somewhere to the middle, put numbers less than 30 at the front of the list, and put numbers greater than 30 at the end:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"♦ Print the updated list to verify that the pivot value 30 is now in `a[5]` and that everything in `a[0]` to `a[4]` is less than 30 and everything in `a[6]` to `a[7]` is greater than 30:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"♦ The first recursive call will sort the sublist from 0 to 4. Call partition again, but this time pass it the boundaries for the sublist:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"♦ Print the list again:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"♦ Retype the command that creates the test list:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"♦ Display the list on the canvas:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"♦ Slow down the animation by setting a 1-second delay between steps:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"♦ Call `partition`, using the entire list as the region to sort:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"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
}