{ "metadata": { "name": "ch15" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": "Chapter 15 : File Input/Output and apmatrixes" }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "example 15.1 page no : 162\n" }, { "cell_type": "code", "collapsed": false, "input": "'''\nexample 15.1 page no : 162\n'''\ntry:\n fileName = \"a.txt\"\n f = open(fileName,\"r\")\nexcept:\n print \"Unable to open the file named \" , fileName;\n import sys\n sys.exit(0)\n\n# reading file line by line\n \nfor i in f.readlines():\n print i\n\nf.close() ", "language": "python", "metadata": {}, "outputs": [ { "ename": "SystemExit", "evalue": "0", "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 0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": "Unable to open the file named a.txt\n" }, { "output_type": "stream", "stream": "stderr", "text": "To exit: use 'exit', 'quit', or Ctrl-D.\n" } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "example 15.2 page no : 163\n" }, { "cell_type": "code", "collapsed": false, "input": "'''\nexample 15.2 page no : 163\n'''\n\ntry:\n fileName = \"a.txt\"\n outputFile = \"b.txt\"\n f = open(fileName,\"r\")\n w = open(outputFile,\"w\")\nexcept:\n print \"Unable to open the file named \" , fileName;\n import sys\n sys.exit(0)\n\n# reading file line by line\n \nfor i in f.readlines():\n w.write(i)\n print i\n \nf.close()\nw.close() ", "language": "python", "metadata": {}, "outputs": [ { "ename": "SystemExit", "evalue": "0", "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 0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": "Unable to open the file named a.txt\n" }, { "output_type": "stream", "stream": "stderr", "text": "To exit: use 'exit', 'quit', or Ctrl-D.\n" } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "example 15.3 page no : 167\n" }, { "cell_type": "code", "collapsed": false, "input": "'''\nexample 15.3 page no : 167\n'''\n\nclass Set:\n def __init__(self,n):\n self.elements = []\n self.numElements = 0\n\n def getNumElements(self):\n return self.numElements\n\n def getElement(self,i):\n if (i < self.numElements):\n return slef.elements[i];\n else:\n print \"Set index out of range.\" \n import sys\n sys.exit(0)\n\n def find(self,s):\n for i in range(self.numElements):\n if (self.elements[i] == s): \n return i;\n return -1;\n\n def add(self,s):\n # if the element is already in the set, return its index\n index = self.find(s);\n if (index != -1):\n return index;\n # if the apvector is full, double its size\n # add the new elements and return its index\n self.elements.append(s)\n self.numElements += 1\n return self.numElements - 1;\n\ncities= Set (2);\ncity1 = 'ahmedabad'\ncity2 = 'rajkot'\nindex1 = cities.add(city1);\nindex2 = cities.add(city2)\nprint index1, index2", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "0 1\n" } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": "", "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": "", "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }