summaryrefslogtreecommitdiff
path: root/_Programming_With_C/chapter10.ipynb
diff options
context:
space:
mode:
authordebashisdeb2014-06-20 15:42:42 +0530
committerdebashisdeb2014-06-20 15:42:42 +0530
commit83c1bfceb1b681b4bb7253b47491be2d8b2014a1 (patch)
treef54eab21dd3d725d64a495fcd47c00d37abed004 /_Programming_With_C/chapter10.ipynb
parenta78126bbe4443e9526a64df9d8245c4af8843044 (diff)
downloadPython-Textbook-Companions-83c1bfceb1b681b4bb7253b47491be2d8b2014a1.tar.gz
Python-Textbook-Companions-83c1bfceb1b681b4bb7253b47491be2d8b2014a1.tar.bz2
Python-Textbook-Companions-83c1bfceb1b681b4bb7253b47491be2d8b2014a1.zip
removing problem statements
Diffstat (limited to '_Programming_With_C/chapter10.ipynb')
-rw-r--r--_Programming_With_C/chapter10.ipynb366
1 files changed, 327 insertions, 39 deletions
diff --git a/_Programming_With_C/chapter10.ipynb b/_Programming_With_C/chapter10.ipynb
index 083baffa..ea2e1985 100644
--- a/_Programming_With_C/chapter10.ipynb
+++ b/_Programming_With_C/chapter10.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "chapter10"
+ "name": "",
+ "signature": "sha256:3323f7a9a077b84fffdd30478d4b0d0b7f4032b86670a58b5b333d42bee04f15"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -10,24 +11,33 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h1>Chapter 10: Strings<h1> "
+ "source": [
+ "<h1>Chapter 10: Strings<h1> "
+ ]
},
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 10.3, Page Number: 10.3<h3>"
+ "source": [
+ "<h3>Example 10.3, Page Number: 10.3<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "st='Programming'\nprint 'The string is %s' %st",
+ "input": [
+ "st='Programming'\n",
+ "print 'The string is %s' %st"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "The string is Programming\n"
+ "text": [
+ "The string is Programming\n"
+ ]
}
],
"prompt_number": 4
@@ -35,19 +45,28 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 10.4, Page Number: 10.4<h3>"
+ "source": [
+ "<h3>Example 10.4, Page Number: 10.4<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "st='Hello World'\nprint 'The line is :'\nprint st",
+ "input": [
+ "st='Hello World'\n",
+ "print 'The line is :'\n",
+ "print st"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "The line is :\nHello World\n"
+ "text": [
+ "The line is :\n",
+ "Hello World\n"
+ ]
}
],
"prompt_number": 5
@@ -55,19 +74,29 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 10.6, Page Number: 10.5<h3>"
+ "source": [
+ "<h3>Example 10.6, Page Number: 10.5<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "#display the string character by character\n\nstrin='Welcome to python'\nfor i in strin:\n print i,",
+ "input": [
+ "\n",
+ "\n",
+ "strin='Welcome to python'\n",
+ "for i in strin:\n",
+ " print i,"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "W e l c o m e t o p y t h o n\n"
+ "text": [
+ "W e l c o m e t o p y t h o n\n"
+ ]
}
],
"prompt_number": 6
@@ -75,19 +104,48 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 10.7, Page Number: 10.6<h3>"
+ "source": [
+ "<h3>Example 10.7, Page Number: 10.6<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# to demonstrate the use of operators related function\n\nstr1='Programming'\nlen1=len(str1)\nprint 'The length of the string is ',len1\n\nstr2=str1\nprint 'First string is %s and copied string is %s' %(str1,str2)\n\nstr3='Computer'\n\nif str1==str3:\n print 'Both strings are equal'\nelif str1<str2:\n print 'First string is lesser than second string'\nelse:\n print 'First string is greater than second string'\n \ntempstr=' with C'\nstr1=str1+tempstr\nprint 'The concated string is ',str1\n",
+ "input": [
+ "\n",
+ "\n",
+ "str1='Programming'\n",
+ "len1=len(str1)\n",
+ "print 'The length of the string is ',len1\n",
+ "\n",
+ "str2=str1\n",
+ "print 'First string is %s and copied string is %s' %(str1,str2)\n",
+ "\n",
+ "str3='Computer'\n",
+ "\n",
+ "if str1==str3:\n",
+ " print 'Both strings are equal'\n",
+ "elif str1<str2:\n",
+ " print 'First string is lesser than second string'\n",
+ "else:\n",
+ " print 'First string is greater than second string'\n",
+ " \n",
+ "tempstr=' with C'\n",
+ "str1=str1+tempstr\n",
+ "print 'The concated string is ',str1\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "The length of the string is 11\nFirst string is Programming and copied string is Programming\nFirst string is greater than second string\nThe concated string is Programming with C\n"
+ "text": [
+ "The length of the string is 11\n",
+ "First string is Programming and copied string is Programming\n",
+ "First string is greater than second string\n",
+ "The concated string is Programming with C\n"
+ ]
}
],
"prompt_number": 7
@@ -95,19 +153,48 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 10.8,Page Number: 10.7<h3>"
+ "source": [
+ "<h3>Example 10.8,Page Number: 10.7<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# user defined function for string length and string copy\n\ndef strlength(str1):\n count=0\n for i in str1:\n count+=1\n \n return count\ndef strcopy(src):\n dst=[]\n for i in src:\n dst.append(i)\n \n dst=''.join(dst)\n \n return dst\n\n\nstr1='New Delhi'\nlen1=strlength(str1)\nprint 'The length of the string is ',len1\n\nstr2=strcopy(str1)\nprint 'First string is %s and copied string is %s' %(str1,str2)\n",
+ "input": [
+ "\n",
+ "def strlength(str1):\n",
+ " count=0\n",
+ " for i in str1:\n",
+ " count+=1\n",
+ " \n",
+ " return count\n",
+ "def strcopy(src):\n",
+ " dst=[]\n",
+ " for i in src:\n",
+ " dst.append(i)\n",
+ " \n",
+ " dst=''.join(dst)\n",
+ " \n",
+ " return dst\n",
+ "\n",
+ "\n",
+ "str1='New Delhi'\n",
+ "len1=strlength(str1)\n",
+ "print 'The length of the string is ',len1\n",
+ "\n",
+ "str2=strcopy(str1)\n",
+ "print 'First string is %s and copied string is %s' %(str1,str2)\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "The length of the string is 9\nFirst string is New Delhi and copied string is New Delhi\n"
+ "text": [
+ "The length of the string is 9\n",
+ "First string is New Delhi and copied string is New Delhi\n"
+ ]
}
],
"prompt_number": 8
@@ -115,19 +202,53 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 10.9,Page Number: 10.9<h3>"
+ "source": [
+ "<h3>Example 10.9,Page Number: 10.9<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# A user defined implementation of comparison of two strings\n\ndef strcompare(str1,str2):\n \n len1=len(str1)\n len2=len(str2)\n \n if len1<len2:\n length=len1\n else:\n length=len2\n \n for i in xrange(0,length):\n if str1[i]<str2[i]:\n return -1\n elif str1[i]>str2[i]:\n return 1\n \n return 0\n\nstr1='Programming'\nstr2='Computer'\nstatus=strcompare(str1,str2)\nif status==-1:\n print 'First string is lesser than second string'\nelif status==1:\n print 'First string is greater than second string'\nelse:\n print 'Both strings ae equal'\n",
+ "input": [
+ "\n",
+ "\n",
+ "def strcompare(str1,str2):\n",
+ " \n",
+ " len1=len(str1)\n",
+ " len2=len(str2)\n",
+ " \n",
+ " if len1<len2:\n",
+ " length=len1\n",
+ " else:\n",
+ " length=len2\n",
+ " \n",
+ " for i in xrange(0,length):\n",
+ " if str1[i]<str2[i]:\n",
+ " return -1\n",
+ " elif str1[i]>str2[i]:\n",
+ " return 1\n",
+ " \n",
+ " return 0\n",
+ "\n",
+ "str1='Programming'\n",
+ "str2='Computer'\n",
+ "status=strcompare(str1,str2)\n",
+ "if status==-1:\n",
+ " print 'First string is lesser than second string'\n",
+ "elif status==1:\n",
+ " print 'First string is greater than second string'\n",
+ "else:\n",
+ " print 'Both strings ae equal'\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "First string is greater than second string\n"
+ "text": [
+ "First string is greater than second string\n"
+ ]
}
],
"prompt_number": 9
@@ -135,19 +256,42 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 10.10, Page Number: 10.10<h3>"
+ "source": [
+ "<h3>Example 10.10, Page Number: 10.10<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Left as well as right concatenation of two strings\n\ndef leftconcat(dst,src):\n dst=src+dst\n return dst\n\ndef rightconcat(dst,src):\n dst=dst+src\n return dst\n\nstr1='Hello'\nstr2='Friends'\n\ntempstr=leftconcat(str2,str1)\nprint 'The first string after left concatenation becomes ', tempstr\n\ntempstr=rightconcat(str2,str1)\nprint 'The first string after right concatenation becomes', tempstr\n",
+ "input": [
+ "\n",
+ "def leftconcat(dst,src):\n",
+ " dst=src+dst\n",
+ " return dst\n",
+ "\n",
+ "def rightconcat(dst,src):\n",
+ " dst=dst+src\n",
+ " return dst\n",
+ "\n",
+ "str1='Hello'\n",
+ "str2='Friends'\n",
+ "\n",
+ "tempstr=leftconcat(str2,str1)\n",
+ "print 'The first string after left concatenation becomes ', tempstr\n",
+ "\n",
+ "tempstr=rightconcat(str2,str1)\n",
+ "print 'The first string after right concatenation becomes', tempstr\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "The first string after left concatenation becomes HelloFriends\nThe first string after right concatenation becomes FriendsHello\n"
+ "text": [
+ "The first string after left concatenation becomes HelloFriends\n",
+ "The first string after right concatenation becomes FriendsHello\n"
+ ]
}
],
"prompt_number": 10
@@ -155,19 +299,34 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 10.11,Page Numbr: 10.12<h3>"
+ "source": [
+ "<h3>Example 10.11,Page Numbr: 10.12<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# program to count total number of vowel in a given text\n\nstr1='All good boys have bread'\ncount=0\n\nfor i in str1:\n if i=='a' or i=='e'or i=='i' or i=='o' or i=='u' or i=='A' or i=='E' or i=='I' or i=='O' or i=='U' :\n count+=1\n \nprint 'Total number of vowels in a given text are ',count",
+ "input": [
+ "\n",
+ "\n",
+ "str1='All good boys have bread'\n",
+ "count=0\n",
+ "\n",
+ "for i in str1:\n",
+ " if i=='a' or i=='e'or i=='i' or i=='o' or i=='u' or i=='A' or i=='E' or i=='I' or i=='O' or i=='U' :\n",
+ " count+=1\n",
+ " \n",
+ "print 'Total number of vowels in a given text are ',count"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Total number of vowels in a given text are 8\n"
+ "text": [
+ "Total number of vowels in a given text are 8\n"
+ ]
}
],
"prompt_number": 11
@@ -175,19 +334,44 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 10.12, Page Number: 10.13<h3>"
+ "source": [
+ "<h3>Example 10.12, Page Number: 10.13<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Character arithmetic examples\n\nch1='A'\nch2=ord(ch1)+3\nprint chr(ch2)\n\nch1=chr(ord(ch1)+1)\nprint ch1\n\nprint ord('a')\nprint ord('l')\n\nval=ord(ch1)*ch2\nprint val\n\nprint chr(100)",
+ "input": [
+ "\n",
+ "ch1='A'\n",
+ "ch2=ord(ch1)+3\n",
+ "print chr(ch2)\n",
+ "\n",
+ "ch1=chr(ord(ch1)+1)\n",
+ "print ch1\n",
+ "\n",
+ "print ord('a')\n",
+ "print ord('l')\n",
+ "\n",
+ "val=ord(ch1)*ch2\n",
+ "print val\n",
+ "\n",
+ "print chr(100)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "D\nB\n97\n108\n4488\nd\n"
+ "text": [
+ "D\n",
+ "B\n",
+ "97\n",
+ "108\n",
+ "4488\n",
+ "d\n"
+ ]
}
],
"prompt_number": 12
@@ -195,19 +379,39 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 10.13, Page Number: 10.13<h3>"
+ "source": [
+ "<h3>Example 10.13, Page Number: 10.13<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Program to convert lowercase alphabets to uppercase in a given text\n\ntext='I am studying 6 Theory Papers & 4 practicals'\n\nlen1=len(text)\ntext=list(text)\nfor i in xrange(0,len1):\n if text[i]>='a' and text[i]<='z':\n text[i]=chr(ord(text[i])+ord('A')-ord('a'))\n \n \ntext=''.join(text)\n\nprint 'The text after converting lowercase alphabets to uppercase is '\nprint text",
+ "input": [
+ "\n",
+ "text='I am studying 6 Theory Papers & 4 practicals'\n",
+ "\n",
+ "len1=len(text)\n",
+ "text=list(text)\n",
+ "for i in xrange(0,len1):\n",
+ " if text[i]>='a' and text[i]<='z':\n",
+ " text[i]=chr(ord(text[i])+ord('A')-ord('a'))\n",
+ " \n",
+ " \n",
+ "text=''.join(text)\n",
+ "\n",
+ "print 'The text after converting lowercase alphabets to uppercase is '\n",
+ "print text"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "The text after converting lowercase alphabets to uppercase is \nI AM STUDYING 6 THEORY PAPERS & 4 PRACTICALS\n"
+ "text": [
+ "The text after converting lowercase alphabets to uppercase is \n",
+ "I AM STUDYING 6 THEORY PAPERS & 4 PRACTICALS\n"
+ ]
}
],
"prompt_number": 13
@@ -215,19 +419,45 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 10.14, Page Number: 10.14<h3>"
+ "source": [
+ "<h3>Example 10.14, Page Number: 10.14<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Program to search for a substring in a given string\n\ntext='The programming is a systematic process'\nsubstr='pro'\ntext_len=len(text)\nsub_len=len(substr)\ntext=list(text)\nsubstr=list(substr)\n\nfor i in xrange(0,text_len-sub_len+1):\n \n for j in xrange(0,sub_len):\n \n if text[i+j]==substr[j]:\n continue\n else:\n break\n \n if j==sub_len-1:\n print 'The substring is present from subscript %d onwards' %i\n \n",
+ "input": [
+ "\n",
+ "text='The programming is a systematic process'\n",
+ "substr='pro'\n",
+ "text_len=len(text)\n",
+ "sub_len=len(substr)\n",
+ "text=list(text)\n",
+ "substr=list(substr)\n",
+ "\n",
+ "for i in xrange(0,text_len-sub_len+1):\n",
+ " \n",
+ " for j in xrange(0,sub_len):\n",
+ " \n",
+ " if text[i+j]==substr[j]:\n",
+ " continue\n",
+ " else:\n",
+ " break\n",
+ " \n",
+ " if j==sub_len-1:\n",
+ " print 'The substring is present from subscript %d onwards' %i\n",
+ " \n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "The substring is present from subscript 4 onwards\nThe substring is present from subscript 32 onwards\n"
+ "text": [
+ "The substring is present from subscript 4 onwards\n",
+ "The substring is present from subscript 32 onwards\n"
+ ]
}
],
"prompt_number": 14
@@ -235,19 +465,77 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 10.15,Page number: 10.15<h3>"
+ "source": [
+ "<h3>Example 10.15,Page number: 10.15<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Reordering a list of strings\n\n\ndef reorder(x):\n\n n=len(x)\n for item in range(0,n-1):\n for i in range(item+1,n):\n if x[item]>x[i]:\n temp=x[item]\n x[item]=x[i]\n x[i]=temp\n\n\n return\n\nx=['PACIFIC','ATLANTIC','INDIAN','CARIBBEAN','BERING','BLACK','RED','NORTH','BALTIC','CASPIAN']\nprint 'Original list of strings :\\n\\n'\n\nfor i in x:\n print \"String : \",i\n\nreorder(x)\n\nprint \"\\nReodered list of strings : \\n\\n\"\n\nfor i in x:\n print \"String : \",i\n",
+ "input": [
+ "\n",
+ "\n",
+ "def reorder(x):\n",
+ "\n",
+ " n=len(x)\n",
+ " for item in range(0,n-1):\n",
+ " for i in range(item+1,n):\n",
+ " if x[item]>x[i]:\n",
+ " temp=x[item]\n",
+ " x[item]=x[i]\n",
+ " x[i]=temp\n",
+ "\n",
+ "\n",
+ " return\n",
+ "\n",
+ "x=['PACIFIC','ATLANTIC','INDIAN','CARIBBEAN','BERING','BLACK','RED','NORTH','BALTIC','CASPIAN']\n",
+ "print 'Original list of strings :\\n\\n'\n",
+ "\n",
+ "for i in x:\n",
+ " print \"String : \",i\n",
+ "\n",
+ "reorder(x)\n",
+ "\n",
+ "print \"\\nReodered list of strings : \\n\\n\"\n",
+ "\n",
+ "for i in x:\n",
+ " print \"String : \",i\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Original list of strings :\n\n\nString : PACIFIC\nString : ATLANTIC\nString : INDIAN\nString : CARIBBEAN\nString : BERING\nString : BLACK\nString : RED\nString : NORTH\nString : BALTIC\nString : CASPIAN\n\nReodered list of strings : \n\n\nString : ATLANTIC\nString : BALTIC\nString : BERING\nString : BLACK\nString : CARIBBEAN\nString : CASPIAN\nString : INDIAN\nString : NORTH\nString : PACIFIC\nString : RED\n"
+ "text": [
+ "Original list of strings :\n",
+ "\n",
+ "\n",
+ "String : PACIFIC\n",
+ "String : ATLANTIC\n",
+ "String : INDIAN\n",
+ "String : CARIBBEAN\n",
+ "String : BERING\n",
+ "String : BLACK\n",
+ "String : RED\n",
+ "String : NORTH\n",
+ "String : BALTIC\n",
+ "String : CASPIAN\n",
+ "\n",
+ "Reodered list of strings : \n",
+ "\n",
+ "\n",
+ "String : ATLANTIC\n",
+ "String : BALTIC\n",
+ "String : BERING\n",
+ "String : BLACK\n",
+ "String : CARIBBEAN\n",
+ "String : CASPIAN\n",
+ "String : INDIAN\n",
+ "String : NORTH\n",
+ "String : PACIFIC\n",
+ "String : RED\n"
+ ]
}
],
"prompt_number": 15
@@ -255,7 +543,7 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "",
+ "input": [],
"language": "python",
"metadata": {},
"outputs": [],