{ "metadata": { "name": "", "signature": "sha256:c7a7a5fed3d237b23773c146200cb1c66c8b51b03326b1d931d288bf060c18a1" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 9 : Introduction to Data Structures" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9.1, Page No 205" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class employee:\n", " def __init__(self,fname,lname,id1,salary):\n", " self.fname = fname\n", " self.lname = lname\n", " self.id1 = id1\n", " self.salary = salary\n", "if __name__ == '__main__':\n", " emp1 = employee(\"Michael\",\"Vine\",123,50000.00)\n", " print \"\\n First name: \" + emp1.fname + \"\\n\"\n", " print \"Last name: \" + emp1.lname + \"\\n\"\n", " print \"Employee ID: \" + str(emp1.id1) + \"\\n\"\n", " print \"Salary: \" + str(emp1.salary) + \"\\n\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", " First name: Michael\n", "\n", "Last name: Vine\n", "\n", "Employee ID: 123\n", "\n", "Salary: 50000.0\n", "\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9.2, Page No 206" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class employee:\n", " def __init__(self,fname,lname,id1,salary):\n", " self.fname = fname\n", " self.lname = lname\n", " self.id1 = id1\n", " self.salary = salary\n", "if __name__ == '__main__':\n", " emp1 = employee(\"Michael\",\"Vine\",123,50000.00)\n", " print \"\\n First name: \" + emp1.fname + \"\\n\"\n", " print \"Last name: \" + emp1.lname + \"\\n\"\n", " print \"Employee ID: \" + str(emp1.id1) + \"\\n\"\n", " print \"Salary: \" + str(emp1.salary) + \"\\n\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", " First name: Michael\n", "\n", "Last name: Vine\n", "\n", "Employee ID: 123\n", "\n", "Salary: 50000.0\n", "\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9.3, Page No 209" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class scores:\n", " def __init__(self,name,score):\n", " self.name = name\n", " self.score = score\n", "if __name__ == '__main__':\n", " highScores = []\n", " highScores.append(scores(\"Hunter\",40768))\n", " highScores.append(scores(\"Kenya\",38568))\n", " highScores.append(scores(\"Apollo\",35985))\n", " for x in range(3):\n", " print \"\\n\" + highScores[x].name + \"\\t\" + str(highScores[x].score) + \"\\n\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Hunter\t40768\n", "\n", "\n", "Kenya\t38568\n", "\n", "\n", "Apollo\t35985\n", "\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9.4, Page No 211" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class employee:\n", " def __init__(self,id1,name,salary):\n", " self.id1 = id1\n", " self.name = name\n", " self.salary = salary\n", "def processEmp(emp1):\n", " emp1.id1 = 123\n", " emp1.name = \"Sheila\"\n", " emp1.salary = 65000.00\n", "if __name__ == '__main__':\n", " emp1 = employee(0,0,0)\n", " processEmp(emp1)\n", " print \"\\n Id : \" + str(emp1.id1) + \"\\n\"\n", " print \"Name : \" + emp1.name + \"\\n\"\n", " print \"Salary : \" + str(emp1.salary) + \"\\n\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", " Id : 123\n", "\n", "Name : Sheila\n", "\n", "Salary : 65000.0\n", "\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9.5, Page No 212" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class player:\n", " def __init__(self,name,score):\n", " self.name = name\n", " self.score = score\n", "if __name__ == '__main__':\n", " aPlayer = player(0,0)\n", " ptrPlayer = id(aPlayer)\n", " aPlayer.name = \"Pinball Wizard\"\n", " aPlayer.score = 1000000.00\n", " print \"\\nPlayer : \" + aPlayer.name + \"\\n\"\n", " print \"Score : \" + str(aPlayer.score)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Player : Pinball Wizard\n", "\n", "Score : 1000000.0\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9.6, Page No 213" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class employee:\n", " def __init__(self,id1,name,salary):\n", " self.id1 = id1\n", " self.name = name\n", " self.salary = salary\n", "def processEmp(emp1):\n", " emp1.id1 = 123\n", " emp1.name = \"Sheila\"\n", " emp1.salary = 65000.00\n", "if __name__ == '__main__':\n", " emp1 = employee(0,0,0)\n", " ptrEmp = id(emp1)\n", " processEmp(emp1)\n", " print \"\\nId : \" + str(emp1.id1) + \"\\n\"\n", " print \"Name : \" + emp1.name + \"\\n\"\n", " print \"Salary : \" + str(emp1.salary) + \"\\n\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Id : 123\n", "\n", "Name : Sheila\n", "\n", "Salary : 65000.0\n", "\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9.7, Page No 215" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class employee:\n", " def __init__(self,id1,name,salary):\n", " self.id1 = id1\n", " self.name = name\n", " self.salary = salary\n", "def processEmp(emp1 = []):\n", " emp1.append(employee(123,\"Sheila\",65000.00))\n", " emp1.append(employee(234,\"Hunter\",28000.00))\n", " emp1.append(employee(456,\"Kenya\",48000.00))\n", " return emp1\n", "if __name__ == '__main__':\n", " emp1 = []\n", " emp1 = employee(0,0,0)\n", " emp1 = processEmp(emp1 = [])\n", " for x in range(3):\n", " print \"\\nId : \" + str(emp1[x].id1) + \"\\n\" \n", " print \"Name : \" + emp1[x].name + \"\\n\"\n", " print \"Salary : \" + str(emp1[x].salary)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Id : 123\n", "\n", "Name : Sheila\n", "\n", "Salary : 65000.0\n", "\n", "Id : 234\n", "\n", "Name : Hunter\n", "\n", "Salary : 28000.0\n", "\n", "Id : 456\n", "\n", "Name : Kenya\n", "\n", "Salary : 48000.0\n" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9.8, Page No 217" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class phonebook:\n", " def __init__(self,name,number,address):\n", " self.name = name\n", " self.number = number\n", " self.address = address\n", "class magazine:\n", " def __init__(self,name,author,isbn):\n", " self.name = name\n", " self.author = author\n", " self.isbn = isbn\n", "if __name__ == '__main__':\n", " aBook = phonebook(\"John\",123,\"John Smith\")\n", " aMagazine = magazine(\"C Programming\",\"Michael Vine\",1-59863-480-1)\n", " print \"\\nUnion Details\\n\"\n", " print \"Address for aBook.name: \" + str(id(aBook.name)) + \"\\n\"\n", " print \"Address for aBook.number: \", str(id(aBook.number)) + \"\\n\"\n", " print \"Address for aBook.address: \", str(id(aBook.address)) + \"\\n\"\n", " print \"\\nStructure Details\\n\"\n", " print \"Address for aMagazine.name: \", str(id(aMagazine.name)) + \"\\n\"\n", " print \"Address for aMagazine.author: \", str(id(aMagazine.author)) + \"\\n\"\n", " print \"Address for aMagazine.isbn: \", str(id(aMagazine.isbn)) + \"\\n\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " \n", "Union Details\n", "\n", "Address for aBook.name: 66178424\n", "\n", "Address for aBook.number: 30796656\n", "\n", "Address for aBook.address: 66151792\n", "\n", "\n", "Structure Details\n", "\n", "Address for aMagazine.name: 66151744\n", "\n", "Address for aMagazine.author: 66150976\n", "\n", "Address for aMagazine.isbn: 64689272\n", "\n" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9.9, Page No 219" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = 12\n", "y = 5\n", "print \"\\nWithout Type-Casting\\n\"\n", "print \"12 \\\\ 5 = \" + str(x/y) + \"\\n\"\n", "print \"\\nWith Type-Casting\\n\"\n", "print \"12 \\\\ 5 = \" + str(float(x)/float(y)) + \"\\n\"\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Without Type-Casting\n", "\n", "12 \\ 5 = 2\n", "\n", "\n", "With Type-Casting\n", "\n", "12 \\ 5 = 2.4\n", "\n" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9.10, Page No 220" ] }, { "cell_type": "code", "collapsed": false, "input": [ "number = 86\n", "letter = 'M'\n", "print \"\\n86 type-casted to char is: \" + chr(number) + \"\\n\"\n", "print \"\\n'M' type-casted to int is: \" + str(ord(letter)) + \"\\n \"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "86 type-casted to char is: V\n", "\n", "\n", "'M' type-casted to int is: 77\n", " \n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9.11, Page No 222" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import random\n", "class dec:\n", " def __init__(self,type1,used,value):\n", " self.type1 = type1\n", " self.used = used\n", " self.value = value\n", "def shuffel(thisDeck):\n", " found = 0\n", " print \"\\nYour five cards are: \\n\\n\"\n", " while found < 5:\n", " #iRnd = random.randint(1, 100) % 51 when we use this it gives error out of range\n", " if thisDeck[iRnd].used == 'n':\n", " if thisDeck[iRnd].value == 12:\n", " print \"Ace of : \" + str(thisDeck[iRnd].type1) + \"\\n\"\n", " elif thisDeck[iRnd].value == 11:\n", " print \"King of : \" + str(thisDeck[iRnd].type1) + \"\\n\"\n", " elif thisDeck[iRnd].value == 10:\n", " print \"Queen of : \" + str(thisDeck[iRnd].type1) + \"\\n\"\n", " elif thisDeck[iRnd].value == 9:\n", " print \"Jack of : \" + str(thisDeck[iRnd].type1) + \"\\n\"\n", " else:\n", " print str(thisDeck[iRnd].value + 2) + \"of\"\n", " print str(thisDeck[iRnd].type1) + \"\\n\" \n", "if __name__ == '__main__':\n", " myDeck = []\n", " for x in range(3):\n", " for y in range(13):\n", " if x==0:\n", " myDeck.append(dec(\"dimonds\",\"y\",\"n\"))\n", " elif x==1:\n", " myDeck.append(dec(\"clubs\",\"y\",\"n\"))\n", " elif x==2:\n", " myDeck.append(dec(\"hearts\",\"y\",\"n\"))\n", " elif x==3:\n", " myDeck.append(dec(\"spades\",\"y\",\"n\"))\n", " shuffel(myDeck)" ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }