{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Workbook for Chapter 4 of Explorations in Computing"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Section 4.6: Scalability"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Execute this cell to initialize the notebook:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from PythonLabs.IterationLab import *"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Exercises "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T57. Type the statement that creates the short list of strings used in earlier experiments:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T58. Use `isearch` to find the location of the string `He`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T59. Find out how many comparisons were made in this search:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T60. Do a search for a string that is not in the list and check the number of comparisons:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T61. Type the line that combines two operations into a single Python statement. This single line will make a random list and search for a random element in that list:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T62. Execute the previous code cell several more times. Are all your results numbers between 0 and 9?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T63. Copy the previous command to the code cell below, then change the call to `isearch` so it looks for an item not in the list. Execute the statement several times."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T64. Ask Python to show the number of comparisons made by the failed search:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T65. The following code cell has been filled in for you. It contains three Python statements all on a single line, separated by semicolons. These statements\n",
"* make a random list named `a` containing 10 different numbers\n",
"* sort `a` using `isort`\n",
"* print the number of comparisons required to do the sort\n",
"\n",
"Type ⌃⏎ in the cell several times and make a record of how many comparisons are made each time."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"a = RandomList(10); isort(a); Counter.value('comparisons')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T66. Because $n$, the length of the list, is 10 in this experiment, the number of comparisons should always be less than $10^2 / 2 = 50$. Is that the case?"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T67. Copy the three commands above to the following code cell, change the list size from 10 to 100, and execute the cell several times."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T68. Use the code cells below to repeat the experiment, sorting lists of 200, 400, and 1,000 items."
]
},
{
"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": [
"T69. Earlier the claim was made that doubling the size of the input list should quadruple the number of comparisons. Is the average count for $n = 200$ approximately four times the average count for $n = 100$? Is the average for $n = 800$ approximately four times the average for $n = 200$?"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T70. When you increase the size of the list by 10 the number of comparisons should increase by a factor of 100. Is the average count for $n = 1,000$ around 100 times greater than the average count for $n = 100$?"
]
},
{
"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
}