summaryrefslogtreecommitdiff
path: root/Programming_With_Java_A_Primer_by_E._Balagurusamy/chapter9.ipynb
diff options
context:
space:
mode:
authorkinitrupti2017-05-12 18:53:46 +0530
committerkinitrupti2017-05-12 18:53:46 +0530
commit6279fa19ac6e2a4087df2e6fe985430ecc2c2d5d (patch)
tree22789c9dbe468dae6697dcd12d8e97de4bcf94a2 /Programming_With_Java_A_Primer_by_E._Balagurusamy/chapter9.ipynb
parentd36fc3b8f88cc3108ffff6151e376b619b9abb01 (diff)
downloadPython-Textbook-Companions-6279fa19ac6e2a4087df2e6fe985430ecc2c2d5d.tar.gz
Python-Textbook-Companions-6279fa19ac6e2a4087df2e6fe985430ecc2c2d5d.tar.bz2
Python-Textbook-Companions-6279fa19ac6e2a4087df2e6fe985430ecc2c2d5d.zip
Removed duplicates
Diffstat (limited to 'Programming_With_Java_A_Primer_by_E._Balagurusamy/chapter9.ipynb')
-rwxr-xr-xProgramming_With_Java_A_Primer_by_E._Balagurusamy/chapter9.ipynb475
1 files changed, 475 insertions, 0 deletions
diff --git a/Programming_With_Java_A_Primer_by_E._Balagurusamy/chapter9.ipynb b/Programming_With_Java_A_Primer_by_E._Balagurusamy/chapter9.ipynb
new file mode 100755
index 00000000..af0b5898
--- /dev/null
+++ b/Programming_With_Java_A_Primer_by_E._Balagurusamy/chapter9.ipynb
@@ -0,0 +1,475 @@
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:5c730697085cba47efcd5224cc57d70aca1160f9a5e192f67596eb53b4b5b4af"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 9: Arrays, Strings & Vectors"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 9.1, page no. 157"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "number = [55, 40, 80, 65, 71]\n",
+ "print \"Given List: \",\n",
+ "for num in number:\n",
+ " print num,\n",
+ "number = sorted(number)[: : -1]\n",
+ "print \"\\nSorted List: \",\n",
+ "for num in number:\n",
+ " print num,"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Given List: 55 40 80 65 71 \n",
+ "Sorted List: 80 71 65 55 40\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 9.2, page no. 160"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "rows = 20\n",
+ "columns = 20\n",
+ "product = [[0 for x in range(rows)] for x in xrange(columns)]\n",
+ "print \"Multiplication Table\"\n",
+ "for i in range(10, rows):\n",
+ " for j in range(10, columns):\n",
+ " product[i][j] = i*j\n",
+ " print \" \", product[i][j],\n",
+ " print \" \""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Multiplication Table\n",
+ " 100 110 120 130 140 150 160 170 180 190 \n",
+ " 110 121 132 143 154 165 176 187 198 209 \n",
+ " 120 132 144 156 168 180 192 204 216 228 \n",
+ " 130 143 156 169 182 195 208 221 234 247 \n",
+ " 140 154 168 182 196 210 224 238 252 266 \n",
+ " 150 165 180 195 210 225 240 255 270 285 \n",
+ " 160 176 192 208 224 240 256 272 288 304 \n",
+ " 170 187 204 221 238 255 272 289 306 323 \n",
+ " 180 198 216 234 252 270 288 306 324 342 \n",
+ " 190 209 228 247 266 285 304 323 342 361 \n"
+ ]
+ }
+ ],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 9.3, page no. 163"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "name = [\"Madras\", \"Delhi\", \"Ahmedabad\", \"Calcutta\", \"Bombay\"]\n",
+ "name = sorted(name)\n",
+ "\n",
+ "for n in name:\n",
+ " print n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Ahmedabad\n",
+ "Bombay\n",
+ "Calcutta\n",
+ "Delhi\n",
+ "Madras\n"
+ ]
+ }
+ ],
+ "prompt_number": 18
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 9.4, page no. 164"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "aString = \"Object language\"\n",
+ "print \"Original String : \", aString\n",
+ "print \"Length of String: \", len(aString)\n",
+ "for i in range(len(aString)):\n",
+ " print \"Character at position: %d is %c\" %(i+1,aString[i])\n",
+ "\n",
+ "aString = aString.split(\" \")\n",
+ "aString.insert(1, \"-Oriented\") \n",
+ "aString = \" \".join(aString)\n",
+ "print \"Modified String: \", aString\n",
+ "aString = aString + \" improves security.\"\n",
+ "print \"Appended String: \", aString\n",
+ "\n",
+ "# Note : In python, string is immutable so we can not edit/update it."
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Original String : Object language\n",
+ "Length of String: 15\n",
+ "Character at position: 1 is O\n",
+ "Character at position: 2 is b\n",
+ "Character at position: 3 is j\n",
+ "Character at position: 4 is e\n",
+ "Character at position: 5 is c\n",
+ "Character at position: 6 is t\n",
+ "Character at position: 7 is \n",
+ "Character at position: 8 is l\n",
+ "Character at position: 9 is a\n",
+ "Character at position: 10 is n\n",
+ "Character at position: 11 is g\n",
+ "Character at position: 12 is u\n",
+ "Character at position: 13 is a\n",
+ "Character at position: 14 is g\n",
+ "Character at position: 15 is e\n",
+ "Modified String: Object -Oriented language\n",
+ "Appended String: Object -Oriented language improves security.\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 9.5, page no. 166"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "#vector is an array only in Python & commandline arguments cannot be taken in IPython Notebook\n",
+ "\n",
+ "v1 = []\n",
+ "v1.append(\"Ada\")\n",
+ "v1.append(\"BASIC\")\n",
+ "v1.append(\"COBOL\")\n",
+ "v1.append(\"C++\")\n",
+ "v1.append(\"FORTRAN\")\n",
+ "v1.append(\"Python\")\n",
+ "\n",
+ "print \"List of Languages: \"\n",
+ "for ele in v1:\n",
+ " print ele"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "List of Languages: \n",
+ "Ada\n",
+ "BASIC\n",
+ "COBOL\n",
+ "C++\n",
+ "FORTRAN\n",
+ "Python\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 9.6, page no. 169"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\"\"\"\n",
+ "There is no wrapper class in Python.\n",
+ "\"\"\"\n",
+ "\n",
+ "principalAmount = float(raw_input(\"Enter Principal Amount: \"))\n",
+ "interestRate = float(raw_input(\"Enter Interest Rate: \"))\n",
+ "numYears = float(raw_input(\"Enter Number of Years: \"))\n",
+ "\n",
+ "def loan(p, r, n):\n",
+ " year = 1\n",
+ " my_sum = p\n",
+ " while year<=n:\n",
+ " my_sum = my_sum*(1+r)\n",
+ " year += 1\n",
+ " return my_sum\n",
+ "\n",
+ "def printline():\n",
+ " for i in range(1, 30):\n",
+ " print \"-\",\n",
+ " print \"\"\n",
+ "\n",
+ "value = loan(principalAmount, interestRate, numYears)\n",
+ "printline()\n",
+ "print \"Final Value: \", round(value,2)\n",
+ "printline()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Principal Amount: 5000\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Interest Rate: 0.15\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Number of Years: 4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - \n",
+ "Final Value: 8745.03\n",
+ "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - \n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 9.7, page no. 195"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\"\"\"\n",
+ "There is no inbuilt module to use stack as it is done in the book \"java.util.stack\". We will use normal lists as stack in Python\n",
+ "\"\"\"\n",
+ "\n",
+ "stack = []\n",
+ "stack.append(10)\n",
+ "stack.append(20)\n",
+ "stksum1 = stack.pop()\n",
+ "stksum2 = stack.pop()\n",
+ "stksum = stksum1 + stksum2\n",
+ "print \"The top most element from the stack is: \", stksum2\n",
+ "print \"The next top most element from the stack is: \", stksum1\n",
+ "print \"The sum of two elements from the stack is: \", stksum"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The top most element from the stack is: 10\n",
+ "The next top most element from the stack is: 20\n",
+ "The sum of two elements from the stack is: 30\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 9.8, page no. 196"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\"\"\"\n",
+ "Enum is not supported by default in Python 2.X version. It has been backported to various versions of Python.\n",
+ "But, the package has to be installed seperately.\n",
+ "Using normal class here.\n",
+ "\"\"\"\n",
+ "\n",
+ "class WorkingDays:\n",
+ " Sunday = 0\n",
+ " Monday = 1\n",
+ " Tuesday = 2\n",
+ " Wednesday = 3\n",
+ " Thursday = 4\n",
+ " Friday = 5\n",
+ " Saturday = 6\n",
+ "\n",
+ "def weekend(d):\n",
+ " if d == WorkingDays.Sunday:\n",
+ " print \"Value = Sunday is a Holiday\"\n",
+ " elif d == WorkingDays.Monday:\n",
+ " print \"Value = Monday is a working day\"\n",
+ " elif d == WorkingDays.Tuesday:\n",
+ " print \"Value = Tuesday is a working day\"\n",
+ " elif d == WorkingDays.Wednesday:\n",
+ " print \"Value = Wednesday is a working day\"\n",
+ " elif d == WorkingDays.Thursday:\n",
+ " print \"Value = Thursday is a working day\" \n",
+ " elif d == WorkingDays.Friday:\n",
+ " print \"Value = Friday is a working day\"\n",
+ " else:\n",
+ " print \"Value = Saturday is a working day\"\n",
+ "\n",
+ "for i in range(WorkingDays.Sunday, WorkingDays.Saturday+1):\n",
+ " weekend(i)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Value = Sunday is a Holiday\n",
+ "Value = Monday is a working day\n",
+ "Value = Tuesday is a working day\n",
+ "Value = Wednesday is a working day\n",
+ "Value = Thursday is a working day\n",
+ "Value = Friday is a working day\n",
+ "Value = Saturday is a working day\n"
+ ]
+ }
+ ],
+ "prompt_number": 18
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 9.9, page no. 199"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\"\"\"\n",
+ "There is no concept of annotations in Python\n",
+ "\"\"\"\n",
+ "\n",
+ "class MySingle:\n",
+ " value = 0\n",
+ " def __init__(self, x):\n",
+ " self.value = 100\n",
+ " def display(self):\n",
+ " print \"The value is: \", self.value\n",
+ "\n",
+ "ob = MySingle(100)\n",
+ "try:\n",
+ " ob.display()\n",
+ "except:\n",
+ " print \"No such method found...\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The value is: 100\n"
+ ]
+ }
+ ],
+ "prompt_number": 50
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file