diff options
author | root | 2014-07-08 17:34:15 +0530 |
---|---|---|
committer | root | 2014-07-08 17:34:15 +0530 |
commit | 14e9455fda0dbd61d1e8539d9e9b8ce303e81d06 (patch) | |
tree | fc89a07c1dcf8a262f3e72ba55cd8a014a2fb349 /C++_from_the_Ground | |
parent | ba659b4b63e3181ffa039371e19ab59388ded54a (diff) | |
download | Python-Textbook-Companions-14e9455fda0dbd61d1e8539d9e9b8ce303e81d06.tar.gz Python-Textbook-Companions-14e9455fda0dbd61d1e8539d9e9b8ce303e81d06.tar.bz2 Python-Textbook-Companions-14e9455fda0dbd61d1e8539d9e9b8ce303e81d06.zip |
removing unwanted files
Diffstat (limited to 'C++_from_the_Ground')
21 files changed, 0 insertions, 17538 deletions
diff --git a/C++_from_the_Ground/Chapter_10(1).ipynb b/C++_from_the_Ground/Chapter_10(1).ipynb deleted file mode 100755 index b19c3a4c..00000000 --- a/C++_from_the_Ground/Chapter_10(1).ipynb +++ /dev/null @@ -1,560 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:73c04e2c14bfab695e4acf07dc7752334b6372d624570a4e051fd91aeeb761f7" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h1>Chapter 10: Structures and Unions<h1>" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 10.1, Page Number: 223<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class inv_type:\n", - " def __init__(self):\n", - " self.item=None\n", - " self.cost=0\n", - " self.retail=0\n", - " self.on_hand=0\n", - " self.lead_time=0\n", - "\n", - "#Variable declaration\n", - "size=100 \n", - "invtry = []*size\n", - "i=5 #User iput for menu selection\n", - "\n", - "#Initialize the array\n", - "def init_list():\n", - " for t in range(size):\n", - " invtry.append(inv_type())\n", - " \n", - "#get a menu selection\n", - "def menu():\n", - " global i\n", - " print \"(E)nter\"\n", - " print \"(D)isplay\"\n", - " print \"(U)pdate\"\n", - " print \"(Q)uit\"\n", - " print \"choose one: \"\n", - " i-=1\n", - " return i\n", - "\n", - "#enter items into the list\n", - "def enter():\n", - " #find the first free structure\n", - " for i in range(size):\n", - " if not(invtry[i].item==None):\n", - " break\n", - " #i will be size if list is full\n", - " if i==size:\n", - " print \"List full.\"\n", - " return\n", - " input(i)\n", - " \n", - "#Input the information\n", - "def input(i):\n", - " #Enter information; User input\n", - " invtry[i].item=\"Gloves\"\n", - " invtry[i].cost=10\n", - " invtry[i].retail=25\n", - " invtry[i].on_hand=50\n", - " invtry[i].lead_time=10\n", - " \n", - "#Modify an existing item\n", - "def update():\n", - " name=\"Gloves\" #User input\n", - " for i in range(size):\n", - " if not(name==invtry[i].item):\n", - " break\n", - " if i==size:\n", - " print \"Item not found.\"\n", - " return\n", - " print \"Enter new information.\"\n", - " input(i)\n", - " \n", - "#Display the list\n", - "def display():\n", - " for t in range(size):\n", - " if not(invtry[t].item==None):\n", - " print invtry[t].item\n", - " print \"Cost: $\",invtry[t].cost\n", - " print \"Retail: $\",invtry[t].retail\n", - " print \"On hand: \",invtry[t].on_hand\n", - " print \"Resupply time: \",invtry[t].lead_time,\" days\"\n", - " \n", - "\n", - "init_list()\n", - "while True:\n", - " choice=menu()\n", - " if choice==4:\n", - " enter()\n", - " elif choice==3:\n", - " display()\n", - " elif choice==2:\n", - " update()\n", - " elif choice==1:\n", - " break" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "(E)nter\n", - "(D)isplay\n", - "(U)pdate\n", - "(Q)uit\n", - "choose one: \n", - "(E)nter\n", - "(D)isplay\n", - "(U)pdate\n", - "(Q)uit\n", - "choose one: \n", - "Gloves\n", - "Cost: $ 10\n", - "Retail: $ 25\n", - "On hand: 50\n", - "Resupply time: 10 days\n", - "(E)nter\n", - "(D)isplay\n", - "(U)pdate\n", - "(Q)uit\n", - "choose one: \n", - "Enter new information.\n", - "(E)nter\n", - "(D)isplay\n", - "(U)pdate\n", - "(Q)uit\n", - "choose one: \n" - ] - } - ], - "prompt_number": 11 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 10.2, Page Number: 226<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class sample:\n", - " a=None\n", - " ch=None\n", - " \n", - "def f1(parm):\n", - " print parm.a,\" \",parm.ch\n", - "\n", - "#declare arg\n", - "arg=sample() \n", - "\n", - "#initialize arg\n", - "arg.a=1000\n", - "arg.ch='X'\n", - "\n", - "#call function\n", - "f1(arg)\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "1000 X\n" - ] - } - ], - "prompt_number": 12 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 10.3, Page Number: 227<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class stype:\n", - " a=None\n", - " b=None\n", - "\n", - "#Variable declaration\n", - "svar1=stype()\n", - "svar2=stype()\n", - "\n", - "svar1.a=svar1.b=10\n", - "svar2.a=svar2.b=20\n", - "\n", - "print \"Structures before assignment.\"\n", - "print \"svar1: \",svar1.a,' ',svar1.b\n", - "print \"svar1: \",svar2.a,' ',svar2.b\n", - "\n", - "svar2=svar1 #assign structures\n", - "\n", - "#Result\n", - "print \"\\nStructures before assignment.\"\n", - "print \"svar1: \",svar1.a,' ',svar1.b\n", - "print \"svar1: \",svar2.a,' ',svar2.b" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Structures before assignment.\n", - "svar1: 10 10\n", - "svar1: 20 20\n", - "\n", - "Structures before assignment.\n", - "svar1: 10 10\n", - "svar1: 10 10\n" - ] - } - ], - "prompt_number": 13 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 10.4, Page Number: 230<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "import datetime\n", - "\n", - "date=datetime.datetime.now()\n", - "\n", - "#Result\n", - "print date.time()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "17:06:28.236000\n" - ] - } - ], - "prompt_number": 14 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 10.5, Page Number: 231<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "import datetime\n", - "\n", - "date=datetime.datetime.now()\n", - "\n", - "#Result\n", - "print date.ctime()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Sat Sep 14 17:07:14 2013\n" - ] - } - ], - "prompt_number": 15 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 10.6, Page Number: 232<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class mystruct:\n", - " a=None\n", - " b=None\n", - "\n", - "def f(var):\n", - " var[0].a=var[0].a*var[0].a\n", - " var[0].b=var[0].b/var[0].b\n", - " return var[0]\n", - " \n", - "#Variable declaration\n", - "x=[]\n", - "x.append(mystruct())\n", - "y=mystruct()\n", - "\n", - "#Initializing\n", - "x[0].a=10\n", - "x[0].b=20\n", - "\n", - "print \"Original x.a and x.b: \",x[0].a,' ',x[0].b\n", - "\n", - "y=f(x) #function call\n", - "\n", - "#Result\n", - "print \"Modified x.a and x.b: \",x[0].a,' ',x[0].b\n", - "print \"Modified y.a and y.b: \",y.a,' ',y.b\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Original x.a and x.b: 10 20\n", - "Modified x.a and x.b: 100 1\n", - "Modified y.a and y.b: 100 1\n" - ] - } - ], - "prompt_number": 16 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 10.7, Page Number: 239<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class swap_bytes:\n", - " ch=[0,0]\n", - "\n", - "#Exchange of bytes\n", - "def disp_binary(u):\n", - " t=128\n", - " while not(t==0):\n", - " if u&t:\n", - " print \"1 \",\n", - " else:\n", - " print \"0 \",\n", - " t=t/2\n", - "\n", - "#Variable declaration\n", - "sb=swap_bytes()\n", - "\n", - "sb.ch[0]=15\n", - "\n", - "print \"Original bytes: \",\n", - "disp_binary(sb.ch[1])\n", - "disp_binary(sb.ch[0])\n", - "\n", - "#Exchange bytes\n", - "temp=sb.ch[0]\n", - "sb.ch[0]=sb.ch[1]\n", - "sb.ch[1]=temp\n", - "\n", - "#Result\n", - "print \"\\nExchanged bytes: \",\n", - "disp_binary(sb.ch[1])\n", - "disp_binary(sb.ch[0])\n", - "\n", - "\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Original bytes: 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 \n", - "Exchanged bytes: 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 \n" - ] - } - ], - "prompt_number": 17 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 10.8, Page Number: 240<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "ch='a'\n", - "\n", - "while True:\n", - " print \"\\n\",ch,\n", - " print bin(ord(ch)) #Display the bit pattern for each character\n", - " ch=chr(ord(ch)+1)\n", - " if ch=='r':\n", - " break" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "a 0b1100001\n", - "\n", - "b 0b1100010\n", - "\n", - "c 0b1100011\n", - "\n", - "d 0b1100100\n", - "\n", - "e 0b1100101\n", - "\n", - "f 0b1100110\n", - "\n", - "g 0b1100111\n", - "\n", - "h 0b1101000\n", - "\n", - "i 0b1101001\n", - "\n", - "j 0b1101010\n", - "\n", - "k 0b1101011\n", - "\n", - "l 0b1101100\n", - "\n", - "m 0b1101101\n", - "\n", - "n 0b1101110\n", - "\n", - "o 0b1101111\n", - "\n", - "p 0b1110000\n", - "\n", - "q 0b1110001\n" - ] - } - ], - "prompt_number": 18 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 10.9, Page Number: 242<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "#Variable declaration\n", - "ch=['X','Y']\n", - "c=\"\"\n", - "def disp_bits(u):\n", - " t=128\n", - " global c\n", - " while not(t==0):\n", - " if u&t:\n", - " c=c+\"1\"\n", - " else:\n", - " c=c+\"0\"\n", - " t=t/2 \n", - " return c\n", - "\n", - "#Result\n", - "print \"union as chars: \",ch[0],ch[1]\n", - "print \"union as integer: \",\n", - "c= disp_bits(ord(ch[1]))\n", - "c= disp_bits(ord(ch[0]))\n", - "print int(str(c),2)\n", - "\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "union as chars: X Y\n", - "union as integer: 22872\n" - ] - } - ], - "prompt_number": 19 - }, - { - "cell_type": "code", - "collapsed": false, - "input": [], - "language": "python", - "metadata": {}, - "outputs": [] - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file diff --git a/C++_from_the_Ground/Chapter_11(1).ipynb b/C++_from_the_Ground/Chapter_11(1).ipynb deleted file mode 100755 index fdd70bcf..00000000 --- a/C++_from_the_Ground/Chapter_11(1).ipynb +++ /dev/null @@ -1,877 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:5428ee61d548a6c3eefd868dac96cac83b29f85d9bfd7876e9904bdfe87b0fad" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h1>Chapter 11: Introducing the Class<h1>" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 11.1, Page Number: 249<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class queue:\n", - " #Initialize the queue \n", - " def __init__(self):\n", - " self.__sloc=0\n", - " self.__rloc=-1\n", - " self.__q=[]\n", - " \n", - " #Put an integer into the queue\n", - " def qput(self,i):\n", - " if self.__sloc==100:\n", - " print \"Queue is full.\"\n", - " return\n", - " self.__sloc+=1\n", - " self.__q.append(i)\n", - " \n", - " #Get an integer from the queue\n", - " def qget(self):\n", - " if self.__rloc==self.__sloc:\n", - " print \"Queue underflow\"\n", - " return\n", - " self.__rloc+=1\n", - " return self.__q[self.__rloc] \n", - " \n", - "\n", - " \n", - "#Create two queue objects\n", - "b=queue()\n", - "a=queue()\n", - "\n", - "a.qput(10)\n", - "b.qput(19)\n", - "\n", - "a.qput(20)\n", - "b.qput(1)\n", - "\n", - "#Result\n", - "print \"Contents of queue a: \",\n", - "print a.qget(),' ',a.qget()\n", - "\n", - "print \"Contents of queue b: \",\n", - "print b.qget(),' ',b.qget()\n", - "\n", - "\n", - " \n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Contents of queue a: 10 20\n", - "Contents of queue b: 19 1\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 11.2, Page Number: 250<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - " \n", - "class myclass:\n", - " def __init__(self):\n", - " self.__a=None #private member\n", - " self.b=None #public member\n", - " #public functions\n", - " def setlab(self,i):\n", - " self.__a=i #refer to a\n", - " self.b=i*i #refer to b\n", - " return\n", - " def geta(self):\n", - " return self.__a #refer to a\n", - " def reset(self):\n", - " #call setlab using self\n", - " self.setlab(0) #the object is already known\n", - " \n", - " \n", - "ob=myclass()\n", - "ob.setlab(5) #set ob.a and ob.b\n", - "print \"ob after setlab(5): \",ob.geta(),' ',\n", - "print ob.b #can access b because it is public\n", - "\n", - "ob.b=20 #can access b because it is public\n", - "print \"ob after ob.b=20: \",\n", - "print ob.geta(),' ',ob.b\n", - "\n", - "ob.reset()\n", - "print \"ob after ob.reset(): \",ob.geta(),' ',ob.b\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "ob after setlab(5): 5 25\n", - "ob after ob.b=20: 5 20\n", - "ob after ob.reset(): 0 0\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 11.3, Page Number: 254<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class queue:\n", - " \n", - " #Constructor\n", - " def __init__(self): \n", - " self.__q=[]\n", - " self.__rloc=-1\n", - " self.__sloc=0\n", - " print \"Queue initialized\"\n", - " \n", - " #Destructor\n", - " def __del__(self):\n", - " print (\"Queue destroyed\")\n", - " \n", - " #Put an integer into the queue\n", - " def qput(self,i):\n", - " if self.__sloc == 100:\n", - " print \"Queue is full\"\n", - " return \n", - " self.__sloc+=1\n", - " self.__q.append(i)\n", - " \n", - " #Get an integer from the queue\n", - " def qget(self):\n", - " if self.__rloc==self.__sloc:\n", - " print \"Queue underflow\"\n", - " return\n", - " self.__rloc+=1\n", - " return self.__q[self.__rloc]\n", - " \n", - "#Create two queue objects\n", - "a=queue()\n", - "\n", - "\n", - "a.qput(10)\n", - "a.qput(20)\n", - "b=queue()\n", - "b.qput(19)\n", - "\n", - "b.qput(1)\n", - "\n", - "#Result\n", - "print a.qget(),' ',\n", - "print a.qget(),' ',\n", - "print b.qget(),' ',\n", - "print b.qget(),' '\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Queue initialized\n", - "Queue destroyed\n", - "Queue initialized\n", - "Queue destroyed\n", - "10 20 19 1 \n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 11.4, Page Number: 257<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class queue: \n", - " \n", - " #Constructor\n", - " def __init__(self,i): \n", - " self.__q=[]\n", - " self.__rloc=-1\n", - " self.__sloc=0\n", - " self.__who=i\n", - " print \"Queue \",self.__who,\" initialized.\"\n", - " \n", - " #Destructor\n", - " def __del__():\n", - " print \"Queue \",self.__who,\" destroyed\"\n", - " \n", - " #Put an integer into the queue\n", - " def qput(self,i):\n", - " if self.__sloc == 100:\n", - " print \"Queue is full\"\n", - " return \n", - " self.__sloc+=1\n", - " self.__q.append(i)\n", - " \n", - " #Get an integer from the queue\n", - " def qget(self):\n", - " if self.__rloc==self.__sloc:\n", - " print \"Queue underflow\"\n", - " return\n", - " self.__rloc+=1\n", - " return self.__q[self.__rloc]\n", - " \n", - "a=queue(1)\n", - "b=queue(2)\n", - "\n", - "a.qput(10)\n", - "b.qput(19)\n", - "\n", - "a.qput(20)\n", - "b.qput(1)\n", - "\n", - "#Result\n", - "print a.qget(),' ',\n", - "print a.qget(),' ',\n", - "print b.qget(),' ',\n", - "print b.qget(),' '\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Queue 1 initialized.\n", - "Queue destroyed\n", - "Queue 2 initialized.\n", - "Queue destroyed\n", - "10 20 19 1 \n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 11.5, Page Number: 258<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class widget:\n", - " \n", - " #Pass two arguments to the constructor\n", - " def __init__(self,a,b):\n", - " self.__i=a\n", - " self.__j=b\n", - " \n", - " def put_widget(self):\n", - " print self.__i,\" \",self.__j\n", - "\n", - "#Initializing\n", - "x=widget(10,20)\n", - "y=widget(0,0)\n", - "\n", - "x.put_widget()\n", - "y.put_widget()\n", - "\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "10 20\n", - "0 0\n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 11.6, Page Number: 259<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class myclass:\n", - " \n", - " #Constructor\n", - " def __init__(self,x):\n", - " self.a=x\n", - " #To get the vale of a\n", - " def get_a(self):\n", - " return self.a\n", - "\n", - "#Initializing\n", - "ob=myclass(4)\n", - "\n", - "#Result\n", - "print ob.get_a()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "4\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 11.7, Page Number: 260<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "from ctypes import *\n", - "\n", - "class c1(Structure):\n", - " _fields_=[(\"__i\", c_int)] #private member\n", - " def get_i(self): #public finctions\n", - " return self.__i\n", - " def put_i(self,j):\n", - " self.__i=j\n", - "\n", - "#Variable declaration\n", - "s=c1()\n", - "\n", - "s.put_i(10)\n", - "\n", - "#Result\n", - "print s.get_i()\n", - " \n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "10\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 11.8, Page Number: 261<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class c1:\n", - " def __init__(self):\n", - " self.__i=None #private member\n", - " def get_i(self): #public finctions\n", - " return self.__i\n", - " def put_i(self,j):\n", - " self.__i=j\n", - "\n", - "#Variable declaration\n", - "s=c1()\n", - "\n", - "s.put_i(10)\n", - "\n", - "#Result\n", - "print s.get_i()\n", - " \n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "10\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 11.9, Page Number: 263<h3> " - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "from ctypes import *\n", - "\n", - "#Creates a union\n", - "class u_type(Union):\n", - " _fields_ = [(\"i\",c_short),\n", - " (\"ch\", c_char*2)]\n", - " #Constructor\n", - " def __init__(self,a):\n", - " self.i=a\n", - " \n", - " #Show the characters that comprise a short int.\n", - " def showchars(self):\n", - " print self.ch[0]\n", - " print self.ch[1]\n", - " \n", - " \n", - "u=u_type(1000)\n", - "\n", - "#Displays char of 1000\n", - "u.showchars()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\ufffd\n", - "\u0003\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 11.10, Page Number: 264<h3> " - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class c1:\n", - " def __init__(self):\n", - " self.__i=None\n", - " def get_i(self):\n", - " return self.i\n", - " def put_i(self,j):\n", - " self.i=j\n", - "\n", - "#Variable declaration\n", - "s=c1()\n", - "\n", - "s.put_i(10)\n", - "\n", - "#Result\n", - "print s.get_i()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "10\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 11.11, Page Number: 265<h3> " - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class c1:\n", - " def __init__(self):\n", - " self.__i=None\n", - " def get_i(self):\n", - " return self.i\n", - " def put_i(self,j):\n", - " self.i=j\n", - "\n", - "#Variable declaration\n", - "s=c1()\n", - "\n", - "s.put_i(10)\n", - "\n", - "#Result\n", - "print s.get_i()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "10\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 11.12, Page Number: 267<h3> " - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class display:\n", - " def __init__(self):\n", - " width=None\n", - " height=None\n", - " res=None\n", - " def set_dim(self,w,h):\n", - " self.width=w\n", - " self.height=h\n", - " def get_dim(self):\n", - " return self.width,self.height\n", - " def set_res(self,r):\n", - " self.res=r\n", - " def get_res(self):\n", - " return self.res\n", - " \n", - "#Variable decleration\n", - "names=[\"low\",\"medium\",\"high\"] \n", - "(low,medium,high)=(0,1,2) #For enumeration type\n", - "w=None\n", - "h=None\n", - "display_mode=[]*3\n", - "\n", - "for i in range(3):\n", - " display_mode.append(display())\n", - "\n", - "#Initialize the array of objects using member functions\n", - "display_mode[0].set_res(low)\n", - "display_mode[0].set_dim(640,480)\n", - "\n", - "display_mode[1].set_res(medium)\n", - "display_mode[1].set_dim(800,600)\n", - "\n", - "display_mode[2].set_res(high)\n", - "display_mode[2].set_dim(1600,1200)\n", - "\n", - "#Result\n", - "print \"Available display modes: \"\n", - "for i in range(3):\n", - " print names[display_mode[i].get_res()],\" : \",\n", - " w,h=display_mode[i].get_dim()\n", - " print w,\" by \",h" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Available display modes: \n", - "low : 640 by 480\n", - "medium : 800 by 600\n", - "high : 1600 by 1200\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 11.13, Page Number: 268<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class samp:\n", - " __a=None\n", - " def __init__(self,n):\n", - " self.__a=n\n", - " def get_a(self):\n", - " return self.__a\n", - "\n", - "#Initializing the list\n", - "sampArray=[samp(-1),samp(-2),samp(-3),samp(-4)]\n", - "\n", - "#Display\n", - "for i in range(4):\n", - " print sampArray[i].get_a(),' '," - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "-1 -2 -3 -4 \n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 11.14, Page Number: 269<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class samp:\n", - " __a=None\n", - " __b=None\n", - " def __init__(self,n,m):\n", - " self.__a=n\n", - " self.__b=m\n", - " def get_a(self):\n", - " return self.__a\n", - " def get_b(self):\n", - " return self.__b\n", - " \n", - "#Initializing the list\n", - "sampArray=[[samp(1,2),samp(3,4)],\n", - " [samp(5,6),samp(7,8)],\n", - " [samp(9,10),samp(11,12)],\n", - " [samp(13,14),samp(15,16)]]\n", - "\n", - "#Display\n", - "for i in range(4):\n", - " print sampArray[i][0].get_a(),' ',\n", - " print sampArray[i][0].get_b()\n", - " print sampArray[i][1].get_a(),' ',\n", - " print sampArray[i][1].get_b()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "1 2\n", - "3 4\n", - "5 6\n", - "7 8\n", - "9 10\n", - "11 12\n", - "13 14\n", - "15 16\n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 11.15, Page Number: 270<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "from ctypes import *\n", - "\n", - "class P_example(Structure):\n", - " __num=None\n", - " def set_num(self,val):\n", - " self.__num=val\n", - " def show_num(self):\n", - " print self.__num\n", - "\n", - "#Variable declaration\n", - "ob=P_example() #Declare an object to the structure\n", - "p=POINTER(P_example) #Declare a pointer to the structure\n", - "\n", - "ob.set_num(1) #access ob directly\n", - "ob.show_num()\n", - "\n", - "p=ob #assign p the address of ob\n", - "p.show_num() #access ob using pointer\n", - " \n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "1\n", - "1\n" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 11.16, Page Number: 271<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class P_example(Structure):\n", - " __num=None\n", - " def set_num(self,val):\n", - " self.__num=val\n", - " def show_num(self):\n", - " print self.__num\n", - " \n", - "#Variable declaration\n", - "ob=[P_example(),P_example()] #Declare an object to the structure\n", - "p=POINTER(P_example) #Declare a pointer to the structure\n", - "\n", - "ob[0].set_num(10) #access objects directly\n", - "ob[1].set_num(20)\n", - "\n", - "p=ob #obtain pointer to first element\n", - "p[0].show_num() #access ob using pointer\n", - "\n", - "p[1].show_num()\n", - "\n", - "p[0].show_num()\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "10\n", - "20\n", - "10\n" - ] - } - ], - "prompt_number": 7 - }, - { - "cell_type": "code", - "collapsed": false, - "input": [], - "language": "python", - "metadata": {}, - "outputs": [] - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file diff --git a/C++_from_the_Ground/Chapter_12(1).ipynb b/C++_from_the_Ground/Chapter_12(1).ipynb deleted file mode 100755 index a29c9b4d..00000000 --- a/C++_from_the_Ground/Chapter_12(1).ipynb +++ /dev/null @@ -1,904 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:0b4a52abc3f3246965d1e59241352e867e78e448510ae25e91b7d38dc4dcc202" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h1>Chapter 12: A Closer Look at Classes<h1>" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 12.1, Page Number: 274<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class myclass:\n", - " __a=None\n", - " __b=None\n", - " def __init__(self,i,j):\n", - " self.__a=i\n", - " self.__b=j\n", - " def sum(self,x): #Friend function\n", - " return sum1(x)\n", - " \n", - "def sum1(x): \n", - " return x._myclass__a +x._myclass__b #accessing private members\n", - "\n", - "#Variable declaration\n", - "n=myclass(3,4)\n", - "\n", - "#Result\n", - "print n.sum(n)\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "7\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 12.2, Page Number: 275<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class c1:\n", - " __status=None\n", - " def set_status(self,state):\n", - " self.__status=state\n", - " \n", - "class c2:\n", - " __status=None\n", - " def set_status(self,state):\n", - " self.status=state\n", - " \n", - "#Friend function \n", - "def idle(a,b):\n", - " if a._c1__status or b._c2__status :\n", - " return 0\n", - " else:\n", - " return 1\n", - " \n", - "#variable declarations\n", - "def IDLE(): #Constants\n", - " return 0\n", - "def INUSE():\n", - " return 1\n", - "x=c1()\n", - "y=c2()\n", - "\n", - "x.set_status(IDLE())\n", - "y.set_status(IDLE())\n", - "\n", - "if idle(x,y):\n", - " print \"Screen Can Be Used.\"\n", - " \n", - "x.set_status(INUSE())\n", - "\n", - "if idle(x,y):\n", - " print \"Screen Can Be Used.\"\n", - "else:\n", - " print \"Pop-up In Use.\"\n", - " \n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Screen Can Be Used.\n", - "Pop-up In Use.\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 12.3, Page Number: 277<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "def IDLE(): \n", - " return 0\n", - "def INUSE():\n", - " return 1\n", - "\n", - "class c1:\n", - " __status=None\n", - " def set_status(self,state):\n", - " self.__status=state\n", - " def idle(self,b): #now a member of c1\n", - " if self.__status or b._c2__status :\n", - " return 0\n", - " else:\n", - " return 1\n", - " \n", - "class c2:\n", - " __status=None #IDLE if off INUSE if on screen\n", - " def set_status(self,state):\n", - " self.status=state\n", - " \n", - "#Variable declarations \n", - "x=c1()\n", - "y=c2()\n", - "\n", - "x.set_status(IDLE())\n", - "y.set_status(IDLE())\n", - "\n", - "if idle(x,y):\n", - " print \"Screen Can Be Used.\"\n", - " \n", - "x.set_status(INUSE())\n", - "\n", - "if idle(x,y):\n", - " print \"Screen Can Be Used.\"\n", - "else:\n", - " print \"Pop-up In Use.\"\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Screen Can Be Used.\n", - "Pop-up In Use.\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 12.4, Page Number: 278<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "import time,string\n", - "\n", - "class timer:\n", - " __seconds=None\n", - " \n", - " def __init__(self,t1,t2=None):\n", - " if t2==None:\n", - " if isinstance(t1,int): #seconds specified as an integer\n", - " self.__seconds=t1\n", - " else: #seconds specified as a string\n", - " self.__seconds=string.atoi(t1)\n", - " else: #time in minutes and seconds\n", - " self.__seconds=t1*60+t2\n", - " \n", - " def run(self):\n", - " t1=time.clock()\n", - " while (time.clock()-t1)<self.__seconds:\n", - " a=10\n", - " print time.clock()-t1\n", - " \n", - " \n", - "a=timer(10)\n", - "b=timer(\"20\")\n", - "c=timer(1,10)\n", - "a.run() #count 10 seconds\n", - "b.run() #count 20 seconds\n", - "c.run() #count 1 minute,10 seconds\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "10.0000009774\n", - "20.0000009774" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "70.0000004887" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 12.5, Page Number: 280<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "import time,string\n", - "\n", - "class timer:\n", - " __seconds=None\n", - " \n", - " def __init__(self,t1,t2=None):\n", - " if t2==None:\n", - " if isinstance(t1,int): #seconds specified as an integer\n", - " self.__seconds=t1\n", - " else: #seconds specified as a string\n", - " self.__seconds=string.atoi(t1)\n", - " else: #time in minutes and seconds\n", - " self.__seconds=t1*60+t2\n", - " \n", - " def run(self):\n", - " t1=time.clock()\n", - " while (time.clock()-t1)<self.__seconds:\n", - " a=10\n", - " print time.clock()-t1\n", - " \n", - "a=timer(10)\n", - "a.run()\n", - "\n", - "print \"Enter number of seconds: \"\n", - "str=\"20\"\n", - "b=timer(str) #initialize at the run time\n", - "c.run()\n", - "\n", - "print \"Enter minutes and seconds: \"\n", - "min=1\n", - "sec=10\n", - "c=timer(min,sec) #initialize at the run time\n", - "c.run()\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "10.0000004887\n", - "Enter number of seconds: \n", - "70.0000009774" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "Enter minutes and seconds: \n", - "70.0000009774" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 12.6, Page Number: 282<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class myclass:\n", - " __a=None #private members\n", - " __b=None\n", - " def setab(self,i,j): #publc functons\n", - " self.__a=i\n", - " self.__b=j\n", - " def showab(self):\n", - " print \"a is \",self.__a\n", - " print \"b is \",self.__b\n", - "\n", - "#Variable declaration\n", - "ob1 = myclass()\n", - "ob2 = myclass()\n", - "\n", - "#Intalizing\n", - "ob1.setab(10,20)\n", - "ob2.setab(0,0)\n", - "\n", - "print \"ob1 before assignment: \"\n", - "ob1.showab()\n", - "print \"ob2 before assignment: \"\n", - "ob2.showab()\n", - "\n", - "ob2 = ob1 #assign ob1 to ob2\n", - "\n", - "#Result\n", - "print \"ob1 after assignment: \"\n", - "ob1.showab()\n", - "print \"ob2 after assignment: \"\n", - "ob2.showab()\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "ob1 before assignment: \n", - "a is 10\n", - "b is 20\n", - "ob2 before assignment: \n", - "a is 0\n", - "b is 0\n", - "ob1 after assignment: \n", - "a is 10\n", - "b is 20\n", - "ob2 after assignment: \n", - "a is 10\n", - "b is 20\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 12.7, Page Number: 283<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "from copy import deepcopy\n", - " \n", - "class OBJ:\n", - " def set_i(self,x):\n", - " self.__i=x\n", - " def out_i(self):\n", - " print self.__i,\n", - " \n", - "def f(x):\n", - " x=deepcopy(x)\n", - " x.out_i() #outputs 10\n", - " x.set_i(100) #this affects only local copy\n", - " x.out_i() #outputs 100\n", - " \n", - "#Variable declaration\n", - "o=OBJ()\n", - "o.set_i(10)\n", - "f(o) \n", - "o.out_i() #still outputs 10, value of i unchanged\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "10 100 10\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 12.8, Page Number: 284<h3> " - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class myclass: \n", - " def __init__(self,i):\n", - " self.__val=i\n", - " print \"Constructing\"\n", - " def __del__(self):\n", - " print \"Destructing\"\n", - " def getval(self):\n", - " return self.__val\n", - " \n", - "def display(ob):\n", - " print ob.getval()\n", - "\n", - "#Varable declaration\n", - "a=myclass(10)\n", - "\n", - "display(a)\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Constructing\n", - "Destructing\n", - "10\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 12.9, Page Number: 286<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "from ctypes import *\n", - "\n", - "class myclass:\n", - " def __init__(self,i):\n", - " print \"Allocating p\"\n", - " self.p=pointer(c_int(i))\n", - " def __del__(self):\n", - " print \"Freeing p\"\n", - " def getval(self):\n", - " return self.p[0]\n", - " \n", - "def display(ob):\n", - " print ob.getval()\n", - "\n", - "#Variable declaration\n", - "a=myclass(10)\n", - "\n", - "display(a)\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Allocating p\n", - "10\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 12.10, Page Number: 287<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "from ctypes import *\n", - "class myclass:\n", - " def __init__(self,i):\n", - " print \"Allocating p\"\n", - " self.p=pointer(c_int(i))\n", - " def __del__(self):\n", - " print \"Freeing p\"\n", - " def getval(self):\n", - " return self.p[0]\n", - " \n", - "def display(ob):\n", - " print ob[0].getval()\n", - "\n", - "#Variable declaration\n", - "a=[]\n", - "a.append(myclass(10))\n", - "\n", - "display(a)\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Freeing p\n", - "Allocating p\n", - "10\n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 12.11, Page Number: 288<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class sample:\n", - " __s=None\n", - " def show(self):\n", - " print self.__s\n", - " def set(self,str):\n", - " self.__s=str\n", - "\n", - "#Return an object of type sample\n", - "def input():\n", - " str=sample()\n", - " instr = \"Hello\" #User input\n", - " str.set(instr)\n", - " return str\n", - "\n", - "#Variable declaration\n", - "ob=sample()\n", - "\n", - "#assign returned object to ob\n", - "ob=input()\n", - "\n", - "#Result\n", - "ob.show()\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Hello\n" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 12.12, Page Number: 289<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class sample:\n", - " __s=None\n", - " def __init__(self):\n", - " self.__s=0\n", - " def __del__(self): \n", - " print \"Freeing p\"\n", - " def show(self):\n", - " print self.__s\n", - " def set(self,str):\n", - " self.__s=str\n", - " \n", - "#This function takes one object parameter\n", - "def input():\n", - " str=sample()\n", - " instr=\"Hello\" #User input\n", - " str.set(instr)\n", - " return str\n", - "\n", - "#Variable declaration\n", - "ob=sample()\n", - "\n", - "#assign returned object to ob\n", - "ob=input()\n", - "\n", - "#Result\n", - "ob.show()\n", - "\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Freeing p\n", - "Freeing p\n", - "Hello\n" - ] - } - ], - "prompt_number": 8 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 12.13, Page Number: 292<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class myclass:\n", - " __p=None\n", - " def __init__(self,i):\n", - " if isinstance(i,int):\n", - " print \"Allocating p\"\n", - " self.__p=i\n", - " else:\n", - " print \"Copy constructor called\"\n", - " self.__p=i.getval()\n", - " def __del__(self): \n", - " print \"Freeing p\"\n", - " def getval(self):\n", - " return self.__p\n", - " \n", - "#This function takes one object parameter\n", - "def display(ob):\n", - " print ob.getval()\n", - "\n", - "#Variable declaration\n", - "ob=myclass(10)\n", - "\n", - "#Result\n", - "display(ob)\n", - "\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Allocating p\n", - "10\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 12.14, Page Number: 294<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "\n", - "class myclass:\n", - " __p=None\n", - " def __init__(self,i):\n", - " if isinstance(i,int):\n", - " print \"Allocating p\"\n", - " self.__p=i\n", - " else:\n", - " print \"Copy constructor called\"\n", - " self.__p=i.getval()\n", - " def __del__(self): \n", - " print \"Freeing p\"\n", - " def getval(self):\n", - " return self.__p\n", - " \n", - "\n", - "#Variable declaration\n", - "a=myclass(10) #calls normal constructor\n", - "b=myclass(a) #calls copy constructor\n", - "\n", - "\n", - "\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Allocating p\n", - "Copy constructor called\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 12.15, Page Number: 295<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class myclass:\n", - " def __init__(self,i=0):\n", - " if isinstance(i,int):\n", - " print \"Normal constructor\"\n", - " else:\n", - " print \"Copy constructor\"\n", - "\n", - "\n", - "#Variable declaration\n", - "a=myclass() #calls normal constructor\n", - "\n", - "f=myclass()\n", - "a=myclass(f) #Invoke copyconstructor\n", - "\n", - "\n", - "\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Normal constructor\n", - "Normal constructor\n", - "Copy constructor\n" - ] - } - ], - "prompt_number": 8 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 12.16, Page Number: 297<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class c1:\n", - " def __init__(self):\n", - " self.__i=None\n", - " def load_i(self,val):\n", - " self.__i=val\n", - " def get_i(self):\n", - " return self.__i\n", - " \n", - "#Variable declaration \n", - "o=c1()\n", - "\n", - "o.load_i(100)\n", - "\n", - "#Result\n", - "print o.get_i()\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "100\n" - ] - } - ], - "prompt_number": 13 - }, - { - "cell_type": "code", - "collapsed": false, - "input": [], - "language": "python", - "metadata": {}, - "outputs": [] - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file diff --git a/C++_from_the_Ground/Chapter_13(1).ipynb b/C++_from_the_Ground/Chapter_13(1).ipynb deleted file mode 100755 index 6d12a5a7..00000000 --- a/C++_from_the_Ground/Chapter_13(1).ipynb +++ /dev/null @@ -1,883 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:be0df45a07fa8844875025463a4211b5faab9834d4e1dd155b475b36ae6ea27e" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h1>Chapter 13: Operator Overloading<h1>" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 13.1, Page Number: 300<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class three_d:\n", - " def __init__(self,i=None,j=None,k=None):\n", - " if i==None:\n", - " self.x=self.y=self.z=0\n", - " else:\n", - " self.x=i\n", - " self.y=j\n", - " self.z=k\n", - " #Overload +\n", - " def __add__(self,op2):\n", - " temp=three_d()\n", - " temp.x=self.x + op2.x #These are integer additions\n", - " temp.y=self.y + op2.y #and the + retains its original\n", - " temp.z=self.z + op2.z #meaning relative to them.\n", - " return temp\n", - " #Overload assignment\n", - " def __assign__(self,op2):\n", - " self.x=op2.x #These are integer assignments\n", - " self.y=op2.y #and the = retains its original \n", - " self.z=op2.z #meaning relative to them\n", - " return self\n", - " #Show x,y,z coordinates\n", - " def show(self):\n", - " print self.x,\",\",self.y,\",\",self.z\n", - " \n", - "#Variable declaration\n", - "a=three_d(1,2,3)\n", - "b=three_d(10,10,10)\n", - "c=three_d()\n", - "\n", - "a.show()\n", - "b.show()\n", - "\n", - "#add a and b together\n", - "c=a+b\n", - "c.show()\n", - "\n", - "#add a,b and c together\n", - "c=a+b+c\n", - "c.show()\n", - "\n", - "#demonstrate multiple assignment\n", - "c=b=a\n", - "c.show()\n", - "b.show()\n", - " \n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "1 , 2 , 3\n", - "10 , 10 , 10\n", - "11 , 12 , 13\n", - "22 , 24 , 26\n", - "1 , 2 , 3\n", - "1 , 2 , 3\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 13.2, Page Number: 303<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class three_d:\n", - " def __init__(self,i=None,j=None,k=None):\n", - " if i==None:\n", - " self.x=self.y=self.z=0\n", - " else:\n", - " self.x=i\n", - " self.y=j\n", - " self.z=k\n", - " #Overload +\n", - " def __add__(self,op2):\n", - " temp=three_d()\n", - " temp.x=self.x + op2.x #These are integer additions\n", - " temp.y=self.y + op2.y #and the + retains its original\n", - " temp.z=self.z + op2.z #meaning relative to them.\n", - " return temp\n", - " #Overload assignment\n", - " def __assign__(self,op2):\n", - " self.x=op2.x #These are integer assignments\n", - " self.y=op2.y #and the = retains its original \n", - " self.z=op2.z #meaning relative to them\n", - " return self\n", - " #Overload the increment operator\n", - " def __iadd__(self,op2):\n", - " self.x+=op2\n", - " self.y+=op2\n", - " self.z+=op2\n", - " return self\n", - " #Show x,y,z coordinates\n", - " def show(self):\n", - " print self.x,\",\",self.y,\",\",self.z\n", - " \n", - "a=three_d(1,2,3)\n", - "b=three_d(10,10,10)\n", - "c=three_d()\n", - "\n", - "a.show()\n", - "b.show()\n", - "\n", - "#add a and b together\n", - "c=a+b\n", - "c.show()\n", - "\n", - "#add a,b and c together\n", - "c=a+b+c\n", - "c.show()\n", - "\n", - "#demonstrate multiple assignment\n", - "c=b=a\n", - "c.show()\n", - "b.show()\n", - " \n", - "#Increment c\n", - "c+=1\n", - "c.show()\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "1 , 2 , 3\n", - "10 , 10 , 10\n", - "11 , 12 , 13\n", - "22 , 24 , 26\n", - "1 , 2 , 3\n", - "1 , 2 , 3\n", - "2 , 3 , 4\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 13.3, Page Number: 306<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class three_d:\n", - " def __init__(self,i=None,j=None,k=None):\n", - " if i==None:\n", - " self.x=self.y=self.z=0\n", - " else:\n", - " self.x=i\n", - " self.y=j\n", - " self.z=k\n", - " #Overload +\n", - " def __add__(self,op2):\n", - " temp=three_d()\n", - " temp.x=self.x + op2.x #These are integer additions\n", - " temp.y=self.y + op2.y #and the + retains its original\n", - " temp.z=self.z + op2.z #meaning relative to them.\n", - " return temp\n", - " #Overload assignment\n", - " def __assign__(self,op2):\n", - " self.x=op2.x #These are integer assignments\n", - " self.y=op2.y #and the = retains its original \n", - " self.z=op2.z #meaning relative to them\n", - " return self\n", - " #Overload the increment operator\n", - " def __iadd__(self,op2):\n", - " self.x+=op2\n", - " self.y+=op2\n", - " self.z+=op2\n", - " return self\n", - " #Show x,y,z coordinates\n", - " def show(self):\n", - " print self.x,\",\",self.y,\",\",self.z\n", - " \n", - "a=three_d(1,2,3)\n", - "b=three_d(10,10,10)\n", - "c=three_d()\n", - "\n", - "a.show()\n", - "b.show()\n", - "\n", - "#add a and b together\n", - "c=a+b\n", - "c.show()\n", - "\n", - "#add a,b and c together\n", - "c=a+b+c\n", - "c.show()\n", - "\n", - "#demonstrate multiple assignment\n", - "c=b=a\n", - "c.show()\n", - "b.show()\n", - " \n", - "#Increment c (prefix)\n", - "c+=1\n", - "c.show()\n", - "\n", - "#Increment c (postfix)\n", - "c+=1\n", - "c.show()\n", - "\n", - "#Implementing prefix\n", - "c+=1\n", - "a=c\n", - "a.show()\n", - "c.show()\n", - "\n", - "#Implementing postfix\n", - "a=c\n", - "a.show()\n", - "c+=1\n", - "c.show()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "1 , 2 , 3\n", - "10 , 10 , 10\n", - "11 , 12 , 13\n", - "22 , 24 , 26\n", - "1 , 2 , 3\n", - "1 , 2 , 3\n", - "2 , 3 , 4\n", - "3 , 4 , 5\n", - "4 , 5 , 6\n", - "4 , 5 , 6\n", - "4 , 5 , 6\n", - "5 , 6 , 7\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 13.4, Page Number: 310<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class three_d:\n", - " def __init__(self,i=None,j=None,k=None):\n", - " if i==None:\n", - " self.x=self.y=self.z=0\n", - " else:\n", - " self.x=i\n", - " self.y=j\n", - " self.z=k\n", - " #Overload +\n", - " def __add__(self,op2):\n", - " return add(self,op2)\n", - " #Overload assignment\n", - " def __assign__(self,op2):\n", - " self.x=op2.x #These are integer assignments\n", - " self.y=op2.y #and the = retains its original \n", - " self.z=op2.z #meaning relative to them\n", - " return self\n", - " #Show x,y,z coordinates\n", - " def show(self):\n", - " print self.x,\",\",self.y,\",\",self.z\n", - " \n", - "#friending the funcion\n", - "def add(op1,op2):\n", - " temp=three_d()\n", - " temp.x=op1.x + op2.x #These are integer additions\n", - " temp.y=op1.y + op2.y #and the + retains its original\n", - " temp.z=op1.z + op2.z #meaning relative to them.\n", - " return temp\n", - "\n", - "a=three_d(1,2,3)\n", - "b=three_d(10,10,10)\n", - "c=three_d()\n", - "\n", - "a.show()\n", - "b.show()\n", - "\n", - "#add a and b together\n", - "c=a+b\n", - "c.show()\n", - "\n", - "#add a,b and c together\n", - "c=a+b+c\n", - "c.show()\n", - "\n", - "#demonstrate multiple assignment\n", - "c=b=a\n", - "c.show()\n", - "b.show()\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "1 , 2 , 3\n", - "10 , 10 , 10\n", - "11 , 12 , 13\n", - "22 , 24 , 26\n", - "1 , 2 , 3\n", - "1 , 2 , 3\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 13.5, Page Number: 311<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class CL:\n", - " def __init__(self):\n", - " self.count=0\n", - " def __assign__(self,obj):\n", - " self.count=obj.count\n", - " return self\n", - " def __add__(self,i): \n", - " return add(self,i)\n", - " def __radd__(self,i):\n", - " return radd(self,i)\n", - "\n", - "#This handles ob + int\n", - "def add(ob,i):\n", - " temp=CL()\n", - " temp.count=ob.count+i\n", - " return temp\n", - " \n", - "#This handles int + ob \n", - "def radd(ob,i):\n", - " temp=CL()\n", - " temp.count=i+ob.count\n", - " return temp\n", - "\n", - "#Variable declaration\n", - "o=CL()\n", - "o.count = 10\n", - "\n", - "#Result\n", - "print o.count, #outputs 10\n", - "o=10+o\n", - "print o.count, #outputs 20\n", - "o=o+12\n", - "print o.count #outputs 32\n", - "\n", - "\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "10 20 32\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 13.6, Page Number: 314<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class three_d:\n", - " def __init__(self,i=None,j=None,k=None):\n", - " if i==None:\n", - " self.x=self.y=self.z=0\n", - " else:\n", - " self.x=i\n", - " self.y=j\n", - " self.z=k\n", - " #Overload +\n", - " def __add__(self,op2):\n", - " return add(self,op2)\n", - " #Overload assignment\n", - " def __assign__(self,op2):\n", - " self.x=op2.x #These are integer assignments\n", - " self.y=op2.y #and the = retains its original \n", - " self.z=op2.z #meaning relative to them\n", - " return self\n", - " #Overload the increment operator\n", - " def __iadd__(self,op2):\n", - " return iadd(self,op2)\n", - " #Show x,y,z coordinates\n", - " def show(self):\n", - " print self.x,\",\",self.y,\",\",self.z\n", - "\n", - "#friending the funcion\n", - "def add(op1,op2):\n", - " temp=three_d()\n", - " temp.x=op1.x + op2.x #These are integer additions\n", - " temp.y=op1.y + op2.y #and the + retains its original\n", - " temp.z=op1.z + op2.z #meaning relative to them.\n", - " return temp\n", - "def iadd(op1,op2):\n", - " op1.x+=op2\n", - " op1.y+=op2\n", - " op1.z+=op2\n", - " return op1\n", - " \n", - "a=three_d(1,2,3)\n", - "b=three_d(10,10,10)\n", - "c=three_d()\n", - "\n", - "a.show()\n", - "b.show()\n", - "\n", - "#add a and b together\n", - "c=a+b\n", - "c.show()\n", - "\n", - "#add a,b and c together\n", - "c=a+b+c\n", - "c.show()\n", - "\n", - "#demonstrate multiple assignment\n", - "c=b=a\n", - "c.show()\n", - "b.show()\n", - " \n", - "#Increment c (prefix)\n", - "c+=1\n", - "c.show()\n", - "\n", - "#Increment c (postfix)\n", - "c+=1\n", - "c.show()\n", - "\n", - "#Implementing prefix\n", - "c+=1\n", - "a=c\n", - "a.show()\n", - "c.show()\n", - "\n", - "#Implementing postfix\n", - "a=c\n", - "a.show()\n", - "c+=1\n", - "c.show()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "1 , 2 , 3\n", - "10 , 10 , 10\n", - "11 , 12 , 13\n", - "22 , 24 , 26\n", - "1 , 2 , 3\n", - "1 , 2 , 3\n", - "2 , 3 , 4\n", - "3 , 4 , 5\n", - "4 , 5 , 6\n", - "4 , 5 , 6\n", - "4 , 5 , 6\n", - "5 , 6 , 7\n" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 13.7, Page Number: 318<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class sample:\n", - " def __init__(self,ob=0):\n", - " if isinstance(ob,int):\n", - " #Normal constructor\n", - " self.__s=\"\"\n", - " return\n", - " else:\n", - " #Copy constructor\n", - " self.__s=obj._sample__s\n", - " return\n", - " def __del__(self):\n", - " print \"Freeing s\"\n", - " def show(self):\n", - " print self.__s\n", - " def set(self,str):\n", - " self.__s=str\n", - " def __assign__(self,ob): #Overload assignment\n", - " self.s=ob._sample__s\n", - " return self\n", - " \n", - "def input():\n", - " str=sample()\n", - " instr=\"Hello\" #User input\n", - " str.set(instr)\n", - " return str\n", - "\n", - "ob=sample()\n", - "\n", - "#assign returned object to ob\n", - "ob=input()\n", - "\n", - "#Result\n", - "ob.show()\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Freeing s\n", - "Freeing s\n", - "Hello\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 13.8, Page Number: 321<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class atype:\n", - " def __init__(self):\n", - " self.__a=[]\n", - " for i in range(SIZE):\n", - " self.__a.append(i)\n", - " def a(self,i):\n", - " return self.__a[i]\n", - " \n", - "#Variable declaration\n", - "SIZE=3\n", - "ob=atype()\n", - "\n", - "#Result\n", - "print ob.a(2),\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "2\n" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 13.9, Page Number: 322<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class atype:\n", - " def __init__(self):\n", - " self.__a=[]\n", - " for i in range(SIZE):\n", - " self.__a.append(i)\n", - " def a(self,i,j=None):\n", - " if j==None:\n", - " return self.__a[i]\n", - " else:\n", - " self.__a[i]=j\n", - " \n", - "#Variable declaration\n", - "SIZE=3 \n", - "ob=atype()\n", - "\n", - "print ob.a(2), #displays 2\n", - "\n", - "ob.a(2,25)\n", - "\n", - "print ob.a(2) #now displays 25" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "2 25\n" - ] - } - ], - "prompt_number": 7 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 13.10, Page Number: 323<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class atype:\n", - " def __init__(self):\n", - " self.__a=[]\n", - " for i in range(SIZE):\n", - " self.__a.append(i)\n", - " def a(self,i,j=None):\n", - " if (i<0 or i>SIZE-1):\n", - " print \"Index value of\",\n", - " print i,\"is out of bounds.\"\n", - " return\n", - " if j==None:\n", - " return self.__a[i]\n", - " else:\n", - " self.__a[i]=j\n", - " \n", - "#Variable declaration\n", - "SIZE=3 \n", - "ob=atype()\n", - "\n", - "print ob.a(2), #displays 2\n", - "\n", - "ob.a(2,25)\n", - "\n", - "print ob.a(2) #now displays 25\n", - "\n", - "ob.a(44,3) #generates runtime error, 3 out of bounds" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "2 25\n", - "Index value of 44 is out of bounds.\n" - ] - } - ], - "prompt_number": 9 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 13.11, Page Number: 324<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class three_d:\n", - " def __init__(self,i=None,j=None,k=None):\n", - " if i==None:\n", - " self.x=self.y=self.z=0 #3-D coordinates\n", - " else:\n", - " self.x=i\n", - " self.y=j\n", - " self.z=k\n", - " #Show X,Y,Z coordinates\n", - " def show(self):\n", - " print self.x,\",\",self.y,\",\",self.z\n", - " #Overload ()\n", - " def a(self,a,b,c):\n", - " temp = three_d()\n", - " temp.x=self.x+a\n", - " temp.y=self.y+b\n", - " temp.z=self.z+c\n", - " return temp\n", - " \n", - "#Variable declaration\n", - "ob1=three_d(1,2,3)\n", - "\n", - "ob2=ob1.a(10,11,12) #invoke operator ()\n", - "\n", - "#Result\n", - "print \"ob1: \",\n", - "ob1.show()\n", - "print \"ob2: \",\n", - "ob2.show()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "ob1: 1 , 2 , 3\n", - "ob2: 11 , 13 , 15\n" - ] - } - ], - "prompt_number": 10 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 13.12, Page Number: 326<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class str_type:\n", - " def __init__(self,str=\"\"):\n", - " self.__string=str\n", - " #String concatenation\n", - " def __add__(self,str):\n", - " temp=str_type()\n", - " if isinstance(str,str_type):\n", - " temp.__string=self.__string+str.__string\n", - " else:\n", - " temp.__string=self.__string+str\n", - " return temp\n", - " #String copy\n", - " def __assign__(self,str):\n", - " if isinstance(str,str_type):\n", - " self.__string=str.__string\n", - " else:\n", - " self.__string=str\n", - " return self\n", - " def show_str(self):\n", - " print self.__string\n", - " \n", - "a=str_type(\"Hello \")\n", - "b=str_type(\"There\")\n", - "c=a+b\n", - "c.show_str()\n", - "\n", - "a=str_type(\"to program in because\")\n", - "a.show_str()\n", - "\n", - "b=c=str_type(\"C++ is fun\")\n", - "\n", - "c=c+\" \"+a+\" \"+b\n", - "c.show_str()\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Hello There\n", - "to program in because\n", - "C++ is fun to program in because C++ is fun\n" - ] - } - ], - "prompt_number": 12 - }, - { - "cell_type": "code", - "collapsed": false, - "input": [], - "language": "python", - "metadata": {}, - "outputs": [] - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file diff --git a/C++_from_the_Ground/Chapter_14(1).ipynb b/C++_from_the_Ground/Chapter_14(1).ipynb deleted file mode 100755 index 2f8447ba..00000000 --- a/C++_from_the_Ground/Chapter_14(1).ipynb +++ /dev/null @@ -1,924 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:d3cc78f10810f320519cd1c8becefb4790578385b5c2b528dc88273651f18c12" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h1>Chapter 14: Inheritance<h1>" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 14.1, Page Number: 333<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class road_vehicle:\n", - " def __init__(self):\n", - " self.__wheels=None\n", - " self.__passengers=None\n", - " def set_wheels(self,num):\n", - " self.__wheels=num\n", - " def get_wheels(self):\n", - " return self.__wheels\n", - " def set_pass(self,num):\n", - " self.__passengers=num\n", - " def get_pass(self):\n", - " return self.__passengers\n", - "\n", - "#Define a truck\n", - "class truck(road_vehicle):\n", - " def __init__(self):\n", - " self.__cargo=None\n", - " def set_cargo(self,size):\n", - " self.__cargo=size\n", - " def get_cargo(self):\n", - " return self.__cargo\n", - " def show(self):\n", - " print \"wheels: \",self.get_wheels()\n", - " print \"passengers: \",self.get_pass()\n", - " print \"cargo capacity in cubic feet: \",self.__cargo\n", - " \n", - "#Define an enum type\n", - "(car,van,wagon)=(1,2,3)\n", - "type=[\"car\",\"van\",\"wagon\"]\n", - " \n", - "#Define an automobile\n", - "class automobile(road_vehicle):\n", - " def __init__(self):\n", - " self.car_type=None\n", - " def set_type(self,t):\n", - " self.car_type=t\n", - " def get_type(self):\n", - " return self.car_type\n", - " def show(self):\n", - " print \"wheels: \",self.get_wheels()\n", - " print \"passengers: \",self.get_pass()\n", - " print \"type: \",\n", - " if self.get_type()==1:\n", - " print \"car\"\n", - " elif self.get_type()==2:\n", - " print \"van\"\n", - " elif self.get_type()==3:\n", - " print \"wagon\"\n", - " \n", - "#Variable declaration\n", - "t1=truck()\n", - "t2=truck()\n", - "c=automobile()\n", - "\n", - "t1.set_wheels(18)\n", - "t1.set_pass(2)\n", - "t1.set_cargo(3200)\n", - "\n", - "t2.set_wheels(6)\n", - "t2.set_pass(3)\n", - "t2.set_cargo(1200)\n", - "\n", - "t1.show()\n", - "t2.show()\n", - "\n", - "c.set_wheels(4)\n", - "c.set_pass(6)\n", - "c.set_type(van)\n", - "\n", - "c.show() \n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "wheels: 18\n", - "passengers: 2\n", - "cargo capacity in cubic feet: 3200\n", - "wheels: 6\n", - "passengers: 3\n", - "cargo capacity in cubic feet: 1200\n", - "wheels: 4\n", - "passengers: 6\n", - "type: van\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 14.2, Page Number: 335<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class base:\n", - " def __init__(self):\n", - " self.__i=self.__j=None\n", - " def set(self,a,b):\n", - " self.__i=a\n", - " self.__j=b\n", - " def show(self):\n", - " print self.__i,self.__j\n", - " \n", - "class derived(base):\n", - " def __init__(self,x):\n", - " self.__k=x\n", - " def showk(self):\n", - " print self.__k\n", - " \n", - "#Variable declaration\n", - "ob = derived(3)\n", - "\n", - "ob.set(1,2) #access member of base\n", - "ob.show() #access member of base\n", - "\n", - "ob.showk() #uses member of derived class\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "1 2\n", - "3\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 14.3, Page Number: 337<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class base:\n", - " def __init__(self):\n", - " self.__i=self.__j=None #These act as protected members\n", - " def set(self,a,b):\n", - " self.__i=a\n", - " self.__j=b\n", - " def show(self):\n", - " print self.__i,self.__j\n", - " \n", - "class derived(base):\n", - " def __init__(self):\n", - " self.__k=None\n", - " def setk(self):\n", - " self.__k=self._base__i*self._base__j #accessing private variables in derived class\n", - " def showk(self):\n", - " print self.__k\n", - " \n", - "#Variable declaration\n", - "ob = derived()\n", - "\n", - "ob.set(2,3) #OK, known to be derived\n", - "ob.show() #OK, known to be derived\n", - "\n", - "ob.setk()\n", - "ob.showk() #uses member of derived class\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "2 3\n", - "6\n" - ] - } - ], - "prompt_number": 10 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 14.4, Page Number: 338<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class base:\n", - " def __init__(self):\n", - " self.__i=None\n", - " self.__j=None\n", - " def set(self,a,b):\n", - " self.__i=a\n", - " self.__j=b\n", - " def show(self):\n", - " print self.__i,self.__j\n", - " \n", - "class derived1(base):\n", - " def __init__(self):\n", - " self.__k=None\n", - " def setk(self):\n", - " self.__k=self._base__i*self._base__j\n", - " def showk(self):\n", - " print self.__k\n", - "\n", - "class derived2(derived1):\n", - " def __init__(self):\n", - " self.__m=None\n", - " def setm(self):\n", - " self.__m=self._base__i-self._base__j\n", - " def showm(self):\n", - " print self.__m\n", - " \n", - " \n", - "#Variable declaration\n", - "ob1 = derived1()\n", - "ob2 = derived2()\n", - "\n", - "ob1.set(2,3) #access member of base\n", - "ob1.show() #access member of base\n", - "ob1.setk() #uses member of derived1 class\n", - "ob1.showk() #uses member of derived1 class\n", - "\n", - "ob2.set(3,4) #access member of base\n", - "ob2.show() #access member of base\n", - "ob2.setk() #access member of derived1 class\n", - "ob2.setm() #access member of derived2 class\n", - "ob2.showk() #uses member of derived1 class\n", - "ob2.showm() #uses member of derived1 class\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "2 3\n", - "6\n", - "3 4\n", - "12\n", - "-1\n" - ] - } - ], - "prompt_number": 11 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 14.5, Page Number: 341<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class base:\n", - " def __init__(self):\n", - " self.__i=None\n", - " self._j=None\n", - " self.k=None\n", - " def seti(self,a):\n", - " self.__i=a\n", - " def geti(self):\n", - " return i\n", - " \n", - "class derived(base):\n", - " def setj(self,a):\n", - " self._j=a\n", - " def setk(self,a):\n", - " self.k=a\n", - " def getj(self):\n", - " return self._j\n", - " def getk(self):\n", - " return self.k\n", - " \n", - "#Variable declaration \n", - "ob=derived()\n", - "\n", - "ob.setk(10)\n", - "print ob.getk(),\n", - "ob.setj(12)\n", - "print ob.getj()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "10 12\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 14.6, Page Number: 342<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class base1:\n", - " def __init__(self):\n", - " self.x=None\n", - " def showx(self):\n", - " print self.x\n", - " \n", - "class base2:\n", - " def __init__(self):\n", - " self.y=None\n", - " def showy(self):\n", - " print self.y\n", - " \n", - "class derived(base1,base2):\n", - " def set(self,i,j):\n", - " self.x=i\n", - " self.y=j\n", - " \n", - "#Variable declaration\n", - "ob = derived()\n", - "\n", - "ob.set(10,20) #provided by derived\n", - "ob.showx() #from base1\n", - "ob.showy() #from base2\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "10\n", - "20\n" - ] - } - ], - "prompt_number": 17 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 14.7, Page Number: 343<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class base:\n", - " def __init__(self):\n", - " print \"Constructing base\"\n", - " def __del__(self):\n", - " print \"Destructing base\"\n", - "\n", - "class derived(base):\n", - " def __init__(self):\n", - " base.__init__(self)\n", - " print \"Constructing derived\"\n", - " def __del__(self):\n", - " print \"Destructing derived\"\n", - " for b in self.__class__.__bases__:\n", - " b.__del__(self)\n", - "\n", - "#Variable declaration\n", - "ob=derived()\n", - "\n", - "#Does nothing but construct and destruct ob" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Constructing base\n", - "Constructing derived\n", - "Destructing derived\n", - "Destructing base\n" - ] - } - ], - "prompt_number": 19 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 14.8, Page Number: 344<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class base:\n", - " def __init__(self):\n", - " print \"Constructing base\"\n", - " def __del__(self):\n", - " print \"Destructing base\"\n", - "\n", - "class derived1(base):\n", - " def __init__(self):\n", - " base.__init__(self)\n", - " print \"Constructing derived1\"\n", - " def __del__(self):\n", - " print \"Destructing derived1\"\n", - " super(derived1,self).__del__(self)\n", - "\n", - "class derived2(derived1):\n", - " def __init__(self):\n", - " derived1.__init__(self)\n", - " print \"Constructing derived2\"\n", - " def __del__(self):\n", - " print \"Destructing derived2\"\n", - " super(self.__class__,self).__del__(self)\n", - " \n", - "#Variable declaration\n", - "ob=derived2()\n", - "\n", - "#Does nothing but construct and destruct ob" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 14.9, Page Number: 345<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class base1:\n", - " def __init__(self):\n", - " print \"Constructing base1\"\n", - " def __del__(self):\n", - " print \"Destructing base1\"\n", - " \n", - "class base2:\n", - " def __init__(self):\n", - " print \"Constructing base2\"\n", - " def __del__(self):\n", - " print \"Destructing base2\"\n", - " \n", - "class derived(base1,base2):\n", - " def __init__(self):\n", - " for b in self.__class__.__bases__:\n", - " b.__init__(self)\n", - " print \"Constructing derived\"\n", - " def __del__(self):\n", - " print \"Destructing derived\"\n", - " for b in self.__class__.__bases__:\n", - " b.__del__(self)\n", - " \n", - "#Variable declaration\n", - "ob = derived()\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Constructing base1\n", - "Constructing base2\n", - "Constructing derived\n", - "Destructing derived\n", - "Destructing base1\n", - "Destructing base2\n" - ] - } - ], - "prompt_number": 21 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 14.10, Page Number: 347<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class base:\n", - " def __init__(self,x):\n", - " self._i=x\n", - " print \"Constructing base\"\n", - " def __del__(self):\n", - " print \"Destructing base\"\n", - "\n", - "class derived(base):\n", - " def __init__(self,x,y):\n", - " base.__init__(self,y)\n", - " self.__j=x\n", - " print \"Constructing derived\"\n", - " def __del__(self):\n", - " print \"Destructing derived\"\n", - " for b in self.__class__.__bases__:\n", - " b.__del__(self)\n", - " def show(self):\n", - " print self._i,self.__j\n", - "\n", - "#Variable declaration\n", - "ob=derived(3,4)\n", - "\n", - "#Result\n", - "ob.show() #shows 4 3" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Constructing base\n", - "Constructing derived\n", - "Destructing derived\n", - "Destructing base\n", - "4 3\n" - ] - } - ], - "prompt_number": 23 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 14.11, Page Number: 348<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class base1:\n", - " def __init__(self,x):\n", - " self._i=x\n", - " print \"Constructing base1\"\n", - " def __del__(self):\n", - " print \"Destructing base1\"\n", - " \n", - "class base2:\n", - " def __init__(self,x):\n", - " self._k=x\n", - " print \"Constructing base2\"\n", - " def __del__(self):\n", - " print \"Destructing base2\"\n", - " \n", - "class derived(base1,base2):\n", - " def __init__(self,x,y,z):\n", - " self.__j=x\n", - " i=0\n", - " for b in self.__class__.__bases__:\n", - " if i==0:\n", - " b.__init__(self,y)\n", - " else :\n", - " b.__init__(self,z)\n", - " i+=1\n", - " print \"Constructing derived\"\n", - " def __del__(self):\n", - " print \"Destructing derived\"\n", - " for b in self.__class__.__bases__:\n", - " b.__del__(self)\n", - " def show(self):\n", - " print self._i,self.__j,self._k\n", - " \n", - "#Variable declaration\n", - "ob = derived(3,4,5)\n", - "\n", - "#Result\n", - "ob.show()\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Constructing base1\n", - "Constructing base2\n", - "Constructing derived\n", - "Destructing derived\n", - "Destructing base1\n", - "Destructing base2\n", - "4 3 5\n" - ] - } - ], - "prompt_number": 25 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 14.12, Page Number: 348<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class base1:\n", - " def __init__(self,x):\n", - " self._i=x\n", - " print \"Constructing base1\"\n", - " def __del__(self):\n", - " print \"Destructing base1\"\n", - " \n", - "class base2:\n", - " def __init__(self,x):\n", - " self._k=x\n", - " print \"Constructing base2\"\n", - " def __del__(self):\n", - " print \"Destructing base2\"\n", - " \n", - "class derived(base1,base2):\n", - " def __init__(self,x,y):\n", - " i=0\n", - " for b in self.__class__.__bases__:\n", - " if i==0:\n", - " b.__init__(self,x)\n", - " else :\n", - " b.__init__(self,y)\n", - " i+=1\n", - " print \"Constructing derived\"\n", - " def __del__(self):\n", - " print \"Destructing derived\"\n", - " for b in self.__class__.__bases__:\n", - " b.__del__(self)\n", - " def show(self):\n", - " print self._i,self._k\n", - " \n", - "#Variable declaration\n", - "ob = derived(3,4)\n", - "\n", - "#Result\n", - "ob.show()\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Constructing base1\n", - "Constructing base2\n", - "Constructing derived\n", - "Destructing derived\n", - "Destructing base1\n", - "Destructing base2\n", - "3 4\n" - ] - } - ], - "prompt_number": 26 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 14.13, Page Number: 351<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class base:\n", - " def __init__(self):\n", - " self.__i=None\n", - " self.j=self.k=None\n", - " def seti(self,x):\n", - " self.__i=x\n", - " def geti(self):\n", - " return self.__i\n", - "\n", - "class derived(base):\n", - " def __init__(self):\n", - " self.a=None\n", - "\n", - "\n", - "#Variable declaration\n", - "ob=derived()\n", - "\n", - "ob._base__i=10 #Accessing private members of base class\n", - "ob.j=20 #legal because j and k are public variable in base\n", - "ob.k=30\n", - "\n", - "ob.a=40 #legal because a is public in derived class\n", - "ob.seti(10)\n", - "\n", - "#Result\n", - "print ob.geti(),ob.j,ob.a" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "10 20 40\n" - ] - } - ], - "prompt_number": 28 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 14.14, Page Number: 354<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class base:\n", - " def __init__(self):\n", - " self.i=None\n", - "\n", - "#derived1 inherits base\n", - "class derived1(base):\n", - " def __init__(self):\n", - " self.__j=None\n", - " \n", - "#derived2 inherits base\n", - "class derived2(base):\n", - " def __init__(self):\n", - " self.__k=None\n", - "\n", - "#derived3 inherits from both derived1 and derived2\n", - "class derived3(derived1,derived2):\n", - " def __init__(self):\n", - " self.__sum=None\n", - " \n", - "#Variable declaration\n", - "ob=derived3()\n", - "\n", - "ob.i=10\n", - "ob.j=20\n", - "ob.k=30\n", - "\n", - "ob.sum=ob.i+ob.j+ob.k\n", - "\n", - "#Result\n", - "print ob.i,ob.j,ob.k,ob.sum" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "10 20 30 60\n" - ] - } - ], - "prompt_number": 29 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 14.15, Page Number: 355<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class base:\n", - " def __init__(self):\n", - " self.i=None\n", - "\n", - "#derived1 inherits base\n", - "class derived1(base):\n", - " def __init__(self):\n", - " self.__j=None\n", - " \n", - "#derived2 inherits base\n", - "class derived2(base):\n", - " def __init__(self):\n", - " self.__k=None\n", - "\n", - "#derived3 inherits from both derived1 and derived2\n", - "class derived3(derived1,derived2):\n", - " def __init__(self):\n", - " self.__sum=None\n", - " \n", - "#Variable declaration\n", - "ob=derived3()\n", - "\n", - "ob.i=10\n", - "ob.j=20\n", - "ob.k=30\n", - "\n", - "ob.sum=ob.i+ob.j+ob.k\n", - "\n", - "#Result\n", - "print ob.i,ob.j,ob.k,ob.sum" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "10 20 30 60\n" - ] - } - ], - "prompt_number": 30 - }, - { - "cell_type": "code", - "collapsed": false, - "input": [], - "language": "python", - "metadata": {}, - "outputs": [] - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file diff --git a/C++_from_the_Ground/Chapter_15(1).ipynb b/C++_from_the_Ground/Chapter_15(1).ipynb deleted file mode 100755 index 211b1c7d..00000000 --- a/C++_from_the_Ground/Chapter_15(1).ipynb +++ /dev/null @@ -1,427 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:1198072630a182533bf651767f275df9ee5758e0d781322254bb408a8b4cffaa" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h1>Chapter 15: Virtual Functions and Polymorphism<h1>" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 15.1, Page Number: 358<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "\n", - "class B_class:\n", - " def __init__(self):\n", - " self.author=None\n", - " def put_author(self,s):\n", - " self.author=s\n", - " def show_author(self):\n", - " print self.author\n", - " \n", - "class D_class(B_class):\n", - " def __init__(self):\n", - " self.title=None\n", - " def put_title(self,num):\n", - " self.title=num\n", - " def show_title(self):\n", - " print \"Title:\",self.title\n", - " \n", - "#Variable declaration\n", - "p=[B_class()] #acts as a pointer to B_class type\n", - "B_ob=B_class()\n", - "\n", - "dp=[D_class()] #acts as a pointer to D_class type\n", - "D_ob=D_class()\n", - "\n", - "p[0]=B_ob #assigning p to object of base\n", - "\n", - "\n", - "#Access B_class via pointer\n", - "p[0].put_author(\"Tom Clancy\")\n", - "\n", - "#Access D_class via base pointer\n", - "p[0]=D_ob\n", - "p[0].put_author(\"William Shakespeare\")\n", - "\n", - "#Show that each author went into proper object\n", - "B_ob.show_author()\n", - "D_ob.show_author()\n", - "print \"\\n\"\n", - "\n", - "#Since put_title() and show_title() are not part of the base class, \n", - "#they are not accessible via the base pointer p and must be accessed \n", - "#either directly, or, as shown here, through a pointer to the \n", - "#derived type\n", - "dp[0]=D_ob\n", - "dp[0].put_title(\"The Tempest\")\n", - "p[0].show_author()\n", - "dp[0].show_title()\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Tom Clancy\n", - "William Shakespeare\n", - "\n", - "\n", - "William Shakespeare\n", - "Title: The Tempest\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 15.2, Page Number: 361<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class base:\n", - " def who(self): #virtual function\n", - " print \"Base\"\n", - "\n", - "class first_d(base):\n", - " def who(self): #redifine who() relative to first_d\n", - " print \"First derivation\"\n", - " \n", - "class second_d(base):\n", - " def who(self): #redifine who() relative to second_d\n", - " print \"Second derivation\"\n", - " \n", - " \n", - "#Variable declaration\n", - "base_obj=base()\n", - "p=[base()]\n", - "first_obj=first_d()\n", - "second_obj=second_d()\n", - "\n", - "p[0]=base_obj\n", - "p[0].who() #access base's who\n", - "\n", - "p[0]=first_obj\n", - "p[0].who() #access first_d's who\n", - "\n", - "p[0]=second_obj\n", - "p[0].who() #access second_d's who\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Base\n", - "First derivation\n", - "Second derivation\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 15.3, Page Number: 363<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class base:\n", - " def who(self): #virtual function\n", - " print \"Base\"\n", - "\n", - "class first_d(base):\n", - " def who(self): #redifine who() relative to first_d\n", - " print \"First derivation\"\n", - " \n", - "class second_d(base):\n", - " #who not defined\n", - " pass\n", - " \n", - " \n", - "#Variable declaration\n", - "base_obj=base()\n", - "p=[base()]\n", - "first_obj=first_d()\n", - "second_obj=second_d()\n", - "\n", - "p[0]=base_obj\n", - "p[0].who() #access base's who\n", - "\n", - "p[0]=first_obj\n", - "p[0].who() #access first_d's who\n", - "\n", - "p[0]=second_obj\n", - "p[0].who() #access base's who because\n", - " #second_d does not redefine it.\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Base\n", - "First derivation\n", - "Base\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 15.4, Page Number: 364<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class base:\n", - " def who(self): #virtual function\n", - " print \"Base\"\n", - "\n", - "class first_d(base):\n", - " def who(self): #redifine who() relative to first_d\n", - " print \"First derivation\"\n", - " \n", - "#second_d now inherited first_d -- not base\n", - "class second_d(first_d):\n", - " #who not defined\n", - " pass\n", - " \n", - " \n", - "#Variable declaration\n", - "base_obj=base()\n", - "p=[base()]\n", - "first_obj=first_d()\n", - "second_obj=second_d()\n", - "\n", - "p[0]=base_obj\n", - "p[0].who() #access base's who\n", - "\n", - "p[0]=first_obj\n", - "p[0].who() #access first_d's who\n", - "\n", - "p[0]=second_obj\n", - "p[0].who() #access first_d's who because\n", - " #second_d does not redefine it.\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Base\n", - "First derivation\n", - "First derivation\n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 15.5, Page Number: 366<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class figure:\n", - " def __init__(self):\n", - " self._x=None\n", - " self._y=None\n", - " def set_dim(self,i,j):\n", - " self._x=i\n", - " self._y=j\n", - " def show_area(self):\n", - " print \"No area computation defined\",\n", - " print \"for this class.\"\n", - " \n", - "class triangle(figure):\n", - " def show_area(self):\n", - " print \"Triangle with height\",\n", - " print self._x,\"and base\",self._y,\n", - " print \"has an area of\",\n", - " print self._x*0.5*self._y,\".\"\n", - " \n", - "class rectangle(figure):\n", - " def show_area(self):\n", - " print \"Rectangle with dimensions\",\n", - " print self._x,\"x\",self._y,\n", - " print \"has an area of\",\n", - " print self._x*self._y,\".\"\n", - " \n", - "#Variable declaration\n", - "p=[figure()] #pointer to base type\n", - "t=triangle() #objects of derived type\n", - "r=rectangle()\n", - "\n", - "p[0]=t\n", - "p[0].set_dim(10.0,5.0)\n", - "p[0].show_area()\n", - "\n", - "p[0]=r\n", - "p[0].set_dim(10.0,5.0)\n", - "p[0].show_area()\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Triangle with height 10.0 and base 5.0 has an area of 25.0 .\n", - "Rectangle with dimensions 10.0 x 5.0 has an area of 50.0 .\n" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 15.6, Page Number: 368<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class figure:\n", - " def __init__(self):\n", - " self._x=None\n", - " self._y=None\n", - " def set_dim(self,i,j=0):\n", - " self._x=i\n", - " self._y=j\n", - " def show_area(self):\n", - " print \"No area computation defined\",\n", - " print \"for this class.\"\n", - " \n", - "class triangle(figure):\n", - " def show_area(self):\n", - " print \"Triangle with height\",\n", - " print self._x,\"and base\",self._y,\n", - " print \"has an area of\",\n", - " print self._x*0.5*self._y,\".\"\n", - " \n", - "class rectangle(figure):\n", - " def show_area(self):\n", - " print \"Rectangle with dimensions\",\n", - " print self._x,\"x\",self._y,\n", - " print \"has an area of\",\n", - " print self._x*self._y,\".\"\n", - " \n", - "class circle(figure):\n", - " def show_area(self):\n", - " print \"Circle with radius\",\n", - " print self._x,\n", - " print \"has an area of\",\n", - " print 3.14*self._x*self._x,\".\"\n", - " \n", - " \n", - "#Variable declaration\n", - "p=[figure()] #pointer to base type\n", - "t=triangle() #objects of derived type\n", - "r=rectangle()\n", - "c=circle()\n", - "\n", - "p[0]=t\n", - "p[0].set_dim(10.0,5.0)\n", - "p[0].show_area()\n", - "\n", - "p[0]=r\n", - "p[0].set_dim(10.0,5.0)\n", - "p[0].show_area()\n", - "\n", - "p[0]=c\n", - "p[0].set_dim(9.0)\n", - "p[0].show_area()\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Triangle with height 10.0 and base 5.0 has an area of 25.0 .\n", - "Rectangle with dimensions 10.0 x 5.0 has an area of 50.0 .\n", - "Circle with radius 9.0 has an area of 254.34 .\n" - ] - } - ], - "prompt_number": 7 - }, - { - "cell_type": "code", - "collapsed": false, - "input": [], - "language": "python", - "metadata": {}, - "outputs": [] - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file diff --git a/C++_from_the_Ground/Chapter_16(1).ipynb b/C++_from_the_Ground/Chapter_16(1).ipynb deleted file mode 100755 index 98e3dc8a..00000000 --- a/C++_from_the_Ground/Chapter_16(1).ipynb +++ /dev/null @@ -1,674 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:60d976a21b55caeaa47bb2c8586e986eed54234a29f15a816e7270f74ad2660e" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h1>Chapter 16: Templates<h1>" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 16.1, Page Number: 376<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def swapargs(a,b):\n", - " temp=a[0]\n", - " a[0]=b[0]\n", - " b[0]=temp\n", - "\n", - "#Variable declaration\n", - "i=[10]\n", - "j=[20]\n", - "x=[10.1]\n", - "y=[23.3]\n", - "a=['x']\n", - "b=['z']\n", - "\n", - "print \"Original i, j: \",i[0],j[0]\n", - "print \"Original x,y: \",x[0],y[0]\n", - "print \"Original a,b: \",a[0],b[0]\n", - "\n", - "swapargs(i,j) #swap integers\n", - "swapargs(x,y) #swap floats\n", - "swapargs(a,b) #swap chars\n", - "\n", - "#Result\n", - "print \"Swapped i, j: \",i[0],j[0]\n", - "print \"Swapped x,y: \",x[0],y[0]\n", - "print \"Swapped a,b: \",a[0],b[0]" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Original i, j: 10 20\n", - "Original x,y: 10.1 23.3\n", - "Original a,b: x z\n", - "Swapped i, j: 20 10\n", - "Swapped x,y: 23.3 10.1\n", - "Swapped a,b: z x\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 16.2, Page Number: 378<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def myfunc(x,y):\n", - " print x,y\n", - " \n", - "myfunc(10,\"hi\")\n", - "myfunc(0.23,10L)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "10 hi\n", - "0.23 10\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 16.3, Page Number: 379<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def swapargs(a,b):\n", - " if isinstance(a[0],int): #integer version\n", - " temp=a[0]\n", - " a[0]=b[0]\n", - " b[0]=temp\n", - " print \"Inside swapargs int specialization.\"\n", - " else: #generic version\n", - " temp=a[0]\n", - " a[0]=b[0]\n", - " b[0]=temp\n", - " print \"Inside template swapargs.\"\n", - "\n", - "#Variable declaration\n", - "i=[10]\n", - "j=[20]\n", - "x=[10.1]\n", - "y=[23.3]\n", - "a=['x']\n", - "b=['z']\n", - "\n", - "print \"Original i, j: \",i[0],j[0]\n", - "print \"Original x,y: \",x[0],y[0]\n", - "print \"Original a,b: \",a[0],b[0]\n", - "\n", - "swapargs(i,j) #calls explicitly overloaded swapargs()\n", - "swapargs(x,y) #calls generic swapargs()\n", - "swapargs(a,b) #calls generic swapargs()\n", - "\n", - "#Result\n", - "print \"Swapped i, j: \",i[0],j[0]\n", - "print \"Swapped x,y: \",x[0],y[0]\n", - "print \"Swapped a,b: \",a[0],b[0]" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Original i, j: 10 20\n", - "Original x,y: 10.1 23.3\n", - "Original a,b: x z\n", - "Inside swapargs int specialization.\n", - "Inside template swapargs.\n", - "Inside template swapargs.\n", - "Swapped i, j: 20 10\n", - "Swapped x,y: 23.3 10.1\n", - "Swapped a,b: z x\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 16.4, Page Number: 381<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def f(a,b=None):\n", - " if(b==None): #First version of f()\n", - " print \"Inside f(X a)\"\n", - " else: #Second version of f()\n", - " print \"Inside f(X a, Y b)\"\n", - " \n", - "f(10) #calls f(X)\n", - "f(10,20) #calls f(X,Y)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Inside f(X a)\n", - "Inside f(X a, Y b)\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 16.5, Page Number: 382<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "def repeat(data,times):\n", - " while times:\n", - " print data\n", - " times-=1\n", - " \n", - "repeat(\"This is a test\",3)\n", - "repeat(100,5)\n", - "repeat(99.0/2, 4)\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "This is a test\n", - "This is a test\n", - "This is a test\n", - "100\n", - "100\n", - "100\n", - "100\n", - "100\n", - "49.5\n", - "49.5\n", - "49.5\n", - "49.5\n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 16.6, Page Number: 383<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def myabs(val):\n", - " if val<0:\n", - " return -val\n", - " else:\n", - " return val\n", - "\n", - "#Result\n", - "print myabs(-10)\n", - "print myabs(-10.0)\n", - "print myabs(-10L)\n", - "print myabs(-10.0)\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "10\n", - "10.0\n", - "10\n", - "10.0\n" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 16.7, Page Number: 385<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def SIZE():\n", - " return 100\n", - "class queue:\n", - " def __init__(self):\n", - " self.q=[]\n", - " self.sloc=self.rloc=0\n", - " #Put an object into the queue\n", - " def qput(self,i):\n", - " if self.sloc==SIZE():\n", - " print \"Queue is full.\"\n", - " return\n", - " self.sloc+=1\n", - " self.q.append(i)\n", - " #Get an object from the queue.\n", - " def qget(self):\n", - " if self.rloc==self.sloc:\n", - " print \"Queue Underflow.\"\n", - " return\n", - " a=self.rloc\n", - " self.rloc+=1\n", - " return self.q[a]\n", - " \n", - "#Create two integer queues\n", - "a=queue()\n", - "b=queue()\n", - "a.qput(10)\n", - "b.qput(19)\n", - "a.qput(20)\n", - "b.qput(1)\n", - "\n", - "print a.qget(),\n", - "print a.qget(),\n", - "print b.qget(),\n", - "print b.qget()\n", - "\n", - "#Create two double queues\n", - "c=queue()\n", - "d=queue()\n", - "c.qput(10.12)\n", - "d.qput(19.99)\n", - "c.qput(-20.0)\n", - "d.qput(0.986)\n", - "\n", - "print c.qget(),\n", - "print c.qget(),\n", - "print d.qget(),\n", - "print d.qget()\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "10 20 19 1\n", - "10.12 -20.0 19.99 0.986\n" - ] - } - ], - "prompt_number": 7 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 16.8, Page Number: 387<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class myclass:\n", - " def __init__(self,a,b):\n", - " self.__i=a\n", - " self.__j=b\n", - " def show(self):\n", - " print self.__i,self.__j\n", - "\n", - "ob1=myclass(10,0.23)\n", - "ob2=myclass('X',\"This is a test\")\n", - "\n", - "#Result\n", - "ob1.show() #Show int,double\n", - "ob2.show() #Show char.char*\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "10 0.23\n", - "X This is a test\n" - ] - } - ], - "prompt_number": 8 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 16.9, Page Number: 388<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "def SIZE():\n", - " return 10\n", - "\n", - "class atype:\n", - " def __init__(self):\n", - " self.a=[]\n", - " for i in range(SIZE()):\n", - " self.a.append(i)\n", - " #Implementing the [] overloading\n", - " def op1(self,i,j):\n", - " if (i<0 or i>=SIZE()):\n", - " print \"\\nIndex value of \",\n", - " print i,\" is out-of-bounds.\"\n", - " return \n", - " self.a[i]=j\n", - " def op2(self,i):\n", - " if (i<0 or i>SIZE()-1):\n", - " print \"\\nIndex value of \",\n", - " print i,\" is out-of-bounds.\"\n", - " return\n", - " return self.a[i]\n", - " \n", - "#Variable declaration\n", - "intob=atype()\n", - "doubleob=atype()\n", - " \n", - "print \"Integer array: \",\n", - "for i in range(SIZE()):\n", - " intob.op1(i,i)\n", - "for i in range(SIZE()):\n", - " print intob.op2(i),\n", - " \n", - "print \"\"\n", - "\n", - "print \"Double array: \",\n", - "for i in range(SIZE()):\n", - " doubleob.op1(i,float(i)/3)\n", - "for i in range(SIZE()):\n", - " print doubleob.op2(i),\n", - " \n", - "intob.op1(12,100)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Integer array: 0 1 2 3 4 5 6 7 8 9 \n", - "Double array: 0.0 0.333333333333 0.666666666667 1.0 1.33333333333 1.66666666667 2.0 2.33333333333 2.66666666667 3.0 \n", - "Index value of 12 is out-of-bounds.\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 16.10, Page Number: 389<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class atype:\n", - " def __init__(self,size):\n", - " self.a=[]\n", - " self.size=size\n", - " for i in range(size):\n", - " self.a.append(i)\n", - " #Implementing the [] overloading\n", - " def op1(self,i,j):\n", - " if (i<0 or i>self.size-1):\n", - " print \"\\nIndex value of \",\n", - " print i,\" is out-of-bounds.\"\n", - " return\n", - " self.a[i]=j\n", - " def op2(self,i):\n", - " if (i<0 or i>self.size-1):\n", - " print \"\\nIndex value of \",\n", - " print i,\" is out-of-bounds.\"\n", - " return\n", - " return self.a[i]\n", - " \n", - "#Variable declaration\n", - "intob=atype(10)\n", - "doubleob=atype(15)\n", - " \n", - "print \"Integer array: \",\n", - "for i in range(10):\n", - " intob.op1(i,i)\n", - "for i in range(10):\n", - " print intob.op2(i),\n", - " \n", - "print \"\"\n", - "\n", - "print \"Double array: \",\n", - "for i in range(15):\n", - " doubleob.op1(i,float(i)/3)\n", - "for i in range(15):\n", - " print doubleob.op2(i),\n", - " \n", - "intob.op1(12,100) #generates runtime error\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Integer array: 0 1 2 3 4 5 6 7 8 9 \n", - "Double array: 0.0 0.333333333333 0.666666666667 1.0 1.33333333333 1.66666666667 2.0 2.33333333333 2.66666666667 3.0 3.33333333333 3.66666666667 4.0 4.33333333333 4.66666666667 \n", - "Index value of 12 is out-of-bounds.\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 16.11, Page Number: 391<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class atype:\n", - " def __init__(self,size=10):\n", - " self.a=[]\n", - " for i in range(size):\n", - " self.a.append(i)\n", - " #Implementing the [] overloading\n", - " def op1(self,i,j):\n", - " if (i<0 and i>SIZE()-1):\n", - " print \"Index value of \"\n", - " print i,\" is out-of-bounds.\"\n", - " exit(1)\n", - " self.a[i]=j\n", - " def op2(self,i):\n", - " if (i<0 and i>SIZE()-1):\n", - " print \"Index value of \"\n", - " print i,\" is out-of-bounds.\"\n", - " exit()\n", - " return self.a[i]\n", - " \n", - "#Variable declaration\n", - "intob=atype(100)\n", - "doubleob=atype()\n", - "defarray=atype()\n", - " \n", - "print \"Integer array: \",\n", - "for i in range(100):\n", - " intob.op1(i,i)\n", - "for i in range(100):\n", - " print intob.op2(i),\n", - " \n", - "print \"\"\n", - "\n", - "print \"Double array: \",\n", - "for i in range(10):\n", - " doubleob.op1(i,float(i)/3)\n", - "for i in range(10):\n", - " print doubleob.op2(i),\n", - " \n", - "print \"\"\n", - "\n", - "print \"Defarray array: \",\n", - "for i in range(10):\n", - " doubleob.op1(i,i)\n", - "for i in range(10):\n", - " print doubleob.op2(i),\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Integer array: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 \n", - "Double array: 0.0 0.333333333333 0.666666666667 1.0 1.33333333333 1.66666666667 2.0 2.33333333333 2.66666666667 3.0 \n", - "Defarray array: 0 1 2 3 4 5 6 7 8 9\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 16.12, Page Number: 393<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class myclass:\n", - " def __init__(self,a):\n", - " if isinstance(a,int):\n", - " print \"Inside myclass<int>specialization\"\n", - " self.__x=a*a\n", - " else:\n", - " print \"Inside generic myclass\"\n", - " self.__x=a\n", - " def getx(self):\n", - " return self.__x\n", - " \n", - "d=myclass(10.1)\n", - "print \"double: \",d.getx(),\"\\n\"\n", - "\n", - "i=myclass(5)\n", - "print \"int: \",i.getx()\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Inside generic myclass\n", - "double: 10.1 \n", - "\n", - "Inside myclass<int>specialization\n", - "int: 25\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "code", - "collapsed": false, - "input": [], - "language": "python", - "metadata": {}, - "outputs": [] - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file diff --git a/C++_from_the_Ground/Chapter_17(1).ipynb b/C++_from_the_Ground/Chapter_17(1).ipynb deleted file mode 100755 index c50f48aa..00000000 --- a/C++_from_the_Ground/Chapter_17(1).ipynb +++ /dev/null @@ -1,730 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:636fe844c88ef46d09064f199c6b3a4c62e262611a41cc6f126f08459b94982e" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h1>Chapter 17: Exception Handling<h1>" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 17.1, Page Number: 397<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "print \"start\"\n", - "\n", - "try: #start a try block\n", - " print \"Inside try block\"\n", - " raise Exception(99) #raise an error\n", - " print \"This will not execute\"\n", - "except Exception,i: #catch an error\n", - " print \"Caught an exception -- value is:\",\n", - " print i\n", - "\n", - "print \"end\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "start\n", - "Inside try block\n", - "Caught an exception -- value is: 99\n", - "end\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 17.2, Page Number: 399<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "import sys\n", - "\n", - "\n", - "\n", - "def main():\n", - " print \"start\"\n", - " try: #start a try block\n", - " print \"Inside try block\"\n", - " raise Exception(99) #raise an error\n", - " print \"This will not execute\"\n", - " except Exception,i: #catch an error\n", - " if isinstance(i,float):\n", - " print \"Caught an exception -- value is:\",\n", - " print i\n", - " else:\n", - " print \"Abnormal program termination\"\n", - " return\n", - " print \"end\"\n", - "\n", - " \n", - "main()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "start\n", - "Inside try block\n", - "Abnormal program termination\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 17.3, Page Number: 400<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def Xtest(test):\n", - " print \"Inside Xtest, test is: \",test\n", - " if(test):\n", - " raise Exception(test)\n", - " \n", - "print \"start\"\n", - "\n", - "try: #start a try block\n", - " print \"Inside try block\"\n", - " Xtest(0)\n", - " Xtest(1)\n", - " Xtest(2)\n", - "except Exception,i: #catch an error\n", - " print \"Caught an exception -- value is:\",\n", - " print i\n", - "\n", - "print \"end\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "start\n", - "Inside try block\n", - "Inside Xtest, test is: 0\n", - "Inside Xtest, test is: 1\n", - "Caught an exception -- value is: 1\n", - "end\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 17.4, Page Number: 401<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def Xhandler(test):\n", - " try:\n", - " if(test):\n", - " raise Exception(test)\n", - " except Exception,i:\n", - " print \"Caught One! Ex #:\",i\n", - " \n", - "print \"start\"\n", - "\n", - "Xhandler(1)\n", - "Xhandler(2)\n", - "Xhandler(0)\n", - "Xhandler(3)\n", - "\n", - "print \"end\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "start\n", - "Caught One! Ex #: 1\n", - "Caught One! Ex #: 2\n", - "Caught One! Ex #: 3\n", - "end\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 17.5, Page Number: 401<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class MyException:\n", - " def __init__(self,s):\n", - " self.str_what=s\n", - " \n", - "#Variable declaration\n", - "a=None \n", - "b=None\n", - "\n", - "try:\n", - " print \"Enter numerator and denominator:\"\n", - " #User-input\n", - " a=10 \n", - " b=0\n", - " if not(b):\n", - " raise MyException(\"Cannot divide by zero!\")\n", - " else:\n", - " print \"Quotient is\",a/b\n", - "except MyException as e: #catch an error\n", - " print e.str_what\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter numerator and denominator:\n", - "Cannot divide by zero!\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 17.6, Page Number: 403<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class MyException:\n", - " def __init__(self,s):\n", - " self.x=s\n", - " \n", - "def Xhandler(test):\n", - " try:\n", - " if(test):\n", - " raise MyException(test)\n", - " else:\n", - " raise MyException(\"Value is zero\")\n", - " except MyException as i:\n", - " if isinstance(i.x,int):\n", - " print \"Caught One! Ex #:\",i.x\n", - " else:\n", - " print \"Caught a string:\",\n", - " print i.x\n", - " \n", - "print \"start\"\n", - "\n", - "Xhandler(1)\n", - "Xhandler(2)\n", - "Xhandler(0)\n", - "Xhandler(3)\n", - "\n", - "print \"end\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "start\n", - "Caught One! Ex #: 1\n", - "Caught One! Ex #: 2\n", - "Caught a string: Value is zero\n", - "Caught One! Ex #: 3\n", - "end\n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 17.7, Page Number: 404<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class B:\n", - " pass\n", - "\n", - "class D(B):\n", - " pass\n", - "\n", - "derived=D()\n", - "\n", - "try:\n", - " raise B()\n", - "except B as b:\n", - " print \"Caught a base class.\"\n", - "except D as d:\n", - " print \"This wont execute.\"\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Caught a base class.\n" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 17.8, Page Number: 405<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def Xhandler(test):\n", - " try:\n", - " if test==0:\n", - " raise Exception(test) #throw int\n", - " if test==1:\n", - " raise Exception('a') #throw char\n", - " if test==2:\n", - " raise Exception(123.23) #throw double\n", - " except: #Catches all exceptions\n", - " print \"Caught One!\"\n", - "\n", - "print \"start\"\n", - "\n", - "Xhandler(0)\n", - "Xhandler(1)\n", - "Xhandler(2)\n", - "\n", - "print \"end\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "start\n", - "Caught One!\n", - "Caught One!\n", - "Caught One!\n", - "end\n" - ] - } - ], - "prompt_number": 7 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 17.9, Page Number: 405<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class MyException:\n", - " def __init__(self,s):\n", - " self.x=s\n", - " \n", - "def Xhandler(test):\n", - " try:\n", - " if test==0:\n", - " raise MyException(test) #throw int\n", - " if test==1:\n", - " raise MyException('a') #throw char\n", - " if test==2:\n", - " raise MyException(123.23) #throw double\n", - " except MyException as i:\n", - " if isinstance(i.x,int): #catch an int exception\n", - " print \"Caught\",i.x \n", - " else: #catch all other exceptions\n", - " print \"Caught One!\"\n", - " \n", - "\n", - "print \"start\"\n", - "\n", - "Xhandler(0)\n", - "Xhandler(1)\n", - "Xhandler(2)\n", - "\n", - "print \"end\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "start\n", - "Caught 0\n", - "Caught One!\n", - "Caught One!\n", - "end\n" - ] - } - ], - "prompt_number": 8 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 17.10, Page Number: 407<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class MyException:\n", - " def __init__(self,s):\n", - " self.x=s\n", - " \n", - "#This function can only throw ints, chars and doubles\n", - "def Xhandler(test):\n", - " if test==0:\n", - " raise MyException(test) #throw int\n", - " if test==1:\n", - " raise MyException('a') #throw char\n", - " if test==2:\n", - " raise MyException(123.23) #throw double\n", - " \n", - "\n", - "print \"start\"\n", - "try:\n", - " Xhandler(0)\n", - "except MyException as i:\n", - " if isinstance(i.x,int): #catch an int exception\n", - " print \"Caught int\" \n", - " elif isinstance(i.x,str): #catch a char exception\n", - " print \"Caught char\"\n", - " elif isinstance(i.x,float): #catch a float exception\n", - " print \"Caught double\"\n", - "\n", - "print \"end\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "start\n", - "Caught int\n", - "end\n" - ] - } - ], - "prompt_number": 9 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 17.11, Page Number: 408<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def Xhandler():\n", - " try:\n", - " raise Exception(\"hello\") #throw a char *\n", - " except Exception,c: #catch a char *\n", - " print \"Caugh char * inside Xhandler\"\n", - " raise #rethrow char * out of function\n", - " \n", - "print \"start\"\n", - "try:\n", - " Xhandler()\n", - "except Exception,c:\n", - " print \"Caught char * inside main\"\n", - " \n", - "print \"end\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "start\n", - "Caugh char * inside Xhandler\n", - "Caught char * inside main\n", - "end\n" - ] - } - ], - "prompt_number": 10 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 17.12, Page Number: 410<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "from ctypes import *\n", - "\n", - "#Variable declaration\n", - "p=[]\n", - "\n", - "try:\n", - " for i in range(32):\n", - " p.append(c_int())\n", - "except MemoryError,m:\n", - " print \"Allocation failure.\"\n", - " \n", - "for i in range(32):\n", - " p[i]=i\n", - " \n", - "for i in range(32):\n", - " print p[i],\n", - " \n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31\n" - ] - } - ], - "prompt_number": 12 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 17.13, Page Number: 410<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "from ctypes import *\n", - "\n", - "#Variable declaration\n", - "p=[]\n", - "\n", - "\n", - "for i in range(32):\n", - " p.append(c_int()) \n", - "if not(p):\n", - " print \"Allocation failure.\"\n", - " \n", - "for i in range(32):\n", - " p[i]=i\n", - " \n", - "for i in range(32):\n", - " print p[i],\n", - " \n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31\n" - ] - } - ], - "prompt_number": 13 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 17.13, Page Number: 412<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class three_d:\n", - " def __init__(self,i=0,j=0,k=0): #3=D coordinates\n", - " if(i==0 and j==0 and k==0):\n", - " self.x=self.y=self.z=0\n", - " print \"Constructing 0, 0, 0\"\n", - " else:\n", - " self.x=i\n", - " self.y=j\n", - " self.z=k\n", - " print \"Constructing\",i,\",\",j,\",\",k\n", - " def __del__(self):\n", - " print \"Destructing\"\n", - " #new overloaded relative to three_d.\n", - " def __new__(typ, *args, **kwargs):\n", - " obj = object.__new__(typ, *args, **kwargs)\n", - " return obj\n", - " def show(self):\n", - " print self.x,\",\",\n", - " print self.y,\",\",\n", - " print self.z\n", - " \n", - "p1=[]*3\n", - "p2=[]\n", - " \n", - "try:\n", - " print \"Allocating array of three_d objects.\"\n", - " for i in range(3): #allocate array\n", - " p1.append(three_d())\n", - " print \"Allocating three_d object.\"\n", - " p2.append(three_d(5,6,7)) #allocate object\n", - "except MemoryError:\n", - " print \"Allocation error\"\n", - " \n", - "p1[2].show()\n", - "p2[0].show()\n", - "\n", - "\n", - "for i in xrange(2,-1,-1):\n", - " del p1[i] #delete array\n", - "print \"Deleting array of thee_d objects.\"\n", - "\n", - "del p2[0] #delete object\n", - "print \"Deleting three_d object.\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Allocating array of three_d objects.\n", - "Constructing 0, 0, 0\n", - "Constructing 0, 0, 0\n", - "Constructing 0, 0, 0\n", - "Allocating three_d object.\n", - "Constructing 5 , 6 , 7\n", - "0 , 0 , 0\n", - "5 , 6 , 7\n", - "Destructing\n", - "Destructing\n", - "Destructing\n", - "Deleting array of thee_d objects.\n", - "Destructing\n", - "Deleting three_d object.\n" - ] - } - ], - "prompt_number": 14 - }, - { - "cell_type": "code", - "collapsed": false, - "input": [], - "language": "python", - "metadata": {}, - "outputs": [] - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file diff --git a/C++_from_the_Ground/Chapter_18(1).ipynb b/C++_from_the_Ground/Chapter_18(1).ipynb deleted file mode 100755 index df4cabc2..00000000 --- a/C++_from_the_Ground/Chapter_18(1).ipynb +++ /dev/null @@ -1,881 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:85eae8322c208f1f26e859bb0f4dac6f86b35c3ac105ed98ac2d0fbf05287f0e" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h1>Chapter 18: The C++ I/O System<h1>" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.1, Page Number: 421<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class three_d:\n", - " def __init__(self,a,b,c): #3D coordinates\n", - " self.x=a\n", - " self.y=b\n", - " self.z=c\n", - " #Display x,y,z coordinates - three_d inserter.\n", - " def __repr__(self):\n", - " return str(self.x)+\", \"+str(self.y)+\", \"+str(self.z)+\"\\n\"\n", - "\n", - "#Variable declaration\n", - "a=three_d(1,2,3)\n", - "b=three_d(3,4,5)\n", - "c=three_d(5,6,7)\n", - "\n", - "#Result\n", - "print a,b,c" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "1, 2, 3\n", - " 3, 4, 5\n", - " 5, 6, 7\n", - "\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.2, Page Number: 423<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class thrnee_d:\n", - " def __init__(self,a,b,c): #3D coordinates\n", - " self.x=a\n", - " self.y=b\n", - " self.z=c\n", - " #Display x,y,z coordinates - three_d inserter.\n", - " __repr__=repr \n", - " \n", - "#Friend function \n", - "def repr():\n", - " return str(self.x)+\", \"+str(self.y)+\", \"+str(self.z)+\"\\n\"\n", - "\n", - "#Variable declaration\n", - "a=three_d(1,2,3)\n", - "b=three_d(3,4,5)\n", - "c=three_d(5,6,7)\n", - "\n", - "print a,b,c" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "1, 2, 3\n", - " 3, 4, 5\n", - " 5, 6, 7\n", - "\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.3, Page Number: 424<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class three_d:\n", - " def __init__(self,a,b,c): #3D coordinates\n", - " self.x=a\n", - " self.y=b\n", - " self.z=c\n", - " #Display x,y,z coordinates - three_d inserter.\n", - " def __repr__(self):\n", - " return str(self.x)+\", \"+str(self.y)+\", \"+str(self.z)\n", - "\n", - "#Variable declaration\n", - "a=three_d(1,2,3)\n", - "\n", - "print a\n", - "\n", - "#User input\n", - "print \"Enter X,Y,Z values:\"\n", - "a=three_d(4,5,6) \n", - "print a" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "1, 2, 3\n", - "Enter X,Y,Z values:\n", - "4, 5, 6\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.4, Page Number: 428<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "print '{0:+d}'.format(123), #for ios::showpos\n", - "if(123.23>0):\n", - " i='{0:e}'.format(123.23) #for ios::scientific \n", - " i='+'+i\n", - " print i\n", - "else :\n", - " print '{0:e}'.format(123.23)\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "+123 +1.232300e+02\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.5, Page Number: 430<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "import string\n", - "\n", - "print '{0:+d}'.format(123), #for ios::showpos\n", - "if(123.23>0):\n", - " i='{0:e}'.format(123.23) #for ios::scientific \n", - " i='+'+i\n", - " print i\n", - "else :\n", - " print '{0:e}'.format(123.23)\n", - "\n", - " \n", - "print '{:10.2f}'.format(123.23), #2 digits left of decimal\n", - "if(123.23>0):\n", - " i='{0:.2e}'.format(123.23) #for ios::scientific \n", - " i='+'+i\n", - " print i\n", - "else :\n", - " print '{0:.2e}'.format(123.23)\n", - " \n", - " \n", - "print '{:#>10}'.format(str(123)), #for ios::fill\n", - "if(123.23>0): \n", - " i='{0:.2e}'.format(123.23) #for ios::scientific \n", - " i='+'+i\n", - " print i\n", - "else :\n", - " print '{0:.2e}'.format(123.23)\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "+123 +1.232300e+02\n", - " 123.23 +1.23e+02\n", - "#######123 +1.23e+02\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.6, Page Number: 432<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "print '{0:.0e}'.format(1000.243) #for setprecision\n", - "print '{:>20}'.format(\"Hello There\") #to set width and right align\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "1e+03\n", - " Hello There\n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.7, Page Number: 433<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "print '{0:+d}'.format(123), #for ios::showpos\n", - "if(123.23>0):\n", - " i='{0:e}'.format(123.23) #for ios::scientific \n", - " i='+'+i\n", - " print i\n", - "else :\n", - " print '{0:e}'.format(123.23)\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "+123 +1.232300e+02\n" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.8, Page Number: 433<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "#User input\n", - "s=\" Hello\"\n", - "\n", - "#Result\n", - "print s.lstrip() #lstrip removes leading spaces" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Hello\n" - ] - } - ], - "prompt_number": 7 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.9, Page Number: 434<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def setup(s):\n", - " return '{:$<10}'.format(str(s))\n", - "\n", - "\n", - "#Result\n", - "print 10,setup(10)\n", - "\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "10 10$$$$$$$$\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.10, Page Number: 435<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def prompt():\n", - " print \"Enter number using hex format:\"\n", - " hex=0x46\n", - " return hex\n", - "\n", - "\n", - "#Result\n", - "i=prompt()\n", - "print i\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.11, Page Number: 438<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "out=open(\"test\",'w')\n", - "\n", - "\n", - "if(not(out)):\n", - " print \"Cannot open file.\"\n", - "else:\n", - " #Write to file\n", - " out.write(\"10 123.23\\n\")\n", - " out.write(\"This is a short text file.\")\n", - " #Close the file\n", - " out.close()" - ], - "language": "python", - "metadata": {}, - "outputs": [], - "prompt_number": 8 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.12, Page Number: 438<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - " \n", - "In=open(\"test\",'r')\n", - " \n", - "if(not(In)):\n", - " print \"Cannot open file.\"\n", - "else:\n", - " #Read file\n", - " i=In.read(2)\n", - " ch=In.read(1)\n", - " f=In.read(6)\n", - " str=In.read()\n", - " print i,f,ch\n", - " print str\n", - " #Close the file\n", - " out.close()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "10 123.23 \n", - "\n", - "This is a short text file.\n" - ] - } - ], - "prompt_number": 9 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.13, Page Number: 439<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "import sys\n", - " \n", - "if not(len(sys.argv)==2):\n", - " print \"Usage: PR <filename>\\n\"\n", - "else:\n", - " #Open a file\n", - " In=open(sys.argv[1],'r')\n", - "\n", - " #In case file cannot open\n", - " if(not(In)):\n", - " print \"Cannot open file.\"\n", - " else:\n", - " #Read file\n", - " ch=In.read()\n", - " print ch\n", - " In.close()\n", - " \n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Usage: PR <filename>\n", - "\n" - ] - } - ], - "prompt_number": 10 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.14, Page Number: 440<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "import sys\n", - "\n", - "p=\"hello there\"\n", - "\n", - "out=open(\"test\",'w')\n", - "\n", - "#In case file cannot open\n", - "if(not(out)):\n", - " print \"Cannot open file.\"\n", - "else:\n", - " #Write to file\n", - " for i in range(len(p)):\n", - " out.write(p[i])\n" - ], - "language": "python", - "metadata": {}, - "outputs": [], - "prompt_number": 11 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.15, Page Number: 441<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "import sys\n", - "\n", - "n=[1,2,3,4,5]\n", - "\n", - "#Open a file 'test'\n", - "out=open(\"test\",'w')\n", - "\n", - "#In case file cannot open\n", - "if(not(out)):\n", - " print \"Cannot open file.\"\n", - "else:\n", - " #Write to file\n", - " for i in range(5):\n", - " out.write(chr(n[i]))\n", - " out.close()\n", - " \n", - "for i in range(5): #clear array\n", - " n[i]=0\n", - " \n", - "#Open the file\n", - "In=open(\"test\",'r')\n", - "\n", - "#In case file cannot open\n", - "if(not(In)):\n", - " print \"Cannot open file.\"\n", - "else:\n", - " #Read file\n", - " for i in range(5):\n", - " n[i]=ord(In.read(1))\n", - "\n", - "#Result, shows value from file\n", - "for i in range(5):\n", - " print n[i],\n", - " \n", - "In.close()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "1 2 3 4 5\n" - ] - } - ], - "prompt_number": 12 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.16, Page Number: 442<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "import sys\n", - " \n", - "if not(len(sys.argv)==2):\n", - " print \"Usage: PR <filename>\\n\"\n", - "else:\n", - " #Open a file\n", - " In=open(sys.argv[1],'r')\n", - "\n", - " #In case file cannot open\n", - " if(not(In)):\n", - " print \"Cannot open file.\"\n", - " else:\n", - " #Read file\n", - " while True:\n", - " ch=In.read(1)\n", - " if not ch:\n", - " break\n", - " print ch,\n", - " #Close file\n", - " In.close()\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Usage: PR <filename>\n", - "\n" - ] - } - ], - "prompt_number": 13 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.17, Page Number: 443<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "out=open(\"test1\",'w')\n", - "out.write(\"Hello\")\n", - "out.close()\n", - " \n", - "out=open(\"test2\",'w')\n", - "out.write(\"There\")\n", - "out.close()\n", - "\n", - " \n", - "f1=open(\"test1\",'r')\n", - " \n", - "if(not(In)):\n", - " print \"Cannot open file.\"\n", - "\n", - " \n", - "f2=open(\"test2\",'r')\n", - " \n", - "if(not(In)):\n", - " print \"Cannot open file.\"\n", - " \n", - "print \"Comparing files...\"\n", - "\n", - "buf1=f1.read()\n", - "buf2=f2.read()\n", - "print buf1,buf2\n", - "\n", - "if len(buf1)==len(buf2):\n", - " print \"Files are of different sizes.\"\n", - " f1.close()\n", - " f2.close()\n", - "else:\n", - " \n", - " flag=1\n", - " for i in range(len(buf1)):\n", - " if not(buf1[i]==buf2[i]):\n", - " print \"Files differ.\"\n", - " f1.close()\n", - " f2.close()\n", - " flag=0\n", - " break\n", - " if flag==1:\n", - " print \"Files are the same.\"\n", - " \n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Comparing files...\n", - "Hello There\n", - "Files are of different sizes.\n" - ] - } - ], - "prompt_number": 14 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.18, Page Number: 445<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "print \"Enter your name:\"\n", - " \n", - "str=\"hello world\"\n", - "\n", - "print str" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter your name:\n", - "hello world\n" - ] - } - ], - "prompt_number": 15 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.19, Page Number: 447<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " out=open(\"test\",'r+b')\n", - "out.write(\"Hello\")\n", - "\n", - " \n", - "if(not(out)):\n", - " print \"Cannot open file.\"\n", - "else:\n", - " out.seek(2,0)\n", - " out.write('X')\n", - " out.close()\n", - " \n" - ], - "language": "python", - "metadata": {}, - "outputs": [], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.20, Page Number: 447<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - " \n", - "out=open(\"test\",'r+b')\n", - "out.write(\"Hello\")\n", - "out.close()\n", - "\n", - "In=open(\"test\",'r')\n", - " \n", - "if(not(In)):\n", - " print \"Cannot open file.\"\n", - "else:\n", - " In.seek(2,0)\n", - " ch=In.read()\n", - " print ch\n", - " \n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "llo\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 18.21, Page Number: 450<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class three_d:\n", - " def __init__(self,a,b,c):\n", - " self.__x=a\n", - " self.__y=b\n", - " self.__z=c\n", - " def __repr__(self):\n", - " c=(\"%d\"%self.__x)+\", \"+(\"%d\"%self.__y)+\", \"+(\"%d\"%self.__z)+\"\\n\"\n", - " return c \n", - "\n", - " \n", - "a=three_d(1,2,3)\n", - "b=three_d(3,4,5)\n", - "c=three_d(5,6,7)\n", - " \n", - "out=open(\"threed\",'w')\n", - " \n", - "if(not(out)):\n", - " print \"Cannot open file.\"\n", - "else:\n", - " out.write(a.__repr__())\n", - " out.write(b.__repr__())\n", - " out.write(c.__repr__())\n", - "\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [], - "prompt_number": 18 - }, - { - "cell_type": "raw", - "metadata": {}, - "source": [] - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file diff --git a/C++_from_the_Ground/Chapter_19(1).ipynb b/C++_from_the_Ground/Chapter_19(1).ipynb deleted file mode 100755 index 7f04b7ea..00000000 --- a/C++_from_the_Ground/Chapter_19(1).ipynb +++ /dev/null @@ -1,725 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:20d38d92a3a25bc02f8bf5fb1b35ef1503e1987dff61d43c4881deab4561fe2c" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h1>Chapter 19: Run-Time Type ID and the Casting Operators<h1>" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 19.1, Page Number: 453<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class myclass:\n", - " pass\n", - "\n", - "#Variable declaration\n", - "i=j=0\n", - "f=0.0\n", - "ob=myclass()\n", - "\n", - "print \"The type of i is:\",type(i).__name__\n", - "print \"The type of f is:\",type(f).__name__\n", - "print \"The type of ob is:\",ob.__class__.__name__\n", - "print \"\\n\"\n", - "\n", - "if type(i)==type(j):\n", - " print \"The types of i and j are the same\"\n", - " \n", - "if not(type(i)==type(f)):\n", - " print \"The types of i and f are not the same\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "The type of i is: int\n", - "The type of f is: float\n", - "The type of ob is: myclass\n", - "\n", - "\n", - "The types of i and j are the same\n", - "The types of i and f are not the same\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 19.2, Page Number: 454<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class Base:\n", - " pass\n", - "class Derived1(Base):\n", - " pass\n", - "class Derived2(Base):\n", - " pass\n", - "\n", - "#Variable declaration\n", - "baseob=Base()\n", - "p=[Base()]\n", - "ob1=Derived1()\n", - "ob2=Derived2()\n", - "\n", - "\n", - "p[0]=baseob\n", - "print \"p is pointing to an object of type\",\n", - "print p[0].__class__.__name__\n", - "\n", - "p[0]=ob1\n", - "print \"p is pointing to an object of type\",\n", - "print p[0].__class__.__name__\n", - "\n", - "p[0]=ob2\n", - "print \"p is pointing to an object of type\",\n", - "print p[0].__class__.__name__" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "p is pointing to an object of type Base\n", - "p is pointing to an object of type Derived1\n", - "p is pointing to an object of type Derived2\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 19.3, Page Number: 455<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class Base:\n", - " pass\n", - "class Derived1(Base):\n", - " pass\n", - "class Derived2(Base):\n", - " pass\n", - "\n", - "def WhatType(ob):\n", - " print \"ob is referencing an object of type\",\n", - " print ob.__class__.__name__\n", - " \n", - " \n", - "#Variable declaration\n", - "baseob=Base()\n", - "p=[Base()]\n", - "ob1=Derived1()\n", - "ob2=Derived2()\n", - "\n", - "WhatType(baseob)\n", - "WhatType(ob1)\n", - "WhatType(ob2)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "ob is referencing an object of type Base\n", - "ob is referencing an object of type Derived1\n", - "ob is referencing an object of type Derived2\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 19.4, Page Number: 456<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "import random\n", - "\n", - "class figure:\n", - " def __init__(self,i,j):\n", - " self._x=i\n", - " self._y=j\n", - " \n", - "class triangle(figure):\n", - " def __init__(self,i,j):\n", - " figure.__init__(self,i,j)\n", - " def area(self):\n", - " return self._x*0.5*self._y\n", - " \n", - "class rectangle(figure):\n", - " def __init__(self,i,j):\n", - " figure.__init__(self,i,j)\n", - " def area(self):\n", - " return self._x*self._y\n", - " \n", - "class circle(figure):\n", - " def __init__(self,i,j=0):\n", - " figure.__init__(self,i,j)\n", - " def area(self):\n", - " return self._x*self._x*3.14\n", - " \n", - "def factory():\n", - " i=random.randint(0,2)\n", - " if i==0:\n", - " return circle(10.0)\n", - " elif i==1:\n", - " return triangle(10.1,5.3)\n", - " elif i==2:\n", - " return rectangle(4.3,5.7)\n", - " \n", - "\n", - "t=c=r=0 \n", - "p=[None]\n", - "\n", - "#generate and count objects\n", - "for i in range(10):\n", - " p[0]=factory() #generate an object\n", - " print \"Object is \",p[0].__class__.__name__,\". \",\n", - " #count it\n", - " if p[0].__class__.__name__==triangle.__name__:\n", - " t+=1\n", - " if p[0].__class__.__name__==rectangle.__name__:\n", - " r+=1\n", - " if p[0].__class__.__name__==circle.__name__:\n", - " c+=1\n", - " #display its area\n", - " print \"Area is\",p[0].area()\n", - "\n", - "print \"Objects generated:\"\n", - "print \"Triangles:\",t\n", - "print \"Rectangles:\",r\n", - "print \"Circles:\",c" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Object is circle . Area is 314.0\n", - "Object is rectangle . Area is 24.51\n", - "Object is rectangle . Area is 24.51\n", - "Object is circle . Area is 314.0\n", - "Object is rectangle . Area is 24.51\n", - "Object is circle . Area is 314.0\n", - "Object is rectangle . Area is 24.51\n", - "Object is triangle . Area is 26.765\n", - "Object is rectangle . Area is 24.51\n", - "Object is circle . Area is 314.0\n", - "Objects generated:\n", - "Triangles: 1\n", - "Rectangles: 5\n", - "Circles: 4\n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 19.5, Page Number: 456<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "class myclass:\n", - " def __init__(self,i):\n", - " self.__a=i\n", - " \n", - "o1=myclass(10)\n", - "o2=myclass(9)\n", - "o3=myclass(7.2)\n", - "\n", - "print \"Type of o1 is\",o1.__class__.__name__\n", - "\n", - "print \"Type of o2 is\",o2.__class__.__name__\n", - "\n", - "print \"Type of o3 is\",o3.__class__.__name__\n", - "\n", - "print\n", - "\n", - "if o1.__class__.__name__==o2.__class__.__name__:\n", - " print \"o1 and o2 are the same type\"\n", - " \n", - "if o1.__class__.__name__==o3.__class__.__name__:\n", - " print \"Error\"\n", - "else:\n", - " print \"o1 and o3 are different types\"\n", - "\n", - "#This prints error because python doesnt use templates." - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Type of o1 is myclass\n", - "Type of o2 is myclass\n", - "Type of o3 is myclass\n", - "\n", - "o1 and o2 are the same type\n", - "Error\n" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 19.6, Page Number: 460<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "import random\n", - "\n", - "class figure:\n", - " def __init__(self,i,j):\n", - " self._x=i\n", - " self._y=j\n", - " \n", - "class triangle(figure):\n", - " def __init__(self,i,j):\n", - " figure.__init__(self,i,j)\n", - " def area(self):\n", - " return self._x*0.5*self._y\n", - " \n", - "class rectangle(figure):\n", - " def __init__(self,i,j):\n", - " figure.__init__(self,i,j)\n", - " def area(self):\n", - " return self._x*self._y\n", - " \n", - "class circle(figure):\n", - " def __init__(self,i,j=0):\n", - " figure.__init__(self,i,j)\n", - " def area(self):\n", - " return self._x*self._x*3.14\n", - " \n", - "def factory():\n", - " i=random.randint(0,2)\n", - " if i==0:\n", - " return circle(10.0)\n", - " elif i==1:\n", - " return triangle(10.1,5.3)\n", - " elif i==2:\n", - " return rectangle(4.3,5.7)\n", - " \n", - "\n", - "t=c=r=0 \n", - "p=[None]\n", - "\n", - "#generate and count objects\n", - "for i in range(10):\n", - " p[0]=factory() #generate an object\n", - " print \"Object is \",p[0].__class__.__name__,\". \",\n", - " #count it\n", - " if p[0].__class__.__name__==triangle.__name__:\n", - " t+=1\n", - " if p[0].__class__.__name__==rectangle.__name__:\n", - " r+=1\n", - " if p[0].__class__.__name__==circle.__name__:\n", - " c+=1\n", - " #display its area\n", - " print \"Area is\",p[0].area()\n", - "\n", - "print \"Objects generated:\"\n", - "print \"Triangles:\",t\n", - "print \"Rectangles:\",r\n", - "print \"Circles:\",c" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Object is triangle . Area is 26.765\n", - "Object is circle . Area is 314.0\n", - "Object is circle . Area is 314.0\n", - "Object is rectangle . Area is 24.51\n", - "Object is rectangle . Area is 24.51\n", - "Object is rectangle . Area is 24.51\n", - "Object is circle . Area is 314.0\n", - "Object is circle . Area is 314.0\n", - "Object is rectangle . Area is 24.51\n", - "Object is triangle . Area is 26.765\n", - "Objects generated:\n", - "Triangles: 2\n", - "Rectangles: 4\n", - "Circles: 4\n" - ] - } - ], - "prompt_number": 7 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 19.7, Page Number: 463<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class Base:\n", - " def f(self):\n", - " print \"Inside Base\"\n", - " \n", - "class Derived(Base):\n", - " def f(self):\n", - " print \"Inside Derived\"\n", - " \n", - "bp=[Base()] #pointer to base\n", - "b_ob=Base() \n", - "dp=[Derived()] #pointer to derived\n", - "d_ob=Derived()\n", - "\n", - "dp[0]=d_ob\n", - "if dp[0]:\n", - " print \"Cast from Derived * to Derived * OK.\"\n", - " dp[0].f()\n", - "else:\n", - " print \"Error\"\n", - "print\n", - "\n", - "bp[0]=d_ob\n", - "if bp[0]:\n", - " print \"Cast from Derived * to Base * OK.\"\n", - " bp[0].f()\n", - "else:\n", - " print \"Error\"\n", - "print\n", - "\n", - "bp[0]=b_ob\n", - "if bp[0]:\n", - " print \"Cast from Base * to Base * OK.\"\n", - " bp[0].f()\n", - "else:\n", - " print \"Error\"\n", - "print\n", - "\n", - "dp[0]=b_ob\n", - "if dp[0]:\n", - " print \"Error\"\n", - "else:\n", - " print \"Cast from Base * to Derived * not OK.\"\n", - "print\n", - "\n", - "bp[0]=d_ob #bp points to Derived object\n", - "dp[0]=bp[0]\n", - "if dp[0]:\n", - " print \"Cast bp to a Derived * OK.\"\n", - " print \"because bp is really pointing\\n\",\n", - " print \"to a Derived object.\"\n", - " dp[0].f()\n", - "else:\n", - " print \"Error\"\n", - "print\n", - "\n", - "bp[0]=b_ob #bp points to Base object\n", - "dp[0]=bp[0]\n", - "if dp[0]:\n", - " print \"Error\"\n", - "else:\n", - " print \"Now casting bp to a Derived *\\n\",\n", - " print \"is not OK because bp is really\\n\",\n", - " print \" pointing to a Base object.\"\n", - "print\n", - "\n", - "dp[0]=d_ob #dp points to Derived object\n", - "bp[0]=dp[0]\n", - "if bp[0]:\n", - " print \"Casting dp to a Base * is OK.\"\n", - " bp[0].f()\n", - "else:\n", - " print \"Error\"\n", - "print\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Cast from Derived * to Derived * OK.\n", - "Inside Derived\n", - "\n", - "Cast from Derived * to Base * OK.\n", - "Inside Derived\n", - "\n", - "Cast from Base * to Base * OK.\n", - "Inside Base\n", - "\n", - "Error\n", - "\n", - "Cast bp to a Derived * OK.\n", - "because bp is really pointing\n", - "to a Derived object.\n", - "Inside Derived\n", - "\n", - "Error\n", - "\n", - "Casting dp to a Base * is OK.\n", - "Inside Derived\n", - "\n" - ] - } - ], - "prompt_number": 8 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 19.8, Page Number: 465<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class Base:\n", - " def f(self):\n", - " pass\n", - " \n", - "class Derived(Base):\n", - " def derivedOnly(self):\n", - " print \"Is a Derived Object\"\n", - " \n", - "bp=[Base()] #pointer to base\n", - "b_ob=Base() \n", - "dp=[Derived()] #pointer to derived\n", - "d_ob=Derived()\n", - "\n", - "#Use typeid\n", - "\n", - "bp[0]=b_ob\n", - "if bp[0].__class__.__name__==Derived.__name__:\n", - " dp[0]=bp[0]\n", - " dp[0].derivedOnly()\n", - "else:\n", - " print \"Cast from Base to Derived failed.\"\n", - " \n", - "bp[0]=d_ob\n", - "if bp[0].__class__.__name__==Derived.__name__:\n", - " dp[0]=bp[0]\n", - " dp[0].derivedOnly()\n", - "else:\n", - " print \"Error, cast should work!\"\n", - " \n", - "#Use dynamic_cast\n", - "\n", - "bp[0]=b_ob\n", - "dp[0]=bp[0]\n", - "if dp[0].__class__.__name__==Derived.__name__:\n", - " dp[0].derivedOnly()\n", - "else:\n", - " print \"Cast from Base to Derived failed.\"\n", - " \n", - "bp[0]=d_ob\n", - "dp[0]=bp[0]\n", - "if dp:\n", - " dp[0].derivedOnly()\n", - "else:\n", - " print \"Error, cast should work!\"\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Cast from Base to Derived failed.\n", - "Is a Derived Object\n", - "Cast from Base to Derived failed.\n", - "Is a Derived Object\n" - ] - } - ], - "prompt_number": 9 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 19.9, Page Number: 467<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def f(p):\n", - " v=p\n", - " v[0]=100\n", - " \n", - "#Variable declaration\n", - "x=[]\n", - "\n", - "x.append(99)\n", - "print \"x before call:\",x[0]\n", - "f(x)\n", - "print \"x after call:\",x[0]\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "x before call: 99\n", - "x after call: 100\n" - ] - } - ], - "prompt_number": 10 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 19.10, Page Number: 468<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "f=199.22\n", - "\n", - "i=f\n", - "print i\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "199.22\n" - ] - } - ], - "prompt_number": 11 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 19.11, Page Number: 469<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "i=0 #int\n", - "p=\"This is a string\"\n", - "\n", - "i=p #cast pointer to integer \n", - "\n", - "#Result\n", - "print i\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "This is a string\n" - ] - } - ], - "prompt_number": 12 - }, - { - "cell_type": "code", - "collapsed": false, - "input": [], - "language": "python", - "metadata": {}, - "outputs": [] - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file diff --git a/C++_from_the_Ground/Chapter_2(1).ipynb b/C++_from_the_Ground/Chapter_2(1).ipynb deleted file mode 100755 index b3976867..00000000 --- a/C++_from_the_Ground/Chapter_2(1).ipynb +++ /dev/null @@ -1,416 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:05876b24f412fcea817693bad944dc90f3dae4fb5142aeb7d9c2c98569067083" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h1>Chapter 2: An Overview of C++<h1>" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 2.1, Page Number: 12<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "print \"This is my first C++ program.\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "This is my first C++ program.\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 2.2, Page Number: 17<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "x=None \n", - "\n", - "x=1023 #this assigns 1023 to x\n", - "\n", - "#Result\n", - "print \"This program prints the value of x: \",x #prints x,i.e, 1023\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "This program prints the value of x: 1023\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 2.3, Page Number: 18<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "#Variable declaration\n", - "gallons=10 #User input\n", - "liters=None\n", - "\n", - "liters=gallons*4 #convert to liters\n", - "\n", - "#Result\n", - "print \"Liters: \",liters" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Liters: 40\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 2.4, Page Number: 20<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "\n", - "#Variable declaration\n", - "gallons=10.20 #User input\n", - "liters=None\n", - "\n", - "liters=gallons*3.7854 #convert to liters\n", - "\n", - "#Result\n", - "print \"Liters: \",liters" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Liters: 38.61108\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 2.5, Page Number: 21<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "def myfunc():\n", - " print \"Inside myfunc() \"\n", - " \n", - "print \"In main()\"\n", - "myfunc() #call myfunc()\n", - "print \"Back in main()\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "In main()\n", - "Inside myfunc() \n", - "Back in main()\n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 2.6, Page Number: 22<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "print abs(-10)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "10\n" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 2.7, Page Number: 23<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "def mul(x,y):\n", - " print x*y,\n", - "\n", - "#calling mul\n", - "mul(10,20)\n", - "mul(5,6)\n", - "mul(8,9)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "200 30 72\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 2.8, Page Number: 24<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "def mul(x,y):\n", - " return x*y #return product of x and y\n", - "\n", - "#Variable declaration\n", - "answer=mul(10,11) #assign return values\n", - "\n", - "#Result\n", - "print \"The answer is: \",answer\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "The answer is: 110\n" - ] - } - ], - "prompt_number": 8 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 2.9, Page Number: 26<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "print \"one\"\n", - "print \"two\" #prints in different line\n", - "print \"three\",\"four\" #prints all in same line" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "one\n", - "two\n", - "three four\n" - ] - } - ], - "prompt_number": 9 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 2.10, Page Number: 27<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "#Variable declaration\n", - "a=10 #user input for two numbers\n", - "b=20\n", - "\n", - "#Result\n", - "if a<b:\n", - " print \"First number is less than second. \"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "First number is less than second. \n" - ] - } - ], - "prompt_number": 10 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 2.11, Page Number: 28<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "for count in range(1,100+1):\n", - " print count," - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 2.12, Page Number: 30<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "a=10 #User input for two numbers\n", - "b=20\n", - "\n", - "#Result\n", - "if a<b:\n", - " print \"First number is less than second\"\n", - " print \"Their difference is: \",b-a\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "First number is less than second\n", - "Their difference is: 10\n" - ] - } - ], - "prompt_number": 12 - }, - { - "cell_type": "code", - "collapsed": false, - "input": [], - "language": "python", - "metadata": {}, - "outputs": [] - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file diff --git a/C++_from_the_Ground/Chapter_20(2).ipynb b/C++_from_the_Ground/Chapter_20(2).ipynb deleted file mode 100755 index 8771c6cd..00000000 --- a/C++_from_the_Ground/Chapter_20(2).ipynb +++ /dev/null @@ -1,937 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:aed4cbb1750b5cf45152f9d0ded2a9f36978f3177285c38b159fb79a8b0d35e4" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h1>Chapter 20: Namespaces and Other Advanced Topics<h1>" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 20.1, Page Number:474<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "\n", - "class CounterNameSpace:\n", - " upperbound=None\n", - " lowerbound=None\n", - " class counter:\n", - " def __init__(self,n):\n", - " if n<=CounterNameSpace.upperbound:\n", - " self.count=n\n", - " else:\n", - " self.count=CounterNameSpace.upperbound\n", - " def reset(self,n):\n", - " if n<=CounterNameSpace.upperbound:\n", - " self.count=n\n", - " def run(self):\n", - " if self.count>CounterNameSpace.lowerbound:\n", - " self.count-=1\n", - " return self.count\n", - " else:\n", - " return CounterNameSpace.lowerbound\n", - " \n", - "CounterNameSpace.upperbound=100\n", - "CounterNameSpace.lowerbound=0\n", - "\n", - "\n", - "ob1=CounterNameSpace.counter(10)\n", - "while True:\n", - " i=ob1.run()\n", - " print i,\n", - " if i<=CounterNameSpace.lowerbound:\n", - " break \n", - "print\n", - "\n", - "\n", - "ob2=CounterNameSpace.counter(20)\n", - "while True:\n", - " i=ob2.run()\n", - " print i,\n", - " if i<=CounterNameSpace.lowerbound:\n", - " break \n", - "print\n", - "\n", - "\n", - "ob2.reset(100)\n", - "CounterNameSpace.lowerbound=90\n", - "while True:\n", - " i=ob2.run()\n", - " print i,\n", - " if i<=CounterNameSpace.lowerbound:\n", - " break " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "9 8 7 6 5 4 3 2 1 0\n", - "19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0\n", - "99 98 97 96 95 94 93 92 91 90\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 20.2, Page Number:476<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "class CounterNameSpace:\n", - " upperbound=None\n", - " lowerbound=None\n", - " class counter:\n", - " def __init__(self,n):\n", - " if n<=c.upperbound:\n", - " self.count=n\n", - " else:\n", - " self.count=c.upperbound\n", - " def reset(self,n):\n", - " if n<=c.upperbound:\n", - " self.count=n\n", - " def run(self):\n", - " if self.count>c.lowerbound:\n", - " self.count-=1\n", - " return self.count\n", - " else:\n", - " return c.lowerbound\n", - "\n", - "#Use only upperbound using c\n", - "c=CounterNameSpace()\n", - "c.upperbound=100\n", - "CounterNameSpace.lowerbound=0\n", - "\n", - "\n", - "ob1=CounterNameSpace.counter(10)\n", - "while True:\n", - " i=ob1.run()\n", - " print i,\n", - " if i<=CounterNameSpace.lowerbound:\n", - " break \n", - "print\n", - "\n", - "#Now use entre CounterName Space using c\n", - "\n", - "ob2=c.counter(20)\n", - "while True:\n", - " i=ob2.run()\n", - " print i,\n", - " if i<=c.lowerbound:\n", - " break \n", - "print\n", - "\n", - "\n", - "ob2.reset(100)\n", - "c.lowerbound=90\n", - "while True:\n", - " i=ob2.run()\n", - " print i,\n", - " if i<=c.lowerbound:\n", - " break " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "9 8 7 6 5 4 3 2 1 0\n", - "19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0\n", - "99 98 97 96 95 94 93 92 91 90\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 20.3, Page Number:479<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "#User-input\n", - "print \"Enter a number:\"\n", - "val= 10.00\n", - "\n", - "#Result\n", - "print \"This is your number:\",val" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter a number:\n", - "This is your number: 10.0\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 20.4, Page Number:479<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "import sys\n", - "\n", - "#User-input\n", - "sys.stdout.write(\"Enter a number:\")\n", - "val= 10.00\n", - "\n", - "#Result\n", - "sys.stdout.write(\"\\nThis is your number: \")\n", - "sys.stdout.write(str(val))" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter a number:\n", - "This is your number: 10.0" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 20.5, Page Number:479<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "from sys import stdout\n", - "\n", - "#User-input\n", - "stdout.write(\"Enter a number:\")\n", - "val= 10.00\n", - "\n", - "#Result\n", - "stdout.write(\"\\nThis is your number: \")\n", - "stdout.write(str(val))" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter a number:\n", - "This is your number: 10.0" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 20.6, Page Number:480<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "def vline(i):\n", - " for j in xrange(i,0,-1):\n", - " print \"|\"\n", - "def hline(i):\n", - " for j in xrange(i,0,-1):\n", - " print \"-\",\n", - " print \n", - "\n", - "p=vline #p points to vline\n", - "p(4) #call vline()\n", - "\n", - "p=hline #p now points to hline\n", - "p(3) #call hline" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "|\n", - "|\n", - "|\n", - "|\n", - "- - -\n" - ] - } - ], - "prompt_number": 7 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 20.7, Page Number:482<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "import string\n", - "\n", - "def qsort(p):\n", - " if p == []: \n", - " return []\n", - " else:\n", - " pivot = p[0]\n", - " lesser = qsort([x for x in p[1:] if x < pivot])\n", - " greater = qsort([x for x in p[1:] if x >= pivot])\n", - " return lesser + [pivot] + greater\n", - " \n", - "#Variable Declaration \n", - "str=\"Function pointers provide flexibility.\" \n", - "\n", - "#sorting the string\n", - "str=qsort(str)\n", - "str=string.join(str)\n", - "\n", - "#Result\n", - "print \"sorted strng: \",str\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "sorted strng: . F b c d e e e f i i i i i i l l n n n o o o p p r r s t t t u v x y\n" - ] - } - ], - "prompt_number": 8 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 20.8, Page Number:482<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "import string\n", - "\n", - "def qsort(p):\n", - " \"\"\"Quicksort using list comprehensions\"\"\"\n", - " if p == []: \n", - " return []\n", - " else:\n", - " pivot = p[0]\n", - " lesser = qsort([x for x in p[1:] if x < pivot])\n", - " greater = qsort([x for x in p[1:] if x >= pivot])\n", - " return lesser + [pivot] + greater\n", - " \n", - "#Variable Declaration \n", - "num=[10,4,3,6,5,7,8]\n", - "\n", - "#sorting the string\n", - "num=qsort(num)\n", - "\n", - "#Result\n", - "for i in range(7):\n", - " print num[i],\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "3 4 5 6 7 8 10\n" - ] - } - ], - "prompt_number": 9 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 20.9, Page Number:484<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def space(count,ch=None):\n", - " if ch==None:\n", - " for i in xrange(count,0,-1):\n", - " print '',\n", - " else:\n", - " for i in xrange(count,0,-1):\n", - " print ch,\n", - " \n", - "\n", - "fp1=space\n", - "fp2=space\n", - "\n", - "fp1(22) #outputs 20 spaces\n", - "print \"|\\n\" \n", - "\n", - "fp2(30,'x') #output 30 xs \n", - "print \"|\\n\"\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - " |\n", - "\n", - "x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x |\n", - "\n" - ] - } - ], - "prompt_number": 10 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 20.10, Page Number:485<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "num=None #static variable\n", - "\n", - "class ShareVar:\n", - " def setnum(i=0):\n", - " global num\n", - " num=i\n", - " def shownum(self):\n", - " global num\n", - " print num\n", - " \n", - "#Variables declaration\n", - "a=ShareVar()\n", - "b=ShareVar()\n", - "\n", - "a.shownum() #prints None\n", - "b.shownum() #prints None\n", - "\n", - "num=10 #set static num to 10\n", - "\n", - "a.shownum() #prints 10\n", - "b.shownum() #prints 10\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "None\n", - "None\n", - "10\n", - "10\n" - ] - } - ], - "prompt_number": 11 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 20.11, Page Number:487<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class Demo:\n", - " i=None\n", - " j=None\n", - " def geti(self):\n", - " return self.i\n", - " def seti(self,x):\n", - " self.i=x\n", - " \n", - "ob=Demo()\n", - "\n", - "ob.seti(1900)\n", - "print ob.geti()\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "1900\n" - ] - } - ], - "prompt_number": 12 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 20.12, Page Number:488<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class myclass:\n", - " def __init__(self,x):\n", - " self.__a=x\n", - " def geta(self):\n", - " return self.__a\n", - " \n", - "#Variable declaration\n", - "ob=myclass(4)\n", - "\n", - "#Result\n", - "print ob.geta()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "4\n" - ] - } - ], - "prompt_number": 15 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 20.13, Page Number:489<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class myclass:\n", - " def __init__(self,x):\n", - " self.__a=x\n", - " def geta():\n", - " return self.__a\n", - " \n", - "ob = myclass(110)" - ], - "language": "python", - "metadata": {}, - "outputs": [], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 20.14, Page Number:490<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class myclass:\n", - " def __init__(self,i):\n", - " self.__num=i\n", - " def getnum(self):\n", - " return self.__num\n", - " \n", - "#Variable declaration\n", - "o=myclass(10)\n", - "\n", - "print o.getnum() #display 10\n", - "\n", - "o=myclass(1000)\n", - "\n", - "print o.getnum() #display 1000" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "10\n", - "1000\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 20.15, Page Number:491<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class myclass:\n", - " def __init__(self,x,y):\n", - " self.__numA=x\n", - " self.__numB=y\n", - " def getnumA(self):\n", - " return self.__numA\n", - " def getnumB(self):\n", - " return self.__numB\n", - " \n", - "#Variable declaration\n", - "ob1=myclass(7,9)\n", - "ob2=myclass(5,2)\n", - "\n", - "#Result\n", - "print \"Values in ob1 are \",ob1.getnumB(),\"and\",ob1.getnumA()\n", - "print \"Values in ob2 are \",ob2.getnumB(),\"and\",ob2.getnumA()\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Values in ob1 are 9 and 7\n", - "Values in ob2 are 2 and 5\n" - ] - } - ], - "prompt_number": 16 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 20.16, Page Number:492<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class myclass:\n", - " def __init__(self,x,y):\n", - " self.__numA=x\n", - " self.__numB=y\n", - " def getnumA(self):\n", - " return self.__numA\n", - " def getnumB(self):\n", - " return self.__numB\n", - " \n", - "#Variable declaration\n", - "ob1=myclass(7,9)\n", - "ob2=myclass(5,2)\n", - "\n", - "#Result\n", - "print \"Values in ob1 are \",ob1.getnumB(),\"and\",ob1.getnumA()\n", - "print \"Values in ob2 are \",ob2.getnumB(),\"and\",ob2.getnumA()\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Values in ob1 are 9 and 7\n", - "Values in ob2 are 2 and 5\n" - ] - } - ], - "prompt_number": 17 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 20.17, Page Number:494<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def myfunc():\n", - " print \"This links as a C function.\"\n", - " \n", - "myfunc()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "This links as a C function.\n" - ] - } - ], - "prompt_number": 18 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 20.18, Page Number:495<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "from ctypes import *\n", - "\n", - "class myclass: \n", - " sum=c_int(0)\n", - " def sum_it(self,x):\n", - " for i in range(x+1):\n", - " self.sum.value+=i\n", - " fp=sum_it #pointer to function\n", - "\n", - "#Variable declaration\n", - "c=myclass()\n", - "fp=myclass.sum_it #get address of function\n", - "dp=pointer(c.sum) #address of data\n", - "c.fp(7) #compute summation of 7\n", - "\n", - "\n", - "#Result\n", - "print \"summation of 7 is\",dp[0]\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "summation of 7 is 28\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 20.19, Page Number:496<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "from ctypes import *\n", - "\n", - "class myclass:\n", - " sum=c_int(0)\n", - " def sum_it(self,x): \n", - " for i in range(x+1):\n", - " self.sum.value+=i\n", - " fp=sum_it #pointer to function\n", - "\n", - "#Variable declaration\n", - "d=myclass()\n", - "c=[d] #ponter to object\n", - "fp=myclass.sum_it #get address of function\n", - "dp=pointer(c[0].sum) #get address of data\n", - "\n", - "c[0].fp(7) #compute summation of 7\n", - "\n", - "\n", - "#Result\n", - "print \"summation of 7 is\",dp[0]\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - " summation of 7 is 28\n" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 20.20, Page Number:497<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "class three_d:\n", - " def __init__(self,a,b,c): #3D coordinates\n", - " self.x=a\n", - " self.y=b\n", - " self.z=c\n", - " #Display x,y,z coordinates - three_d inserter.\n", - " def __repr__(self):\n", - " return str(self.x)+\", \"+str(self.y)+\", \"+str(self.z)+\"\\n\"\n", - " def __add__(self,op2):\n", - " if isinstance(op2,int):\n", - " c=self.x*self.y*self.z+op2\n", - " return c\n", - " temp=three_d(self.x+op2.x,self.y+op2.y,self.z+op2.z)\n", - " return temp\n", - " \n", - "a=three_d(1,2,3)\n", - "b=three_d(2,3,4)\n", - "\n", - "print a,b,\n", - "\n", - "print b+100 #displays 124 because of conversion to int\n", - "\n", - "a=a+b #add two three_d objects - no conversion\n", - "\n", - "print a #displays 3,5,7" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "1, 2, 3\n", - " 2, 3, 4\n", - " 124\n", - "3, 5, 7\n", - "\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "code", - "collapsed": false, - "input": [], - "language": "python", - "metadata": {}, - "outputs": [] - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file diff --git a/C++_from_the_Ground/Chapter_21(1).ipynb b/C++_from_the_Ground/Chapter_21(1).ipynb deleted file mode 100755 index 76345922..00000000 --- a/C++_from_the_Ground/Chapter_21(1).ipynb +++ /dev/null @@ -1,1187 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:1f6065242a1ea235eb4712b9ec147fb29625df8861301b52ad6c880a94467811" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h1>Chapter 21: Introducing the Standard Template Library<h1>" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 21.1, Page Number:507<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "v=[] #Create a zero length vector\n", - " \n", - "print \"Size =\",len(v)\n", - " \n", - "\n", - "for i in range(10):\n", - " v.append(i)\n", - " \n", - "#display current size of v\n", - "print \"Current contents: \"\n", - "print \"Size now =\",len(v)\n", - "\n", - "#display contents of vector\n", - "for i in range(len(v)):\n", - " print v[i],\n", - " \n", - "print\n", - " \n", - "#put more values onto end of vector\n", - "#again, vector will grow as needed.\n", - "for i in range(10):\n", - " v.append(i+10)\n", - "#display current size of v\n", - "print \"Size now =\",len(v)\n", - "\n", - "#display contents of vector\n", - "print \"Current contents:\"\n", - "for i in range(len(v)):\n", - " print v[i],\n", - "print\n", - " \n", - "#change contents of vector\n", - "for i in range(len(v)):\n", - " v[i]=v[i]+v[i]\n", - " \n", - "#display contents of vector\n", - "print \"Contents doubled:\"\n", - "for i in range(len(v)):\n", - " print v[i]," - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Size = 0\n", - "Current contents: \n", - "Size now = 10\n", - "0 1 2 3 4 5 6 7 8 9\n", - "Size now = 20\n", - "Current contents:\n", - "0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19\n", - "Contents doubled:\n", - "0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 21.2, Page Number:508<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "#Variable declaration\n", - "v=[] #Create a zero length vector\n", - "\n", - "#put values onto end of vector\n", - "for i in range(10):\n", - " v.append(chr(ord('A')+i))\n", - " \n", - "#can access vector contents using subscripts\n", - "for i in range(len(v)):\n", - " print v[i], \n", - "print\n", - " \n", - "#access via iterator\n", - "for p in v:\n", - " print p,\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "A B C D E F G H I J\n", - "A B C D E F G H I J\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 21.3, Page Number:509<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "#Variable declaration\n", - "v=[] #Create a zero length vector\n", - "\n", - "#put values onto end of vector\n", - "for i in range(10):\n", - " v.append(chr(ord('A')+i))\n", - " \n", - "#Display original contents of vector\n", - "print \"Size =\",len(v)\n", - "print \"Original contents:\"\n", - "for i in range(len(v)):\n", - " print v[i], \n", - "print \"\\n\"\n", - " \n", - "p=2 #point to 3rd element\n", - "for i in range(10):\n", - " v.insert(p+i,'X')\n", - " \n", - "#display contents after insertion\n", - "print \"Size after insert =\",len(v)\n", - "print \"Contents after insert:\"\n", - "for i in range(len(v)):\n", - " print v[i],\n", - "print \"\\n\"\n", - "\n", - "#remove those elements\n", - "p=2 #point to 3rd element\n", - "for i in range(10):\n", - " v.pop(p)\n", - " \n", - "#display contents after insertion\n", - "print \"Size after erase =\",len(v)\n", - "print \"Contents after insert:\"\n", - "for i in range(len(v)):\n", - " print v[i],\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Size = 10\n", - "Original contents:\n", - "A B C D E F G H I J \n", - "\n", - "Size after insert = 20\n", - "Contents after insert:\n", - "A B X X X X X X X X X X C D E F G H I J \n", - "\n", - "Size after erase = 10\n", - "Contents after insert:\n", - "A B C D E F G H I J\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 21.4, Page Number:511<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class three_d:\n", - " def __init__(self,a,b,c): #3D coordinates\n", - " self.x=a\n", - " self.y=b\n", - " self.z=c\n", - " #Display x,y,z coordinates - three_d inserter.\n", - " def __repr__(self):\n", - " return str(self.x)+\", \"+str(self.y)+\", \"+str(self.z)+\"\\n\"\n", - " def __add__(self,a):\n", - " self.x+=a\n", - " self.y+=a\n", - " self.z+=a\n", - " return self\n", - " def __lt__(self,b):\n", - " return (self.x+self.y+self.z)<(b.x+b.y+b.z)\n", - " def __eq__(self,b):\n", - " return (self.x+self.y+self.z)==(b.x+b.y+b.z) \n", - " \n", - "#Variable declaration\n", - "v=[]\n", - "\n", - "#add objects to the vector\n", - "for i in range(10):\n", - " v.append(three_d(i,i+2,i-3))\n", - " \n", - "#Display contents of vector\n", - "for i in range(len(v)):\n", - " print v[i], \n", - "print\n", - "\n", - "\n", - "#Modify objects in a vector\n", - "for i in range(len(v)):\n", - " v[i]=v[i]+10 \n", - "\n", - "#Display modified vector\n", - "for i in range(len(v)):\n", - " print v[i], \n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "0, 2, -3\n", - " 1, 3, -2\n", - " 2, 4, -1\n", - " 3, 5, 0\n", - " 4, 6, 1\n", - " 5, 7, 2\n", - " 6, 8, 3\n", - " 7, 9, 4\n", - " 8, 10, 5\n", - " 9, 11, 6\n", - "\n", - "10, 12, 7\n", - " 11, 13, 8\n", - " 12, 14, 9\n", - " 13, 15, 10\n", - " 14, 16, 11\n", - " 15, 17, 12\n", - " 16, 18, 13\n", - " 17, 19, 14\n", - " 18, 20, 15\n", - " 19, 21, 16\n", - "\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 21.5, Page Number:513<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "v=[]\n", - "v2=[]\n", - "\n", - "for i in range(10):\n", - " v.append(chr(ord('A')+i))\n", - " \n", - "#Display original contents of vector\n", - "print \"Size =\",len(v)\n", - "print \"Original contents:\"\n", - "for i in range(len(v)):\n", - " print v[i], \n", - "print \"\\n\"\n", - "\n", - "#initialze second vector\n", - "str=\"-STL Power-\"\n", - "for i in range(len(str)):\n", - " v2.append(str[i])\n", - " \n", - "#get iterators to the middle of v and to the start and end of v2.\n", - "p=5\n", - "p2start=0\n", - "p2end=len(v2)-1\n", - "\n", - "#insert v2 into v\n", - "for i in range(p2end):\n", - " v.insert(p+i,v2[p2start+i])\n", - " \n", - "#display result\n", - "print \"Contents of v after inserton:\"\n", - "for i in range(len(v)):\n", - " print v[i],\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Size = 10\n", - "Original contents:\n", - "A B C D E F G H I J \n", - "\n", - "Contents of v after inserton:\n", - "A B C D E - S T L P o w e r F G H I J\n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 21.6, Page Number:517<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "#Variable declaration\n", - "lst=[] #create an empty list\n", - "\n", - "for i in range(10):\n", - " lst.append(chr(ord('A')+i))\n", - " \n", - "print \"Size =\",len(lst)\n", - "\n", - "print \"Contents:\",\n", - "for p in lst:\n", - " print p, \n", - "print \"\\n\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Size = 10\n", - "Contents: A B C D E F G H I J \n", - "\n" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 21.7, Page Number:518<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "#Variable declaration\n", - "lst=[] \n", - "revlst=[]\n", - "\n", - "for i in range(10):\n", - " lst.append(chr(ord('A')+i))\n", - " \n", - "print \"Size of list =\",len(lst)\n", - "\n", - "print \"Original Contents:\",\n", - "#Remove elements from lst and put them into revlst in reverse order.\n", - "for p in lst:\n", - " print p, \n", - " revlst.insert(0,p) \n", - "for i in range(10):\n", - " lst.pop(0)\n", - "print \"\\n\"\n", - "\n", - "print \"Size of revlst =\",len(revlst)\n", - "\n", - "print \"Reversed Contents:\",\n", - "for p in revlst:\n", - " print p," - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Size of list = 10\n", - "Original Contents: A B C D E F G H I J \n", - "\n", - "Size of revlst = 10\n", - "Reversed Contents: J I H G F E D C B A\n" - ] - } - ], - "prompt_number": 8 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 21.8, Page Number:519<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "import random\n", - "\n", - "#Variable declaration\n", - "lst=[] \n", - "\n", - "#create a list of random integers\n", - "for i in range(10):\n", - " lst.append(random.randint(0,100))\n", - "\n", - "print \"Original Contents:\",\n", - "for p in lst:\n", - " print p, \n", - "print \"\\n\"\n", - "\n", - "#sort the list\n", - "lst.sort()\n", - "\n", - "print \"Sorted Contents:\",\n", - "for p in lst:\n", - " print p," - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Original Contents: 75 73 72 4 88 7 85 21 67 42 \n", - "\n", - "Sorted Contents: 4 7 21 42 67 72 73 75 85 88\n" - ] - } - ], - "prompt_number": 9 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 21.9, Page Number:520<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "#Variable declaration\n", - "lst1=[] \n", - "lst2=[]\n", - "\n", - "for i in xrange(0,10,2):\n", - " lst1.append(chr(ord('A')+i))\n", - "for i in xrange(1,11,2):\n", - " lst2.append(chr(ord('A')+i))\n", - "\n", - "print \"Contents of lst1:\",\n", - "for p in lst1:\n", - " print p, \n", - "print \"\\n\"\n", - "\n", - "print \"Contents of lst2:\",\n", - "for p in lst2:\n", - " print p, \n", - "print \"\\n\"\n", - "\n", - "#merge the lists\n", - "lst1=lst1+lst2\n", - "lst1.sort()\n", - "lst2=[]\n", - "\n", - "if lst2==[]:\n", - " print \"lst2 is now empty\"\n", - "\n", - "print \"Contentsof lst1 after merge:\"\n", - "for p in lst1:\n", - " print p," - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Contents of lst1: A C E G I \n", - "\n", - "Contents of lst2: B D F H J \n", - "\n", - "lst2 is now empty\n", - "Contentsof lst1 after merge:\n", - "A B C D E F G H I J\n" - ] - } - ], - "prompt_number": 10 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 21.10, Page Number:521<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "class myclass:\n", - " def __init__(self,i=0,j=0):\n", - " self.__a=i\n", - " self.__b=j\n", - " self.sum=self.__a+self.__b\n", - " def getsum(self):\n", - " return self.sum\n", - " def __lt__(self,o2):\n", - " return self.sum<o2.sum\n", - " def __gt__(self,o2):\n", - " return self.sum>o2.sum\n", - " def __eq__(self,o2):\n", - " return self.sum==o2.sum\n", - " def __ne__(self, other):\n", - " return not self.__eq__(self)\n", - " \n", - "#create first list\n", - "lst1=[]\n", - "for i in range(10):\n", - " lst1.append(myclass(i,i))\n", - " \n", - "print \"First list:\",\n", - "for p in lst1:\n", - " print p.getsum(),\n", - "print\n", - "\n", - "#create second list\n", - "lst2=[]\n", - "for i in range(10):\n", - " lst2.append(myclass(i*2,i*3))\n", - " \n", - "print \"First list:\",\n", - "for p in lst2:\n", - " print p.getsum(),\n", - "print\n", - " \n", - "#Now merge list\n", - "lst1=lst1+lst2\n", - "lst1.sort()\n", - "\n", - "#Display merge list\n", - "print \"Merged list:\",\n", - "for p in lst1:\n", - " print p.getsum(),\n", - "print" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "First list: 0 2 4 6 8 10 12 14 16 18\n", - "First list: 0 5 10 15 20 25 30 35 40 45\n", - "Merged list: 0 0 2 4 5 6 8 10 10 12 14 15 16 18 20 25 30 35 40 45\n" - ] - } - ], - "prompt_number": 12 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 21.11, Page Number:527<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "#Variable declaration\n", - "m=[] \n", - "\n", - "#define the function find\n", - "def find(x,ch):\n", - " for p in x:\n", - " if p[0]==ch:\n", - " return p\n", - " return -1\n", - "\n", - "#put pairs into map\n", - "for i in range(10):\n", - " m.append([chr(ord('A')+i),i])\n", - " \n", - "#User Input\n", - "ch='D'\n", - "\n", - "#find value of the given key\n", - "p=find(m,ch)\n", - "\n", - "if not(p==-1):\n", - " print p[1]\n", - "else:\n", - " print \"Key not in the map\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "3\n" - ] - } - ], - "prompt_number": 13 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 21.12, Page Number:528<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "def find(x,ch):\n", - " for p in x:\n", - " if p[0].get()==ch.get():\n", - " return p\n", - " return -1\n", - "\n", - "\n", - "class word:\n", - " def __init__(self,s=\"\"):\n", - " self.str=s\n", - " def get(self):\n", - " return self.str\n", - " #must define less than relative to word objects\n", - " def __lt__(self,b):\n", - " return self.str<b.str\n", - "\n", - "class meaning:\n", - " def __init__(self,s=\"\"):\n", - " self.str=s\n", - " def get(self):\n", - " return self.str\n", - "\n", - "dictionary=[]\n", - "\n", - "dictionary.append([word(\"house\"),meaning(\"A place of dwelling\")])\n", - "dictionary.append([word(\"keyboard\"),meaning(\"An input device\")])\n", - "dictionary.append([word(\"programming\"),meaning(\"The act of writing a program\")])\n", - "dictionary.append([word(\"STL\"),meaning(\"Standard Template Library\")])\n", - "\n", - "#given a word, find meaning\n", - "print \"Enter word:\"\n", - "str=\"house\" #User input\n", - "\n", - "p=find(dictionary,word(str))\n", - "\n", - "if not(p==-1):\n", - " print \"Definition:\",p[1].get()\n", - "else:\n", - " print \"Word not in the dictionary.\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter word:\n", - "Definition: A place of dwelling\n" - ] - } - ], - "prompt_number": 14 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 21.13, Page Number:532<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "def isvowel(ch):\n", - " ch=ch.lower()\n", - " if (ch=='a' or ch=='e'or ch=='i' or ch=='o' or ch=='u'):\n", - " return 1\n", - " else:\n", - " return 0\n", - "\n", - "str=\"STL programming is powerful.\"\n", - "v=[]\n", - "\n", - "for i in range(len(str)):\n", - " v.append(str[i])\n", - " \n", - "print \"Sequence:\",\n", - "for i in range(len(v)):\n", - " print v[i],\n", - "print\n", - "\n", - "n=str.count('p')\n", - "print n,\"characters are p\"\n", - "\n", - "#count if vowel\n", - "n=0\n", - "for i in v:\n", - " if isvowel(i):\n", - " n+=1\n", - " \n", - "print n,\"characters are vowels.\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Sequence: S T L p r o g r a m m i n g i s p o w e r f u l .\n", - "2 characters are p\n", - "7 characters are vowels.\n" - ] - } - ], - "prompt_number": 15 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 21.14, Page Number:534<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "str=\"This is a test\"\n", - "v=[]\n", - "v2=[]\n", - "\n", - "for i in range(len(str)):\n", - " v.append(str[i])\n", - " \n", - "# ***implement remove_copy***\n", - "print \"Input sequence:\",\n", - "for i in range(len(v)):\n", - " print v[i],\n", - "print \n", - "\n", - "#Remove all i's\n", - "v2 = str.replace(\"i\", \"\")\n", - "\n", - "print \"Result after removing i's: \",\n", - "print v2,\"\\n\"\n", - "\n", - "\n", - "# ***implement replace_copy***\n", - "print \"Input sequence:\",\n", - "for i in range(len(v)):\n", - " print v[i],\n", - "print \n", - "\n", - "#Replace s's with X's\n", - "v2 = str.replace(\"s\", \"X\")\n", - "\n", - "print \"Result after replacning s's with X's: \",\n", - "print v2" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Input sequence: T h i s i s a t e s t\n", - "Result after removing i's: Ths s a test \n", - "\n", - "Input sequence: T h i s i s a t e s t\n", - "Result after replacning s's with X's: ThiX iX a teXt\n" - ] - } - ], - "prompt_number": 16 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 21.15, Page Number:535<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "v=[]\n", - "\n", - "for i in range(10):\n", - " v.append(i)\n", - " \n", - "print \"Initial:\",\n", - "for i in range(len(v)):\n", - " print v[i],\n", - "print\n", - "\n", - "#Reversing the list\n", - "v.reverse()\n", - "\n", - "print \"Reversed:\",\n", - "for i in range(len(v)):\n", - " print v[i],\n", - "print \n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Initial: 0 1 2 3 4 5 6 7 8 9\n", - "Reversed: 9 8 7 6 5 4 3 2 1 0\n" - ] - } - ], - "prompt_number": 17 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 21.16, Page Number:536<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "def xform(i):\n", - " return i*i #square original value\n", - "\n", - "#the transorm function\n", - "def transform(x,f):\n", - " for i in range(len(x)):\n", - " x[i]= f(x[i])\n", - " \n", - "#Variable declaration\n", - "x1=[]\n", - "\n", - "#put values into list\n", - "for i in range(10):\n", - " x1.append(i)\n", - "\n", - "print \"Original contents of x1: \",\n", - "for p in x1:\n", - " print p,\n", - "print \n", - "\n", - "#transform x1\n", - "p=transform(x1,xform)\n", - "\n", - "print \"Transformed contents of x1:\",\n", - "for p in x1:\n", - " print p," - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Original contents of x1: 0 1 2 3 4 5 6 7 8 9\n", - "Transformed contents of x1: 0 1 4 9 16 25 36 49 64 81\n" - ] - } - ], - "prompt_number": 18 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 21.17, Page Number:540<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "str1=\"The string class gives \"\n", - "str2=\"C++ high strng handlng.\"\n", - "\n", - "#assign a string\n", - "str3=str1\n", - "print str1,\"\\n\",str3\n", - "\n", - "#Concatenate two strings\n", - "str3=str1+str2\n", - "print str3\n", - "\n", - "#Compare strings\n", - "if str3>str1:\n", - " print \"str3 > str1\"\n", - "if str3==str1+str2:\n", - " print \"str3 == str1+str2\"\n", - " \n", - "str1=\"This is a null-terminated string.\"\n", - "print str1\n", - "\n", - "#create a string object using another string object\n", - "str4=str1\n", - "print str4\n", - "\n", - "#nput a string\n", - "print \"Enter a string:\"\n", - "str4=\"Hello\"\n", - "print str4" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "The string class gives \n", - "The string class gives \n", - "The string class gives C++ high strng handlng.\n", - "str3 > str1\n", - "str3 == str1+str2\n", - "This is a null-terminated string.\n", - "This is a null-terminated string.\n", - "Enter a string:\n", - "Hello\n" - ] - } - ], - "prompt_number": 19 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 21.18, Page Number:542<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "str1=\"This is a test\"\n", - "str2=\"ABCDEFG\"\n", - "\n", - "print \"Initial strings:\"\n", - "print \"str1:\",str1\n", - "print \"str2:\",str2\n", - "print\n", - "\n", - "#demonstrate insert\n", - "print \"Insert str2 into str1:\"\n", - "str1=str1[:5]+str2+str1[5:]\n", - "print str1,\"\\n\"\n", - "\n", - "#demonstrate erase\n", - "print \"Remove 7 charecters from str1:\"\n", - "str1=str[:5]+str[5:]\n", - "print str1,\"\\n\"\n", - "\n", - "#demonstrate replace\n", - "print \"Replace 2 characters in str1 with str2:\"\n", - "str1=str1[:5]+str2+str1[7:]\n", - "print str1\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Initial strings:\n", - "str1: This is a test\n", - "str2: ABCDEFG\n", - "\n", - "Insert str2 into str1:\n", - "This ABCDEFGis a test \n", - "\n", - "Remove 7 charecters from str1:\n", - "This is a test \n", - "\n", - "Replace 2 characters in str1 with str2:\n", - "This ABCDEFG a test\n" - ] - } - ], - "prompt_number": 21 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 21.19, Page Number:543<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "import string\n", - "\n", - "#Variable declaration \n", - "s1=\"The string class makes string handling easy.\"\n", - "\n", - "i=string.find(s1,\"class\")\n", - "if not(i==-1):\n", - " print \"Match found at\",i\n", - " print \"Remaining string is:\",\n", - " s2=s1[i:]\n", - " print s2" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Match found at 11\n", - "Remaining string is: class makes string handling easy.\n" - ] - } - ], - "prompt_number": 22 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 21.20, Page Number:545<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "def find(x,ch):\n", - " for p in x:\n", - " if p[0]==ch:\n", - " return p\n", - " return -1\n", - "\n", - "\n", - "dictionary=[]\n", - "\n", - "dictionary.append([\"house\",\"A place of dwelling\"])\n", - "dictionary.append([\"keyboard\",\"An input device\"])\n", - "dictionary.append([\"programming\",\"The act of writing a program\"])\n", - "dictionary.append([\"STL\",\"Standard Template Library\"])\n", - "\n", - "#given a word, find meaning\n", - "print \"Enter word:\"\n", - "str=\"house\" #User input\n", - "\n", - "p=find(dictionary,str)\n", - "\n", - "if not(p==-1):\n", - " print \"Definition:\",p[1]\n", - "else:\n", - " print \"Word not in the dictionary.\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter word:\n", - "Definition: A place of dwelling\n" - ] - } - ], - "prompt_number": 23 - }, - { - "cell_type": "code", - "collapsed": false, - "input": [], - "language": "python", - "metadata": {}, - "outputs": [] - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file diff --git a/C++_from_the_Ground/Chapter_22(1).ipynb b/C++_from_the_Ground/Chapter_22(1).ipynb deleted file mode 100755 index 39b8cb88..00000000 --- a/C++_from_the_Ground/Chapter_22(1).ipynb +++ /dev/null @@ -1,347 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:2d1838d005f866ea4e20c93ff6de54ebfd0d0c26c2e3055fc05467a06502c1dc" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h1>Chapter 22: The C++ Preprocessor<h1>" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 22.1, Page Number: 550<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def MIN(a,b):\n", - " if a<b:\n", - " return a\n", - " else:\n", - " return b\n", - "\n", - "#Variable declaration\n", - "x=10\n", - "y=20\n", - "\n", - "#Result\n", - "print \"The minimum is\",MIN(x,y)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "The minimum is 10\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 22.2, Page Number: 551<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "def EVEN(a):\n", - " if a%2==0:\n", - " return 1\n", - " else:\n", - " return 0\n", - "\n", - "if EVEN(9+1):\n", - " print \"is even\"\n", - "else:\n", - " print \"is odd\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "is even\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 22.3, Page Number: 551<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def EVEN(a):\n", - " if a%2==0:\n", - " return 1\n", - " else:\n", - " return 0\n", - "\n", - "if EVEN(9+1):\n", - " print \"is even\"\n", - "else:\n", - " print \"is odd\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "is even\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 22.4, Page Number: 553<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def MAX():\n", - " return 100\n", - "\n", - "if MAX()>10:\n", - " print \"Extra memory required.\"\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Extra memory required.\n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 22.5, Page Number: 554<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def MAX():\n", - " return 6\n", - "\n", - "if MAX()>10:\n", - " print \"Extra memory required.\"\n", - "else:\n", - " print \"Current memory OK.\"\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Current memory OK.\n" - ] - } - ], - "prompt_number": 7 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 22.6, Page Number: 556<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def TOM():\n", - " pass\n", - "\n", - "\n", - "try:\n", - " TOM()\n", - "except NameError:\n", - " print \"Programmer is unknown.\"\n", - "else:\n", - " print \"Programmer is Tom.\"\n", - " \n", - "try:\n", - " RALPH()\n", - "except NameError:\n", - " print \"RALPH not defined.\"\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Programmer is Tom.\n", - "RALPH not defined.\n" - ] - } - ], - "prompt_number": 8 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 22.7, Page Number: 558<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "import inspect\n", - " \n", - "def lineno():\n", - " return inspect.currentframe().f_back.f_lineno\n", - " \n", - "print lineno()+200\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "209\n" - ] - } - ], - "prompt_number": 9 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 22.8, Page Number: 559<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "def mkstr(s):\n", - " return str(s)\n", - "\n", - "#Result\n", - "print mkstr('I like C++')\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "I like C++\n" - ] - } - ], - "prompt_number": 10 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 22.9, Page Number: 560<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def concat(a,b):\n", - " return a+b\n", - "#Variable declaration\n", - "xy=10\n", - "\n", - "#Result\n", - "exec(\"print %s\")%concat('x','y')" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "10\n" - ] - } - ], - "prompt_number": 11 - }, - { - "cell_type": "code", - "collapsed": false, - "input": [], - "language": "python", - "metadata": {}, - "outputs": [] - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file diff --git a/C++_from_the_Ground/Chapter_3(1).ipynb b/C++_from_the_Ground/Chapter_3(1).ipynb deleted file mode 100755 index 9d0cf326..00000000 --- a/C++_from_the_Ground/Chapter_3(1).ipynb +++ /dev/null @@ -1,473 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:13095677f4efa0909d9aeb25aa7b8b57bea864fd1d9a0a797f9e06000e52dc18" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h1>Chapter 3: The Basic Data Types<h1>" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 3.1, Page Number: 35<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "def func():\n", - " x=-199 #local to func\n", - " print x #displays -199 \n", - " \n", - "x=10 #local to main\n", - "\n", - "func()\n", - "print x #displays 10\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "-199\n", - "10\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 3.2, Page Number: 37<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "def func1():\n", - " global count\n", - " print \"count: \",count #access global count\n", - " func2()\n", - "\n", - "def func2():\n", - " for count in range(3): #this is local variable\n", - " print '.',\n", - "\n", - " \n", - "count=None #global variables\n", - "\n", - "for i in range(10):\n", - " count=i*2\n", - " func1()\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "count: 0\n", - ". . . count: 2\n", - ". . . count: 4\n", - ". . . count: 6\n", - ". . . count: 8\n", - ". . . count: 10\n", - ". . . count: 12\n", - ". . . count: 14\n", - ". . . count: 16\n", - ". . . count: 18\n", - ". . .\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 3.3, Page Number: 40<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "from ctypes import *\n", - "\n", - "#Variable declaration\n", - "j=c_uint(60000)\n", - "i=c_int(60000)\n", - "\n", - "#Result\n", - "print i.value,j.value" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "60000 60000\n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 3.4, Page Number: 41<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "for letter in xrange(ord('Z'),ord('A')-1,-1):\n", - " print chr(letter),\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Z Y X W V U T S R Q P O N M L K J I H G F E D C B A\n" - ] - } - ], - "prompt_number": 8 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 3.5, Page Number: 44<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "print \"\\n\\\\\\b\"\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "\\\b\n" - ] - } - ], - "prompt_number": 9 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 3.6, Page Number: 45<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def total(x):\n", - " sum=0\n", - " for i in xrange(1,x+1):\n", - " sum=sum+i\n", - " for count in range(10):\n", - " print '-',\n", - " print \"The current sum is\",sum\n", - " \n", - "print \"Computing summation of 5.\"\n", - "total(5)\n", - "\n", - "print \"Computing summation of 6.\"\n", - "total(6)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Computing summation of 5.\n", - "- - - - - - - - - - The current sum is 1\n", - "- - - - - - - - - - The current sum is 3\n", - "- - - - - - - - - - The current sum is 6\n", - "- - - - - - - - - - The current sum is 10\n", - "- - - - - - - - - - The current sum is 15\n", - "Computing summation of 6.\n", - "- - - - - - - - - - The current sum is 1\n", - "- - - - - - - - - - The current sum is 3\n", - "- - - - - - - - - - The current sum is 6\n", - "- - - - - - - - - - The current sum is 10\n", - "- - - - - - - - - - The current sum is 15\n", - "- - - - - - - - - - The current sum is 21\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 3.7, Page Number: 47<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "#Variable declaration\n", - "x=10\n", - "y=3\n", - "\n", - "print x/y #will display 3\n", - "print x%y #will display1, the remainder\n", - "\n", - "x=1\n", - "y=2\n", - "\n", - "#Result\n", - "print x/y,x%y #will display 0 1" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "3\n", - "1\n", - "0 1\n" - ] - } - ], - "prompt_number": 11 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 3.8, Page Number: 51<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "def xor(a,b):\n", - " return (a or b)and(not(a and b))\n", - "\n", - "#User-input\n", - "print \"Enter P(0 or 1):\"\n", - "p=1 \n", - "print \"Enter Q(0 or 1):\"\n", - "q=0\n", - "\n", - "#Result\n", - "print \"P AND Q:\",(p and q)\n", - "print \"P OR Q:\",(p or q)\n", - "print \"P XOR Q:\",xor(p,q)\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter P(0 or 1):\n", - "Enter Q(0 or 1):\n", - "P AND Q: 0\n", - "P OR Q: 1\n", - "P XOR Q: True\n" - ] - } - ], - "prompt_number": 12 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 3.9, Page Number: 54<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "for i in xrange(1,100+1):\n", - " print i,\"/ 2 is:\",float(i)/2\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "1 / 2 is: 0.5\n", - "2 / 2 is: 1.0\n", - "3 / 2 is: 1.5\n", - "4 / 2 is: 2.0\n", - "5 / 2 is: 2.5\n", - "6 / 2 is: 3.0\n", - "7 / 2 is: 3.5\n", - "8 / 2 is: 4.0\n", - "9 / 2 is: 4.5\n", - "10 / 2 is: 5.0\n", - "11 / 2 is: 5.5\n", - "12 / 2 is: 6.0\n", - "13 / 2 is: 6.5\n", - "14 / 2 is: 7.0\n", - "15 / 2 is: 7.5\n", - "16 / 2 is: 8.0\n", - "17 / 2 is: 8.5\n", - "18 / 2 is: 9.0\n", - "19 / 2 is: 9.5\n", - "20 / 2 is: 10.0\n", - "21 / 2 is: 10.5\n", - "22 / 2 is: 11.0\n", - "23 / 2 is: 11.5\n", - "24 / 2 is: 12.0\n", - "25 / 2 is: 12.5\n", - "26 / 2 is: 13.0\n", - "27 / 2 is: 13.5\n", - "28 / 2 is: 14.0\n", - "29 / 2 is: 14.5\n", - "30 / 2 is: 15.0\n", - "31 / 2 is: 15.5\n", - "32 / 2 is: 16.0\n", - "33 / 2 is: 16.5\n", - "34 / 2 is: 17.0\n", - "35 / 2 is: 17.5\n", - "36 / 2 is: 18.0\n", - "37 / 2 is: 18.5\n", - "38 / 2 is: 19.0\n", - "39 / 2 is: 19.5\n", - "40 / 2 is: 20.0\n", - "41 / 2 is: 20.5\n", - "42 / 2 is: 21.0\n", - "43 / 2 is: 21.5\n", - "44 / 2 is: 22.0\n", - "45 / 2 is: 22.5\n", - "46 / 2 is: 23.0\n", - "47 / 2 is: 23.5\n", - "48 / 2 is: 24.0\n", - "49 / 2 is: 24.5\n", - "50 / 2 is: 25.0\n", - "51 / 2 is: 25.5\n", - "52 / 2 is: 26.0\n", - "53 / 2 is: 26.5\n", - "54 / 2 is: 27.0\n", - "55 / 2 is: 27.5\n", - "56 / 2 is: 28.0\n", - "57 / 2 is: 28.5\n", - "58 / 2 is: 29.0\n", - "59 / 2 is: 29.5\n", - "60 / 2 is: 30.0\n", - "61 / 2 is: 30.5\n", - "62 / 2 is: 31.0\n", - "63 / 2 is: 31.5\n", - "64 / 2 is: 32.0\n", - "65 / 2 is: 32.5\n", - "66 / 2 is: 33.0\n", - "67 / 2 is: 33.5\n", - "68 / 2 is: 34.0\n", - "69 / 2 is: 34.5\n", - "70 / 2 is: 35.0\n", - "71 / 2 is: 35.5\n", - "72 / 2 is: 36.0\n", - "73 / 2 is: 36.5\n", - "74 / 2 is: 37.0\n", - "75 / 2 is: 37.5\n", - "76 / 2 is: 38.0\n", - "77 / 2 is: 38.5\n", - "78 / 2 is: 39.0\n", - "79 / 2 is: 39.5\n", - "80 / 2 is: 40.0\n", - "81 / 2 is: 40.5\n", - "82 / 2 is: 41.0\n", - "83 / 2 is: 41.5\n", - "84 / 2 is: 42.0\n", - "85 / 2 is: 42.5\n", - "86 / 2 is: 43.0\n", - "87 / 2 is: 43.5\n", - "88 / 2 is: 44.0\n", - "89 / 2 is: 44.5\n", - "90 / 2 is: 45.0\n", - "91 / 2 is: 45.5\n", - "92 / 2 is: 46.0\n", - "93 / 2 is: 46.5\n", - "94 / 2 is: 47.0\n", - "95 / 2 is: 47.5\n", - "96 / 2 is: 48.0\n", - "97 / 2 is: 48.5\n", - "98 / 2 is: 49.0\n", - "99 / 2 is: 49.5\n", - "100 / 2 is: 50.0\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "code", - "collapsed": false, - "input": [], - "language": "python", - "metadata": {}, - "outputs": [] - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file diff --git a/C++_from_the_Ground/Chapter_4(1).ipynb b/C++_from_the_Ground/Chapter_4(1).ipynb deleted file mode 100755 index ca8e76db..00000000 --- a/C++_from_the_Ground/Chapter_4(1).ipynb +++ /dev/null @@ -1,1340 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:f944500666c92742656b5a25638e93a82042acafb19b205aac684e2d9ac2df51" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h1>Chapter 4: Program Control Statements<h1>" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 4.1, Page Number: 58<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "import random\n", - "\n", - "#Variable declaration and initialization\n", - "magic = random.randint(0,100) #Number which the user has to guess\n", - "guess = 10 #Number which the user guesses\n", - "\n", - "if guess == magic:\n", - " print \"***Right***\" #Result\n", - "\n", - "\n", - " \n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [], - "prompt_number": 10 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 4.2, Page Number: 59<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "\n", - "import random\n", - "\n", - "#Variable decleration and initialization\n", - "magic=random.randint(0,100) #Number to be guessed\n", - "guess = 10 #Number the user guesses\n", - "\n", - "#Result\n", - "if guess == magic:\n", - " print \"***Right***\"\n", - "else:\n", - " print \"... Sorry, you're wrong.\"\n", - "\n", - "\n", - "\n", - " \n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "... Sorry, you're wrong.\n" - ] - } - ], - "prompt_number": 8 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 4.3, Page Number: 60<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "#Variable decleration and initialization\n", - "a = 30 #User input for the two nos.\n", - "b = 10\n", - "\n", - "#Calculation and Result\n", - "if b:\n", - " print a/b\n", - "else:\n", - " print \"Cannot divide by zero.\"\n", - "\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "3\n" - ] - } - ], - "prompt_number": 11 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 4.4, Page Number: 61<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "\n", - "import random\n", - "\n", - "#Variable decleration \n", - "magic = random.randint(0,100) #Numbr to be guessed\n", - "guess = 10 #Number the user guesses\n", - "\n", - "#Result\n", - "if guess == magic:\n", - " print \"***Right***\"\n", - " print magic,\" is the magic number.\"\n", - "else:\n", - " print \"... Sorry, you're wrong\" \n", - " if(guess>magic): #use a nested if statement\n", - " print \"Your guess is too high\"\n", - " else:\n", - " print \"Your guess is too low\"\n", - "\n", - " \n", - " \n", - "\n", - " \n", - " \n" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 4.5, Page Number: 62<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "\n", - "for x in range(6):\n", - " if x==1: #using if-else ladder in a for loop\n", - " print \"x is one\"\n", - " elif x==2:\n", - " print \"x is two\"\n", - " elif x==3:\n", - " print \"x is three\"\n", - " elif x==4:\n", - " print \"x is four\"\n", - " else:\n", - " print \"x is not between 1 nd 4\"\n", - " \n", - " \n", - " \n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "x is not between 1 nd 4\n", - "x is one\n", - "x is two\n", - "x is three\n", - "x is four\n", - "x is not between 1 nd 4\n" - ] - } - ], - "prompt_number": 13 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 4.6, Page Number: 63<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "import math\n", - "\n", - "for num in range(100):\n", - " sq_root = math.sqrt(float(num)) #Calculation\n", - " print num,\" \",sq_root #Result\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "0 0.0\n", - "1 1.0\n", - "2 1.41421356237\n", - "3 1.73205080757\n", - "4 2.0\n", - "5 2.2360679775\n", - "6 2.44948974278\n", - "7 2.64575131106\n", - "8 2.82842712475\n", - "9 3.0\n", - "10 3.16227766017\n", - "11 3.31662479036\n", - "12 3.46410161514\n", - "13 3.60555127546\n", - "14 3.74165738677\n", - "15 3.87298334621\n", - "16 4.0\n", - "17 4.12310562562\n", - "18 4.24264068712\n", - "19 4.35889894354\n", - "20 4.472135955\n", - "21 4.58257569496\n", - "22 4.69041575982\n", - "23 4.79583152331\n", - "24 4.89897948557\n", - "25 5.0\n", - "26 5.09901951359\n", - "27 5.19615242271\n", - "28 5.29150262213\n", - "29 5.38516480713\n", - "30 5.47722557505\n", - "31 5.56776436283\n", - "32 5.65685424949\n", - "33 5.74456264654\n", - "34 5.83095189485\n", - "35 5.9160797831\n", - "36 6.0\n", - "37 6.0827625303\n", - "38 6.16441400297\n", - "39 6.2449979984\n", - "40 6.32455532034\n", - "41 6.40312423743\n", - "42 6.48074069841\n", - "43 6.5574385243\n", - "44 6.63324958071\n", - "45 6.7082039325\n", - "46 6.78232998313\n", - "47 6.8556546004\n", - "48 6.92820323028\n", - "49 7.0\n", - "50 7.07106781187\n", - "51 7.14142842854\n", - "52 7.21110255093\n", - "53 7.28010988928\n", - "54 7.34846922835\n", - "55 7.4161984871\n", - "56 7.48331477355\n", - "57 7.54983443527\n", - "58 7.61577310586\n", - "59 7.68114574787\n", - "60 7.74596669241\n", - "61 7.81024967591\n", - "62 7.87400787401\n", - "63 7.93725393319\n", - "64 8.0\n", - "65 8.0622577483\n", - "66 8.12403840464\n", - "67 8.18535277187\n", - "68 8.24621125124\n", - "69 8.30662386292\n", - "70 8.36660026534\n", - "71 8.42614977318\n", - "72 8.48528137424\n", - "73 8.54400374532\n", - "74 8.60232526704\n", - "75 8.66025403784\n", - "76 8.71779788708\n", - "77 8.77496438739\n", - "78 8.83176086633\n", - "79 8.88819441732\n", - "80 8.94427191\n", - "81 9.0\n", - "82 9.05538513814\n", - "83 9.11043357914\n", - "84 9.16515138991\n", - "85 9.21954445729\n", - "86 9.2736184955\n", - "87 9.32737905309\n", - "88 9.38083151965\n", - "89 9.43398113206\n", - "90 9.48683298051\n", - "91 9.53939201417\n", - "92 9.59166304663\n", - "93 9.64365076099\n", - "94 9.69535971483\n", - "95 9.74679434481\n", - "96 9.79795897113\n", - "97 9.8488578018\n", - "98 9.89949493661\n", - "99 9.94987437107\n" - ] - } - ], - "prompt_number": 14 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 4.7, Page Number: 64<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "for i in xrange(100,-100,-5): \n", - " print i,\n", - " \n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "100 95 90 85 80 75 70 65 60 55 50 45 40 35 30 25 20 15 10 5 0 -5 -10 -15 -20 -25 -30 -35 -40 -45 -50 -55 -60 -65 -70 -75 -80 -85 -90 -95\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 4.8, Page Number: 66<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "\n", - "#Variable decleration\n", - "x=0\n", - "\n", - "#Loop till 123 is entered\n", - "while x!=123:\n", - " print \"Enter a number: \"\n", - " x = 123\n", - " \n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter a number: \n" - ] - } - ], - "prompt_number": 11 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 4.9, Page Number: 68<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "print \"Help on:\"\n", - "print \"1. for\"\n", - "print \"2. if\"\n", - "print \"3. while\"\n", - "\n", - "\n", - "choice = 2 #Choice of user\n", - "\n", - "if choice==1: #Executing users choice with if-else\n", - " print \"for is c++'s most versatile loop.\"\n", - "elif choice==2:\n", - " print \"if is c++'s conditional branch statement.\"\n", - "elif choice==3:\n", - " print \"switch is C++'s multiway branch statement. \"\n", - "else:\n", - " print \"You must enter a number between 1 and 3.\"\n", - " \n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Help on:\n", - "1. for\n", - "2. if\n", - "3. while\n", - "if is c++'s conditional branch statement.\n" - ] - } - ], - "prompt_number": 19 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 4.10, Page Number: 69<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "for i in range(5):\n", - " if i==0:\n", - " print \"less than 1\"\n", - " elif i==1:\n", - " print \"less than 2\"\n", - " elif i==2:\n", - " print \"less than 3\"\n", - " elif i==3:\n", - " print \"less than 4\"\n", - " elif i==4:\n", - " print \"less than 5\"\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "less than 1\n", - "less than 2\n", - "less than 3\n", - "less than 4\n", - "less than 5\n" - ] - } - ], - "prompt_number": 20 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 4.11, Page Number: 71<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "#Variable decleration\n", - "ch = 32\n", - "\n", - "#Loop to print the characters\n", - "for ch in range(128):\n", - " print chr(ch)\n", - " \n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\u0000\n", - "\u0001\n", - "\u0002\n", - "\u0003\n", - "\u0004\n", - "\u0005\n", - "\u0006\n", - "\u0007\n", - "\b\n", - "\t\n", - "\n", - "\n", - "\u000b", - "\n", - "\f", - "\n", - "\r\n", - "\u000e\n", - "\u000f\n", - "\u0010\n", - "\u0011\n", - "\u0012\n", - "\u0013\n", - "\u0014\n", - "\u0015\n", - "\u0016\n", - "\u0017\n", - "\u0018\n", - "\u0019\n", - "\u001a\n", - "\u001b\n", - "\u001c", - "\n", - "\u001d", - "\n", - "\u001e", - "\n", - "\u001f\n", - " \n", - "!\n", - "\"\n", - "#\n", - "$\n", - "%\n", - "&\n", - "'\n", - "(\n", - ")\n", - "*\n", - "+\n", - ",\n", - "-\n", - ".\n", - "/\n", - "0\n", - "1\n", - "2\n", - "3\n", - "4\n", - "5\n", - "6\n", - "7\n", - "8\n", - "9\n", - ":\n", - ";\n", - "<\n", - "=\n", - ">\n", - "?\n", - "@\n", - "A\n", - "B\n", - "C\n", - "D\n", - "E\n", - "F\n", - "G\n", - "H\n", - "I\n", - "J\n", - "K\n", - "L\n", - "M\n", - "N\n", - "O\n", - "P\n", - "Q\n", - "R\n", - "S\n", - "T\n", - "U\n", - "V\n", - "W\n", - "X\n", - "Y\n", - "Z\n", - "[\n", - "\\\n", - "]\n", - "^\n", - "_\n", - "`\n", - "a\n", - "b\n", - "c\n", - "d\n", - "e\n", - "f\n", - "g\n", - "h\n", - "i\n", - "j\n", - "k\n", - "l\n", - "m\n", - "n\n", - "o\n", - "p\n", - "q\n", - "r\n", - "s\n", - "t\n", - "u\n", - "v\n", - "w\n", - "x\n", - "y\n", - "z\n", - "{\n", - "|\n", - "}\n", - "~\n", - "\u007f\n" - ] - } - ], - "prompt_number": 21 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 4.12, Page Number: 72<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "#Variable decleration\n", - "len = 5\n", - "\n", - "while (len>0 & len<80):\n", - " print \".\"\n", - " len-=1\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - ".\n", - ".\n", - ".\n", - ".\n", - ".\n" - ] - } - ], - "prompt_number": 22 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 4.13, Page Number: 73<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "#Variable Declaration\n", - "num=95\n", - "\n", - "while True:\n", - " print \"Enter a number(100 to stop): \"\n", - " num+=1 #User input, incrementing num till it reaches 100\n", - " if(num==100): #Condition check to stop loop\n", - " break\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter a number(100 to stop): \n", - "Enter a number(100 to stop): \n", - "Enter a number(100 to stop): \n", - "Enter a number(100 to stop): \n", - "Enter a number(100 to stop): \n" - ] - } - ], - "prompt_number": 9 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 4.14, Page Number: 73<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "import random\n", - "\n", - "#Variable decleration\n", - "magic = random.randint(0,100) #Number which the user has to guess\n", - "low=0\n", - "high=100\n", - "\n", - "#Play thr magic number game\n", - "while True:\n", - " guess = random.randint(low,high) #Number which the user guesses\n", - " if guess==magic: \n", - " print \"**Right**\"\n", - " print magic,\" is the magic number.\"\n", - " break\n", - " else:\n", - " print \"...Sorry, you're wrong.\"\n", - " if(guess>magic):\n", - " print \"Your guess is too high.\"\n", - " high=guess\n", - " else:\n", - " print \"Your guess is too low.\"\n", - " low=guess\n", - " \n", - " \n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "...Sorry, you're wrong.\n", - "Your guess is too low.\n", - "...Sorry, you're wrong.\n", - "Your guess is too high.\n", - "...Sorry, you're wrong.\n", - "Your guess is too low.\n", - "...Sorry, you're wrong.\n", - "Your guess is too high.\n", - "...Sorry, you're wrong.\n", - "Your guess is too low.\n", - "...Sorry, you're wrong.\n", - "Your guess is too high.\n", - "...Sorry, you're wrong.\n", - "Your guess is too low.\n", - "...Sorry, you're wrong.\n", - "Your guess is too high.\n", - "...Sorry, you're wrong.\n", - "Your guess is too low.\n", - "...Sorry, you're wrong.\n", - "Your guess is too low.\n", - "...Sorry, you're wrong.\n", - "Your guess is too high.\n", - "...Sorry, you're wrong.\n", - "Your guess is too high.\n", - "...Sorry, you're wrong.\n", - "Your guess is too high.\n", - "...Sorry, you're wrong.\n", - "Your guess is too low.\n", - "**Right**\n", - "72 is the magic number.\n" - ] - } - ], - "prompt_number": 26 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 4.15, Page Number: 74<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "for x in range(100+1):\n", - " if x%2: #Condition check to continue the loop\n", - " continue\n", - " print x,\n", - " \n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 4.16, Page Number: 75<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "for t in range(100):\n", - " if t==10: #Condition check to break out of the loop\n", - " break\n", - " print t,\n", - " \n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "0 1 2 3 4 5 6 7 8 9\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 4.17, Page Number: 75<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "for t in range(100):\n", - " count = 1 \n", - " while True:\n", - " print count,\n", - " count+=1\n", - " if count==10: \n", - " break #Breaks from the inner loop\n", - " print\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n", - "1 2 3 4 5 6 7 8 9\n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 4.18, Page Number: 76<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "for i in range(2,1000):\n", - " j=2\n", - " for j in range(2,i/j): #Nested for loop\n", - " if i%j == False: #Check for prime no.\n", - " break\n", - " if j>(i/j):\n", - " print i,\" is prime.\" #Result\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "2 is prime.\n", - "3 is prime.\n", - "11 is prime.\n", - "13 is prime.\n", - "17 is prime.\n", - "19 is prime.\n", - "23 is prime.\n", - "29 is prime.\n", - "31 is prime.\n", - "37 is prime.\n", - "41 is prime.\n", - "43 is prime.\n", - "47 is prime.\n", - "53 is prime.\n", - "59 is prime.\n", - "61 is prime.\n", - "67 is prime.\n", - "71 is prime.\n", - "73 is prime.\n", - "79 is prime.\n", - "83 is prime.\n", - "89 is prime.\n", - "97 is prime.\n", - "101 is prime.\n", - "103 is prime.\n", - "107 is prime.\n", - "109 is prime.\n", - "113 is prime.\n", - "127 is prime.\n", - "131 is prime.\n", - "137 is prime.\n", - "139 is prime.\n", - "149 is prime.\n", - "151 is prime.\n", - "157 is prime.\n", - "163 is prime.\n", - "167 is prime.\n", - "173 is prime.\n", - "179 is prime.\n", - "181 is prime.\n", - "191 is prime.\n", - "193 is prime.\n", - "197 is prime.\n", - "199 is prime.\n", - "211 is prime.\n", - "223 is prime.\n", - "227 is prime.\n", - "229 is prime.\n", - "233 is prime.\n", - "239 is prime.\n", - "241 is prime.\n", - "251 is prime.\n", - "257 is prime.\n", - "263 is prime.\n", - "269 is prime.\n", - "271 is prime.\n", - "277 is prime.\n", - "281 is prime.\n", - "283 is prime.\n", - "293 is prime.\n", - "307 is prime.\n", - "311 is prime.\n", - "313 is prime.\n", - "317 is prime.\n", - "331 is prime.\n", - "337 is prime.\n", - "347 is prime.\n", - "349 is prime.\n", - "353 is prime.\n", - "359 is prime.\n", - "367 is prime.\n", - "373 is prime.\n", - "379 is prime.\n", - "383 is prime.\n", - "389 is prime.\n", - "397 is prime.\n", - "401 is prime.\n", - "409 is prime.\n", - "419 is prime.\n", - "421 is prime.\n", - "431 is prime.\n", - "433 is prime.\n", - "439 is prime.\n", - "443 is prime.\n", - "449 is prime.\n", - "457 is prime.\n", - "461 is prime.\n", - "463 is prime.\n", - "467 is prime.\n", - "479 is prime.\n", - "487 is prime.\n", - "491 is prime.\n", - "499 is prime.\n", - "503 is prime.\n", - "509 is prime.\n", - "521 is prime.\n", - "523 is prime.\n", - "541 is prime.\n", - "547 is prime.\n", - "557 is prime.\n", - "563 is prime.\n", - "569 is prime.\n", - "571 is prime.\n", - "577 is prime.\n", - "587 is prime.\n", - "593 is prime.\n", - "599 is prime.\n", - "601 is prime.\n", - "607 is prime.\n", - "613 is prime.\n", - "617 is prime.\n", - "619 is prime.\n", - "631 is prime.\n", - "641 is prime.\n", - "643 is prime.\n", - "647 is prime.\n", - "653 is prime.\n", - "659 is prime.\n", - "661 is prime.\n", - "673 is prime.\n", - "677 is prime.\n", - "683 is prime.\n", - "691 is prime.\n", - "701 is prime.\n", - "709 is prime.\n", - "719 is prime.\n", - "727 is prime.\n", - "733 is prime.\n", - "739 is prime.\n", - "743 is prime.\n", - "751 is prime.\n", - "757 is prime.\n", - "761 is prime.\n", - "769 is prime.\n", - "773 is prime.\n", - "787 is prime.\n", - "797 is prime.\n", - "809 is prime.\n", - "811 is prime.\n", - "821 is prime.\n", - "823 is prime.\n", - "827 is prime.\n", - "829 is prime.\n", - "839 is prime.\n", - "853 is prime.\n", - "857 is prime.\n", - "859 is prime.\n", - "863 is prime.\n", - "877" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - " is prime.\n", - "881 is prime.\n", - "883 is prime.\n", - "887 is prime.\n", - "907 is prime.\n", - "911 is prime.\n", - "919 is prime.\n", - "929 is prime.\n", - "937 is prime.\n", - "941 is prime.\n", - "947 is prime.\n", - "953 is prime.\n", - "967 is prime.\n", - "971 is prime.\n", - "977 is prime.\n", - "983 is prime.\n", - "991 is prime.\n", - "997 is prime.\n" - ] - } - ], - "prompt_number": 30 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 4.19, Page Number: 78<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "import random\n", - "\n", - "#Variable declaration\n", - "magic = random.randint(0,100) #Number to be guessed\n", - "i=1\n", - "high=100\n", - "low=0\n", - "\n", - "#Function to play the magic number game\n", - "def play(m):\n", - " low=0\n", - " high=100\n", - " for t in range(100):\n", - " x = random.randint(low,high) #Number guessed by the user\n", - " if x==m:\n", - " print \"***Right***\"\n", - " return\n", - " else:\n", - " if(x<m):\n", - " print \"Too low.\"\n", - " low=x\n", - " else:\n", - " print \"Too high.\"\n", - " high=x\n", - " print \"You've used up all your guesses\" \n", - "\n", - "#Menu\n", - "while True:\n", - " print \"1.Get a new magic number.\" \n", - " print \"2.Play\"\n", - " print \"3.Quit\"\n", - " while True:\n", - " option = i \n", - " if option>=1 and option<=3:\n", - " break\n", - " if option==1:\n", - " magic=random.randint(0,100) #Number to be guessed\n", - " elif option==2:\n", - " play(magic) #Calls the function play\n", - " elif option==3:\n", - " print \"Goodbye\" \n", - " break\n", - " i+=1 #increments i such that the 3 options get selected sequentially\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "1.Get a new magic number.\n", - "2.Play\n", - "3.Quit\n", - "1.Get a new magic number.\n", - "2.Play\n", - "3.Quit\n", - "Too low.\n", - "Too low.\n", - "Too low.\n", - "Too low.\n", - "Too high.\n", - "Too low.\n", - "Too high.\n", - "Too low.\n", - "Too high.\n", - "Too high.\n", - "Too high.\n", - "Too high.\n", - "Too low.\n", - "***Right***\n", - "1.Get a new magic number.\n", - "2.Play\n", - "3.Quit\n", - "Goodbye\n" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "code", - "collapsed": false, - "input": [], - "language": "python", - "metadata": {}, - "outputs": [] - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file diff --git a/C++_from_the_Ground/Chapter_5(1).ipynb b/C++_from_the_Ground/Chapter_5(1).ipynb deleted file mode 100755 index 13085f5e..00000000 --- a/C++_from_the_Ground/Chapter_5(1).ipynb +++ /dev/null @@ -1,1191 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:f41d93d043c68dcb6af847a40af38d47fc3a3d45c60675afb574707e38d804d9" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h1>Chapter 5: Arrays ans Strings<h1>" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 5.1, Page Number: 82<h3>\n", - " " - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "#Variable declaration\n", - "sample = range(10) #Declares an array [0,1,2,3,4,5,6,7,8,9]\n", - "\n", - "#Displays the array\n", - "for t in range(10):\n", - " print sample[t],\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "0 1 2 3 4 5 6 7 8 9\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 5.2, Page Number: 83<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "import random\n", - "\n", - "#Variable declaration\n", - "list = [] #List of integers\n", - "\n", - "for i in range(10):\n", - " list.append(random.randint(0,1000)) #randomely assigning integers\n", - " \n", - "#Finding the minimum value\n", - "min_value = list[0]\n", - "\n", - "for i in range(10):\n", - " if min_value>list[i]:\n", - " min_value=list[i]\n", - "print \"minimum value: \",min_value #Result:Minimum value\n", - "\n", - "#Finding maximum value\n", - "max_value = list[0]\n", - "\n", - "for i in range(10):\n", - " if max_value<list[i]:\n", - " max_value=list[i]\n", - "print \"maximum value: \",max_value #Result:Maximum value\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "minimum value: 192\n", - "maximum value: 684\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 5.3, Page Number: 85<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "\n", - "import random\n", - "\n", - "#Variable declaration\n", - "nums = []\n", - "size = 10\n", - "\n", - "#Initializing list with random numbers\n", - "for t in range(size):\n", - " nums.append(random.randint(0,1000))\n", - "\n", - "#Displays original array\n", - "print \"Original array is: \"\n", - "print nums\n", - "\n", - "#Bubble Sort\n", - "for a in range(size):\n", - " for b in xrange(size-1,a,-1):\n", - " if nums[b-1]>nums[b]:\n", - " t=nums[b-1]\n", - " nums[b-1]=nums[b]\n", - " nums[b] = t\n", - "\n", - "#Display sorted array\n", - "print \"Sorted array is: \"\n", - "print nums\n", - "\n", - " \n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Original array is: \n", - "[388, 661, 218, 595, 167, 46, 704, 140, 559, 428]\n", - "Sorted array is: \n", - "[46, 140, 167, 218, 388, 428, 559, 595, 661, 704]\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 5.4, Page Number: 87<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "#Variable decleration\n", - "str = \"Hello\"\n", - "\n", - "#Result\n", - "print \"Here is your string:\",\n", - "print str\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Here is your string: Hello\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 5.5, Page Number: 88<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "\n", - "print \"Enter a string: \"\n", - "\n", - " \n", - "str = \"Hello\"\n", - "\n", - "#Result\n", - "print \"Here is your string:\",\n", - "print str\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter a string: \n", - "Here is your string: Hello\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 5.6, Page Number: 89<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "#Variable decleration\n", - "s=str\n", - "str = s #copying s into str\n", - "\n", - "#Result\n", - "print str\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Hello\n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 5.7, Page Number: 89<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "#Variable decleration\n", - "s1 = \"Hello\"\n", - "s2 = \" There\"\n", - "\n", - "#Concatenation\n", - "s1+=s2\n", - "\n", - "#Result\n", - "print s1\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Hello There\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 5.8, Page Number: 90<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def password():\n", - " print \"Enter password: \" #User input for password\n", - " s= \"pass\"\n", - " if s==\"password\":\n", - " return True\n", - " else:\n", - " print \"Invalid password.\"\n", - " return False\n", - "\n", - "#Result\n", - "if password() :\n", - " print \"Logged On.\"\n", - "else:\n", - " print \"Access denied\"\n", - " \n", - " \n", - " \n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter password: \n", - "Invalid password.\n", - "Access denied\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 5.9, Page Number: 91<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "import string\n", - "\n", - " s=raw_input(\"Enter a string: \") #User input of string;\n", - " if s==\"quit\": #Sting comparison\n", - " break\n", - " \n", - " \n", - "\n", - " \n", - " \n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter a string: ddc\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter a string: hello\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter a string: quit\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 5.10, Page Number: 91<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "#Variable declaration\n", - "str = \"Hello\"\n", - "\n", - "#Result\n", - "print \"Length is: \",len(str)\n", - " \n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Length is: 5\n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 5.11, Page Number: 92<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "str = \"Hello World\"\n", - "\n", - "#Reversing a String\n", - "rev = str[::-1]\n", - "\n", - "#Result\n", - "print rev\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "dlroW olleH\n" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 5.12, Page Number: 92<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "s1 = \"Hello\"\n", - "s2 = \"there\"\n", - "\n", - "#Printing lengths\n", - "print \"lengths: \",len(s1),' ',len(s2)\n", - "\n", - "#Comparing\n", - "if(s1==s2):\n", - " print \"The strings are equal\"\n", - "else:\n", - " print \"not equal\"\n", - "\n", - "#Concatenation\n", - "s1+=s2\n", - "print s1\n", - "\n", - "\n", - "#Copying\n", - "s1=s2\n", - "\n", - "#Result\n", - "print s1,\"and\",s2,\" are now the same\"\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "lengths: 5 5\n", - "not equal\n", - "Hellothere\n", - "there and there are now the same\n" - ] - } - ], - "prompt_number": 9 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 5.13, Page Number: 93<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "import string\n", - "\n", - "#Variable Initialization\n", - "str= \"this is a test\"\n", - "\n", - "str=string.upper(str)\n", - "\n", - "#Result\n", - "print str\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "THIS IS A TEST\n" - ] - } - ], - "prompt_number": 12 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 5.14, Page Number: 94<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "arr=[] #The 2-D list\n", - "new=[] #The nested list\n", - "\n", - "#Initializing the 2-D array\n", - "for i in range(3):\n", - " new=[]\n", - " for j in range(4):\n", - " new.append(i*4+j+1)\n", - " arr.append(new)\n", - "\n", - "#Result\n", - "for i in range(3):\n", - " for j in range(4):\n", - " print arr[i][j],\n", - " print" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "1 2 3 4\n", - "5 6 7 8\n", - "9 10 11 12\n" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 5.15, Page Number: 98<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "#Variabe Initialzation\n", - "sqrs= [[1,1],[2,4],[3,9],[4,16],[5,25],\n", - " [6,36],[7,49],[8,64],[9,81],[10,100]] #Array storing the squares\n", - "i=6 #User input of number whose square is to be looked up\n", - "\n", - "#Search for the number\n", - "for j in range(10):\n", - " if sqrs[j][0]==i:\n", - " break\n", - " \n", - "#Result\n", - "print \"The square of \",i,\" is \", sqrs[j][1]" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "The square of 6 is 36\n" - ] - } - ], - "prompt_number": 15 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 5.16, Page Number: 99<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "def f1():\n", - " s=\"this is a test\" #initial s\n", - " print s\n", - " s=\"CHANGED\" #s is now changed \n", - " print s\n", - "\n", - "#Calling the function twice\n", - "f1()\n", - "f1()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "this is a test\n", - "CHANGED\n", - "this is a test\n", - "CHANGED\n" - ] - } - ], - "prompt_number": 17 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 5.17, Page Number: 101<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "#Variable decleration\n", - "text=[]\n", - "str=['eat','play','work'] #user input of strings\n", - "p=len(str)\n", - "\n", - "for t in range(p):\n", - " print t,\":\"\n", - " text.append(str[t]) #Here, user input taken from the list\n", - " \n", - "#Result; redisplay the strings\n", - "for i in range(p):\n", - " print text[i]\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "0 :\n", - "1 :\n", - "2 :\n", - "eat\n", - "play\n", - "work\n" - ] - } - ], - "prompt_number": 18 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 5.18, Page Number: 103<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "import random \n", - "\n", - "#Variable decleration\n", - "name=[] #this list holds employee names\n", - "wage=[] #their phone numbers\n", - "phone=[] #hours worked per week\n", - "hours=[] #wage\n", - "num=0 #User choice\n", - "\n", - "#Menu \n", - "def menu():\n", - " global num #All options are chosen one by one\n", - " print \"0.Quit.\"\n", - " print \"1.Enter information\"\n", - " print \"2.Report information\"\n", - " print \"Choose one: \"\n", - " num=int(input())\n", - " return num #Return users selction\n", - "\n", - "#Enter information\n", - "def enter():\n", - " for i in range(10):\n", - " n=raw_input(\"Enter your name: \")\n", - " name.append(n)\n", - " phone.append(int(input(\"Enter your phone number\")))\n", - " hours.append(int(input(\"Enter number of hours worked: \")))\n", - " wage.append(int(input(\"Enter wage: \")))\n", - "\n", - "#Display report\n", - "def report():\n", - " p=len(name)\n", - " for i in range(p):\n", - " print name[i],' ',phone[i]\n", - " print \"Pay for the week: \",wage[i]*hours[i]\n", - "\n", - "\n", - "while True:\n", - " ch=menu() #get selection\n", - " if ch==0:\n", - " break\n", - " elif ch==1:\n", - " enter()\n", - " elif ch==2:\n", - " report()\n", - " else:\n", - " print \"Try again.\"\n", - " if ch==0:\n", - " break\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "0.Quit.\n", - "1.Enter information\n", - "2.Report information\n", - "Choose one: \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "1\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter your name: Anny\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter your phone number987654321\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter number of hours worked: 10\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter wage: 50\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter your name: Billy\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter your phone number9456783652\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter number of hours worked: 10\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter wage: 50\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter your name: Catherene\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter your phone number9476836578\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter number of hours worked: 10\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter wage: 50\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter your name: Dolly\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter your phone number9831356748\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter number of hours worked: 15\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter wage: 50\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter your name: Emily\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter your phone number9576843721\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter number of hours worked: 15\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter wage: 40\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter your name: Jack\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter your phone number9485738567\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter number of hours worked: 10\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter wage: 45\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter your name: Kevin\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter your phone number9345678923\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter number of hours worked: 10\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter wage: 45\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter your name: Lily\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter your phone number9345672831\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter number of hours worked: 10\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter wage: 45\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter your name: Monica\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter your phone number9475867483\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter number of hours worked: 10\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter wage: 50\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter your name: Irene\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter your phone number5674356776\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter number of hours worked: 10\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter wage: 20\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "0.Quit.\n", - "1.Enter information\n", - "2.Report information\n", - "Choose one: \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "2\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Anny 987654321\n", - "Pay for the week: 500\n", - "Billy 9456783652\n", - "Pay for the week: 500\n", - "Catherene 9476836578\n", - "Pay for the week: 500\n", - "Dolly 9831356748\n", - "Pay for the week: 750\n", - "Emily 9576843721\n", - "Pay for the week: 600\n", - "Jack 9485738567\n", - "Pay for the week: 450\n", - "Kevin 9345678923\n", - "Pay for the week: 450\n", - "Lily 9345672831\n", - "Pay for the week: 450\n", - "Monica 9475867483\n", - "Pay for the week: 500\n", - "Irene 5674356776\n", - "Pay for the week: 200\n", - "0.Quit.\n", - "1.Enter information\n", - "2.Report information\n", - "Choose one: \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "0\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "code", - "collapsed": false, - "input": [], - "language": "python", - "metadata": {}, - "outputs": [] - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file diff --git a/C++_from_the_Ground/Chapter_6(1).ipynb b/C++_from_the_Ground/Chapter_6(1).ipynb deleted file mode 100755 index 5010922a..00000000 --- a/C++_from_the_Ground/Chapter_6(1).ipynb +++ /dev/null @@ -1,558 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:b382733cd221154cfe7c9ffe63477448084168d501d248c2b1d0c253ed011821" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h1>Chapter 6: Pointers<h1>" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 6.1, Page Number: 107<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "from ctypes import *\n", - "\n", - "balance =c_int(3200) #int variable\n", - "balptr=pointer(balance) #pointer to int\n", - "value=balptr[0] #accessing the value using the pointer\n", - "\n", - "#Result\n", - "print \"balance: \",value \n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "balance: 3200\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 6.2, Page Number: 109<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "#Variable declaration\n", - "x=123.23\n", - "y=c_double()\n", - "p=POINTER(c_int)\n", - "\n", - "p=pointer(c_int(int(x))) #type case double to int\n", - "y=p[0]\n", - "\n", - "#Result\n", - "print y" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "123\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 6.3, Page Number: 110<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "from ctypes import *\n", - "\n", - "#Variable declaration\n", - "num=c_int() #declaring int\n", - "p=pointer(num) #pointer to int\n", - "\n", - "p[0]=100\n", - "print num.value,\n", - "p[0]+=1\n", - "print num.value,\n", - "p[0]-=1\n", - "print num.value" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "100 101 100\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 6.4, Page Number: 111<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "from ctypes import *\n", - "\n", - "#Variable declaration\n", - "j=c_int()\n", - "g=c_double()\n", - "i=pointer(j)\n", - "f=pointer(g)\n", - "\n", - "\n", - "for x in range(10):\n", - " print addressof(i.contents)+x,addressof(f.contents)+x\n", - " \n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "84638480 84639248\n", - "84638481 84639249\n", - "84638482 84639250\n", - "84638483 84639251\n", - "84638484 84639252\n", - "84638485 84639253\n", - "84638486 84639254\n", - "84638487 84639255\n", - "84638488 84639256\n", - "84638489 84639257\n" - ] - } - ], - "prompt_number": 10 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 6.5, Page Number: 114<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "from ctypes import *\n", - "\n", - "#Variable declaration\n", - "str=\"This is a test\"\n", - "token =\"\"\n", - "i=0\n", - "\n", - "#Read a token at a time from the string\n", - "while i<len(str):\n", - " token=c_char_p(\"\") #set token to null string\n", - " q=pointer(token) \n", - " #Read characters until either a space or the null terminator is encountered'''\n", - " while i<len(str) and not(str[i]==\" \"):\n", - " q[0]+=str[i]\n", - " i+=1\n", - " i+=1 #advance past the space\n", - " print q[0]" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "This\n", - "is\n", - "a\n", - "test\n" - ] - } - ], - "prompt_number": 12 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 6.6, Page Number: 115<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "#Variable declaration\n", - "str=\"This is a test\"\n", - "i=0\n", - "\n", - "#Read a token at a time from the string\n", - "while i<len(str):\n", - " token=\"\" #set q to null string\n", - " #Read characters until either a space or the null terminator is encountered'''\n", - " while i<len(str) and not(str[i]==\" \"):\n", - " token+=str[i]\n", - " i+=1\n", - " i+=1 #advance past the space\n", - " print token" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "This\n", - "is\n", - "a\n", - "test\n" - ] - } - ], - "prompt_number": 14 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 6.7, Page Number: 115<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "import string\n", - "\n", - "str=c_char_p(\"hello tom\")\n", - "q=\"\"\n", - "p=pointer(str) #put address of str into p\n", - " \n", - "p[0]=string.upper(p[0])\n", - "\n", - "#Result\n", - "print p[0]" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "HELLO TOM\n" - ] - } - ], - "prompt_number": 15 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 6.8, Page Number: 117<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "#Varible declaration\n", - "s=\"Pointers are fun to use\"\n", - "\n", - "#Result\n", - "print s\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Pointers are fun to use\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 6.9, Page Number: 118<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "from ctypes import *\n", - "\n", - "#Variable declaration\n", - "num=[]\n", - "\n", - "#User input\n", - "for i in range(10):\n", - " num.append(i)\n", - " \n", - "start=num #set start to the starting pointer \n", - "for i in range(10):\n", - " print start[i],\n", - " \n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "0 1 2 3 4 5 6 7 8 9\n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 6.10, Page Number: 119<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "import random\n", - "\n", - "fortunes=[\"Soon, you will come into some money.\",\n", - " \"A new love will enter your lifr. \",\n", - " \"You will live long and prosper.\",\n", - " \"Now is a good time to invest for the future.\",\n", - " \"A close friend will ask for a favour.\"]\n", - "\n", - "print \"To see your fortune, press a key: \"\n", - "\n", - "#Randomize the random number generator\n", - "chance=random.randint(0,100)\n", - "chance=chance%5\n", - "\n", - "#Result\n", - "print fortunes[chance]" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "To see your fortune, press a key: \n", - "A close friend will ask for a favour.\n" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 6.11, Page Number: 120<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "keyword=[[\"for\",\"for(initialization; condition; increment\"],\n", - " [\"if\",\"if(condition) ... else ...\"],\n", - " [\"switch\",\"switch(value) { case-list }\"],\n", - " [\"while\",\"while(condition) ...\"],\n", - " [\"\",\"\"]] #Terminates the list with nulls\n", - " \n", - "#User input \n", - "print \"Enter keyword: \"\n", - "str=\"for\" \n", - "\n", - "for i in range(4):\n", - " if str==keyword[i][0]:\n", - " print keyword[i][1]\n", - " \n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter keyword: \n", - "for(initialization; condition; increment\n" - ] - } - ], - "prompt_number": 7 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 6.12, Page Number: 123<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "from ctypes import *\n", - "\n", - "#Variable declaration\n", - "x=c_int(10) #int variable\n", - "p=pointer(x) #pointer to int\n", - "q=pointer(p) #pointer to a pointer\n", - "\n", - "#Result\n", - "print q[0][0] #accessing the value using a pointer to a pointer\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "10\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 6.13, Page Number: 126<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "from ctypes import *\n", - "\n", - "#Variable declaration\n", - "s=c_char_p()\n", - "p1=pointer(s)\n", - "x=0\n", - "\n", - "while True:\n", - " print \"\\nEnter a string: \",\n", - " if x==2:\n", - " p1[0]=c_char_p(\"done\")\n", - " else:\n", - " p1[0]=c_char_p(\"Hello\")\n", - " #print the ASCII values of each characcter\n", - " for i in range(0,len(p1[0])):\n", - " print ord(p1[0][i]),\n", - " x+=1\n", - " if p1[0]==\"done\":\n", - " break\n", - " \n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "Enter a string: 72 101 108 108 111 \n", - "Enter a string: 72 101 108 108 111 \n", - "Enter a string: 100 111 110 101\n" - ] - } - ], - "prompt_number": 9 - }, - { - "cell_type": "code", - "collapsed": false, - "input": [], - "language": "python", - "metadata": {}, - "outputs": [] - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file diff --git a/C++_from_the_Ground/Chapter_7(1).ipynb b/C++_from_the_Ground/Chapter_7(1).ipynb deleted file mode 100755 index 027b30f5..00000000 --- a/C++_from_the_Ground/Chapter_7(1).ipynb +++ /dev/null @@ -1,908 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:ab47bb393809bc88589e7e92320813b90077813594dc417229af49780e24b53b" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h1>Chapter 7: Functions,Part One: The Fundamentals<h1>" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 7.1, Page Number: 129<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def f1():\n", - " print \"Enter something: \"\n", - " str= \"Hello\" #User-input\n", - " print str\n", - " \n", - "#Variable decleration\n", - "str=\"This is str in main\"\n", - "\n", - "print str\n", - "f1() #function call\n", - "print str" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "This is str in main\n", - "Enter something: \n", - "Hello\n", - "This is str in main\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 7.2, Page Number: 130<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "#Variable decleration\n", - "choice=0\n", - "\n", - "print \"(1) add numbers or (2) concatenate strings?: \"\n", - "choice=2 #User Input taken as 2\n", - "\n", - "if choice==1:\n", - " print \"Enter two numbers: \"\n", - " a=5 #Variable decleration; User Input\n", - " b=7\n", - " print a+b #Result\n", - "else :\n", - " print \"Enter two strings: \"\n", - " s1=\"Hello\" #Variable decleration; User Input\n", - " s2=\"World\"\n", - " s1+=s2 #Concatenation\n", - " print s1 #Result" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "(1) add numbers or (2) concatenate strings?: \n", - "Enter two strings: \n", - "HelloWorld\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 7.3, Page Number: 131<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "#Variable decleration\n", - "i=10\n", - "j=100\n", - "\n", - "if j>0:\n", - " i=None \n", - " i= j/2\n", - " print \"inner i: \",i #Result\n", - "\n", - "print \"outer i: \",i #Result" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "inner i: 50\n", - "outer i: 50\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 7.4, Page Number: 132<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "a=5 #Variable decleration; user input\n", - "\n", - "b=10 #declaration of another variable; user input\n", - "\n", - "#Result\n", - "print \"Product: \",a*b" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Product: 50\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 7.5, Page Number: 134<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "import random\n", - "\n", - "#Variable decleration\n", - "count=None\n", - "num_right=0\n", - "\n", - "#Function for the drill\n", - "def drill():\n", - " #Generate two numbers between 0 and 99.\n", - " a=random.randint(0,99)\n", - " b=random.randint(0,99)\n", - " #The user gets three tries to get it right.\n", - " for count in range(3):\n", - " print \"What is \",a,\" + \",b,\"? \"\n", - " ans = random.randint(0,200) #user input\n", - " if ans==a+b:\n", - " print \"Right\"\n", - " num_right+=1\n", - " print \"You've used up all your tries.\"\n", - " print \"The answer is \",a+b\n", - " \n", - "#Main function \n", - "print \"How many practice problems: \"\n", - "count=2 #User input taken as 2\n", - "num_right=0\n", - "while True:\n", - " drill()\n", - " count-=1\n", - " if(count==0):\n", - " break\n", - " \n", - "#Result\n", - "print \"You got \",num_right,\" right. \" \n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "How many practice problems: \n", - "What is 9 + 89 ? \n", - "What is 9 + 89 ? \n", - "What is 9 + 89 ? \n", - "You've used up all your tries.\n", - "The answer is 98\n", - "What is 85 + 98 ? \n", - "What is 85 + 98 ? \n", - "What is 85 + 98 ? \n", - "You've used up all your tries.\n", - "The answer is 183\n", - "You got 0 right. \n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 7.6, Page Number: 136<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "from ctypes import *\n", - " \n", - "def f(j):\n", - " j[0]=100 #j is assigned 100\n", - "\n", - "#Variable decleration\n", - "i=c_int(1)\n", - "p=pointer(i)\n", - "\n", - "#Calling the function\n", - "f(p) \n", - "\n", - "print i" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "c_long(100)\n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 7.7, Page Number: 137<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "from ctypes import *\n", - "\n", - " \n", - "def f(j):\n", - " j[0]=100 #j is assigned 100\n", - "\n", - "#Variable decleration\n", - "i=c_int(1)\n", - "p=pointer(i)\n", - "\n", - "#Calling the function\n", - "f(p) \n", - "\n", - "print i" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "c_long(100)\n" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 7.8, Page Number: 137<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "def display(num):\n", - " for i in range(10):\n", - " print num[i],\n", - " \n", - "#Variable declaration\n", - "t=[]\n", - "\n", - "for i in range(10):\n", - " t.append(i)\n", - "#Pass list to a function\n", - "display(t)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "0 1 2 3 4 5 6 7 8 9\n" - ] - } - ], - "prompt_number": 7 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 7.9, Page Number: 138<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "def display(num):\n", - " print num,\n", - "\n", - "#Variable declaration\n", - "t=[]\n", - "\n", - "for i in range(10):\n", - " t.append(i)\n", - " \n", - "#Printing without passing entire list\n", - "for i in range(10):\n", - " display(t[i])" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "0 1 2 3 4 5 6 7 8 9\n" - ] - } - ], - "prompt_number": 8 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 7.10, Page Number: 139<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "def cube(n,num):\n", - " num-=1\n", - " while num:\n", - " n[num]=n[num]*n[num]*n[num]\n", - " num-=1\n", - "\n", - "#Variable declaration\n", - "nums=[]\n", - "\n", - "for i in range(10):\n", - " nums.append(i+1)\n", - " \n", - "print \"Original contents: \",\n", - "for i in range(10):\n", - " print nums[i],\n", - "print\n", - "\n", - "cube(nums,10) #Compute cubes\n", - "\n", - "#Result\n", - "print \"Altered contents: \",\n", - "for i in range(10):\n", - " print nums[i]," - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Original contents: 1 2 3 4 5 6 7 8 9 10\n", - "Altered contents: 1 8 27 64 125 216 343 512 729 1000\n" - ] - } - ], - "prompt_number": 10 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 7.11, Page Number: 140<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "import string\n", - "\n", - "def stringupper(str):\n", - " str=string.upper(str) #convert to uppercase\n", - " return str\n", - "\n", - "#Variable declaration \n", - "str=\"this is a test\"\n", - "\n", - "#Calling the function\n", - "str=stringupper(str)\n", - "\n", - "#Result\n", - "print str" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "THIS IS A TEST\n" - ] - } - ], - "prompt_number": 7 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 7.12, Page Number: 141<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "def mystrlen(str):\n", - " l=len(str)\n", - " return l\n", - "\n", - "#Result\n", - "print \"Length of Hello There is: \",\n", - "print mystrlen(\"Hello There\")" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Length of Hello There is: 11\n" - ] - } - ], - "prompt_number": 11 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 7.13, Page Number: 142<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "import sys\n", - "\n", - "def main():\n", - " if len(sys.argv)!=2:\n", - " print \"You forgot to type your name.\" #CHECK!!!!\n", - " return\n", - " #Result\n", - " print \"Hello \",sys.argv[1]\n", - "\n", - "main()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "You forgot to type your name.\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 7.14, Page Number: 143<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "import sys\n", - "\n", - "#Result\n", - "for t in range(len(sys.argv)):\n", - " i=0\n", - " print sys.argv[t] " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "-c\n", - "-f\n", - "C:\\Users\\Anandi\\.ipython\\profile_default\\security\\kernel-6e851974-75ff-4911-bdf9-e089a03e5741.json\n", - "--KernelApp.parent_appname='ipython-notebook'\n", - "--interrupt=904\n", - "--parent=876\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 7.15, Page Number: 144<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "import sys\n", - "\n", - "if len(sys.argv)!=3:\n", - " print \"usage: add num num\" \n", - "else :\n", - " #Variable Decleration\n", - " a=sys.argv[1]\n", - " b=sys.argv[2]\n", - " #Result\n", - " print a+b\n", - " \n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "usage: add num num\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 7.16, Page Number: 145<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "import string\n", - "\n", - "#Variable Decleration\n", - "i=string.atoi(\"100\")\n", - "j=string.atoi(\"100000\")\n", - "k=string.atof(\"-0.123\")\n", - "\n", - "#Result\n", - "print i,\" \",j,\" \",k" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "100 100000 -0.123\n" - ] - } - ], - "prompt_number": 13 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 7.17, Page Number: 147<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "#Variable Decleration\n", - "i=abs(-10)\n", - "\n", - "#Result\n", - "print abs(-23)\n", - "abs(100)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "23\n" - ] - }, - { - "metadata": {}, - "output_type": "pyout", - "prompt_number": 5, - "text": [ - "100" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 7.18, Page Number: 148<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "def find_substr(sub,str):\n", - " l=str.find(sub)\n", - " return l\n", - "\n", - "#Variable decleration;Calling the function\n", - "index=find_substr(\"three\",\"one two three four\")\n", - "\n", - "#Result\n", - "print \"Index of three is \",index" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Index of three is 8\n" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 7.19, Page Number: 149<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "import sys\n", - "\n", - "def print_vertical(str):\n", - " l=len(str)\n", - " for i in range(l):\n", - " print str[i],\n", - " \n", - "print_vertical(sys.argv[1])\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "- f\n" - ] - } - ], - "prompt_number": 14 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 7.20, Page Number: 150<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "def find_substr(sub,str):\n", - " return str.find(sub)\n", - "\n", - "#Variable declaration\n", - "s=\"one two three four\"\n", - "\n", - "substr = find_substr(\"three\",s)\n", - "\n", - "#Result\n", - "print \"substring found:\",s[substr:]\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "substring found: three four\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 7.21, Page Number: 154<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "def factr(n):\n", - " if n==1:\n", - " return 1\n", - " answer=factr(n-1)*n\n", - " return answer\n", - "\n", - "#Iterative version\n", - "def fact(n):\n", - " answer=1\n", - " for t in range(n):\n", - " answer=answer*(t+1)\n", - " return answer\n", - "\n", - "#Using recursion version\n", - "print \"4 factorial is \",factr(4)\n", - "\n", - "#Using iterative version\n", - "print \"4 factorial is \",fact(4)\n", - "\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "4 factorial is 24\n", - "4 factorial is 24\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 7.22, Page Number: 155<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "def reverse(s):\n", - " r = \"\"\n", - " for c in s:\n", - " r=c+r\n", - " print r\n", - " \n", - "#VariabDecleration \n", - "str=\"this is a test\"\n", - "\n", - "#Calling function\n", - "reverse(str)\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "tset a si siht\n" - ] - } - ], - "prompt_number": 9 - }, - { - "cell_type": "code", - "collapsed": false, - "input": [], - "language": "python", - "metadata": {}, - "outputs": [] - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file diff --git a/C++_from_the_Ground/Chapter_8(1).ipynb b/C++_from_the_Ground/Chapter_8(1).ipynb deleted file mode 100755 index 4684861e..00000000 --- a/C++_from_the_Ground/Chapter_8(1).ipynb +++ /dev/null @@ -1,1783 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:cb32b4e80623f372b375323b458cf1a594dc5626da9cbe9346e1ec33417221d4" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h1>Chapter 8: Functions,Part Two: References, Overloading, and Default Arguments<h1>" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 8.1, Page Number: 158<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def sqr_it(x):\n", - " x=x*x\n", - " return x\n", - "\n", - "#Variable decleration\n", - "t=10\n", - "\n", - "#Result; function calling\n", - "print sqr_it(t),' ',t\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "100 10\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 8.2, Page Number: 159<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "def swap(x,y):\n", - " temp=x\n", - " x=y\n", - " y=temp\n", - " return x, y\n", - "\n", - "#Variable decleration\n", - "i=10\n", - "j=20 \n", - "\n", - "#Initial values\n", - "print \"Initial values of i and j: \",\n", - "print i,' ',j\n", - "\n", - "#Calling function to swap\n", - "i, j=swap(i,j)\n", - "\n", - "#Result\n", - "print \"Swapped values of i and j: \",\n", - "print i,' ',j" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Initial values of i and j: 10 20\n", - "Swapped values of i and j: 20 10\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 8.3, Page Number: 161<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "def f(i):\n", - " i=10\n", - " return i #Returning the value since the function cannot access the variables in the calling scope.\n", - "\n", - "#Variable Decleration\n", - "val=1\n", - "\n", - "print \"Old value for val: \",val\n", - "val=f(val) #Function call\n", - "\n", - "#Result\n", - "print \"New value for val: \",val\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Old value for val: 1\n", - "New value for val: 10\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 8.4, Page Number: 162<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def swap(i,j):\n", - " temp=i[0]\n", - " i[0]=j[0]\n", - " j[0]=temp\n", - " return i, j\n", - "\n", - "#Variable decleration\n", - "i=[]\n", - "j=[]\n", - "i.append(10)\n", - "j.append(20)\n", - "\n", - "#Initial values\n", - "print \"Initial values of i and j: \",\n", - "print i[0],' ',j[0]\n", - "\n", - "#Calling function to swap\n", - "i, j=swap(i,j)\n", - "\n", - "#Result\n", - "print \"Swapped values of i and j: \",\n", - "print i[0],' ',j[0]" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Initial values of i and j: 10 20\n", - "Swapped values of i and j: 20 10\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 8.5, Page Number: 164<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "#Variable Decleration\n", - "val = 100.0\n", - "\n", - "def f():\n", - " global val\n", - " return val\n", - "\n", - "#Result\n", - "print val\n", - "\n", - "newval=f() #function call\n", - "print newval\n", - "\n", - "val=99.1 #change val's value\n", - "print f() #print val's new value\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "100.0\n", - "100.0\n", - "99.1\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 8.6, Page Number: 166<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "#Function definition\n", - "def change_it(i,n):\n", - " global vals\n", - " vals[i]=n\n", - "\n", - "#Variable Decleration\n", - "vals=[1.1,2.2,3.3,4.4,5.5]\n", - "\n", - "print \"Here are the original values: \",\n", - "for i in range(5):\n", - " print vals[i],\n", - "print\n", - " \n", - "#Function call\n", - "change_it(1,5298.23)\n", - "change_it(3,-98.8)\n", - "\n", - "#Result\n", - "print \"Here are the changed values: \",\n", - "for i in range(5):\n", - " print vals[i]," - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Here are the original values: 1.1 2.2 3.3 4.4 5.5\n", - "Here are the changed values: 1.1 5298.23 3.3 -98.8 5.5\n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 8.7, Page Number: 167<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "#Variable Decleration\n", - "vals=[None]*10\n", - "error=-1\n", - "\n", - "#put values into the array\n", - "def put(i,n):\n", - " global vals\n", - " if i>=0 and i<10:\n", - " vals[i]=n\n", - " else:\n", - " print \"Bounds Error!\"\n", - " error=n\n", - "\n", - "#obtain a value from the array\n", - "def get(i):\n", - " if i>=0 and i<10:\n", - " return vals[i]\n", - " else:\n", - " print \"Bounds error!\"\n", - " return -1\n", - " \n", - "#put values into the array\n", - "put(0,10)\n", - "put(1,20)\n", - "put(9,30)\n", - "\n", - "#Result\n", - "print get(0),' ',get(1),' ',get(9),\n", - "\n", - "#now, intentionally generate an errer\n", - "put(12,1)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "10 20 30 Bounds Error!\n" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 8.8, Page Number: 169<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "#Variable Decleration\n", - "i=[]\n", - "j=i #independent reference\n", - "\n", - "j.append(10) #Here i and j are just references to [10]\n", - "\n", - "print j[0],\" \",i[0] \n", - "\n", - "k=121\n", - "i[0]=k #copies k's value into j[0] \n", - "\n", - "#Result\n", - "print j[0]" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "10 10\n", - "121\n" - ] - } - ], - "prompt_number": 7 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 8.9, Page Number: 170<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def f(i,j=None):\n", - " if j==None: \n", - " if isinstance(i,int): #for 1st function\n", - " print \"In f(int), i is \",i \n", - " else: #for 3rd function\n", - " print \"In f(double), k is \",i\n", - " else: #for 2nd arguments\n", - " print \"In f(int,int), i is \",i,\", j is \",j\n", - " \n", - "#calling function\n", - "f(10)\n", - "f(10,20)\n", - "f(12.23)\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "In f(int), i is 10\n", - "In f(int,int), i is 10 , j is 20\n", - "In f(double), k is 12.23\n" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 8.10, Page Number: 171<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "def myabs(i):\n", - " if isinstance(i,int): #first instance\n", - " print \"Using integer myabs(): \",\n", - " if i<0:\n", - " return -i\n", - " else:\n", - " return i\n", - " elif isinstance(i,float): #second instance\n", - " print \"Using double myabs(): \",\n", - " if(i<0.0):\n", - " return -i\n", - " else:\n", - " return i\n", - " elif isinstance(i,long): #third instance\n", - " print \"Using long myabs(): \",\n", - " if i<0:\n", - " return -i\n", - " else:\n", - " return i\n", - "\n", - "#Result; calling the function \n", - "print myabs(-10)\n", - "print myabs(-11.0)\n", - "print myabs(-9L)\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Using integer myabs(): 10\n", - "Using double myabs(): 11.0\n", - "Using long myabs(): 9\n" - ] - } - ], - "prompt_number": 8 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 8.11, Page Number: 174<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "import os\n", - "\n", - "def clrscr(size=25):\n", - " while(size):\n", - " print \"\"\n", - " size-=1\n", - " \n", - "for i in range(30):\n", - " print i\n", - " clrscr() #clears 25 lines\n", - "\n", - "for i in range(30):\n", - " print i\n", - " clrscr(10) #clears 10 lines\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "0\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "1\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "2\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "3\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "4\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "5\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "6\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "7\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "8\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "9\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "10\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "11\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "12\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "13\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "14\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "15\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "16\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "17\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "18\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "19\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "20\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "21\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "22\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "23\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "24\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "25\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "26\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "27\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "28\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "29\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "0\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "1\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "2\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "3\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "4\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "5\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "6\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "7\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "8\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "9\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "10\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "11\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "12\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "13\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "14\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "15\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "16\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "17\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "18\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "19\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "20\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "21\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "22\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "23\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "24\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "25\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "26\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "27\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "28\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "29\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n" - ] - } - ], - "prompt_number": 8 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 8.12, Page Number: 176<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "#Variable Decleration\n", - "str1 = \"This is a test\"\n", - "str2 = \"0123456789\"\n", - "\n", - "#function for concatenation\n", - "def mystrcat(s1,s2,l=-1):\n", - " if l==-1:\n", - " l=len(str2)\n", - " s2=s2[:l] #truncates s2\n", - " s1=s1+s2 #concatenates the 2 strings\n", - " return s1\n", - "\n", - "str1=mystrcat(str1,str2,5) #concatenate 5 chars\n", - "print str1\n", - "\n", - "str1 = \"this is a test\" #reset str1\n", - "\n", - "str1=mystrcat(str1, str2) #concatenate entire string\n", - "print str1" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "This is a test01234\n", - "this is a test0123456789\n" - ] - } - ], - "prompt_number": 9 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 8.13, Page Number: 177<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def myfunc(i):\n", - " return i\n", - " \n", - "print myfunc(10.1),\n", - "print myfunc(10)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "10.1 10\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 8.14, Page Number: 178<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "from ctypes import *\n", - "\n", - "def myfunc(ch):\n", - " if isinstance(ch,c_int):\n", - " return chr(ch.value+1)\n", - " elif isinstance(ch,c_uint):\n", - " return chr(ch.value-1)\n", - " \n", - " \n", - "print myfunc(c_int(ord('c'))),\n", - "print myfunc(c_uint(88))" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - " d W\n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 8.15, Page Number: 179<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def myfunc(i,j=1):\n", - " if j==1:\n", - " return i*j\n", - " else:\n", - " return i\n", - " \n", - " \n", - "print myfunc(4,5),\n", - "print myfunc(10)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "4 10\n" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "code", - "collapsed": false, - "input": [], - "language": "python", - "metadata": {}, - "outputs": [] - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file diff --git a/C++_from_the_Ground/Chapter_9(1).ipynb b/C++_from_the_Ground/Chapter_9(1).ipynb deleted file mode 100755 index 0f30e9a8..00000000 --- a/C++_from_the_Ground/Chapter_9(1).ipynb +++ /dev/null @@ -1,813 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:441f69a4dc3d0bee097a6151c42e10920892966ba4b3b82086ec3feb521e7da7" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h1>Chapter 9: More Data Types and Operations<h1>" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 9.1, Page Number: 182<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def code(str):\n", - " print str\n", - "\n", - "#Calling function\n", - "code(\"this is a test\")" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "this is a test\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 9.2, Page Number: 183<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def f(i):\n", - " i=100\n", - " print i\n", - " \n", - "#Variable declaration\n", - "k=10\n", - "\n", - "#function call\n", - "f(k)\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "100\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 9.3, Page Number: 187<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "#Variable Declaration\n", - "first=10 #global definition of first and last\n", - "last=20\n", - "\n", - "#Result\n", - "print first,last\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "10 20\n" - ] - } - ], - "prompt_number": 7 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 9.4, Page Number: 188<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "#Variable declaration\n", - "sum=0 \n", - "count=0\n", - "num=5 #Loop for user entries\n", - "\n", - "#compute a running average\n", - "def r_avg(i):\n", - " global sum,count\n", - " sum=sum+i\n", - " count+=1\n", - " return sum/count\n", - "\n", - "\n", - "while True:\n", - " print \"Enter numbers(-1 to quit): \"\n", - " num-=1 #User input\n", - " if not(num==-1):\n", - " print \"Running average is: \",r_avg(num) #Result\n", - " if num<0:\n", - " break" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter numbers(-1 to quit): \n", - "Running average is: 4\n", - "Enter numbers(-1 to quit): \n", - "Running average is: 3\n", - "Enter numbers(-1 to quit): \n", - "Running average is: 3\n", - "Enter numbers(-1 to quit): \n", - "Running average is: 2\n", - "Enter numbers(-1 to quit): \n", - "Running average is: 2\n", - "Enter numbers(-1 to quit): \n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 9.5, Page Number: 189<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "#Variable declaration\n", - "sum=0 \n", - "count=0\n", - "num=10 #Loop for user entries\n", - "\n", - "#user input given: 9,8,7,6,-2,4,3,2,1,-1\n", - "\n", - "#compute a running average\n", - "def r_avg(i):\n", - " global sum,count\n", - " sum=sum+i\n", - " count+=1\n", - " return sum/count\n", - "\n", - "def reset():\n", - " global sum,count\n", - " sum=0\n", - " count=0\n", - " \n", - "while True:\n", - " print \"Enter numbers(-1 to quit, -2 to reset): \"\n", - " num-=1 #User input\n", - " if num==5:\n", - " num=-2\n", - " if num==-2: #for reset\n", - " num=4\n", - " reset()\n", - " continue\n", - " if not(num==-1):\n", - " print \"Running average is: \",r_avg(num) #Result\n", - " else:\n", - " break" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter numbers(-1 to quit, -2 to reset): \n", - "Running average is: 9\n", - "Enter numbers(-1 to quit, -2 to reset): \n", - "Running average is: 8\n", - "Enter numbers(-1 to quit, -2 to reset): \n", - "Running average is: 8\n", - "Enter numbers(-1 to quit, -2 to reset): \n", - "Running average is: 7\n", - "Enter numbers(-1 to quit, -2 to reset): \n", - "Enter numbers(-1 to quit, -2 to reset): \n", - "Running average is: 3\n", - "Enter numbers(-1 to quit, -2 to reset): \n", - "Running average is: 2\n", - "Enter numbers(-1 to quit, -2 to reset): \n", - "Running average is: 2\n", - "Enter numbers(-1 to quit, -2 to reset): \n", - "Running average is: 1\n", - "Enter numbers(-1 to quit, -2 to reset): \n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 9.6, Page Number: 196<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "name=[\"Jonathan\",\"Golden Delicious\",\"Red Delicious\",\"Winesap\",\n", - " \"Cortland\",\"McIntosh\"]\n", - "\n", - "#enumeration type\n", - "(Jonathan,Golden_Delicious,Red_Delicious,Winesap,Cortland,McIntosh) = (0,1,2,3,4,5)\n", - "\n", - "fruit=Jonathan\n", - "print name[fruit]\n", - "\n", - "fruit = Winesap\n", - "print name[fruit]\n", - "\n", - "fruit = McIntosh\n", - "print name[fruit]" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Jonathan\n", - "Winesap\n", - "McIntosh\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 9.7, Page Number: 198<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "ch='j' #User input\n", - "while True:\n", - " #This statement turns off the 6th but.\n", - " c=chr(ord(ch)&223) #ch is now uppercase\n", - " print c\n", - " if c=='Q':\n", - " break \n", - " else:\n", - " ch = chr(ord(ch)+1) #incrementing for different user inputs\n", - " \n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "J\n", - "K\n", - "L\n", - "M\n", - "N\n", - "O\n", - "P\n", - "Q\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 9.8, Page Number: 200<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "#Variable declaration\n", - "ch='J' #User input\n", - "while True:\n", - " #This statement turns off the 6th but.\n", - " c=chr(ord(ch)|32) #ch is now uppercase\n", - " print c\n", - " if c=='q':\n", - " break \n", - " else:\n", - " ch = chr(ord(ch)+1) #incrementing for different user inputs\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "j\n", - "k\n", - "l\n", - "m\n", - "n\n", - "o\n", - "p\n", - "q\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 9.9, Page Number: 201<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "def disp_binary(u):\n", - " t=128\n", - " while t:\n", - " if u&t:\n", - " print 1,\n", - " else:\n", - " print 0,\n", - " t=t/2\n", - " print \"\"\n", - " \n", - "#Variable declaration\n", - "u=99 #User Input\n", - "\n", - "print \"Here's the number in binary: \",\n", - "disp_binary(u)\n", - "\n", - "print \"Here's the complement of th number: \",\n", - "disp_binary(~u)\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Here's the number in binary: 0 1 1 0 0 0 1 1 \n", - "Here's the complement of th number: 1 0 0 1 1 1 0 0 \n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 9.10, Page Number: 202<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - " \n", - "def disp_binary(u):\n", - " t=128\n", - " while t:\n", - " if u&t:\n", - " print 1,\n", - " else:\n", - " print 0,\n", - " t=t/2\n", - " print \"\"\n", - " \n", - "#Variable dclaration\n", - "i=1\n", - "\n", - "#Result\n", - "for t in range(8):\n", - " disp_binary(i)\n", - " i=i<<1\n", - "\n", - "print\"\\n\"\n", - "\n", - "for t in range(8):\n", - " i=i>>1\n", - " disp_binary(i)\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "0 0 0 0 0 0 0 1 \n", - "0 0 0 0 0 0 1 0 \n", - "0 0 0 0 0 1 0 0 \n", - "0 0 0 0 1 0 0 0 \n", - "0 0 0 1 0 0 0 0 \n", - "0 0 1 0 0 0 0 0 \n", - "0 1 0 0 0 0 0 0 \n", - "1 0 0 0 0 0 0 0 \n", - "\n", - "\n", - "1 0 0 0 0 0 0 0 \n", - "0 1 0 0 0 0 0 0 \n", - "0 0 1 0 0 0 0 0 \n", - "0 0 0 1 0 0 0 0 \n", - "0 0 0 0 1 0 0 0 \n", - "0 0 0 0 0 1 0 0 \n", - "0 0 0 0 0 0 1 0 \n", - "0 0 0 0 0 0 0 1 \n" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 9.11, Page Number: 204<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "def div_zero():\n", - " print \"Cannot divide by zero.\"\n", - " return 0\n", - " \n", - "#Variable declaration\n", - "i=10 #User Input\n", - "j=0\n", - "\n", - "#This statement prevents a divide by zero\n", - "if j:\n", - " result=i/j\n", - "else:\n", - " result=div_zero()\n", - "\n", - "#Result\n", - "print \"Result: \",result" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Cannot divide by zero.\n", - "Result: 0\n" - ] - } - ], - "prompt_number": 7 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 9.12, Page Number: 206<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "#Variable declaration\n", - "j=10\n", - "i=None\n", - "\n", - "j+=1\n", - "j+100\n", - "i=999+j\n", - "\n", - "#Result\n", - "print i" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "1010\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 9.13, Page Number: 207<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "\n", - "from ctypes import *\n", - "\n", - "#Variable declaration\n", - "ch=c_char\n", - "i=c_int\n", - "\n", - "print sizeof(ch), #size of char\n", - "print sizeof(i), #size of int\n", - "print sizeof(c_float), #size of float\n", - "print sizeof(c_double) #size of double\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "1 4 4 8\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 9.14, Page Number: 209<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "from ctypes import *\n", - "\n", - "#Variabke declaration \n", - "i=c_int(20) #allocate memory for int\n", - "p=pointer(i) #assign a pointer to the memory\n", - "\n", - "#Result\n", - "print p[0] #proove that it works by displaying value\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "20\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 9.15, Page Number: 210<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "from ctypes import *\n", - "\n", - "#Variable declaration \n", - "i=c_int(99) #initialize with 99\n", - "p=pointer(i) #assign a pointer to the value\n", - "\n", - "#Result\n", - "print p[0] #displays 99\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "99\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 9.16, Page Number: 211<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "from ctypes import *\n", - "\n", - "#Variable declaration \n", - "i=c_double(10) \n", - "p=pointer(i)\n", - "\n", - "#assign the values 100 to 109\n", - "for i in range(10):\n", - " p[i]=100.00+i\n", - "\n", - "#display the contents of the array\n", - "for i in range(10):\n", - " print p[i],\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "100.0 101.0 102.0 103.0 104.0 105.0 106.0 107.0 108.0 109.0\n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 9.17, Page Number: 211<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "#Variable declaration\n", - "i=c_int() \n", - "j=c_double()\n", - "pi=pointer(i) #pointer to int\n", - "pj=pointer(j) #pointer to double\n", - "\n", - "#Assign values using pointers\n", - "pi[0]=10\n", - "pj[0]=100.123\n", - "\n", - "#Result\n", - "print pi[0],pj[0]\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "10 100.123\n" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<h3>Example 9.18, Page Number: 212<h3>" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "from ctypes import *\n", - "\n", - "#Variable declaration\n", - "i=pointer(c_int())\n", - "j=pointer(c_double())\n", - "\n", - "#Checking if i and j have been allocated memory addresses\n", - "if not(id(i)):\n", - " print \"Allocation Failure.\"\n", - " \n", - "if not(id(j)):\n", - " print \"Allocation Failure.\" \n", - "\n", - "i[0]=10\n", - "j[0]=100.123\n", - "\n", - "\n", - "#Result\n", - "print i[0],j[0]\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "10 100.123\n" - ] - } - ], - "prompt_number": 11 - }, - { - "cell_type": "code", - "collapsed": false, - "input": [], - "language": "python", - "metadata": {}, - "outputs": [] - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file |