{
 "metadata": {
  "name": "Chapter X"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 10: Character strings"
     ]
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Program 10.1, Page number: 197"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "import sys\n",
      "\n",
      "def concat(result,str1,n1,str2,n2):                 #Calculations\n",
      "        \n",
      "        #copy str1 to result\n",
      "        for i in range(0,n1):\n",
      "                result[i]=str1[i]\n",
      "        \n",
      "        #copy str2 to result        \n",
      "        for j in range(0,n2):\n",
      "                result[n1+j]=str2[j]\n",
      "\n",
      "\n",
      "def main():\n",
      "\n",
      "        #List Declaration\n",
      "        s1 = [ 'T', 'e', 's', 't', ' ']\n",
      "        s2 = [ 'w', 'o', 'r', 'k', 's', '.' ]\n",
      "        s3=[0]*11\n",
      "\n",
      "        concat(s3,s1,5,s2,6)                        #Fucntion call\n",
      "\n",
      "        for i in range(0,11):                       #Result\n",
      "                sys.stdout.write(\"{0}\".format(s3[i]))\n",
      "\n",
      "        sys.stdout.write(\"\\n\")\n",
      "\n",
      "\n",
      "if __name__=='__main__':\n",
      "        main()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Test works.\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Program 10.2, Page number: 199"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "def stringLength(string):\n",
      "        count = 0\n",
      "        while(string[count]!='\\0'):                  #Calculation\n",
      "                count+=1\n",
      "        return count\n",
      "\n",
      "def main():\n",
      "\n",
      "        #List Declaration\n",
      "        word1 = [ 'a', 's', 't', 'e', 'r', '\\0' ]\n",
      "        word2 = [ 'a', 't', '\\0' ]\n",
      "        word3 = [ 'a', 'w', 'e', '\\0' ]\n",
      "\n",
      "        #Result\n",
      "        print(\"{0}  {1}  {2}\\n\".format(stringLength (word1),stringLength (word2),stringLength (word3)))\n",
      "\n",
      "\n",
      "if __name__=='__main__':\n",
      "        main()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "5  2  3\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Program 10.3, Page number: 202"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "def concat(result,str1,str2):                            #Calculations\n",
      "        \n",
      "        #copy str1 to result\n",
      "        for i in str1:\n",
      "                result.append(i)\n",
      "        \n",
      "        #copy str2 to result        \n",
      "        for j in str2:\n",
      "                result.append(j)\n",
      "                \n",
      "\n",
      "def main():\n",
      "\n",
      "        #String Declaration\n",
      "        s1 = \"Test \"\n",
      "        s2 = \"works.\"\n",
      "        s3=[]\n",
      "\n",
      "        concat(s3,s1,s2)                                 #Fucntion call\n",
      "        print(''.join(s3))                               #Result\n",
      "\n",
      "\n",
      "if __name__=='__main__':\n",
      "        main()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Test works.\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Program 10.4, Page number: 204"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "def equalStrings(s1,s2):\n",
      "        \n",
      "        #Calculation\n",
      "        if(s1==s2):\n",
      "                areEqual=True\n",
      "        else:\n",
      "                areEqual=False\n",
      "        return areEqual\n",
      "\n",
      "\n",
      "\n",
      "def main():\n",
      "\n",
      "        #String Declaration\n",
      "        stra = \"string compare test\";\n",
      "        strb = \"string\";\n",
      "\n",
      "        #Result\n",
      "        print(\"{0}\".format(equalStrings (stra, strb)))\n",
      "        print(\"{0}\".format(equalStrings (stra, stra)))\n",
      "        print(\"{0}\".format(equalStrings (strb, \"string\")))\n",
      "\n",
      "\n",
      "if __name__=='__main__':\n",
      "        main()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "False\n",
        "True\n",
        "True\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Program 10.5, Page number: 207"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "def main():\n",
      "        print(\"Enter Text:\\n\")\n",
      "\n",
      "        #User Input\n",
      "        s1,s2,s3=map(str,\"System expansion bus\".split())\n",
      "        #s1,s2,s3=map(str,raw_input().split())\n",
      "        \n",
      "        \n",
      "        #Result\n",
      "        print(\"\\ns1={0}\\ns2={1}\\ns3={2}\\n\".format(s1,s2,s3))\n",
      "\n",
      "\n",
      "if __name__=='__main__':\n",
      "        main()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter Text:\n",
        "\n",
        "\n",
        "s1=System\n",
        "s2=expansion\n",
        "s3=bus\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Program 10.6, Page number: 209"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "def main():\n",
      "\n",
      "        #List declaration\n",
      "        line=['sample text']\n",
      "        for i in range(0,3):\n",
      "                readLine(line)                         #Function call\n",
      "                print(\"{0}\\n\".format(''.join(line)))   #Result\n",
      "\n",
      "\n",
      "def readLine(line):\n",
      "        \n",
      "        line.pop()\n",
      "        line.append(\"This is a sample line of text\"+\" \")\n",
      "        #line.append(raw_input()+\" \")\n",
      "\n",
      "\n",
      "if __name__=='__main__':\n",
      "        main()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "This is a sample line of text \n",
        "\n",
        "This is a sample line of text \n",
        "\n",
        "This is a sample line of text \n",
        "\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Program 10.7, Page number: 211"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "def alphabetic(c):\n",
      "        if ( (c >= 'a' and c <= 'z') or (c >= 'A' and c <= 'Z') ):\n",
      "                return True\n",
      "        else:\n",
      "                return False\n",
      "\n",
      "\n",
      "def countWords(string):\n",
      "        \n",
      "        #Variable Declaration\n",
      "        wordCount=0\n",
      "        lookingForWord=True\n",
      "        \n",
      "        #Calculations\n",
      "        for i in string:\n",
      "                if(alphabetic(i)):\n",
      "                        if(lookingForWord):\n",
      "                                wordCount+=1\n",
      "                                lookingForWord=False\n",
      "                else:\n",
      "                        lookingForWord=True\n",
      "        return wordCount\n",
      "\n",
      "\n",
      "def main():\n",
      "\n",
      "        #String Declaration\n",
      "        text1 = \"Well, here goes.\"\n",
      "        text2 = \"And here we go... again.\"\n",
      "        \n",
      "        #Result\n",
      "        print(\"{0} - words = {1}\\n\".format(text1,countWords(text1)))\n",
      "        print(\"{0} - words = {1}\\n\".format(text2,countWords(text2)))\n",
      "\n",
      "\n",
      "if __name__=='__main__':\n",
      "        main()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Well, here goes. - words = 3\n",
        "\n",
        "And here we go... again. - words = 5\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Program 10.8, Page number: 214"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "def readLine(line):\n",
      "        \n",
      "        line.pop()\n",
      "        line.append(\"This is a dummy text to replace raw_input()\"+\" \")\n",
      "        #line.append(raw_input()+\" \")\n",
      "\n",
      "\n",
      "def alphabetic(c):\n",
      "        if ( (c >= 'a' and c <= 'z') or (c >= 'A' and c <= 'Z') ):\n",
      "                return True\n",
      "        else:\n",
      "                return False\n",
      "\n",
      "\n",
      "def countWords(string):\n",
      "        \n",
      "        #Variable Declaration\n",
      "        wordCount=0\n",
      "        lookingForWord=True\n",
      "        \n",
      "        #Calculations\n",
      "        for i in string:\n",
      "                if(alphabetic(i)):\n",
      "                        if(lookingForWord):\n",
      "                                wordCount+=1\n",
      "                                lookingForWord=False\n",
      "                else:\n",
      "                        lookingForWord=True\n",
      "        return wordCount\n",
      "\n",
      "\n",
      "\n",
      "def main():\n",
      "        #Variable Declaration\n",
      "        totalWords=0\n",
      "        endOfText=False\n",
      "        text=['sample']\n",
      "\n",
      "        print(\"Type in your text.\\n\")\n",
      "        print(\"When you are done, press 'RETURN'.\\n\\n\")\n",
      "       \n",
      "        readLine (text)\n",
      "        \n",
      "        #Increment Counter\n",
      "        totalWords += countWords(''.join(text))\n",
      "        \n",
      "        #Result\n",
      "        print(\"\\nThere are {0} words in the above text.\\n\".format(totalWords))\n",
      "\n",
      "\n",
      "if __name__=='__main__':\n",
      "        main()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Type in your text.\n",
        "\n",
        "When you are done, press 'RETURN'.\n",
        "\n",
        "\n",
        "\n",
        "There are 9 words in the above text.\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Program 10.9, Page number: 220"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "class entry:\n",
      "        def __init__(self,w,d):\n",
      "                self.word=w\n",
      "                self.definition=d\n",
      "\n",
      "\n",
      "def equalStrings(str1,str2):\n",
      "        if(str1==str2):\n",
      "                return True\n",
      "        else:\n",
      "                return False\n",
      "\n",
      "def lookup(dictionary,search,entries):\n",
      "        for i in range(0,entries):\n",
      "                if ( equalStrings(search, dictionary[i].word) ):\n",
      "                        return i;\n",
      "        return -1;\n",
      "\n",
      "def main():\n",
      "\n",
      "        entries=10\n",
      "        global entry\n",
      "        dictionary=[]\n",
      "        \n",
      "        dictionary.append(entry(\"aardvark\",\"a burrowing African mammal\"))\n",
      "        dictionary.append(entry(\"abyss\",\"a bottomless pit\"))\n",
      "        dictionary.append(entry(\"acumen\",\"mentally sharp keen\"))\n",
      "        dictionary.append(entry(\"addle\",\"to become confused\"))\n",
      "        dictionary.append(entry(\"aerie\",\"a high nest\"))\n",
      "        dictionary.append(entry(\"affix\",\"to append; attach\"))\n",
      "        dictionary.append(entry(\"agar\",\"a jelly made from seaweed\"))\n",
      "        dictionary.append(entry(\"ahoy\",\"nautical call of greeting\"))\n",
      "        dictionary.append(entry(\"aigrette\",\"an ornamental cluster of feathers\"))\n",
      "        dictionary.append(entry(\"ajar\",\"partially opened\"))\n",
      "\n",
      "\n",
      "        print(\"Enter word:\")\n",
      "        word='ajar'                            #word=raw_input()\n",
      "        entry=lookup(dictionary,word,entries)\n",
      "\n",
      "        #Result\n",
      "        if(entry!=-1):\n",
      "                print(\"{0}\".format(dictionary[entry].definition))\n",
      "        else:\n",
      "                print(\"Sorry, the word {0} is not in my dictionary.\\n\".format(word))\n",
      "\n",
      "\n",
      "if __name__=='__main__':\n",
      "        main()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter word:\n",
        "partially opened\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Program 10.10, Page number: 224"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "class entry:\n",
      "        def __init__(self,w,d):                 #Class constructor\n",
      "                self.word=w\n",
      "                self.definition=d\n",
      "\n",
      "\n",
      "def compareStrings(s1,s2):\n",
      "\n",
      "        #Calculations\n",
      "        if( s1 < s2 ):\n",
      "                answer = -1;\n",
      "        elif( s1 == s2 ):\n",
      "                answer = 0;\n",
      "        else:\n",
      "                answer=1\n",
      "\n",
      "        return answer\n",
      "\n",
      "\n",
      "\n",
      "def lookup(dictionary,search,entries):\n",
      "        \n",
      "        #Variable Declaration\n",
      "        low=0\n",
      "        high=entries-1\n",
      "\n",
      "        #Calculations\n",
      "        while(low<=high):\n",
      "                mid=(low+high)/2\n",
      "                result=compareStrings(dictionary[mid].word,search)\n",
      "\n",
      "                if(result==-1):\n",
      "                        low=mid+1\n",
      "                elif(result==1):\n",
      "                        high=mid-1\n",
      "                else:\n",
      "                        return mid\n",
      "\n",
      "        return -1\n",
      "\n",
      "\n",
      "def main():\n",
      "\n",
      "        #Variable Declaration\n",
      "        entries=10\n",
      "        global entry\n",
      "        dictionary=[]\n",
      "        \n",
      "        dictionary.append(entry(\"aardvark\",\"a burrowing African mammal\"))\n",
      "        dictionary.append(entry(\"abyss\",\"a bottomless pit\"))\n",
      "        dictionary.append(entry(\"acumen\",\"mentally sharp keen\"))\n",
      "        dictionary.append(entry(\"addle\",\"to become confused\"))\n",
      "        dictionary.append(entry(\"aerie\",\"a high nest\"))\n",
      "        dictionary.append(entry(\"affix\",\"to append; attach\"))\n",
      "        dictionary.append(entry(\"agar\",\"a jelly made from seaweed\"))\n",
      "        dictionary.append(entry(\"ahoy\",\"nautical call of greeting\"))\n",
      "        dictionary.append(entry(\"aigrette\",\"an ornamental cluster of feathers\"))\n",
      "        dictionary.append(entry(\"ajar\",\"partially opened\"))\n",
      "\n",
      "        print(\"Enter word:\")\n",
      "        word='ahoy'                             #word=raw_input()\n",
      "        entry=lookup(dictionary,word,entries)\n",
      "\n",
      "        #Result\n",
      "        if(entry!=-1):\n",
      "                print(\"{0}\".format(dictionary[entry].definition))\n",
      "        else:\n",
      "                print(\"Sorry, the word {0} is not in my dictionary.\\n\".format(word))\n",
      "\n",
      "\n",
      "if __name__=='__main__':\n",
      "        main()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter word:\n",
        "nautical call of greeting\n"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Program 10.11, Page number: 229"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "def strToInt(string):\n",
      "        \n",
      "        return int(string)\n",
      "\n",
      "def main():\n",
      "\n",
      "        #Result\n",
      "        print(\"{0}\\n\".format(strToInt(\"245\")))\n",
      "        print(\"{0}\\n\".format(strToInt(\"100\") + 25))\n",
      "        print(\"{0}\\n\".format(strToInt(\"13\")))\n",
      "\n",
      "\n",
      "if __name__=='__main__':\n",
      "        main()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "245\n",
        "\n",
        "125\n",
        "\n",
        "13\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 9
    }
   ],
   "metadata": {}
  }
 ]
}