diff options
author | Jovina Dsouza | 2014-07-07 16:34:28 +0530 |
---|---|---|
committer | Jovina Dsouza | 2014-07-07 16:34:28 +0530 |
commit | fffcc90da91b66ee607066d410b57f34024bd1de (patch) | |
tree | 7b8011d61013305e0bf7794a275706abd1fdb0d3 /Beginning_C++_through_Game_Programming/ch15.ipynb | |
parent | 299711403e92ffa94a643fbd960c6f879639302c (diff) | |
download | Python-Textbook-Companions-fffcc90da91b66ee607066d410b57f34024bd1de.tar.gz Python-Textbook-Companions-fffcc90da91b66ee607066d410b57f34024bd1de.tar.bz2 Python-Textbook-Companions-fffcc90da91b66ee607066d410b57f34024bd1de.zip |
adding book
Diffstat (limited to 'Beginning_C++_through_Game_Programming/ch15.ipynb')
-rwxr-xr-x | Beginning_C++_through_Game_Programming/ch15.ipynb | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/Beginning_C++_through_Game_Programming/ch15.ipynb b/Beginning_C++_through_Game_Programming/ch15.ipynb new file mode 100755 index 00000000..cc4b221f --- /dev/null +++ b/Beginning_C++_through_Game_Programming/ch15.ipynb @@ -0,0 +1,127 @@ +{ + "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": {} + } + ] +}
\ No newline at end of file |