summaryrefslogtreecommitdiff
path: root/C++_from_the_Ground/Chapter_7(1).ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'C++_from_the_Ground/Chapter_7(1).ipynb')
-rw-r--r--C++_from_the_Ground/Chapter_7(1).ipynb591
1 files changed, 521 insertions, 70 deletions
diff --git a/C++_from_the_Ground/Chapter_7(1).ipynb b/C++_from_the_Ground/Chapter_7(1).ipynb
index 4e347f41..28829c6d 100644
--- a/C++_from_the_Ground/Chapter_7(1).ipynb
+++ b/C++_from_the_Ground/Chapter_7(1).ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "Chapter 7"
+ "name": "",
+ "signature": "sha256:9a6978851f88c0c91bde88465299d72777f3dae7854964b1cadad7b80092df72"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -10,24 +11,46 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h1>Chapter 7: Functions,Part One: The Fundamentals<h1>"
+ "source": [
+ "<h1>Chapter 7: Functions,Part One: The Fundamentals<h1>"
+ ]
},
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 7.1, Page Number: 129<h3>"
+ "source": [
+ "<h3>Example 7.1, Page Number: 129<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Example of a function'''\n\ndef f1():\n print \"Enter something: \"\n str= \"Hello\" #User-input\n print str\n \n#Variable decleration\nstr=\"This is str in main\"\n\nprint str\nf1() #function call\nprint str",
+ "input": [
+ "\n",
+ "def f1():\n",
+ " print \"Enter something: \"\n",
+ " str= \"Hello\" #User-input\n",
+ " print str\n",
+ " \n",
+ "#Variable decleration\n",
+ "str=\"This is str in main\"\n",
+ "\n",
+ "print str\n",
+ "f1() #function call\n",
+ "print str"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "This is str in main\nEnter something: \nHello\nThis is str in main\n"
+ "text": [
+ "This is str in main\n",
+ "Enter something: \n",
+ "Hello\n",
+ "This is str in main\n"
+ ]
}
],
"prompt_number": 1
@@ -35,19 +58,44 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 7.2, Page Number: 130<h3>"
+ "source": [
+ "<h3>Example 7.2, Page Number: 130<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''This program illustrates how variables can be local to a block'''\n\n#Variable decleration\nchoice=0\n\nprint \"(1) add numbers or (2) concatenate strings?: \"\nchoice=2 #User Input taken as 2\n\nif choice==1:\n print \"Enter two numbers: \"\n a=5 #Variable decleration; User Input\n b=7\n print a+b #Result\nelse :\n print \"Enter two strings: \"\n s1=\"Hello\" #Variable decleration; User Input\n s2=\"World\"\n s1+=s2 #Concatenation\n print s1 #Result",
+ "input": [
+ "\n",
+ "#Variable decleration\n",
+ "choice=0\n",
+ "\n",
+ "print \"(1) add numbers or (2) concatenate strings?: \"\n",
+ "choice=2 #User Input taken as 2\n",
+ "\n",
+ "if choice==1:\n",
+ " print \"Enter two numbers: \"\n",
+ " a=5 #Variable decleration; User Input\n",
+ " b=7\n",
+ " print a+b #Result\n",
+ "else :\n",
+ " print \"Enter two strings: \"\n",
+ " s1=\"Hello\" #Variable decleration; User Input\n",
+ " s2=\"World\"\n",
+ " s1+=s2 #Concatenation\n",
+ " print s1 #Result"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "(1) add numbers or (2) concatenate strings?: \nEnter two strings: \nHelloWorld\n"
+ "text": [
+ "(1) add numbers or (2) concatenate strings?: \n",
+ "Enter two strings: \n",
+ "HelloWorld\n"
+ ]
}
],
"prompt_number": 1
@@ -55,19 +103,36 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 7.3, Page Number: 131<h3>"
+ "source": [
+ "<h3>Example 7.3, Page Number: 131<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''This program illustrates the scope of variables'''\n\n'''This program will give same output for inner and outer i's\n because pyhton doesnt consider the if statment block as a scope'''\n#Variable decleration\ni=10\nj=100\n\nif j>0:\n i=None \n i= j/2\n print \"inner i: \",i #Result\n\nprint \"outer i: \",i #Result",
+ "input": [
+ "\n",
+ "#Variable decleration\n",
+ "i=10\n",
+ "j=100\n",
+ "\n",
+ "if j>0:\n",
+ " i=None \n",
+ " i= j/2\n",
+ " print \"inner i: \",i #Result\n",
+ "\n",
+ "print \"outer i: \",i #Result"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "inner i: 50\nouter i: 50\n"
+ "text": [
+ "inner i: 50\n",
+ "outer i: 50\n"
+ ]
}
],
"prompt_number": 2
@@ -75,19 +140,32 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 7.4, Page Number: 132<h3>"
+ "source": [
+ "<h3>Example 7.4, Page Number: 132<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Example for declaring variables anywhere in the program'''\n\na=5 #Variable decleration; user input\n\nb=10 #declaration of another variable; user input\n\n#Result\nprint \"Product: \",a*b",
+ "input": [
+ "\n",
+ "\n",
+ "a=5 #Variable decleration; user input\n",
+ "\n",
+ "b=10 #declaration of another variable; user input\n",
+ "\n",
+ "#Result\n",
+ "print \"Product: \",a*b"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Product: 50\n"
+ "text": [
+ "Product: 50\n"
+ ]
}
],
"prompt_number": 4
@@ -95,19 +173,70 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 7.5, Page Number: 134<h3>"
+ "source": [
+ "<h3>Example 7.5, Page Number: 134<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''A simple addition drill program.'''\n\nimport random\n\n#Variable decleration\ncount=None\nnum_right=0\n\n#Function for the drill\ndef drill():\n #Generate two numbers between 0 and 99.\n a=random.randint(0,99)\n b=random.randint(0,99)\n #The user gets three tries to get it right.\n for count in range(3):\n print \"What is \",a,\" + \",b,\"? \"\n ans = random.randint(0,200) #user input\n if ans==a+b:\n print \"Right\"\n num_right+=1\n print \"You've used up all your tries.\"\n print \"The answer is \",a+b\n \n#Main function \nprint \"How many practice problems: \"\ncount=2 #User input taken as 2\nnum_right=0\nwhile True:\n drill()\n count-=1\n if(count==0):\n break\n \n#Result\nprint \"You got \",num_right,\" right. \" \n",
+ "input": [
+ "\n",
+ "\n",
+ "import random\n",
+ "\n",
+ "#Variable decleration\n",
+ "count=None\n",
+ "num_right=0\n",
+ "\n",
+ "#Function for the drill\n",
+ "def drill():\n",
+ " #Generate two numbers between 0 and 99.\n",
+ " a=random.randint(0,99)\n",
+ " b=random.randint(0,99)\n",
+ " #The user gets three tries to get it right.\n",
+ " for count in range(3):\n",
+ " print \"What is \",a,\" + \",b,\"? \"\n",
+ " ans = random.randint(0,200) #user input\n",
+ " if ans==a+b:\n",
+ " print \"Right\"\n",
+ " num_right+=1\n",
+ " print \"You've used up all your tries.\"\n",
+ " print \"The answer is \",a+b\n",
+ " \n",
+ "#Main function \n",
+ "print \"How many practice problems: \"\n",
+ "count=2 #User input taken as 2\n",
+ "num_right=0\n",
+ "while True:\n",
+ " drill()\n",
+ " count-=1\n",
+ " if(count==0):\n",
+ " break\n",
+ " \n",
+ "#Result\n",
+ "print \"You got \",num_right,\" right. \" \n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "How many practice problems: \nWhat is 9 + 89 ? \nWhat is 9 + 89 ? \nWhat is 9 + 89 ? \nYou've used up all your tries.\nThe answer is 98\nWhat is 85 + 98 ? \nWhat is 85 + 98 ? \nWhat is 85 + 98 ? \nYou've used up all your tries.\nThe answer is 183\nYou got 0 right. \n"
+ "text": [
+ "How many practice problems: \n",
+ "What is 9 + 89 ? \n",
+ "What is 9 + 89 ? \n",
+ "What is 9 + 89 ? \n",
+ "You've used up all your tries.\n",
+ "The answer is 98\n",
+ "What is 85 + 98 ? \n",
+ "What is 85 + 98 ? \n",
+ "What is 85 + 98 ? \n",
+ "You've used up all your tries.\n",
+ "The answer is 183\n",
+ "You got 0 right. \n"
+ ]
}
],
"prompt_number": 5
@@ -115,19 +244,39 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 7.6, Page Number: 136<h3>"
+ "source": [
+ "<h3>Example 7.6, Page Number: 136<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Pass by reference(pointer) to a function'''\n\nfrom ctypes import *\n\n#Function to change the reference value\ndef f(j):\n j[0]=100 #j is assigned 100\n\n#Variable decleration\ni=c_int(1)\np=pointer(i)\n\n#Calling the function\nf(p) \n\nprint i",
+ "input": [
+ "\n",
+ "from ctypes import *\n",
+ "\n",
+ "#Function to change the reference value\n",
+ "def f(j):\n",
+ " j[0]=100 #j is assigned 100\n",
+ "\n",
+ "#Variable decleration\n",
+ "i=c_int(1)\n",
+ "p=pointer(i)\n",
+ "\n",
+ "#Calling the function\n",
+ "f(p) \n",
+ "\n",
+ "print i"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "c_long(100)\n"
+ "text": [
+ "c_long(100)\n"
+ ]
}
],
"prompt_number": 5
@@ -135,19 +284,39 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 7.7, Page Number: 137<h3>"
+ "source": [
+ "<h3>Example 7.7, Page Number: 137<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Pass by reference(pointer) to a function'''\n\nfrom ctypes import *\n\n#Function to change the reference value\ndef f(j):\n j[0]=100 #j is assigned 100\n\n#Variable decleration\ni=c_int(1)\np=pointer(i)\n\n#Calling the function\nf(p) \n\nprint i",
+ "input": [
+ "\n",
+ "from ctypes import *\n",
+ "\n",
+ "#Function to change the reference value\n",
+ "def f(j):\n",
+ " j[0]=100 #j is assigned 100\n",
+ "\n",
+ "#Variable decleration\n",
+ "i=c_int(1)\n",
+ "p=pointer(i)\n",
+ "\n",
+ "#Calling the function\n",
+ "f(p) \n",
+ "\n",
+ "print i"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "c_long(100)\n"
+ "text": [
+ "c_long(100)\n"
+ ]
}
],
"prompt_number": 6
@@ -155,19 +324,37 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 7.8, Page Number: 137<h3>"
+ "source": [
+ "<h3>Example 7.8, Page Number: 137<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Calling function with a list(array)'''\n\n#function to print some nos.\ndef display(num):\n for i in range(10):\n print num[i],\n \n#Variable declaration\nt=[]\n\nfor i in range(10):\n t.append(i)\n#Pass list to a function\ndisplay(t)",
+ "input": [
+ "\n",
+ "#function to print some nos.\n",
+ "def display(num):\n",
+ " for i in range(10):\n",
+ " print num[i],\n",
+ " \n",
+ "#Variable declaration\n",
+ "t=[]\n",
+ "\n",
+ "for i in range(10):\n",
+ " t.append(i)\n",
+ "#Pass list to a function\n",
+ "display(t)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "0 1 2 3 4 5 6 7 8 9\n"
+ "text": [
+ "0 1 2 3 4 5 6 7 8 9\n"
+ ]
}
],
"prompt_number": 7
@@ -175,19 +362,39 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 7.9, Page Number: 138<h3>"
+ "source": [
+ "<h3>Example 7.9, Page Number: 138<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Displaying list without passing the list'''\n\n#Print some nos.\ndef display(num):\n print num,\n\n#Variable declaration\nt=[]\n\nfor i in range(10):\n t.append(i)\n \n#Printing without passing entire list\nfor i in range(10):\n display(t[i])",
+ "input": [
+ "\n",
+ "\n",
+ "#Print some nos.\n",
+ "def display(num):\n",
+ " print num,\n",
+ "\n",
+ "#Variable declaration\n",
+ "t=[]\n",
+ "\n",
+ "for i in range(10):\n",
+ " t.append(i)\n",
+ " \n",
+ "#Printing without passing entire list\n",
+ "for i in range(10):\n",
+ " display(t[i])"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "0 1 2 3 4 5 6 7 8 9\n"
+ "text": [
+ "0 1 2 3 4 5 6 7 8 9\n"
+ ]
}
],
"prompt_number": 8
@@ -195,19 +402,51 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 7.10, Page Number: 139<h3>"
+ "source": [
+ "<h3>Example 7.10, Page Number: 139<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Program to convert each of the array element into its cube'''\n\n#Function to convert into cubes\ndef cube(n,num):\n num-=1\n while num:\n n[num]=n[num]*n[num]*n[num]\n num-=1\n\n#Variable declaration\nnums=[]\n\nfor i in range(10):\n nums.append(i+1)\n \nprint \"Original contents: \",\nfor i in range(10):\n print nums[i],\nprint\n\ncube(nums,10) #Compute cubes\n\n#Result\nprint \"Altered contents: \",\nfor i in range(10):\n print nums[i],",
+ "input": [
+ "\n",
+ "\n",
+ "#Function to convert into cubes\n",
+ "def cube(n,num):\n",
+ " num-=1\n",
+ " while num:\n",
+ " n[num]=n[num]*n[num]*n[num]\n",
+ " num-=1\n",
+ "\n",
+ "#Variable declaration\n",
+ "nums=[]\n",
+ "\n",
+ "for i in range(10):\n",
+ " nums.append(i+1)\n",
+ " \n",
+ "print \"Original contents: \",\n",
+ "for i in range(10):\n",
+ " print nums[i],\n",
+ "print\n",
+ "\n",
+ "cube(nums,10) #Compute cubes\n",
+ "\n",
+ "#Result\n",
+ "print \"Altered contents: \",\n",
+ "for i in range(10):\n",
+ " print nums[i],"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Original contents: 1 2 3 4 5 6 7 8 9 10\nAltered contents: 1 8 27 64 125 216 343 512 729 1000\n"
+ "text": [
+ "Original contents: 1 2 3 4 5 6 7 8 9 10\n",
+ "Altered contents: 1 8 27 64 125 216 343 512 729 1000\n"
+ ]
}
],
"prompt_number": 10
@@ -215,19 +454,39 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 7.11, Page Number: 140<h3>"
+ "source": [
+ "<h3>Example 7.11, Page Number: 140<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Program to convert a string to uppercase'''\n\nimport string\n\ndef stringupper(str):\n str=string.upper(str) #convert to uppercase\n return str\n\n#Variable declaration \nstr=\"this is a test\"\n\n#Calling the function\nstr=stringupper(str)\n\n#Result\nprint str",
+ "input": [
+ "\n",
+ "import string\n",
+ "\n",
+ "def stringupper(str):\n",
+ " str=string.upper(str) #convert to uppercase\n",
+ " return str\n",
+ "\n",
+ "#Variable declaration \n",
+ "str=\"this is a test\"\n",
+ "\n",
+ "#Calling the function\n",
+ "str=stringupper(str)\n",
+ "\n",
+ "#Result\n",
+ "print str"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "THIS IS A TEST\n"
+ "text": [
+ "THIS IS A TEST\n"
+ ]
}
],
"prompt_number": 7
@@ -235,19 +494,33 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 7.12, Page Number: 141<h3>"
+ "source": [
+ "<h3>Example 7.12, Page Number: 141<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Program to find length of string'''\n\n#Function to fond length\ndef mystrlen(str):\n l=len(str)\n return l\n\n#Result\nprint \"Length of Hello There is: \",\nprint mystrlen(\"Hello There\")",
+ "input": [
+ "\n",
+ "#Function to fond length\n",
+ "def mystrlen(str):\n",
+ " l=len(str)\n",
+ " return l\n",
+ "\n",
+ "#Result\n",
+ "print \"Length of Hello There is: \",\n",
+ "print mystrlen(\"Hello There\")"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Length of Hello There is: 11\n"
+ "text": [
+ "Length of Hello There is: 11\n"
+ ]
}
],
"prompt_number": 11
@@ -255,19 +528,35 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 7.13, Page Number: 142<h3>"
+ "source": [
+ "<h3>Example 7.13, Page Number: 142<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Demonstrates how to access command line arguments.'''\n#This wont work because we cannot provide command line arguments\n\nimport sys\n\ndef main():\n if len(sys.argv)!=2:\n print \"You forgot to type your name.\" #CHECK!!!!\n return\n #Result\n print \"Hello \",sys.argv[1]\n\nmain()",
+ "input": [
+ "\n",
+ "import sys\n",
+ "\n",
+ "def main():\n",
+ " if len(sys.argv)!=2:\n",
+ " print \"You forgot to type your name.\" #CHECK!!!!\n",
+ " return\n",
+ " #Result\n",
+ " print \"Hello \",sys.argv[1]\n",
+ "\n",
+ "main()"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "You forgot to type your name.\n"
+ "text": [
+ "You forgot to type your name.\n"
+ ]
}
],
"prompt_number": 2
@@ -275,19 +564,36 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 7.14, Page Number: 143<h3>"
+ "source": [
+ "<h3>Example 7.14, Page Number: 143<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''The program prints all command line arguments '''\nimport sys\n\n#Result\nfor t in range(len(sys.argv)):\n i=0\n print sys.argv[t] ",
+ "input": [
+ "'''The program prints all command line arguments '''\n",
+ "import sys\n",
+ "\n",
+ "#Result\n",
+ "for t in range(len(sys.argv)):\n",
+ " i=0\n",
+ " print sys.argv[t] "
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "-c\n-f\nC:\\Users\\Anandi\\.ipython\\profile_default\\security\\kernel-6e851974-75ff-4911-bdf9-e089a03e5741.json\n--KernelApp.parent_appname='ipython-notebook'\n--interrupt=904\n--parent=876\n"
+ "text": [
+ "-c\n",
+ "-f\n",
+ "C:\\Users\\Anandi\\.ipython\\profile_default\\security\\kernel-6e851974-75ff-4911-bdf9-e089a03e5741.json\n",
+ "--KernelApp.parent_appname='ipython-notebook'\n",
+ "--interrupt=904\n",
+ "--parent=876\n"
+ ]
}
],
"prompt_number": 2
@@ -295,19 +601,36 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 7.15, Page Number: 144<h3>"
+ "source": [
+ "<h3>Example 7.15, Page Number: 144<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''This program concatenates the two string arguments and displays them'''\n\nimport sys\n\nif len(sys.argv)!=3:\n print \"usage: add num num\" \nelse :\n #Variable Decleration\n a=sys.argv[1]\n b=sys.argv[2]\n #Result\n print a+b\n \n",
+ "input": [
+ "\n",
+ "import sys\n",
+ "\n",
+ "if len(sys.argv)!=3:\n",
+ " print \"usage: add num num\" \n",
+ "else :\n",
+ " #Variable Decleration\n",
+ " a=sys.argv[1]\n",
+ " b=sys.argv[2]\n",
+ " #Result\n",
+ " print a+b\n",
+ " \n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "usage: add num num\n"
+ "text": [
+ "usage: add num num\n"
+ ]
}
],
"prompt_number": 3
@@ -315,19 +638,34 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 7.16, Page Number: 145<h3>"
+ "source": [
+ "<h3>Example 7.16, Page Number: 145<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''An example program'''\n\nimport string\n\n#Variable Decleration\ni=string.atoi(\"100\")\nj=string.atoi(\"100000\")\nk=string.atof(\"-0.123\")\n\n#Result\nprint i,\" \",j,\" \",k",
+ "input": [
+ "\n",
+ "import string\n",
+ "\n",
+ "#Variable Decleration\n",
+ "i=string.atoi(\"100\")\n",
+ "j=string.atoi(\"100000\")\n",
+ "k=string.atof(\"-0.123\")\n",
+ "\n",
+ "#Result\n",
+ "print i,\" \",j,\" \",k"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "100 100000 -0.123\n"
+ "text": [
+ "100 100000 -0.123\n"
+ ]
}
],
"prompt_number": 13
@@ -335,24 +673,39 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 7.17, Page Number: 147<h3>"
+ "source": [
+ "<h3>Example 7.17, Page Number: 147<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Example to use the function abs()'''\n\n#Variable Decleration\ni=abs(-10)\n\n#Result\nprint abs(-23)\nabs(100)",
+ "input": [
+ "\n",
+ "#Variable Decleration\n",
+ "i=abs(-10)\n",
+ "\n",
+ "#Result\n",
+ "print abs(-23)\n",
+ "abs(100)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "23\n"
+ "text": [
+ "23\n"
+ ]
},
{
+ "metadata": {},
"output_type": "pyout",
"prompt_number": 5,
- "text": "100"
+ "text": [
+ "100"
+ ]
}
],
"prompt_number": 5
@@ -360,19 +713,36 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 7.18, Page Number: 148<h3>"
+ "source": [
+ "<h3>Example 7.18, Page Number: 148<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Program to find a substring'''\n\n#Return index of substring or -1 if not found.\ndef find_substr(sub,str):\n l=str.find(sub)\n return l\n\n#Variable decleration;Calling the function\nindex=find_substr(\"three\",\"one two three four\")\n\n#Result\nprint \"Index of three is \",index",
+ "input": [
+ "\n",
+ "\n",
+ "#Return index of substring or -1 if not found.\n",
+ "def find_substr(sub,str):\n",
+ " l=str.find(sub)\n",
+ " return l\n",
+ "\n",
+ "#Variable decleration;Calling the function\n",
+ "index=find_substr(\"three\",\"one two three four\")\n",
+ "\n",
+ "#Result\n",
+ "print \"Index of three is \",index"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Index of three is 8\n"
+ "text": [
+ "Index of three is 8\n"
+ ]
}
],
"prompt_number": 6
@@ -380,19 +750,35 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 7.19, Page Number: 149<h3>"
+ "source": [
+ "<h3>Example 7.19, Page Number: 149<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Program to print a string vertically downward'''\n\nimport sys\n\ndef print_vertical(str):\n l=len(str)\n for i in range(l):\n print str[i],\n \nprint_vertical(sys.argv[1])\n ",
+ "input": [
+ "\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "def print_vertical(str):\n",
+ " l=len(str)\n",
+ " for i in range(l):\n",
+ " print str[i],\n",
+ " \n",
+ "print_vertical(sys.argv[1])\n",
+ " "
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "- f\n"
+ "text": [
+ "- f\n"
+ ]
}
],
"prompt_number": 14
@@ -400,19 +786,36 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 7.20, Page Number: 150<h3>"
+ "source": [
+ "<h3>Example 7.20, Page Number: 150<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Rework find_substr() to return pointer'''\n\n#Return position of substring\ndef find_substr(sub,str):\n return str.find(sub)\n\n#Variable declaration\ns=\"one two three four\"\n\nsubstr = find_substr(\"three\",s)\n\n#Result\nprint \"substring found:\",s[substr:]\n",
+ "input": [
+ "\n",
+ "#Return position of substring\n",
+ "def find_substr(sub,str):\n",
+ " return str.find(sub)\n",
+ "\n",
+ "#Variable declaration\n",
+ "s=\"one two three four\"\n",
+ "\n",
+ "substr = find_substr(\"three\",s)\n",
+ "\n",
+ "#Result\n",
+ "print \"substring found:\",s[substr:]\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "substring found: three four\n"
+ "text": [
+ "substring found: three four\n"
+ ]
}
],
"prompt_number": 2
@@ -420,19 +823,48 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 7.21, Page Number: 154<h3>"
+ "source": [
+ "<h3>Example 7.21, Page Number: 154<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Program to calculate factorial'''\n\n#Recursive version\ndef factr(n):\n if n==1:\n return 1\n answer=factr(n-1)*n\n return answer\n\n#Iterative version\ndef fact(n):\n answer=1\n for t in range(n):\n answer=answer*(t+1)\n return answer\n\n#Using recursion version\nprint \"4 factorial is \",factr(4)\n\n#Using iterative version\nprint \"4 factorial is \",fact(4)\n\n ",
+ "input": [
+ "\n",
+ "\n",
+ "#Recursive version\n",
+ "def factr(n):\n",
+ " if n==1:\n",
+ " return 1\n",
+ " answer=factr(n-1)*n\n",
+ " return answer\n",
+ "\n",
+ "#Iterative version\n",
+ "def fact(n):\n",
+ " answer=1\n",
+ " for t in range(n):\n",
+ " answer=answer*(t+1)\n",
+ " return answer\n",
+ "\n",
+ "#Using recursion version\n",
+ "print \"4 factorial is \",factr(4)\n",
+ "\n",
+ "#Using iterative version\n",
+ "print \"4 factorial is \",fact(4)\n",
+ "\n",
+ " "
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "4 factorial is 24\n4 factorial is 24\n"
+ "text": [
+ "4 factorial is 24\n",
+ "4 factorial is 24\n"
+ ]
}
],
"prompt_number": 3
@@ -440,19 +872,38 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 7.22, Page Number: 155<h3>"
+ "source": [
+ "<h3>Example 7.22, Page Number: 155<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Print a string backwards using recursion'''\n\n#Reversing a string\ndef reverse(s):\n r = \"\"\n for c in s:\n r=c+r\n print r\n \n#VariabDecleration \nstr=\"this is a test\"\n\n#Calling function\nreverse(str)\n",
+ "input": [
+ "\n",
+ "\n",
+ "#Reversing a string\n",
+ "def reverse(s):\n",
+ " r = \"\"\n",
+ " for c in s:\n",
+ " r=c+r\n",
+ " print r\n",
+ " \n",
+ "#VariabDecleration \n",
+ "str=\"this is a test\"\n",
+ "\n",
+ "#Calling function\n",
+ "reverse(str)\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "tset a si siht\n"
+ "text": [
+ "tset a si siht\n"
+ ]
}
],
"prompt_number": 9
@@ -460,7 +911,7 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "",
+ "input": [],
"language": "python",
"metadata": {},
"outputs": []