{ "metadata": { "name": "", "signature": "sha256:b65111374697402ea20166f271bb1488f1e63cc13c5da33961c35513244b0d0c" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 18: Java Collections" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 18.1, page no. 348" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "Note: there is no such class in Python. We will perform normal operations on array\n", "\"\"\"\n", "\n", "arrayList = []\n", "print \"Initial size of arrayList: \", len(arrayList)\n", "arrayList.append(\"A\")\n", "arrayList.append(\"B\")\n", "arrayList.append(\"C\")\n", "arrayList.append(\"D\")\n", "arrayList.insert(2, \"E\")\n", "print \"Changed contents of arraylist by adding element at the given index: \", arrayList\n", "arrayList.pop(3)\n", "arrayList.remove(\"A\")\n", "print \"Changed contents of arraylist by removing element from the list: \", arrayList" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Initial size of arrayList: 0\n", "Changed contents of arraylist by adding element at the given index: ['A', 'B', 'E', 'C', 'D']\n", "Changed contents of arraylist by removing element from the list: ['B', 'E', 'D']\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 18.2, page no. 349" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "Note: There is no such collection in Python. We will dqueue instead, which is very much similar\n", "\"\"\"\n", "\n", "from collections import deque\n", "\n", "d = deque()\n", "\n", "class MyStack():\n", " def push1(self, o):\n", " d.appendleft(o)\n", " def push2(self, o):\n", " d.append(o)\n", " def bottom(self):\n", " return d.pop()\n", " def pop(self):\n", " return d.popleft()\n", "\n", "class Car():\n", " car1 = \"Benz\"\n", " car2 = \"Toyoto\"\n", " car3 = \"Qualis\"\n", " car4 = \"Santro\"\n", "\n", "class Bird():\n", " bird1 = \"parrot\"\n", " bird2 = \"duck\"\n", " bird3 = \"raven\"\n", "\n", "myCar = Car()\n", "myBird = Bird()\n", "s = MyStack()\n", "s.push1(myCar.car1)\n", "s.push2(myBird.bird3)\n", "myCar = s.pop()\n", "print \"The first element in the list: \", myCar\n", "print \"The last element in the list: \", myBird.bird3" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The first element in the list: Benz\n", "The last element in the list: raven\n" ] } ], "prompt_number": 15 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 18.3, page no. 351" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "Note: there is no collection hash set in Python. We will solve it in different manner\n", "\"\"\"\n", "\n", "hs = set()\n", "\n", "hs.add(\"D\")\n", "hs.add(\"A\")\n", "hs.add(\"C\")\n", "hs.add(\"B\")\n", "hs.add(\"E\")\n", "\n", "#answer will differ due to difference in built-in techniques\n", "print \"The elements available in the hash set are: \", hs" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The elements available in the hash set are: set(['A', 'C', 'B', 'E', 'D'])\n" ] } ], "prompt_number": 23 }, { "cell_type": "code", "collapsed": false, "input": [ "#There is no TreeSet or similar collection in Python (can be done using normal list like 18.1) so skipping example 18.4" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 25 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 18.5, page no. 353" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "Note: there is no vector in Python. Doing using normal lists.\n", "\"\"\"\n", "\n", "fruits = []\n", "\n", "fruits.append(\"Apple\")\n", "fruits.append(\"Orange\")\n", "fruits.append(\"Grapes\")\n", "fruits.append(\"Pine\")\n", "\n", "for ele in fruits:\n", " print ele" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Apple\n", "Orange\n", "Grapes\n", "Pine\n" ] } ], "prompt_number": 26 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 18.6, page no. 354" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "In python we can use list itself as stack\n", "\"\"\"\n", "\n", "st = []\n", "\n", "st.append(\"Java\")\n", "st.append(\"latest\")\n", "st.append(\"Edition\")\n", "st.append(\"-fifth\")\n", "\n", "print \"The elements in the Stack: \", st\n", "print \"The element at the top: \", st[-1:]\n", "print \"The element poped out of the stack: \", st.pop()\n", "print \"The element in a stack after pop out element: \", st\n", "try:\n", " \"The result of searching: \", st.index(\"r u\")\n", "except ValueError:\n", " print \"The result of searching: \", -1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The elements in the Stack: ['Java', 'latest', 'Edition', '-fifth']\n", "The element at the top: ['-fifth']\n", "The element poped out of the stack: -fifth\n", "The element in a stack after pop out element: ['Java', 'latest', 'Edition']\n", "The result of searching: -1\n" ] } ], "prompt_number": 29 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 18.7, page no. 355" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "Note: we will use dictionaries\n", "\"\"\"\n", "\n", "ht ={}\n", "\n", "ht['item1'] = \"Apple\"\n", "ht['item2'] = \"Orange\"\n", "ht['item3'] = \"Grapes\"\n", "ht['item4'] = \"Pine\"\n", "\n", "for key in ht.iterkeys():\n", " print ht[key]\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Orange\n", "Grapes\n", "Apple\n", "Pine\n" ] } ], "prompt_number": 38 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 18.8, page no. 357" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Answers will differ due to difference in built-in techniques of the classes\n", "\n", "from random import shuffle\n", "\n", "l = []\n", "\n", "l.append(\"java\")\n", "l.append(\"is\")\n", "l.append(\"platform\")\n", "l.append(\"independent\")\n", "\n", "l_r = list(reversed(l))\n", "l_r = sorted(l_r)\n", "\n", "print \"List sorted in reverse order is: \"\n", "for ele in l_r:\n", " print ele,\n", "\n", "print \"\\nList shuffled: \"\n", "shuffle(l_r)\n", "for ele in l_r:\n", " print ele,\n", "\n", "print \"\\nMinimum: \", min(l_r)\n", "print \"Maximum: \", max(l_r)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "List sorted in reverse order is: \n", "independent is java platform \n", "List shuffled: \n", "java independent is platform \n", "Minimum: independent\n", "Maximum: platform\n" ] } ], "prompt_number": 2 } ], "metadata": {} } ] }