{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 7: Pointers" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 7.1, page no. 266" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "A simple program using pointers\n", "note: there are no pointers in Python.\n", "\"\"\"\n", "\n", "import sys\n", "\n", "number = 10;\n", "print \"number's address: \", id(number)\n", "print \"number's value: \", number\n", " \n", "pnumber = id(number)\n", "print \"pnumber's address: \", id(pnumber)\n", "print \"pnumber's size: %d bytes\" %(sys.getsizeof(pnumber))\n", "print \"pnumber's value: \", pnumber\n", "print \"value pointed to: \", number" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "number's address: 42843088\n", "number's value: 10\n", "pnumber's address: 54575640\n", "pnumber's size: 24 bytes\n", "pnumber's value: 42843088\n", "value pointed to: 10\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 7.2, page no. 270" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "What's the pointer of it all\n", "\"\"\"\n", "\n", "num1 = 0\n", "num2 = 0\n", "pnum = None\n", " \n", "pnum = id(num1)\n", "num1 = 2\n", "num2 += 1\n", "num2 += num1\n", "pnum = id(num2)\n", "num2 += 1\n", "print \"num1 = %d num2 = %d pnum = %d pnum + num2 = %d\" %(num1, num2, pnum, pnum + num2)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "num1 = 2 num2 = 4 pnum = 42843256 pnum + num2 = 42843260\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 7.3, page no. 272" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "Using pointer arguments to scanf_s\n", "note: in Python you cannot use pointers to input a value\n", "\"\"\"\n", "\n", "print \"Input an integer: \",\n", "value = int(raw_input())\n", "print \"You entered: \", value" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Input an integer: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "7\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " You entered: 7\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 7.4, page no. 276" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "Arrays and pointers\n", "\"\"\"\n", "\n", "multiple = ['M', 'y','s','t', 'r', 'i', 'n', 'g']\n", " \n", "p = id(multiple[0])\n", "print \"The address of the first array element : \", p\n", "p = id(multiple)\n", "print \"The address obtained from the array name: \", p" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The address of the first array element : 140170253709440\n", "The address obtained from the array name: 58009432\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 7.5, page no. 277" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "Incrementing a pointer to an array\n", "\"\"\"\n", "\n", "multiple = \"a string\"\n", "p = multiple\n", " \n", "for i in range(len(multiple)):\n", " print \"multiple[%d] = %c *(p+%d) = %c id(multiple[%d]) = %d p+%d = %d \" %(i, multiple[i], i, p[i], i, id(multiple[i]), i, id(p[i]))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "multiple[0] = a *(p+0) = a id(multiple[0]) = 140170254071168 p+0 = 140170254071168 \n", "multiple[1] = *(p+1) = id(multiple[1]) = 140170254278248 p+1 = 140170254278248 \n", "multiple[2] = s *(p+2) = s id(multiple[2]) = 140170254071048 p+2 = 140170254071048 \n", "multiple[3] = t *(p+3) = t id(multiple[3]) = 140170254070208 p+3 = 140170254070208 \n", "multiple[4] = r *(p+4) = r id(multiple[4]) = 140170254275768 p+4 = 140170254275768 \n", "multiple[5] = i *(p+5) = i id(multiple[5]) = 140170254277248 p+5 = 140170254277248 \n", "multiple[6] = n *(p+6) = n id(multiple[6]) = 140170254276568 p+6 = 140170254276568 \n", "multiple[7] = g *(p+7) = g id(multiple[7]) = 140170254073528 p+7 = 140170254073528 \n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 7.6, page no. 277" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "Incrementing a pointer to an array of integers\n", "\"\"\"\n", "\n", "import sys\n", "\n", "multiple = [15, 25, 35, 45]\n", "p = multiple\n", " \n", "for i in range(sys.getsizeof(multiple)/sys.getsizeof(multiple[0])):\n", " print \"address p+%d (id(multiple[%d])): %d *(p+%d) value: %d\" %( i, i, id(p[i]), i, p[i])\n", " \n", "print \"Type integer occupies: %d bytes\" %sys.getsizeof(int())" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "address p+0 (id(multiple[0])): 42842968 *(p+0) value: 15\n", "address p+1 (id(multiple[1])): 42842728 *(p+1) value: 25\n", "address p+2 (id(multiple[2])): 42842488 *(p+2) value: 35\n", "address p+3 (id(multiple[3])): 42844240 *(p+3) value: 45\n", "Type integer occupies: 24 bytes\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 7.7, page no. 279" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "Two-dimensional arrays and pointers\n", "\"\"\"\n", "\n", "board = [['1','2','3'],\n", " ['4','5','6'],\n", " ['7','8','9']]\n", "print \"address of board : \", id(board)\n", "print \"address of board[0][0] : \", id(board[0][0])\n", "print \"value of board[0]: \", board[0]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "address of board : 58026608\n", "address of board[0][0] : 140170254070808\n", "value of board[0]: ['1', '2', '3']\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 7.7A, page no. 280" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "Two-dimensional arrays and pointers\n", "\"\"\"\n", "\n", "board = [['1','2','3'],\n", " ['4','5','6'],\n", " ['7','8','9']]\n", " \n", "print \"value of board[0][0] : %c\" % (board[0][0])\n", "print \"value of board[0]: \", board[0][0]\n", "print \"value of **board: \", board[0][0]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "value of board[0][0] : 1\n", "value of board[0]: 1\n", "value of **board: 1\n" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 7.8, page no. 281" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "Getting values in a two-dimensional array\n", "\"\"\"\n", "\n", "board = [['1','2','3'],\n", " ['4','5','6'],\n", " ['7','8','9']]\n", "\n", "for i in range(3):\n", " for j in range(3):\n", " print \" board: \", board[i][j]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " board: 1\n", " board: 2\n", " board: 3\n", " board: 4\n", " board: 5\n", " board: 6\n", " board: 7\n", " board: 8\n", " board: 9\n" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ " Program 7.9, page no. 283" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "Multidimensional arrays and pointers\n", "\"\"\"\n", "\n", "board = [['1','2','3'],\n", " ['4','5','6'],\n", " ['7','8','9']]\n", "\n", "for i in range(3):\n", " for j in range(3):\n", " print \" board: \", board[i][j]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " board: 1\n", " board: 2\n", " board: 3\n", " board: 4\n", " board: 5\n", " board: 6\n", " board: 7\n", " board: 8\n", " board: 9\n" ] } ], "prompt_number": 12 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 7.10, page no. 285" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "Understand pointers to your hat size - if you dare\n", "\"\"\"\n", "\n", "size = [['6', '6', '6', '6', '7', '7', '7', '7', '7', '7', '7', '7'],\n", " ['1', '5', '3', '7', ' ', '1', '1', '3', '1', '5', '3', '7'],\n", " ['2', '8', '4', '8', ' ', '8', '4', '8', '2', '8', '4', '8']]\n", "headsize = [164, 166, 169, 172, 175, 178, 181, 184, 188, 191, 194, 197]\n", "hat_found = False\n", "print \"Enter the circumference of your head above your eyebrows in inches as a decimal value: \",\n", "cranium = float(raw_input())\n", "your_head = int(8.0*cranium)\n", "i = 0\n", "if(your_head == headsize[i]):\n", " hat_found = True\n", "else:\n", " for i in range(1, len(headsize)):\n", " if(your_head > headsize[i - 1] and your_head <= headsize[i]):\n", " hat_found = True\n", " break\n", "if(hat_found):\n", " print \"Your hat size is %c %c%c%c\" %(size[0][i], size[1][i], ' ' if (size[1][i]==' ') else '/', size[2][i])\n", "else:\n", " if(your_head < headsize[0]):\n", " print \"You are the proverbial pinhead. No hat for you I'm afraid.\"\n", " else:\n", " print \"You, in technical parlance, are a fathead, No hat for you, I'm afraid.\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the circumference of your head above your eyebrows in inches as a decimal value: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "22.5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Your hat size is 7 1/4\n" ] } ], "prompt_number": 13 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 7.11, page no. 290" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "A dynamic prime example\n", "note: program 7.12 will remain same\n", "\"\"\"\n", "\n", "pPrimes = []\n", "found = False\n", " \n", "print \"How many primes would you like - you'll get at least 4? \",\n", "total = int(raw_input())\n", "total = 4 if total < 4 else total\n", " \n", "pPrimes.append(2) # First prime\n", "pPrimes.append(3) # Second prime\n", "pPrimes.append(5) # Third prime\n", "count = 3\n", "trial = 5\n", " \n", "while(count < total):\n", " trial += 2\n", " for i in range(1, count):\n", " if((trial % pPrimes[i]) == 0):\n", " found = False\n", " break\n", " else:\n", " found = True\n", " if(found):\n", " pPrimes.append(trial)\n", " count += 1\n", "\n", "for i in range(total):\n", " print \"\\t\", pPrimes[i],\n", " if(not((i+1) % 5)):\n", " print \"\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "How many primes would you like - you'll get at least 4? " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "25\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " \t2 \t3 \t5 \t7 \t11 \n", "\t13 \t17 \t19 \t23 \t29 \n", "\t31 \t37 \t41 \t43 \t47 \n", "\t53 \t59 \t61 \t67 \t71 \n", "\t73 \t79 \t83 \t89 \t97 \n" ] } ], "prompt_number": 14 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 7.13, page no. 300" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "Extending dynamically allocated memory for strings\n", "\"\"\"\n", "\n", "import sys\n", "\n", "print \"Enter text on an arbitrary number of lines (go on typing hit enter to terminate): \"\n", "print \"Should be less than 10000 characters: \"\n", "text = raw_input()\n", "\n", "if len(text) > 10000:\n", " print \"Maximum length exceeded, terminating...\"\n", " sys.exit()\n", "\n", "distinct_words = []\n", "word_occurrance = []\n", "list_text = text.split(\" \")\n", "for word in list_text:\n", " if not word in distinct_words:\n", " distinct_words.append(word)\n", " word_occurrance.append(0)\n", " \n", "for i in range(len(list_text)):\n", " if list_text[i] in distinct_words:\n", " index = distinct_words.index(list_text[i])\n", " word_occurrance[index] += 1\n", "\n", "for i in range(len(distinct_words)):\n", " if(i % 5 == 0):\n", " print \"\\n\"\n", " print distinct_words[i], \"\\t \", word_occurrance[i]," ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter text on an arbitrary number of lines (go on typing hit enter to terminate): \n", "Should be less than 10000 characters: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Peter Piper picked a peck of pickled pepper. A peck of pickled pepper Peter Piper picked. If Peter Piper picked a peck of pickled pepper, Where's the peck of pickled pepper Peter Piper picked?\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", "Peter \t 4 Piper \t 4 picked \t 2 a \t 2 peck \t 4 \n", "\n", "of \t 4 pickled \t 4 pepper. \t 1 A \t 1 pepper \t 2 \n", "\n", "picked. \t 1 If \t 1 pepper, \t 1 Where's \t 1 the \t 1 \n", "\n", "picked? \t 1\n" ] } ], "prompt_number": 15 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 7.14, page no. 306" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "Using array notation with pointers to sort strings\n", "\"\"\"\n", "\n", "print \"Enter strings to be sorted, separated by '.' Press Enter to end: \"\n", "text = raw_input()\n", "\n", "dot_separated = text.split('.')\n", "text_sorted = sorted(dot_separated)\n", "\n", "for str in text_sorted:\n", " print str" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter strings to be sorted, separated by '.' Press Enter to end: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Many a mickle makes a muckle. A fool and your money are soon partners. Every dog has his day. Do unto others before they do it to you. A nod is as good as a wink to a blind horse. The bigger they are, the harder they hit. Least said, soonest mended.\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", " A fool and your money are soon partners\n", " A nod is as good as a wink to a blind horse\n", " Do unto others before they do it to you\n", " Every dog has his day\n", " Least said, soonest mended\n", " The bigger they are, the harder they hit\n", "Many a mickle makes a muckle\n" ] } ], "prompt_number": 16 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 7.15, page no. 316" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "An improved calculator\n", "\"\"\"\n", "\n", "print \"To use this calculator, enter any expression with or without spaces.\"\n", "print \"An expression may include the operators\"\n", "print \" +, -, *, /, %, or **(raise to a power).\"\n", "print \"Use = at the beginning of a line to operate on \"\n", "print \"the result of the previous calculation.\"\n", "print \"Enter quit to stop the calculator.\"\n", "\n", "result = 0\n", "while True:\n", " e = raw_input()\n", " if e == 'quit':\n", " break\n", " else:\n", " try:\n", " result = eval(str(result) + e)\n", " print \"= \",result\n", " except ZeroDivisionError:\n", " print \"Division by zero\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "To use this calculator, enter any expression with or without spaces.\n", "An expression may include the operators\n", " +, -, *, /, %, or **(raise to a power).\n", "Use = at the beginning of a line to operate on \n", "the result of the previous calculation.\n", "Enter quit to stop the calculator.\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "7/8\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "= 0\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "/0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Division by zero\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "+3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "= 3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "/2\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "= 1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "**5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "= 1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "+8\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "= 9\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "*2\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "= 18\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "quit\n" ] } ], "prompt_number": 2 } ], "metadata": {} } ] }