summaryrefslogtreecommitdiff
path: root/Computer_Programming_Theory_and_Practice_/Chapter7.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'Computer_Programming_Theory_and_Practice_/Chapter7.ipynb')
-rwxr-xr-xComputer_Programming_Theory_and_Practice_/Chapter7.ipynb557
1 files changed, 557 insertions, 0 deletions
diff --git a/Computer_Programming_Theory_and_Practice_/Chapter7.ipynb b/Computer_Programming_Theory_and_Practice_/Chapter7.ipynb
new file mode 100755
index 00000000..0d79c17f
--- /dev/null
+++ b/Computer_Programming_Theory_and_Practice_/Chapter7.ipynb
@@ -0,0 +1,557 @@
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:37ae6cc76fe6f4a8cc8dba62582a6a103f99da5c7579d1a782cb95241f542fd8"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 1 , Page number: CP-148"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Program to count the occurence of a particular in a string\n",
+ "# Variable declaration\n",
+ "\n",
+ "st = \"MISSISSIPPI\"\n",
+ "ch = \"S\"\n",
+ "count = 0\n",
+ "\n",
+ "print \"Enter the string :\",st\n",
+ "print \"Which character to be counted ?\",ch\n",
+ "\n",
+ "# Loop to check occurrence of aa character\n",
+ "\n",
+ "l = len(st)\n",
+ "\n",
+ "for i in st:\n",
+ " if i == ch:\n",
+ " count = count + 1\n",
+ "\n",
+ "print \"The character \" + ch + \" occurs %d times\" % count "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the string : MISSISSIPPI\n",
+ "Which character to be counted ? S\n",
+ "The character S occurs 4 times\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2 , Page number: CP-149"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Program to count the number of vowels in a sentence\n",
+ "# Variable declaration\n",
+ "\n",
+ "st = \"This is a book\"\n",
+ "count = 0\n",
+ "\n",
+ "print \"Enter the sentence :\"\n",
+ "print st\n",
+ "\n",
+ "# Loop to count the vowels in the string\n",
+ "\n",
+ "for i in st:\n",
+ " if i == \"A\":\n",
+ " count = count + 1\n",
+ " elif i == \"E\":\n",
+ " count = count + 1\n",
+ " elif i == \"I\":\n",
+ " count = count + 1\n",
+ " elif i == \"O\":\n",
+ " count = count + 1\n",
+ " elif i == \"U\":\n",
+ " count = count + 1\n",
+ " elif i == \"a\":\n",
+ " count = count + 1\n",
+ " elif i == \"e\":\n",
+ " count = count + 1\n",
+ " elif i == \"i\":\n",
+ " count = count + 1\n",
+ " elif i == \"o\":\n",
+ " count = count + 1\n",
+ " elif i == \"u\":\n",
+ " count = count + 1\n",
+ "\n",
+ "print \"%d vowels are present in the sentence\" % count\n",
+ " \n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the sentence :\n",
+ "This is a book\n",
+ "5 vowels are present in the sentence\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 3 , Page number: CP-150"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Program to test whether a given string is a palindrome or not\n",
+ "# Variable declaration\n",
+ "\n",
+ "st = \"HYDERABAD\"\n",
+ "rst = \"\"\n",
+ "i = 0\n",
+ "j = len(st) - 1\n",
+ "\n",
+ "print \"Enter the string :\",st\n",
+ "\n",
+ "# Palindrome or not \n",
+ "\n",
+ "rst = reversed(st)\n",
+ "\n",
+ "if list(st) == list(rst):\n",
+ " print \"%s is a palindrome string \" % st\n",
+ "else:\n",
+ " print \"%s is not a palindrome string \" % st"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the string : HYDERABAD\n",
+ "HYDERABAD is not a palindrome string \n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4 , Page number: CP-152"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Program to conctenate two strings\n",
+ "# Variable declaration\n",
+ "\n",
+ "st1 = \"NEW \"\n",
+ "st2 = \"DELHI\"\n",
+ "\n",
+ "# input of two strings to be concatenated\n",
+ "\n",
+ "print \"Enter first string :\",st1\n",
+ "print \"Enter second string :\",st2\n",
+ "\n",
+ "# concatenation of two strings\n",
+ "st = st1 + st2\n",
+ "\n",
+ "print \"Resultant string is \",st "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first string : NEW \n",
+ "Enter second string : DELHI\n",
+ "Resultant string is NEW DELHI\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5 , Page number: CP-154"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Program to compare two strings\n",
+ "# Variable declaration\n",
+ "\n",
+ "st1 = \"ALPHA\"\n",
+ "st2 = \"BETA\"\n",
+ "\n",
+ "print \"Enter string 1:\",st1\n",
+ "print \"Enter string 2:\",st2\n",
+ "\n",
+ "# compare strings\n",
+ "\n",
+ "if (cmp(st1,st2)>0):\n",
+ " print \"%s \" + st1 + \"is alphabetically greater string\"\n",
+ "else:\n",
+ " print st2 + \" is alphabetically greater string\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter string 1: ALPHA\n",
+ "Enter string 2: BETA\n",
+ "BETA is alphabetically greater string\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 6 , Page number: CP-155"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Program to sort an array of names in alphabetical order\n",
+ "# Variable declaration\n",
+ "\n",
+ "n = 4\n",
+ "names = [\"DEEPAK\",\"SHERIN\",\"SONIKA\",\"ARUN\"]\n",
+ "\n",
+ "print \"How many names ?\",n\n",
+ "print \"Enter the 4 names one by one\"\n",
+ "for i in names:\n",
+ " print i\n",
+ "\n",
+ "# Loop to arrange names in alphabetical order\n",
+ "\n",
+ "for i in range(0,n-1):\n",
+ " for j in range(i+1,n):\n",
+ " if cmp(names[i],names[j])>0:\n",
+ " \n",
+ " temp = names[i]\n",
+ " names[i] = names[j]\n",
+ " names[j] = temp\n",
+ "\n",
+ "print \"Names in alphabetical order\"\n",
+ "for i in names:\n",
+ " print i"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "How many names ? 4\n",
+ "Enter the 4 names one by one\n",
+ "DEEPAK\n",
+ "SHERIN\n",
+ "SONIKA\n",
+ "ARUN\n",
+ "Names in alphabetical order\n",
+ "ARUN\n",
+ "DEEPAK\n",
+ "SHERIN\n",
+ "SONIKA\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7 , Page number: CP-157"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Program to convert a line from lower case to upper case\n",
+ "# Variable declaretion\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "st = ['l','o','g','i','c','a','l',' ','t','h','i','n','k','i','n','g',' ','i','s',' ','a',' ','m','u','s','t',' ','t','o',' ','l','e','a','r','n',' ','p','r','o','g','r','a','m','m','i','n','g']\n",
+ "\n",
+ "print \"Enter a sentence :\"\n",
+ "for i in st:\n",
+ " print i,\n",
+ "print \n",
+ "print \"The converted upper case string is\"\n",
+ "# loop to convert lower case alphabet to upper case text\n",
+ "for i in range(len(st)):\n",
+ " if st[i] >= 'a' and st[i] <= 'z':\n",
+ " st[i] = chr(ord(st[i])-32)\n",
+ "\n",
+ "\n",
+ " sys.stdout.write(st[i])"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a sentence :\n",
+ "l o g i c a l t h i n k i n g i s a m u s t t o l e a r n p r o g r a m m i n g\n",
+ "The converted upper case string is\n",
+ "LOGICAL THINKING IS A MUST TO LEARN PROGRAMMING"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9 , Page number: CP-160"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Program to read a text and to omit al occurrences of a particular word\n",
+ "# Variable declartion\n",
+ "\n",
+ "st = \"TO ACCESS THE NAME OF THE CITY IN THE LIST\"\n",
+ "i = 0\n",
+ "omit = \"THE\"\n",
+ "l = len(st)\n",
+ "j = 0\n",
+ "word = []\n",
+ "newst = \"\"\n",
+ "onesps = \"\"\n",
+ "print \"Enter a sentence :\"\n",
+ "print st\n",
+ "print \"Enter word to omit :\",omit\n",
+ "\n",
+ "# loop to omit the given word\n",
+ "\n",
+ "for i in range(l):\n",
+ " ch = i\n",
+ " if ch == ' ':\n",
+ " for j in word:\n",
+ " j = \" \" \n",
+ " if j == omit:\n",
+ " newst = j\n",
+ " newst = onesps\n",
+ " j = \" \"\n",
+ " j = 0\n",
+ " else:\n",
+ " j = ch\n",
+ " j = j + 1\n",
+ " i = i + 1\n",
+ "\n",
+ "print \"After omiting the word \" + omit\n",
+ "print newst\n",
+ "print \"Press any key to continue\"\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a sentence :\n",
+ "TO ACCESS THE NAME OF THE CITY IN THE LIST\n",
+ "Enter word to omit : THE\n",
+ "After omiting the word THE\n",
+ "\n",
+ "Press any key to continue\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 10 , Page number: CP-161"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Program to calculate the amount to be paid for the telegram\n",
+ "# Variable declaration\n",
+ "\n",
+ "count = 0\n",
+ "st = \"Congratulations on your success in Examinations.\"\n",
+ "l = len(st)\n",
+ "\n",
+ "print \"Type the sentence for Telegram\"\n",
+ "print st\n",
+ "\n",
+ "# loop to count number of words\n",
+ "\n",
+ "for i in range(l):\n",
+ " if st[i] == '?':\n",
+ " count = count + 1\n",
+ "\n",
+ "if count <= 10:\n",
+ " amt = 5\n",
+ "else:\n",
+ " amt = 5 + (count - 10) * 1.25\n",
+ "\n",
+ "print \"Amount to be paid for telegram = Rs.%0.2f\" % amt "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Type the sentence for Telegram\n",
+ "Congratulations on your success in Examinations.\n",
+ "Amount to be paid for telegram = Rs.5.00\n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 11 , Page number: CP-163"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Program to count number of lines,words and characters\n",
+ "# Variable declaration\n",
+ "\n",
+ "txt = \"What is a string? How do you initialize it? Explain with example.$\"\n",
+ "st = \"\"\n",
+ "i = 0\n",
+ "lns = 0\n",
+ "wds = 0\n",
+ "chs = 0\n",
+ "\n",
+ "print \"Enter the text, type $ at end.\"\n",
+ "print txt\n",
+ "\n",
+ "# loop to count lines,words and characters in text\n",
+ "\n",
+ "while txt[i] != '$':\n",
+ " # switch case statements\n",
+ " if txt[i] == ' ':\n",
+ " wds = wds + 1\n",
+ " chs = chs + 1\n",
+ " elif txt[i] == '.':\n",
+ " wds = wds + 1\n",
+ " lns = lns + 1\n",
+ " chs = chs + 1\n",
+ " elif txt[i] == '?':\n",
+ " lns = lns + 1\n",
+ " else: # default\n",
+ " chs = chs + 1\n",
+ "\n",
+ " i = i + 1\n",
+ "\n",
+ "print \"Number of char (incl. blanks) =\",chs\n",
+ "print \"Number of words =\",wds\n",
+ "print \"Number of lines =\",lns\n",
+ "\n",
+ "\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the text, type $ at end.\n",
+ "What is a string? How do you initialize it? Explain with example.$\n",
+ "Number of char (incl. blanks) = 63\n",
+ "Number of words = 12\n",
+ "Number of lines = 3\n"
+ ]
+ }
+ ],
+ "prompt_number": 16
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file