summaryrefslogtreecommitdiff
path: root/Beginning_C_By_Ivon_Horton/chapter6.ipynb
diff options
context:
space:
mode:
authorJovina Dsouza2014-06-18 12:43:07 +0530
committerJovina Dsouza2014-06-18 12:43:07 +0530
commit206d0358703aa05d5d7315900fe1d054c2817ddc (patch)
treef2403e29f3aded0caf7a2434ea50dd507f6545e2 /Beginning_C_By_Ivon_Horton/chapter6.ipynb
parentc6f0d6aeb95beaf41e4b679e78bb42c4ffe45a40 (diff)
downloadPython-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.tar.gz
Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.tar.bz2
Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.zip
adding book
Diffstat (limited to 'Beginning_C_By_Ivon_Horton/chapter6.ipynb')
-rw-r--r--Beginning_C_By_Ivon_Horton/chapter6.ipynb573
1 files changed, 573 insertions, 0 deletions
diff --git a/Beginning_C_By_Ivon_Horton/chapter6.ipynb b/Beginning_C_By_Ivon_Horton/chapter6.ipynb
new file mode 100644
index 00000000..fdf576c5
--- /dev/null
+++ b/Beginning_C_By_Ivon_Horton/chapter6.ipynb
@@ -0,0 +1,573 @@
+{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 6: Applications with Strings and Text"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Program 6.1, page no. 221"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "Displaying a string\n",
+ "\"\"\"\n",
+ "\n",
+ "print \"The character \\\\0 is used to terminate a string.\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The character \\0 is used to terminate a string.\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Program 6.2, page no. 223"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "Lengths\n",
+ "\"\"\"\n",
+ "\n",
+ "str1 = \"To be or not to be\"\n",
+ "str2 = \",that is a question\"\n",
+ "\n",
+ "print \"Length of the string \\\"\", str1, \"\\\" is \", len(str1), \" characters\"\n",
+ "print \"Length of the string \\\"\", str2, \"\\\" is \", len(str2), \" characters\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Length of the string \" To be or not to be \" is 18 characters\n",
+ "Length of the string \" ,that is a question \" is 19 characters\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Program 6.3, page no. 225"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "Arrays of strings\n",
+ "\"\"\"\n",
+ "\n",
+ "str1 = \"Computers do what you tell them to do, not what you want them to do.\"\n",
+ "str2 = \"When you put something in memory, remember where you put it.\"\n",
+ "str3 = \"Never test for a condition you don't know what to do with.\"\n",
+ "\n",
+ "print \"There are 3 strings.\"\n",
+ "print \"The string: \"\n",
+ "print str1\n",
+ "print \"contains \", len(str1), \" characters\"\n",
+ "print \"The string: \"\n",
+ "print str2\n",
+ "print \"contains \", len(str3), \" characters\"\n",
+ "print \"The string: \"\n",
+ "print str3\n",
+ "print \"contains \", len(str3), \" characters\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "There are 3 strings.\n",
+ "The string: \n",
+ "Computers do what you tell them to do, not what you want them to do.\n",
+ "contains 68 characters\n",
+ "The string: \n",
+ "When you put something in memory, remember where you put it.\n",
+ "contains 58 characters\n",
+ "The string: \n",
+ "Never test for a condition you don't know what to do with.\n",
+ "contains 58 characters\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Program 6.4, page no. 230"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "Joining strings\n",
+ "\"\"\"\n",
+ "\n",
+ "preamble = \"The joke is: \"\n",
+ "str1 = \"My dog hasn\\'t got any nose.\\n\"\n",
+ "str2 = \"How does your dog smell then?\\n\"\n",
+ "str3 = \"My dog smells horrible.\\n\"\n",
+ "joke = str1 + str2 + str3\n",
+ "\n",
+ "print preamble\n",
+ "print joke"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The joke is: \n",
+ "My dog hasn't got any nose.\n",
+ "How does your dog smell then?\n",
+ "My dog smells horrible.\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Program 6.5, page no. 234"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "Comparing strings\n",
+ "\"\"\"\n",
+ "\n",
+ "print \"Type in the first word (maximum 20 characters): \",\n",
+ "word1 = raw_input()\n",
+ "print \"Type in the second word (maximum 20 characters): \",\n",
+ "word2 = raw_input()\n",
+ "\n",
+ "if(word1 == word2):\n",
+ " print \"You have entered identical words\"\n",
+ "else:\n",
+ " print \"%s precedes %s\\n\" %((word2 if(word1 > word2) else word1), (word1 if(word1 > word2) else word2)) "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Program 6.6, page no. 240 "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "A demonstration of seeking and finding\n",
+ "\"\"\"\n",
+ "\n",
+ "str1 = \"This string contains the holy grail.\"\n",
+ "str2 = \"the holy grail\"\n",
+ "str3 = \"the holy grill\"\n",
+ " \n",
+ "if str2 in str1:\n",
+ " print \"\\\"%s\\\" was found in \\\"%s\\\"\\n\" %(str2, str1)\n",
+ "else:\n",
+ " print \"\\n\\\"%s\\\" was not found.\" %str2\n",
+ " \n",
+ "if not(str3 in str1):\n",
+ " print \"\\\"%s\\\" was not found.\\n\" % str3\n",
+ "else:\n",
+ " print \"\\nWe shouldn't get to here!\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\"the holy grail\" was found in \"This string contains the holy grail.\"\n",
+ "\n",
+ "\"the holy grill\" was not found.\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Program 6.7, page no. 243"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "Find all the words\n",
+ "note: program 7A in the book will remain same in Python\n",
+ "\"\"\"\n",
+ "\n",
+ "print \"Enter some prose that is less than 1000 characters (go on typing hit enter to terminate):\"\n",
+ "str1 = raw_input()\n",
+ "\n",
+ "if len(str1) > 1000:\n",
+ " print \"Maximum permitted input length exceeded.\"\n",
+ "\n",
+ "list_str1 = str1.split(\" \")\n",
+ "\n",
+ "print \"The words in the prose that you entered are: \"\n",
+ "\n",
+ "for i in range(0, len(list_str1)):\n",
+ " print list_str1[i], \"\\t\",\n",
+ " if i % 5 == 0:\n",
+ " print \"\\n\"\n",
+ "\n",
+ "print \"Words found \", len(list_str1)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter some prose that is less than 1000 characters (go on typing hit enter to terminate):\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "y father's family name being Pirrip, and my Christian name Philip, my infant tongue could make of both names nothing longer or more explicit than Pip. So, I called myself Pip, and came to be called Pip.\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The words in the prose that you entered are: \n",
+ "y \t\n",
+ "\n",
+ "father's \tfamily \tname \tbeing \tPirrip, \t\n",
+ "\n",
+ "and \tmy \tChristian \tname \tPhilip, \t\n",
+ "\n",
+ "my \tinfant \ttongue \tcould \tmake \t\n",
+ "\n",
+ "of \tboth \tnames \tnothing \tlonger \t\n",
+ "\n",
+ "or \tmore \texplicit \tthan \tPip. \t\n",
+ "\n",
+ "So, \tI \tcalled \tmyself \tPip, \t\n",
+ "\n",
+ "and \tcame \tto \tbe \tcalled \t\n",
+ "\n",
+ "Pip. \tWords found 37\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Program 6.8, page no. 249"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "Testing characters in a string\n",
+ "\"\"\"\n",
+ "\n",
+ "\n",
+ "nLetters = 0\n",
+ "nDigits = 0\n",
+ "nPunct = 0\n",
+ "print \"Enter an interesting string of less than 100 characters: \"\n",
+ "str1 = raw_input()\n",
+ "\n",
+ "for char in str1:\n",
+ " if char.isalpha():\n",
+ " nLetters += 1\n",
+ " elif char.isdigit():\n",
+ " nDigits += 1\n",
+ " elif char == \" \":\n",
+ " pass\n",
+ " else:\n",
+ " nPunct += 1\n",
+ "\n",
+ "print \"Your string contained %d letters, %d digits and %d punctuation characters. \" %(nLetters, nDigits, nPunct)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter an interesting string of less than 100 characters: \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "I was born on the 3rd of October 1895, which is long ago.\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Your string contained 38 letters, 5 digits and 2 punctuation characters. \n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Program 6.9, page no. 251"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "Finding occurrences of one string in another\n",
+ "\"\"\"\n",
+ "\n",
+ "print \"Enter the string to be searched (less than 100 characters): \",\n",
+ "text = raw_input()\n",
+ " \n",
+ "print \"Enter the string sought (less than 40 characters):\",\n",
+ "substring = raw_input()\n",
+ " \n",
+ "print \"First string entered: \", text\n",
+ "print \"Second string entered: \", substring\n",
+ " \n",
+ "text = text.upper()\n",
+ "substring = substring.upper() \n",
+ "print \"The second string %s found in the first. \" %(\"was not\" if not substring in text else \"was\")"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the string to be searched (less than 100 characters): "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Cry havoc, and let slip the dogs of war.\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Enter the string sought (less than 40 characters):"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The Dogs of War\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " First string entered: Cry havoc, and let slip the dogs of war.\n",
+ "Second string entered: The Dogs of War\n",
+ "The second string was found in the first. \n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Program 6.10, page no. 259"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "Analyzing Text\n",
+ "\"\"\"\n",
+ "\n",
+ "import numpy\n",
+ "\n",
+ "print \"Enter text (hit enter to exit): \",\n",
+ "text = raw_input()\n",
+ "\n",
+ "text_split = text.split()\n",
+ "distinct_words = []\n",
+ "\n",
+ "for i in text_split:\n",
+ " if i not in distinct_words:\n",
+ " distinct_words.append(i)\n",
+ "\n",
+ "word_count = numpy.zeros(len(distinct_words))\n",
+ "\n",
+ "for i in range(len(distinct_words)):\n",
+ " for j in range(len(text_split)):\n",
+ " if distinct_words[i] == text_split[j]:\n",
+ " word_count[i] += 1\n",
+ "\n",
+ "for i in range(len(distinct_words)):\n",
+ " print distinct_words[i], int(word_count[i])\n",
+ " if i == 5:\n",
+ " print \"\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter text (hit enter to exit): "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "When I makes tea I makes tea, as old mother Grogan said. And when I makes water I makes water. Begob, ma'am, says Mrs Cahill, God send you don't make them in the same pot.\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " When 1\n",
+ "I 4\n",
+ "makes 4\n",
+ "tea 1\n",
+ "tea, 1\n",
+ "as 1\n",
+ "\n",
+ "old 1\n",
+ "mother 1\n",
+ "Grogan 1\n",
+ "said. 1\n",
+ "And 1\n",
+ "when 1\n",
+ "water 1\n",
+ "water. 1\n",
+ "Begob, 1\n",
+ "ma'am, 1\n",
+ "says 1\n",
+ "Mrs 1\n",
+ "Cahill, 1\n",
+ "God 1\n",
+ "send 1\n",
+ "you 1\n",
+ "don't 1\n",
+ "make 1\n",
+ "them 1\n",
+ "in 1\n",
+ "the 1\n",
+ "same 1\n",
+ "pot. 1\n"
+ ]
+ }
+ ],
+ "prompt_number": 21
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file