{ "metadata": { "name": "ch14" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 14: Standard C++ Vectors" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 14.1, Page no: 324" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def load(v):\n", " v.append(\"Japan\")\n", " v.append(\"Italy\")\n", " v.append(\"Spain\")\n", " v.append(\"Egypt\")\n", " v.append(\"Chile\")\n", " v.append(\"Zaire\")\n", " v.append(\"Nepal\")\n", " v.append(\"Kenya\")\n", "\n", "def print_(v):\n", " for i in v:\n", " print i\n", "\n", "v = []\n", "load(v)\n", "print_(v)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Japan\n", "Italy\n", "Spain\n", "Egypt\n", "Chile\n", "Zaire\n", "Nepal\n", "Kenya\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 14.2, Page no: 325" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def load(v):\n", " v.append(\"Japan\")\n", " v.append(\"Italy\")\n", " v.append(\"Spain\")\n", " v.append(\"Egypt\")\n", " v.append(\"Chile\")\n", " v.append(\"Zaire\")\n", " v.append(\"Nepal\")\n", " v.append(\"Kenya\")\n", "\n", "def print_(v):\n", " for i in v:\n", " print i\n", "\n", "v = []\n", "load(v)\n", "print_(v)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Japan\n", "Italy\n", "Spain\n", "Egypt\n", "Chile\n", "Zaire\n", "Nepal\n", "Kenya\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 14.3, Page no: 326" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def load(v):\n", " v.append(\"Japan\")\n", " v.append(\"Italy\")\n", " v.append(\"Spain\")\n", " v.append(\"Egypt\")\n", " v.append(\"Chile\")\n", " v.append(\"Zaire\")\n", " v.append(\"Nepal\")\n", " v.append(\"Kenya\")\n", "\n", "def print_(v):\n", " for i in range(len(v)):\n", " print v[i]\n", "\n", "v = []\n", "load(v)\n", "print_(v)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Japan\n", "Italy\n", "Spain\n", "Egypt\n", "Chile\n", "Zaire\n", "Nepal\n", "Kenya\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 14.4, Page no: 326" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def load(v):\n", " v.append(\"Japan\")\n", " v.append(\"Italy\")\n", " v.append(\"Spain\")\n", " v.append(\"Egypt\")\n", " v.append(\"Chile\")\n", " v.append(\"Zaire\")\n", " v.append(\"Nepal\")\n", " v.append(\"Kenya\")\n", "\n", "def print_(v):\n", " for i in v:\n", " print i\n", "\n", "v = []\n", "load(v)\n", "v.sort()\n", "print_(v)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Chile\n", "Egypt\n", "Italy\n", "Japan\n", "Kenya\n", "Nepal\n", "Spain\n", "Zaire\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 14.5, Page no: 327" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def load(v):\n", " v.append(\"Japan\")\n", " v.append(\"Italy\")\n", " v.append(\"Spain\")\n", " v.append(\"Egypt\")\n", " v.append(\"Chile\")\n", " v.append(\"Zaire\")\n", " v.append(\"Nepal\")\n", " v.append(\"Kenya\")\n", "\n", "def print_(v):\n", " for i in v:\n", " print i\n", "\n", "v = []\n", "load(v)\n", "w = v\n", "print_(v)\n", "print_(w)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Japan\n", "Italy\n", "Spain\n", "Egypt\n", "Chile\n", "Zaire\n", "Nepal\n", "Kenya\n", "Japan\n", "Italy\n", "Spain\n", "Egypt\n", "Chile\n", "Zaire\n", "Nepal\n", "Kenya\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 14.6, Page no: 327" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def load(v):\n", " v.append(\"Japan\")\n", " v.append(\"Italy\")\n", " v.append(\"Spain\")\n", " v.append(\"Egypt\")\n", " v.append(\"Chile\")\n", " v.append(\"Zaire\")\n", " v.append(\"Nepal\")\n", " v.append(\"Kenya\")\n", "\n", "def print_(v):\n", " for i in v:\n", " print i\n", "\n", "v = []\n", "load(v)\n", "v.sort()\n", "print_(v)\n", "print \"v.front() = \" + v[0]\n", "print \"v.back() = \" + v.pop(-1) \n", "print \"v.back() = \" + v.pop(-1) \n", "print \"v.back() = \" + v[-1] \n", "print_(v)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Chile\n", "Egypt\n", "Italy\n", "Japan\n", "Kenya\n", "Nepal\n", "Spain\n", "Zaire\n", "v.front() = Chile\n", "v.back() = Zaire\n", "v.back() = Spain\n", "v.back() = Nepal\n", "Chile\n", "Egypt\n", "Italy\n", "Japan\n", "Kenya\n", "Nepal\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 14.7, Page no: 328" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def load(v):\n", " v.append(\"Japan\")\n", " v.append(\"Italy\")\n", " v.append(\"Spain\")\n", " v.append(\"Egypt\")\n", " v.append(\"Chile\")\n", " v.append(\"Zaire\")\n", " v.append(\"Nepal\")\n", " v.append(\"Kenya\")\n", "\n", "def print_(v):\n", " for i in v:\n", " print i\n", "\n", "v = []\n", "load(v)\n", "v.sort()\n", "print_(v)\n", "v.pop(2) # removes Italy\n", "v.pop(-2) # removes Spain\n", "print_(v)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Chile\n", "Egypt\n", "Italy\n", "Japan\n", "Kenya\n", "Nepal\n", "Spain\n", "Zaire\n", "Chile\n", "Egypt\n", "Japan\n", "Kenya\n", "Nepal\n", "Zaire\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 14.8, Page no: 329" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def load(v):\n", " v.append(\"Japan\")\n", " v.append(\"Italy\")\n", " v.append(\"Spain\")\n", " v.append(\"Egypt\")\n", " v.append(\"Chile\")\n", " v.append(\"Zaire\")\n", " v.append(\"Nepal\")\n", " v.append(\"Kenya\")\n", "\n", "def print_(v):\n", " for i in v:\n", " print i\n", "v = []\n", "load(v)\n", "v.sort()\n", "print_(v)\n", "r = []\n", "for i in range(2,len(v)-2):\n", " r.append(v[i]) #removes the segment Italy..Nepal\n", " \n", "for i in r:\n", " v.remove(i)\n", "print_(v)\n", "v.insert(2,\"India\")\n", "print_(v)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Chile\n", "Egypt\n", "Italy\n", "Japan\n", "Kenya\n", "Nepal\n", "Spain\n", "Zaire\n", "Chile\n", "Egypt\n", "Spain\n", "Zaire\n", "Chile\n", "Egypt\n", "India\n", "Spain\n", "Zaire\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 14.9, Page no: 329" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def load(v):\n", " v.append(\"Japan\")\n", " v.append(\"Italy\")\n", " v.append(\"Spain\")\n", " v.append(\"Egypt\")\n", " v.append(\"Chile\")\n", " v.append(\"Zaire\")\n", " v.append(\"Nepal\")\n", " v.append(\"Kenya\")\n", " v.append(\"India\")\n", " v.append(\"China\")\n", " v.append(\"Malta\")\n", " v.append(\"Syria\")\n", "\n", "def print_(v):\n", " for i in v:\n", " print i\n", "\n", "v = []\n", "load(v)\n", "print_(v)\n", "egypt = v.index('Egypt')\n", "malta = v.index('Malta')\n", "w = v[egypt:malta+1]\n", "w.sort()\n", "print_(w)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Japan\n", "Italy\n", "Spain\n", "Egypt\n", "Chile\n", "Zaire\n", "Nepal\n", "Kenya\n", "India\n", "China\n", "Malta\n", "Syria\n", "Chile\n", "China\n", "Egypt\n", "India\n", "Kenya\n", "Malta\n", "Nepal\n", "Zaire\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 14.10, Page no: 329" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def copy(v,x,n):\n", " for i in x:\n", " v.append(i)\n", "\n", "def projection(v,b):\n", " v_size = len(v)\n", " w = []\n", " for i in range(0,v_size):\n", " if b[i]:\n", " w.append(v[i])\n", " return w\n", "\n", "def print_(v):\n", " for i in v:\n", " print i,\n", " print ''\n", "\n", "x = [ 22.2, 33.3, 44.4, 55.5, 66.6, 77.7, 88.8, 99.9 ]\n", "v = []\n", "copy(v, x, 8)\n", "y = [ False, True, False, True, True, True, False, True ]\n", "b = []\n", "copy(b, y, 8)\n", "w = projection(v, b)\n", "print_(v)\n", "print_(w)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "22.2 33.3 44.4 55.5 66.6 77.7 88.8 99.9 \n", "33.3 55.5 66.6 77.7 99.9 \n" ] } ], "prompt_number": 10 } ], "metadata": {} } ] }