diff options
author | kinitrupti | 2015-10-15 17:40:30 +0530 |
---|---|---|
committer | kinitrupti | 2015-10-15 17:40:30 +0530 |
commit | 8e4f9ec414f553746330423a974684bbac2163a8 (patch) | |
tree | 7d91d4997a3962c3ad62355ad369f7b6b7738ddb /Schaum's_Outline/ch7.ipynb | |
parent | 79c8f29f70cc014d5fd8b9900d3c19309134bc1d (diff) | |
download | Python-Textbook-Companions-8e4f9ec414f553746330423a974684bbac2163a8.tar.gz Python-Textbook-Companions-8e4f9ec414f553746330423a974684bbac2163a8.tar.bz2 Python-Textbook-Companions-8e4f9ec414f553746330423a974684bbac2163a8.zip |
solved errors
Diffstat (limited to 'Schaum's_Outline/ch7.ipynb')
-rwxr-xr-x | Schaum's_Outline/ch7.ipynb | 750 |
1 files changed, 750 insertions, 0 deletions
diff --git a/Schaum's_Outline/ch7.ipynb b/Schaum's_Outline/ch7.ipynb new file mode 100755 index 00000000..63248e07 --- /dev/null +++ b/Schaum's_Outline/ch7.ipynb @@ -0,0 +1,750 @@ +{
+ "metadata": {
+ "name": "ch7"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Capter 7: Pointers and References"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.1, Page no: 157"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "n=44\n",
+ "print \"n = \" , n \n",
+ "# prints the value of n\n",
+ "print \"&n = \" , hex(id(n)) # prints the address of n\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "n = 44\n",
+ "&n = 0x8fc0eec\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.2, Page no: 157"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''\n",
+ "Note : Python doesn't support reference/pointer variable. But can be achieved by using mutable datatypes i.e. list.\n",
+ "'''\n",
+ "\n",
+ "\n",
+ "n = [44]\n",
+ "rn=n # r is a synonym for n\n",
+ "print \"n = \" , n , \", rn = \" , rn \n",
+ "n[0] -= 1\n",
+ "print \"n = \" , n , \", rn = \" , rn \n",
+ "rn[0] *= 2\n",
+ "print \"n = \" , n , \", rn = \" , rn \n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "n = [44] , rn = [44]\n",
+ "n = [43] , rn = [43]\n",
+ "n = [86] , rn = [86]\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.3, Page no: 158"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "n = [44]\n",
+ "rn=n # r is a synonym for n\n",
+ "print \"&n = \" , hex(id(n)) , \", rn = \" , hex(id(rn ))\n",
+ "rn2 = n\n",
+ "rn3 = rn\n",
+ "print \"&rn2 = \" , hex(id(rn2)) , \", rn = \" , hex(id(rn ))\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "&n = 0x9c6228c , rn = 0x9c6228c\n",
+ "&rn2 = 0x9c6228c , rn = 0x9c6228c\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.4, Page no: 159"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "n = [44]\n",
+ "print \"n = \" , n , \", &n = \" , hex(id(n))\n",
+ "pn = n\n",
+ "print \"pn = \" , hex(id(pn)) , \", &pn = \" , hex(id(hex(id(pn))))\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "n = [44] , &n = 0x9c624ec\n",
+ "pn = 0x9c624ec , &pn = 0x9c6aa60\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.5, Page no: 159"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "n = [44]\n",
+ "print \"n = \" , n , \", &n = \" , hex(id(n))\n",
+ "pn = n\n",
+ "print \"\\tpn = \" , hex(id(pn)) , \",\\n &pn = \" , hex(id(hex(id(pn))))\n",
+ "print \"*pn = \" , pn\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "n = [44] , &n = 0x9c58d6c\n",
+ "\tpn = 0x9c58d6c ,\n",
+ " &pn = 0x9c6ab20\n",
+ "*pn = [44]\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.6, Page no: 160"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "n = [44]\n",
+ "print \"n = \" , n , \", &n = \" , hex(id(n))\n",
+ "pn = n\n",
+ "print \"\\tpn = \" , hex(id(pn)) , \",\\n &pn = \" , hex(id(hex(id(pn))))\n",
+ "print \"*pn = \" , pn\n",
+ "ppn = pn\n",
+ "\n",
+ "print \" ppn = \" , hex(id(hex(id(ppn)))) \n",
+ "print \" &ppn = \" , hex(id(hex(id(hex(id(ppn))))))\n",
+ "print \" *ppn = \" , hex(id(ppn)) \n",
+ "print \"**ppn = \" , ppn \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "n = [44] , &n = 0x9bf05ac\n",
+ "\tpn = 0x9bf05ac ,\n",
+ " &pn = 0x9c58160\n",
+ "*pn = [44]\n",
+ " ppn = 0x9c58680\n",
+ " &ppn = 0x9c58160\n",
+ " *ppn = 0x9bf05ac\n",
+ "**ppn = [44]\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.7, Page no: 160"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "n = [44]\n",
+ "print \"n = \" , n , \"\\n &n = \" , hex(id(n))\n",
+ "pn = n\n",
+ "print \"\\tpn = \" , hex(id(pn)) , \",\\n &pn = \" , hex(id(hex(id(pn))))\n",
+ "print \"*pn = \" , pn\n",
+ "nn = pn\n",
+ "print \" ppn = \" , hex(id(nn))\n",
+ "print \" &ppn = \" , hex(id(hex(id(nn))))\n",
+ "rpn = pn\n",
+ "print \" ppn = \" , hex(id(rpn))\n",
+ "print \" &ppn = \" , hex(id(hex(id(rpn))))\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "n = [44] \n",
+ " &n = 0x9bf60ec\n",
+ "\tpn = 0x9bf60ec ,\n",
+ " &pn = 0x9bf0e40\n",
+ "*pn = [44]\n",
+ " ppn = 0x9bf60ec\n",
+ " &ppn = 0x9bf0e40\n",
+ " ppn = 0x9bf60ec\n",
+ " &ppn = 0x9bf0f20\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.8, Page no: 162"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def max_(m,n):\n",
+ " if m>n:\n",
+ " return m\n",
+ " else:\n",
+ " return n\n",
+ "\n",
+ "m = 44\n",
+ "n = 22\n",
+ "print m , \", \" , n , \", \" , max_(m,n)\n",
+ "m = max_(m,n) \n",
+ "m = 55\n",
+ "# changes the value of m from 44 to 55\n",
+ "print m , \", \" , n , \", \" , max_(m,n) \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "44 , 22 , 44\n",
+ "55 , 22 , 55\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.9, Page no: 163"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "v = []\n",
+ "for k in range(1,5):\n",
+ " v.append(1.0/k)\n",
+ "\n",
+ "for i in range(4):\n",
+ " print \"v[\" , i , \"] = \" , v[i]\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "v[ 0 ] = 1.0\n",
+ "v[ 1 ] = 0.5\n",
+ "v[ 2 ] = 0.333333333333\n",
+ "v[ 3 ] = 0.25\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.10, Page no: 163"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "import sys\n",
+ "a = [22, 33, 44]\n",
+ "\n",
+ "print \"a = \" , hex(id(a))\n",
+ "print \"sizeof(int) = \" , sys.getsizeof(1) \n",
+ "s = 0\n",
+ "for i in a:\n",
+ " s += i\n",
+ " print \"\\t i = \" , hex(id(i)),\n",
+ " print \"\\t *i = \" , i,\n",
+ " print \"\\t sum = \" , s\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "a = 0x9bf688c\n",
+ "sizeof(int) = 12\n",
+ "\t i = 0x8fc0ff4 \t *i = 22 \t sum = 22\n",
+ "\t i = 0x8fc0f70 \t *i = 33 \t sum = 55\n",
+ "\t i = 0x8fc0eec \t *i = 44 \t sum = 99\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.11, Page no: 165"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "a = [22, 33, 44, 55, 66]\n",
+ "print \"a = \" , hex(id(a)) , \", *a = \" , a[0] \n",
+ "for p in a:\n",
+ " print \"p = \" , hex(id(p)) , \", *p = \" , p \n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "a = 0x9c6526c , *a = 22\n",
+ "p = 0x8fc0ff4 , *p = 22\n",
+ "p = 0x8fc0f70 , *p = 33\n",
+ "p = 0x8fc0eec , *p = 44\n",
+ "p = 0x8fc0e68 , *p = 55\n",
+ "p = 0x8fc0de4 , *p = 66\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.12, Page no: 165"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "def loc(a1,a2,n1,n2):\n",
+ " p = []\n",
+ " for element in a2:\n",
+ " if element in a1:\n",
+ " p.append(element)\n",
+ " return p\n",
+ "\n",
+ "a1 = [11, 11, 11, 11, 11, 22, 33, 44, 55]\n",
+ "a2 = [11, 11, 11, 22, 33]\n",
+ "print \"Array a1 begins at location\\t\" , hex(id(a1 ))\n",
+ "print \"Array a2 begins at location\\t\" , hex(id(a2)) \n",
+ "p = loc(a1, a2, 9, 5)\n",
+ "if (p):\n",
+ " print \"Array a2 found at location\\t\" , hex(id(p))\n",
+ " for i in range(len(p)):\n",
+ " print \"\\t\" , hex(id(p[i])) , \": \" , p[i], \"\\t\" , hex(id(a2[i])) , \": \" , a2[i] \n",
+ "else:\n",
+ " print \"Not found.\"\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Array a1 begins at location\t0x9bea56c\n",
+ "Array a2 begins at location\t0x9bea62c\n",
+ "Array a2 found at location\t0x9bea6cc\n",
+ "\t0x8fc1078 : 11 \t0x8fc1078 : 11\n",
+ "\t0x8fc1078 : 11 \t0x8fc1078 : 11\n",
+ "\t0x8fc1078 : 11 \t0x8fc1078 : 11\n",
+ "\t0x8fc0ff4 : 22 \t0x8fc0ff4 : 22\n",
+ "\t0x8fc0f70 : 33 \t0x8fc0f70 : 33\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.13, Page no: 166"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def get(a):\n",
+ " print \"Enter number of items: \"\n",
+ " n = int(raw_input())\n",
+ " print \"Enter \" , n , \" items, one per line:\"\n",
+ " for i in range(n):\n",
+ " print \"\\t\" , i+1 , \": \",\n",
+ " a.append(float(raw_input()))\n",
+ "\n",
+ "def print_(a):\n",
+ " for i in range(len(a)):\n",
+ " print a[i] ,\n",
+ " print ''\n",
+ "\n",
+ "a = []\n",
+ "get(a)\n",
+ "print_(a)\n",
+ "a = []\n",
+ "get(a)\n",
+ "print_(a)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter number of items: \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter 4 items, one per line:\n",
+ "\t1 : "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "44.4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " \t2 : "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "77.7\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " \t3 : "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "22.2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " \t4 : "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "88.8\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " 44.4 77.7 22.2 88.8 \n",
+ "Enter number of items: \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter 2 items, one per line:\n",
+ "\t1 : "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3.33\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " \t2 : "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "9.99\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " 3.33 9.99 \n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.14, Page no: 167"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def sort(p, n):\n",
+ " for i in range(1,n):\n",
+ " for j in range(n-i):\n",
+ " if (p[j] > p[j+1]):\n",
+ " p[j],p[j+1] = p[j+1],p[j]\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.15, Page no: 168"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def sum_(k,n):\n",
+ " # returns the sum f(0) + f(1) + f(2) + . . . + f(n-1):\n",
+ " s = 0\n",
+ " for i in range(1,n+1):\n",
+ " s += k(i)\n",
+ " return s\n",
+ "\n",
+ "def square(k):\n",
+ " return k*k\n",
+ "\n",
+ "def cube(k):\n",
+ " return k*k*k\n",
+ "\n",
+ "\n",
+ "print sum_(square,4) # 1 + 4 + 9 + 16\n",
+ "print sum_(cube,4) # 1 + 8 + 27 + 64\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "30\n",
+ "100\n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file |