{ "metadata": { "name": "Chapter 13" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": "Chapter 13: Simple pointers" }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "Example 13.1, Page number: 225" }, { "cell_type": "code", "collapsed": false, "input": "# Example 13.1.py\n# To print the value of 'thing_var' and 'thing_ptr'\n\n\n# Variable declaration\nthing_var = 2\n\n# Calculation and result\nprint ('Thing %d' % thing_var)\n\nthing_ptr = thing_var\nthing_ptr = 3\nprint ('Thing %d' % thing_ptr)", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "Thing 2\nThing 3\n" } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "Example 13.2, Page number: 227" }, { "cell_type": "code", "collapsed": false, "input": "# Example 13.2.py\n# To loop through the value of 'count' from 1 to 10\n\n\n# Function declaration\ndef inc_count (count_ptr) :\n count_ptr += 1\n return count_ptr\n\n# Calculation and result\ncount = 0\n\nwhile (count < 10) :\n print ('Count %d\\n' % inc_count (count))\n count += 1", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "Count 1\n\nCount 2\n\nCount 3\n\nCount 4\n\nCount 5\n\nCount 6\n\nCount 7\n\nCount 8\n\nCount 9\n\nCount 10\n\n" } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "Example 13.3, Page number: 230" }, { "cell_type": "code", "collapsed": false, "input": "# Example 13.3.py\n# To print the addresses and elements of a character array\n\n\n# Variable declaration\narray = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']\n\n# Calculation and result\nprint ('&array[index] (array+index) array[index]\\n')\n\nfor index in range (0, 10) :\n print ('0x%s 0x%s 0x%s\\n' % (id (array[index]), id (array[index]), array[index]))", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "&array[index] (array+index) array[index]\n\n0x20916008 0x20916008 0x0\n\n0x20270280 0x20270280 0x1\n\n0x20733728 0x20733728 0x2\n\n0x20916464 0x20916464 0x3\n\n0x20270232 0x20270232 0x4\n\n0x20733560 0x20733560 0x5\n\n0x20270256 0x20270256 0x6\n\n0x20733752 0x20733752 0x7\n\n0x20916512 0x20916512 0x8\n\n0x20916032 0x20916032 0x9\n\n" } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "Example 13.4, Page number: 232" }, { "cell_type": "code", "collapsed": false, "input": "# Example 13.4.py\n# To count the number of elements before zero in an array\n\n\n# Variable declaration\narray = [4, 5, 8, 9, 8, 1, 0, 1, 9, 3]\nindex = 0\n\n# Calculation and result\nwhile (array[index] != 0) :\n index += 1\n\nprint ('Number of elements before zero %d\\n' % index)", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "Number of elements before zero 6\n\n" } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "Example 13.5, Page number: 232" }, { "cell_type": "code", "collapsed": false, "input": "# Example 13.5.py\n# To count the number of elements before zero in an array\n\n\n# Variable declaration\narray = [4, 5, 8, 9, 8, 1, 0, 1, 9, 3]\nindex = 0\n\n# Calculation and result\nwhile (array[index] != 0) :\n index += 1\n\nprint ('Number of elements before zero %d\\n' % index)", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "Number of elements before zero 6\n\n" } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "Example 13.6, Page number: 233" }, { "cell_type": "code", "collapsed": false, "input": "# Example 13.6.py\n# To initialize the elements of an array to 0\n\n\n# Variable declaration\ndata = []\n\n# Calculation and result\nfor index in range (0, 10) :\n data.append(0)\nprint (data)", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n" } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "Example 13.10, Page number: 238" }, { "cell_type": "code", "collapsed": false, "input": "# Example 13.10.py\n# To split a string of the form 'First/Last' into two strings\n\n\n# Variable declaration\nline = 'Steve/Oualline'\n\n# Calculation and result\nfirst_ptr, last_ptr = line.split('/')\nprint ('First: %s Last: %s\\n' % (first_ptr, last_ptr))", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "First: Steve Last: Oualline\n\n" } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "Example 13.11, Page number: 240" }, { "cell_type": "code", "collapsed": false, "input": "# Example 13.11.py\n# To return a temporary filename\n\n\n# Function declaration\ndef tmp_name() :\n if not hasattr (tmp_name, 'sequence') :\n tmp_name.sequence = 0\n tmp_name.sequence += 1\n name = 'tmp'\n name += str (tmp_name.sequence)\n return name\n\n# Calculation and result\nprint ('Name: %s\\n' % tmp_name())", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "Name: tmp1\n\n" } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "Example 13.12, Page number: 245" }, { "cell_type": "code", "collapsed": false, "input": "# Example 13.12.py\n# To format files for printing\n\n\n# Variable and function declaration\nimport sys\nv_count = sys.argv\ncounter = len(sys.argv)\n\n# Produces verbose messages\nverbose = 0\n\n# Sends output to a file\nout_file = open ('print.out', 'w')\n\n# Sets the number of lines per page\nline_max = 66\n\ndef do_file (name) :\n print ('Verbose %d Lines %d Input %s Output %s\\n' % (verbose, line_max, name, out_file.name))\n\ndef usage() :\n print ('Usage is %s [options] [file-list]\\n' % program_name)\n print ('Options\\n')\n print (' -v verbose\\n')\n print (' -l Number of lines\\n')\n print (' -o Set output filename\\n')\n sys.exit(1)\n \n# Calculation and result\nprogram_name = str (sys.argv[0])\n\nwhile ((counter > 1) and (sys.argv[1][0] == '-')) :\n if (sys.argv[1][1] == 'v') :\n verbose = 1\n break\n\n elif (sys.argv[1][1] == 'o') :\n temp = str (sys.argv[1])\n out_file.write (temp)\n break\n\n elif (sys.argv[1][1] == 'l') :\n line_max = int (sys.argv[1][2])\n break\n\n else :\n print ('Bad option %s\\n' % sys.argv[1])\n usage()\n\n for index in range (0, counter) :\n if (index == counter - 1) :\n break\n else :\n v_count[index] = v_count[index + 1]\n\n counter -= 1\n\nif (counter == 1) :\n do_file ('print.in')\n\nelse :\n while (counter > 1) :\n do_file (sys.argv[1])\n\n for index in range (0, counter) :\n if (index == counter - 1) :\n break\n else :\n v_count[index] = v_count[index + 1]\n \n counter -= 1\n\nout_file.close()", "language": "python", "metadata": {}, "outputs": [ { "ename": "SystemExit", "evalue": "1", "output_type": "pyerr", "traceback": [ "An exception has occurred, use %tb to see the full traceback.\n", "\u001b[1;31mSystemExit\u001b[0m\u001b[1;31m:\u001b[0m 1\n" ] }, { "output_type": "stream", "stream": "stdout", "text": "Bad option -f\n\nUsage is -c [options] [file-list]\n\nOptions\n\n -v verbose\n\n -l Number of lines\n\n -o Set output filename\n\n" }, { "output_type": "stream", "stream": "stderr", "text": "To exit: use 'exit', 'quit', or Ctrl-D." } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "Example 13.13, Page number: 248" }, { "cell_type": "code", "collapsed": false, "input": "# Example 13.13.py\n# To return a new temporary filename\n\n\n# Function declaration\ndef tmp_name() :\n if not hasattr (tmp_name, 'sequence') :\n tmp_name.sequence = 0\n tmp_name.sequence += 1\n name = 'tmp'\n name += str (tmp_name.sequence)\n return name\n\n# Calculation and result\nname1 = tmp_name()\nname2 = tmp_name()\nprint ('Name1: %s\\n' % name1)\nprint ('Name2: %s\\n' % name2)", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "Name1: tmp1\n\nName2: tmp2\n\n" } ], "prompt_number": 11 } ], "metadata": {} } ] }