summaryrefslogtreecommitdiff
path: root/ANSI_C_Programming/chapter9.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'ANSI_C_Programming/chapter9.ipynb')
-rw-r--r--ANSI_C_Programming/chapter9.ipynb1914
1 files changed, 957 insertions, 957 deletions
diff --git a/ANSI_C_Programming/chapter9.ipynb b/ANSI_C_Programming/chapter9.ipynb
index 43e1d355..54eec269 100644
--- a/ANSI_C_Programming/chapter9.ipynb
+++ b/ANSI_C_Programming/chapter9.ipynb
@@ -1,958 +1,958 @@
-{
- "metadata": {
- "name": "chapter9.ipynb"
- },
- "nbformat": 3,
- "nbformat_minor": 0,
- "worksheets": [
- {
- "cells": [
- {
- "cell_type": "heading",
- "level": 1,
- "metadata": {},
- "source": [
- "CHAPTER 9: PUPPETTING ON STRINGS"
- ]
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:311"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Program to demonstrate printing of a string\n",
- "name=\"Klinsman\"\n",
- "i=0\n",
- "while i<=7:\n",
- " print \"%c\" % (name[i]), #method of printing without line feed\n",
- " i+=1\n",
- "print \"\\n\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "K l i n s m a n \n",
- "\n"
- ]
- }
- ],
- "prompt_number": 4
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:311-312"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#printing a string stored in 1-D array\n",
- "name=\"Klinsman \"\n",
- "i=0\n",
- "while name[i]!=' ':\n",
- " print \"%c\" % (name[i]), \n",
- " i+=1\n",
- "print \"\\n\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- " K l i n s m a n \n",
- "\n"
- ]
- }
- ],
- "prompt_number": 14
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:312"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Program to demonstrate printing of a string\n",
- "name=\"Klinsman \"\n",
- "ptr=[]\n",
- "ptr=name #store base address of string\n",
- "i=0\n",
- "while name[i]!=' ':\n",
- " print \"%c\" % (ptr[i]), \n",
- " i+=1\n",
- "print \"\\n\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "K l i n s m a n \n",
- "\n"
- ]
- }
- ],
- "prompt_number": 18
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:313"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#printing a string\n",
- "name=\"Klinsman\"\n",
- "print \"%s\" % (name)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Klinsman\n"
- ]
- }
- ],
- "prompt_number": 19
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:313"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#storing & printing a string\n",
- "print \"Enter your name\"\n",
- "name=raw_input()\n",
- "print \"Hello %s!\\n\" % (name)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter your name\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Debashish\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Hello Debashish!\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 20
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:314"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#storing & printing a string\n",
- "print \"Enter your full name:\"\n",
- "name=raw_input()\n",
- "print \"Hello!\"\n",
- "print name"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter your full name:\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Debashish Roy\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Hello!\n",
- "Debashish Roy\n"
- ]
- }
- ],
- "prompt_number": 21
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:315"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Dynamic variables in python\n",
- "str1=\"Hello\"\n",
- "s=\"Good Morning\"\n",
- "str2=str1 #No error in Python since all variables are by default pointers\n",
- "q=s"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 6
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:316"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Dynamic allocation in python\n",
- "str1=\"Hello\"\n",
- "p=\"Hello\"\n",
- "str1=\"Bye\" #No error in python\n",
- "p=\"Bye\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 5
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:317"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#to count number of characters in an array\n",
- "arr=\"Bamboozled\"\n",
- "len1=len(arr) #return length of string\n",
- "len2=len(\"Humpty Dumpty\")\n",
- "print \"string=%s length=%d\\n\" % (arr,len1)\n",
- "print \"string=%s length=%d\\n\" % (\"Humpty Dumpty\",len2)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "string=Bamboozled length=10\n",
- "\n",
- "string=Humpty Dumpty length=13\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 22
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:317-318"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#A look-alike of the function strlen()\n",
- "def xstrlen(s):\n",
- " length=0\n",
- " for item in s:\n",
- " if s!=\"\\0\":\n",
- " length+=1\n",
- " return length\n",
- "arr=\"Bamboozled\"\n",
- "len1=xstrlen(arr) \n",
- "len2=xstrlen(\"Humpty Dumpty\")\n",
- "print \"string=%s length=%d\\n\" % (arr,len1)\n",
- "print \"string=%s length=%d\\n\" % (\"Humpty dumpty\",len2)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "string=Bamboozled length=10\n",
- "\n",
- "string=Humpty dumpty length=13\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 23
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:319"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Copying one string into another\n",
- "import copy\n",
- "source=\"Sayonara\"\n",
- "target=copy.copy(source) #copy string\n",
- "print \"source string=%s\\n\" % (source)\n",
- "print \"target string=%s\\n\" % (target)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "source string=Sayonara\n",
- "\n",
- "target string=Sayonara\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 24
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:319-320"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#A look-alike of the function strcpy()\n",
- "def xstrcpy(t,s):\n",
- " t=\"\\0\"\n",
- " for item in s:\n",
- " t=t+str(item)\n",
- " return t\n",
- "source=\"Sayonara\"\n",
- "target=[]\n",
- "target=xstrcpy(target,source)\n",
- "print \"source string=%s\\n\" % (source)\n",
- "print \"target string=%s\\n\" % (target)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "source string=Sayonara\n",
- "\n",
- "target string=\u0000Sayonara\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 25
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:321"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#print area of circle\n",
- "pi=3.14\n",
- "print \"Enter radius of circle\"\n",
- "r=eval(raw_input())\n",
- "a=pi*r*r\n",
- "print \"Area of circle=%f\\n\" % (a)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter radius of circle\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "3\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Area of circle=28.260000\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 26
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:322"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Concatenating two strings\n",
- "source=\"Folks!\"\n",
- "target=\"Hello\"\n",
- "target=target+source\n",
- "print \"source string=%s\\n\" % (source)\n",
- "print \"target string=%s\\n\" % (target)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "source string=Folks!\n",
- "\n",
- "target string=HelloFolks!\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 27
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:322-323"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Comparing two strings\n",
- "string1=\"Jerry\"\n",
- "string2=\"Ferry\"\n",
- "i=cmp(string1,\"Jerry\")\n",
- "j=cmp(string1,string2)\n",
- "k=cmp(string1,\"Jerry boy\")\n",
- "print \"%d %d %d\\n\" % (i,j,k)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "0 1 -1\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 29
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:323-324"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Application of 2-D araay of characters\n",
- "def FOUND():\n",
- " return 1\n",
- "def NOTFOUND():\n",
- " return 0\n",
- "masterlist=[\"akshay\",\"parag\",\"raman\",\"srinivas\",\"gopal\",\"rajesh\"]\n",
- "print \"Enter your name\"\n",
- "yourname=raw_input()\n",
- "flag=NOTFOUND()\n",
- "for i in range(0,6,1):\n",
- " a=cmp(masterlist[i],yourname)\n",
- " if a==0:\n",
- " print \"Welcome,you can enter the palace\\n\"\n",
- " flag=FOUND()\n",
- " break\n",
- "if flag==NOTFOUND():\n",
- " print \"Sorry,you are a trespasser\\n\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter your name\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "gopal\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Welcome,you can enter the palace\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 51
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:327"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Exchange names using 2-D array of characters\n",
- "names=[\"akshay\",\"parag\",\"raman\",\"srinivas\",\"gopal\",\"rajesh\"]\n",
- "names = map(bytearray, names)\n",
- "print \"Original:%s %s\\n\" % (names[2],names[3])\n",
- "s = sorted((names[2], names[3]), key=len)\n",
- "for i in range(len(s[1])):\n",
- " try:\n",
- " t=s[0][i]\n",
- " s[0][i]=s[1][i]\n",
- " s[1][i]=t\n",
- " except IndexError:\n",
- " for _ in range(i, len(s[1])):\n",
- " #remove the items from the longer string to and append them to the shorter one.\n",
- " s[0].append(s[1].pop(i)) \n",
- "print \"New:%s %s\\n\" % (names[2],names[3])"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Original:raman srinivas\n",
- "\n",
- "New:srinivas raman\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 46
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE-328"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Exchange names using 2-D array of characters\n",
- "names=[\"akshay\",\"parag\",\"raman\",\"srinivas\",\"gopal\",\"rajesh\"]\n",
- "print \"Original:%s %s\\n\" % (names[2],names[3])\n",
- "t=names[2]\n",
- "names[2]=names[3]\n",
- "names[3]=t\n",
- "print \"New:%s %s\\n\" % (names[2],names[3])"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Original:raman srinivas\n",
- "\n",
- "New:srinivas raman\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 48
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:329"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Storing elements in an array\n",
- "names=[]\n",
- "for i in range(6):\n",
- " names.append(\"\\0\") \n",
- "for i in range(0,6,1):\n",
- " print \"Enter name\"\n",
- " names[i]=raw_input()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter name\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "RAM\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter name\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "GOPAL\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter name\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "PANDEY\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter name\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "GOPU\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter name\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "CHOTU\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter name\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "RADHEY\n"
- ]
- }
- ],
- "prompt_number": 50
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:329-330"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Dynamic memory allocation of variables\n",
- "#No need of malloc func. in python as by default it's dynamic\n",
- "names=[]\n",
- "for i in range(0,6):\n",
- " names.append('\\0')\n",
- "for i in range(0,6,1):\n",
- " print \"Enter name\"\n",
- " n=raw_input()\n",
- " length=len(n)\n",
- " import copy\n",
- " p=copy.copy(n)\n",
- " names[i]=p\n",
- "for i in range(0,6,1):\n",
- " print \"%s\\n\" % (names[i])"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter name\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "MUKUT\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter name\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "BIHARI\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter name\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "PANDEY\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter name\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "MADHUBALA\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter name\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "PANDEY\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter name\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "SUNITA\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "MUKUT\n",
- "\n",
- "BIHARI\n",
- "\n",
- "PANDEY\n",
- "\n",
- "MADHUBALA\n",
- "\n",
- "PANDEY\n",
- "\n",
- "SUNITA\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 7
- }
- ],
- "metadata": {}
- }
- ]
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:d543de519ea53ce295e62c624f3b779f3394a58f64540659aef438fffab60f98"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "CHAPTER 9: PUPPETTING ON STRINGS"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:311"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "name=\"Klinsman\"\n",
+ "i=0\n",
+ "while i<=7:\n",
+ " print \"%c\" % (name[i]), #method of printing without line feed\n",
+ " i+=1\n",
+ "print \"\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "K l i n s m a n \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:311-312"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "name=\"Klinsman \"\n",
+ "i=0\n",
+ "while name[i]!=' ':\n",
+ " print \"%c\" % (name[i]), \n",
+ " i+=1\n",
+ "print \"\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " K l i n s m a n \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:312"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "name=\"Klinsman \"\n",
+ "ptr=[]\n",
+ "ptr=name #store base address of string\n",
+ "i=0\n",
+ "while name[i]!=' ':\n",
+ " print \"%c\" % (ptr[i]), \n",
+ " i+=1\n",
+ "print \"\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "K l i n s m a n \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 18
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:313"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "name=\"Klinsman\"\n",
+ "print \"%s\" % (name)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Klinsman\n"
+ ]
+ }
+ ],
+ "prompt_number": 19
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:313"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "print \"Enter your name\"\n",
+ "name=raw_input()\n",
+ "print \"Hello %s!\\n\" % (name)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter your name\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Debashish\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Hello Debashish!\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 20
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:314"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "print \"Enter your full name:\"\n",
+ "name=raw_input()\n",
+ "print \"Hello!\"\n",
+ "print name"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter your full name:\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Debashish Roy\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Hello!\n",
+ "Debashish Roy\n"
+ ]
+ }
+ ],
+ "prompt_number": 21
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:315"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "str1=\"Hello\"\n",
+ "s=\"Good Morning\"\n",
+ "str2=str1 #No error in Python since all variables are by default pointers\n",
+ "q=s"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:316"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "str1=\"Hello\"\n",
+ "p=\"Hello\"\n",
+ "str1=\"Bye\" #No error in python\n",
+ "p=\"Bye\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:317"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "arr=\"Bamboozled\"\n",
+ "len1=len(arr) #return length of string\n",
+ "len2=len(\"Humpty Dumpty\")\n",
+ "print \"string=%s length=%d\\n\" % (arr,len1)\n",
+ "print \"string=%s length=%d\\n\" % (\"Humpty Dumpty\",len2)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "string=Bamboozled length=10\n",
+ "\n",
+ "string=Humpty Dumpty length=13\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 22
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:317-318"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "def xstrlen(s):\n",
+ " length=0\n",
+ " for item in s:\n",
+ " if s!=\"\\0\":\n",
+ " length+=1\n",
+ " return length\n",
+ "arr=\"Bamboozled\"\n",
+ "len1=xstrlen(arr) \n",
+ "len2=xstrlen(\"Humpty Dumpty\")\n",
+ "print \"string=%s length=%d\\n\" % (arr,len1)\n",
+ "print \"string=%s length=%d\\n\" % (\"Humpty dumpty\",len2)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "string=Bamboozled length=10\n",
+ "\n",
+ "string=Humpty dumpty length=13\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 23
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:319"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "import copy\n",
+ "source=\"Sayonara\"\n",
+ "target=copy.copy(source) #copy string\n",
+ "print \"source string=%s\\n\" % (source)\n",
+ "print \"target string=%s\\n\" % (target)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "source string=Sayonara\n",
+ "\n",
+ "target string=Sayonara\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 24
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:319-320"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "def xstrcpy(t,s):\n",
+ " t=\"\\0\"\n",
+ " for item in s:\n",
+ " t=t+str(item)\n",
+ " return t\n",
+ "source=\"Sayonara\"\n",
+ "target=[]\n",
+ "target=xstrcpy(target,source)\n",
+ "print \"source string=%s\\n\" % (source)\n",
+ "print \"target string=%s\\n\" % (target)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "source string=Sayonara\n",
+ "\n",
+ "target string=\u0000Sayonara\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 25
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:321"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "pi=3.14\n",
+ "print \"Enter radius of circle\"\n",
+ "r=eval(raw_input())\n",
+ "a=pi*r*r\n",
+ "print \"Area of circle=%f\\n\" % (a)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter radius of circle\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Area of circle=28.260000\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 26
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:322"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "source=\"Folks!\"\n",
+ "target=\"Hello\"\n",
+ "target=target+source\n",
+ "print \"source string=%s\\n\" % (source)\n",
+ "print \"target string=%s\\n\" % (target)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "source string=Folks!\n",
+ "\n",
+ "target string=HelloFolks!\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 27
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:322-323"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "string1=\"Jerry\"\n",
+ "string2=\"Ferry\"\n",
+ "i=cmp(string1,\"Jerry\")\n",
+ "j=cmp(string1,string2)\n",
+ "k=cmp(string1,\"Jerry boy\")\n",
+ "print \"%d %d %d\\n\" % (i,j,k)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "0 1 -1\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 29
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:323-324"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "def FOUND():\n",
+ " return 1\n",
+ "def NOTFOUND():\n",
+ " return 0\n",
+ "masterlist=[\"akshay\",\"parag\",\"raman\",\"srinivas\",\"gopal\",\"rajesh\"]\n",
+ "print \"Enter your name\"\n",
+ "yourname=raw_input()\n",
+ "flag=NOTFOUND()\n",
+ "for i in range(0,6,1):\n",
+ " a=cmp(masterlist[i],yourname)\n",
+ " if a==0:\n",
+ " print \"Welcome,you can enter the palace\\n\"\n",
+ " flag=FOUND()\n",
+ " break\n",
+ "if flag==NOTFOUND():\n",
+ " print \"Sorry,you are a trespasser\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter your name\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "gopal\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Welcome,you can enter the palace\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 51
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:327"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "names=[\"akshay\",\"parag\",\"raman\",\"srinivas\",\"gopal\",\"rajesh\"]\n",
+ "names = map(bytearray, names)\n",
+ "print \"Original:%s %s\\n\" % (names[2],names[3])\n",
+ "s = sorted((names[2], names[3]), key=len)\n",
+ "for i in range(len(s[1])):\n",
+ " try:\n",
+ " t=s[0][i]\n",
+ " s[0][i]=s[1][i]\n",
+ " s[1][i]=t\n",
+ " except IndexError:\n",
+ " for _ in range(i, len(s[1])):\n",
+ " #remove the items from the longer string to and append them to the shorter one.\n",
+ " s[0].append(s[1].pop(i)) \n",
+ "print \"New:%s %s\\n\" % (names[2],names[3])"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Original:raman srinivas\n",
+ "\n",
+ "New:srinivas raman\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 46
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE-328"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "names=[\"akshay\",\"parag\",\"raman\",\"srinivas\",\"gopal\",\"rajesh\"]\n",
+ "print \"Original:%s %s\\n\" % (names[2],names[3])\n",
+ "t=names[2]\n",
+ "names[2]=names[3]\n",
+ "names[3]=t\n",
+ "print \"New:%s %s\\n\" % (names[2],names[3])"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Original:raman srinivas\n",
+ "\n",
+ "New:srinivas raman\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 48
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:329"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "names=[]\n",
+ "for i in range(6):\n",
+ " names.append(\"\\0\") \n",
+ "for i in range(0,6,1):\n",
+ " print \"Enter name\"\n",
+ " names[i]=raw_input()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter name\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "RAM\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter name\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "GOPAL\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter name\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "PANDEY\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter name\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "GOPU\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter name\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "CHOTU\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter name\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "RADHEY\n"
+ ]
+ }
+ ],
+ "prompt_number": 50
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:329-330"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "names=[]\n",
+ "for i in range(0,6):\n",
+ " names.append('\\0')\n",
+ "for i in range(0,6,1):\n",
+ " print \"Enter name\"\n",
+ " n=raw_input()\n",
+ " length=len(n)\n",
+ " import copy\n",
+ " p=copy.copy(n)\n",
+ " names[i]=p\n",
+ "for i in range(0,6,1):\n",
+ " print \"%s\\n\" % (names[i])"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter name\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "MUKUT\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter name\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "BIHARI\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter name\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "PANDEY\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter name\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "MADHUBALA\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter name\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "PANDEY\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter name\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "SUNITA\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "MUKUT\n",
+ "\n",
+ "BIHARI\n",
+ "\n",
+ "PANDEY\n",
+ "\n",
+ "MADHUBALA\n",
+ "\n",
+ "PANDEY\n",
+ "\n",
+ "SUNITA\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ }
+ ],
+ "metadata": {}
+ }
+ ]
} \ No newline at end of file