diff options
author | nice | 2014-10-09 18:07:00 +0530 |
---|---|---|
committer | nice | 2014-10-09 18:07:00 +0530 |
commit | 36a03d6d76bac315dba73b2ba9555c7e3fe0234f (patch) | |
tree | 7e46e8873a7c92be2eef962a36e664c775aa6bf2 /Structured_Programing_with_C++/Chapter6.ipynb | |
parent | b8bb8bbfa81499ad7fc3f3508be257da65f543af (diff) | |
download | Python-Textbook-Companions-36a03d6d76bac315dba73b2ba9555c7e3fe0234f.tar.gz Python-Textbook-Companions-36a03d6d76bac315dba73b2ba9555c7e3fe0234f.tar.bz2 Python-Textbook-Companions-36a03d6d76bac315dba73b2ba9555c7e3fe0234f.zip |
updated books
Diffstat (limited to 'Structured_Programing_with_C++/Chapter6.ipynb')
-rwxr-xr-x | Structured_Programing_with_C++/Chapter6.ipynb | 549 |
1 files changed, 549 insertions, 0 deletions
diff --git a/Structured_Programing_with_C++/Chapter6.ipynb b/Structured_Programing_with_C++/Chapter6.ipynb new file mode 100755 index 00000000..6ee5ccef --- /dev/null +++ b/Structured_Programing_with_C++/Chapter6.ipynb @@ -0,0 +1,549 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:3541583e6368ad0fd94057f7e595031de565be72b75fadedd8abb1168ccd2728"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 6 : Functions"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 6.1, Page No 126"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def dAverage(x1,x2):\n",
+ " av = (x1+x2)/2\n",
+ " return av\n",
+ "\n",
+ "print \"Enter two numbers:\"\n",
+ "dNo1= float(raw_input(\"\"))\n",
+ "dNo2= float(raw_input(\"\"))\n",
+ "dAvg= dAverage(dNo1,dNo2)\n",
+ "print \"The average is: \",dAvg"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter two numbers:\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "30\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "20\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The average is: 25.0\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 6.2, Page No 127"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def min(x,y):\n",
+ " if (x<y):\n",
+ " return x\n",
+ " else:\n",
+ " return y\n",
+ " \n",
+ "print \"Enter Three integer: \"\n",
+ "a=int(raw_input(\"\"))\n",
+ "b=int(raw_input(\"\"))\n",
+ "c=int(raw_input(\"\"))\n",
+ "m=min(a,b)\n",
+ "m=min(m,c)\n",
+ "print m"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Three integer: \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 6.3, Page No 129"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def min(x,y):\n",
+ " if(x<y):\n",
+ " return x\n",
+ " else:\n",
+ " return y\n",
+ "t=0\n",
+ "iNos=range(5)\n",
+ "for t in range(5):\n",
+ " print \"Enter integer no\",t+1,\":\"\n",
+ " iNos[t]=int(raw_input(\"\"))\n",
+ "\n",
+ "m=iNos[0]\n",
+ "\n",
+ "for t in range(5):\n",
+ " m=min(iNos[t],m)\n",
+ "\n",
+ "print m,\"is the least integer\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter integer no 1 :\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "10\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter integer no 2 :\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter integer no 3 :\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter integer no 4 :\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter integer no 5 :\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "7\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3 is the least integer\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 6.4, Page No 131"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def iLgth(s):\n",
+ " n=0\n",
+ " while(s[n] != '\\0'):\n",
+ " n=n+1\n",
+ " return n\n",
+ "\n",
+ "cWord=range(30)\n",
+ "cWord=raw_input(\"Enter a word:\")\n",
+ "cWord=cWord + '\\0' #by default string array don't appen NULL character hence, it is manually appended\n",
+ "iLen=iLgth(cWord)\n",
+ "print \"Length of the word is: \",iLen"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a word:bhavin mehta\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Length of the word is: 12\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 6.5, Page No 134"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def dDiscount(iQty,dLinePrice):\n",
+ " if(dLinePrice>1000):\n",
+ " return 0.15\n",
+ " elif(dLinePrice>500 or iQty>10):\n",
+ " return 0.10\n",
+ " else:\n",
+ " return 0\n",
+ "\n",
+ "def dPrice(iNo,dUnitPrice):\n",
+ " dTax=0.25\n",
+ " dLinePr=iNo*dUnitPrice\n",
+ " dDiscPerc=dDiscount(iNo,dLinePr)\n",
+ " return dLinePr*(1-dDiscPerc)*(1+dTax)\n",
+ "\n",
+ "print \"Enter quantity and unit price: \"\n",
+ "iQuantity=int(raw_input(\"\"))\n",
+ "dUnitPrice=float(raw_input(\"\"))\n",
+ "\n",
+ "print \"To be paid\", dPrice(iQuantity,dUnitPrice)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter quantity and unit price: \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "30\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "50\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "To be paid 1593.75\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 6.6, Page No 137"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def replace1(s,c,cnew):\n",
+ " n=0\n",
+ " s = list(s)\n",
+ " while(s[n]!='\\0'):\n",
+ " if(s[n]==c):\n",
+ " s[n]=cnew\n",
+ " n=n+1\n",
+ " print \"\".join(map(str,s))\n",
+ "a=\"C:/Mydocumnets/Sheets\\0\"\n",
+ "print a\n",
+ "replace1(a,'/','-')"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "C:/Mydocumnets/Sheets\u0000\n",
+ "C:-Mydocumnets-Sheets\u0000\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 6.7, Page No 139"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def word(s):\n",
+ " i=0\n",
+ " j=len(s)\n",
+ " while(i<j):\n",
+ " if((s[i]>='a' and s[i]<='z')or(s[i]>='A' and s[i]<='Z')):\n",
+ " i=i+1\n",
+ " else:\n",
+ " return 0;\n",
+ " return 1;\n",
+ "\n",
+ "str=range(100)\n",
+ "str=raw_input(\"Enter word: \")\n",
+ "while(str):\n",
+ " if(word(str)):\n",
+ " print \"A word\"\n",
+ " else:\n",
+ " print \"No word\"\n",
+ " str=raw_input(\"Enter word: \")"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter word: john\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "A word\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter word: smith\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "A word\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter word: john smith\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "No word\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter word: \n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 6.8, Page No 143"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#import myfunc we have created this header file and its going to work as header file user has to make it\n",
+ "print \"To be paid: \" + str(dPrice(10,700))\n",
+ "\n",
+ "# Code for myfunc Header file is here\n",
+ "\n",
+ "\"\"\"\n",
+ "def underline(n):\n",
+ " for i in range(n):\n",
+ " print \"=\",\n",
+ "def dDiscount(iQty,dLinePrice):\n",
+ " if(dLinePrice > 1000):\n",
+ " return 0.15\n",
+ " elif(dLinePrice>500 or iQty>10):\n",
+ " return 0.10\n",
+ " else:\n",
+ " return 0\n",
+ "def dPrice(iNo,dUnitPrice):\n",
+ " dTax = 0.25\n",
+ " dLinePr = iNo * dUnitPrice\n",
+ " dDiscPerc = dDiscount(iNo,dLinePr)\n",
+ " return dLinePr * (1-dDiscPerc) * (1+dTax)\n",
+ "\"\"\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file |