diff options
author | hardythe1 | 2015-05-05 14:21:39 +0530 |
---|---|---|
committer | hardythe1 | 2015-05-05 14:21:39 +0530 |
commit | fba055ce5aa0955e22bac2413c33493b10ae6532 (patch) | |
tree | be70ef4fccd07c9c88de778014219201b4ea971f /The_Spirit_of_C/Chapter11.ipynb | |
parent | 67068710030ddd6b6c809518c34af2e04e0bf7ca (diff) | |
download | Python-Textbook-Companions-fba055ce5aa0955e22bac2413c33493b10ae6532.tar.gz Python-Textbook-Companions-fba055ce5aa0955e22bac2413c33493b10ae6532.tar.bz2 Python-Textbook-Companions-fba055ce5aa0955e22bac2413c33493b10ae6532.zip |
add books
Diffstat (limited to 'The_Spirit_of_C/Chapter11.ipynb')
-rwxr-xr-x | The_Spirit_of_C/Chapter11.ipynb | 383 |
1 files changed, 383 insertions, 0 deletions
diff --git a/The_Spirit_of_C/Chapter11.ipynb b/The_Spirit_of_C/Chapter11.ipynb new file mode 100755 index 00000000..241e1c1e --- /dev/null +++ b/The_Spirit_of_C/Chapter11.ipynb @@ -0,0 +1,383 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:3a9b6dffdf9d20cf5158c028c4012a0a2cf4ee76994f18e9b81499931df7cb5d" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 11, Pointers and Indirection" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program 11-1, Page Number : 278" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Introduction to pointers\n", + "\n", + "# There is no concept of pointers in python\n", + "i = 3\n", + "\n", + "j = id(i)\t# id() returns the address of the variable supplied\n", + "j = i \t\t# assign value of i to j such that it seems like j points to i\n", + "k = j\n", + "j = 4\n", + "\n", + "print \"i = %d, *j = %d, k = %d\"%(i,j,k)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "i = 3, *j = 4, k = 3\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program 11-2, Page Number: 280" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Demonstration of passing by reference for a function to be able to modify its parameters\n", + "\n", + "def modify(i) :\n", + "\ti = 3\n", + "\treturn i\n", + "\n", + "i = 1\n", + "print \"i is now equal to %d\"%i\n", + "\n", + "i = modify(i)\t\t\t\n", + "print \"i is now equal to %d\"%i" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "i is now equal to 1\n", + "i is now equal to 3\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program 11-3, Page Number: 281" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Pointer parameters in action: the swap routine\n", + "\n", + "def swap(i,j) :\n", + "\t\"\"\" exchange i with j and return \"\"\"\n", + "\ttemp = i \t\t\t# temp variable can be avoided using i,j = j,i\n", + "\ti = j\n", + "\tj = temp\n", + "\treturn i,j\n", + "\n", + "i = 1\n", + "j = 9\n", + "print \"i = %d and j = %d\"%(i,j)\n", + "\n", + "# Now exchange them\n", + "i, j = swap(i,j)\n", + "print \"\\nnow i = %d and j = %d\"%(i,j)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "i = 1 and j = 9\n", + "\n", + "now i = 9 and j = 1\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program 11-4, Page Number: 282" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Array names are constant pointers\n", + "\n", + "an_array = [ 3, 7 ]\n", + "\n", + "def main() :\n", + "\talso = an_array\n", + "\talso[0] = 14\n", + "\tprint \"an_array = { %d, %d }\"%(an_array[0],an_array[1])\n", + "\n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "an_array = { 14, 7 }\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program 11-5, Page Number: 284" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Sorting an array revisited\n", + "# Passing an array as parameter\n", + "\n", + "# Macro definition\n", + "MAX_ARRAY_SIZE = 20\t\t# The array may have up to 20 elements\n", + "# Message flags for \"print array\"\n", + "ORIGINAL = 0\t# Display original array\n", + "SORTED = 1\t\t# Display sorted array\n", + "\n", + "numbers = [ 10, 42, 7, 0, 923, 12, -5, 6, 19, 3 ]\n", + "array_size = 0\n", + "\n", + "def get_size(size)\t:\n", + "\t\"\"\" reads the size of the array (until a valid value is entered) \"\"\"\n", + "\tglobal array_size\n", + "\tprint \"How many numbers?\",\n", + "\tsize = 10\n", + "\tprint size\n", + "\twhile size < 1 or size > MAX_ARRAY_SIZE\t:\n", + "\t\tprint \"How many numbers?\",\n", + "\t\tsize = 10\n", + "\t\tprint size\n", + "\tarray_size = size\n", + "\n", + "def get_array(array, size) :\n", + "\t\"\"\" read in the array called 'array' of size 'size' \"\"\"\n", + "\tprint \"Please enter the integers:\"\n", + "\tfor i in array :\n", + "\t\tprint i,\n", + "\tprint\n", + "\n", + "def print_array(array, size, message) :\n", + "\t\"\"\" print out the array on a single line. 'message' is a flag indicating which message to display(ORIGINAL or SORTED) \"\"\"\n", + "\tif message == ORIGINAL :\n", + "\t\tprint \"\\nThe original array is:\"\n", + "\telse :\n", + "\t\tprint \"\\nThe sorted array is:\"\n", + "\tfor i in array :\n", + "\t\tprint i,\n", + "\tprint\n", + "\n", + "def sort_array(array,size) :\n", + "\t\"\"\" sort 'array' of size 'size' \"\"\"\n", + "\tfor i in range(size - 1) :\n", + "\t\tfor j in range(i+1, size) :\n", + "\t\t\tif array[i] > array[j] :\n", + "\t\t\t\telement_swap(array, i, j)\n", + "\n", + "def element_swap(array, i, j) :\n", + "\t\"\"\" swap elements 'i' and 'j' of 'array' \"\"\"\n", + "\tglobal numbers\n", + "\tnumbers[i],numbers[j] = numbers[j],numbers[i]\n", + "\n", + "\n", + "get_size(array_size)\t\t\t\t\t\t# Read in the array's size\n", + "get_array(numbers, array_size)\t\t\t\t\t# Read in the array 'numbers' of size 'array_size'\n", + "print_array(numbers, array_size, ORIGINAL) \t# Echo the array \n", + "sort_array(numbers, array_size)\n", + "print_array(numbers, array_size, SORTED)\t\t# Print the sorted array" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many numbers? 10\n", + "Please enter the integers:\n", + "10 42 7 0 923 12 -5 6 19 3\n", + "\n", + "The original array is:\n", + "10 42 7 0 923 12 -5 6 19 3\n", + "\n", + "The sorted array is:\n", + "-5 0 3 6 7 10 12 19 42 923\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program 11-6, Page Number: 294" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Sorting of array revisited\n", + "# Two dimensional char array (array of strings)\n", + "\n", + "# Macro definition\n", + "MAX_ARRAY_SIZE = 20\t\t# The array may have up to 20 elements\n", + "MAX_STRING_SIZE = 10 \t# Each string may have up to 10 characters\n", + "# Message flags for \"print_array\"\n", + "ORIGINAL = 0\n", + "SORTED = 1\n", + "\n", + "words = [ \"cat\", \"pig\", \"dog\", \"earlobe\", \"bingo\", \"elbow\", \"likely\", \"radar\" ]\n", + "array_size = 0\n", + "\n", + "def get_size(size) :\n", + "\t\"\"\" Read the size of the array into size (until a value is entered) \"\"\"\n", + "\tprint \"How many words? \",\n", + "\tsize = 8\n", + "\tprint size\n", + "\twhile size < 1 or size > MAX_ARRAY_SIZE :\n", + "\t\tprint \"How many words? \",\n", + "\t\tsize = 8\n", + "\t\tprint size\n", + "\tglobal array_size\n", + "\tarray_size = size\n", + "\n", + "def get_array(array,size) :\n", + "\t\"\"\" Read in the string array called 'array' of size 'size' \"\"\"\n", + "\tprint \"Please enter the words:\"\n", + "\tfor i in array :\n", + "\t\tprint i\n", + "\t\n", + "def print_array(array,size,message) :\n", + "\t\"\"\" Print out string array 'array' of size 'size'. 'message' is a flag indicating which message to display(ORIGINAL or SORTED) \"\"\"\n", + "\tif message == ORIGINAL :\n", + "\t\tprint \"\\nThe original array is:\"\n", + "\telse :\n", + "\t\tprint \"\\nThe sorted array is:\"\n", + "\tfor i in array :\n", + "\t\tprint i\n", + "\n", + "def sort_array(array,size) :\n", + "\t\"\"\" sort string array \"\"\"\n", + "\tfor i in range (size-1) :\n", + "\t\tfor j in range (i + 1, size) :\n", + "\t\t\tif array[i] > array[j] :\n", + "\t\t\t\tstring_swap(i,j)\n", + "\n", + "def string_swap(i, j) :\n", + "\t\"\"\" Exchange strings \"\"\"\n", + "\tglobal words\n", + "\twords[i],words[j] = words[j],words[i]\n", + "\n", + "get_size(array_size)\t\t# Read in the array size\n", + "get_array(words, array_size)\t# Read in the string array \"words\" of size \"array_size\"\n", + "print_array(words, array_size, ORIGINAL)\t# Echo the original array\n", + "sort_array(words, array_size)\t\n", + "print_array(words, array_size, SORTED) # Print the sorted array" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many words? 8\n", + "Please enter the words:\n", + "cat\n", + "pig\n", + "dog\n", + "earlobe\n", + "bingo\n", + "elbow\n", + "likely\n", + "radar\n", + "\n", + "The original array is:\n", + "cat\n", + "pig\n", + "dog\n", + "earlobe\n", + "bingo\n", + "elbow\n", + "likely\n", + "radar\n", + "\n", + "The sorted array is:\n", + "bingo\n", + "cat\n", + "dog\n", + "earlobe\n", + "elbow\n", + "likely\n", + "pig\n", + "radar\n" + ] + } + ], + "prompt_number": 6 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file |