{ "metadata": { "name": "ch9" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 9: Standard C++ strings" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9.2, Page no: 214" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "while True:\n", " try:\n", " n = int(raw_input())\n", " print \"n = \" , n \n", " except:\n", " break" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "46\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "n = 46\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "22\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "n = 22\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "44\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "n = 44\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "66\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "n = 66\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "88\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "n = 88\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "33,\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9.4, Page no: 215" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "king = [] # defines king to be an array \n", "n=0\n", "while True:\n", " name = raw_input()\n", " if len(name) < 1:\n", " break\n", " king.append(name)\n", " n += 1\n", "# now n == the number of names read\n", "for i in range(n):\n", " print '\\t' , i+1 , \". \" , king[i] " ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Kenneth II (971-995)\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Constantine III (995-997)\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Kenneth III (997-1005)\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Malcolm II (1005-1034)\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Duncan I (1034-1040)\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Macbeth (1040-1057)\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Lulach (1057-1058)\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Malcolm III (1058-1093)\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t1 . Kenneth II (971-995)\n", "\t2 . Constantine III (995-997)\n", "\t3 . Kenneth III (997-1005)\n", "\t4 . Malcolm II (1005-1034)\n", "\t5 . Duncan I (1034-1040)\n", "\t6 . Macbeth (1040-1057)\n", "\t7 . Lulach (1057-1058)\n", "\t8 . Malcolm III (1058-1093)\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9.6, Page no: 217" ] }, { "cell_type": "code", "collapsed": false, "input": [ "'''\n", "Note : you need input.txt in same directory for the program to run without any errors.\n", "'''\n", "\n", "infile = open(\"input.txt\",\"r\")\n", "outfile = open(\"output.txt\",\"w\")\n", "\n", "for i in infile:\n", " s = i.title()\n", " outfile.write(s)\n", "\n", "infile.close()\n", "outfile.close()" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "IOError", "evalue": "[Errno 2] No such file or directory: 'input.txt'", "output_type": "pyerr", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mIOError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 5\u001b[0m '''\n\u001b[0;32m 6\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[0minfile\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"input.txt\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"r\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 8\u001b[0m \u001b[0moutfile\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"output.txt\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"w\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mIOError\u001b[0m: [Errno 2] No such file or directory: 'input.txt'" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9.7, Page no: 218" ] }, { "cell_type": "code", "collapsed": false, "input": [ "'''Note: You need the files - north.dat, south.dat, combined.dat in the same directory for the program to run without any errors'''\n", "\n", "fin1 = open(\"north.dat\",\"r\")\n", "fin2 = open(\"south.dat\",\"r\")\n", "fout = open(\"combined.dat\",\"w\")\n", "\n", "file1 = []\n", "file2 = []\n", "for i in fin1:\n", " try:\n", " s = i.split(\" \")\n", " for j in s:\n", " file1.append(int(j))\n", " except:\n", " continue\n", " \n", "for i in fin2:\n", " try:\n", " s = i.split(\" \")\n", " for j in s:\n", " file2.append(int(j))\n", " except:\n", " continue\n", "\n", "\n", "for i in sorted(file1 + file2):\n", " fout.write(str(i) + \" \")\n", "\n", "fin1.close()\n", "fin2.close()\n", "fout.close()" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "IOError", "evalue": "[Errno 2] No such file or directory: 'north.dat'", "output_type": "pyerr", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mIOError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 3\u001b[0m '''\n\u001b[0;32m 4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0mfin1\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"north.dat\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"r\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 6\u001b[0m \u001b[0mfin2\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"south.dat\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"r\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[0mfout\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"combined.dat\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"w\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mIOError\u001b[0m: [Errno 2] No such file or directory: 'north.dat'" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9.8, Page no: 219" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def print_(oss):\n", " print 'oss.str() = \"' , str(oss) , '\"'\n", "\n", "s=\"ABCDEFG\"\n", "n=33\n", "x=2.718\n", "l = ''\n", "print_(l)\n", "l += s\n", "print_(l)\n", "l += ( \" \" + str(n) )\n", "print_(l)\n", "l += ( \" \" + str(x) )\n", "print_(l)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "oss.str() = \" \"\n", "oss.str() = \" ABCDEFG \"\n", "oss.str() = \" ABCDEFG 33 \"\n", "oss.str() = \" ABCDEFG 33 2.718 \"\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9.9, Page no: 220" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def print_(iss,s='',n=0,x=0.0):\n", " print 's = \"' , s , '\", n = ' , n , \", x = \" , x, ', iss.str() = \"' \\\n", " , iss , '\"' \n", "\n", "s=\"\"\n", "n=0\n", "x=0.0\n", "l = ''\n", "iss = \"ABCDEFG 44 3.14\"\n", "print_(iss)\n", "s = \"ABCDEFG\"\n", "print_(iss,s)\n", "n = 44\n", "print_(iss,s,n)\n", "x = 3.14\n", "print_(iss,s,n,x)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "s = \" \", n = 0 , x = 0.0 , iss.str() = \" ABCDEFG 44 3.14 \"\n", "s = \" ABCDEFG \", n = 0 , x = 0.0 , iss.str() = \" ABCDEFG 44 3.14 \"\n", "s = \" ABCDEFG \", n = 44 , x = 0.0 , iss.str() = \" ABCDEFG 44 3.14 \"\n", "s = \" ABCDEFG \", n = 44 , x = 3.14 , iss.str() = \" ABCDEFG 44 3.14 \"\n" ] } ], "prompt_number": 6 } ], "metadata": {} } ] }