summaryrefslogtreecommitdiff
path: root/ANSI_C_Programming/chapter4.ipynb
diff options
context:
space:
mode:
authordebashisdeb2014-06-20 15:42:42 +0530
committerdebashisdeb2014-06-20 15:42:42 +0530
commit83c1bfceb1b681b4bb7253b47491be2d8b2014a1 (patch)
treef54eab21dd3d725d64a495fcd47c00d37abed004 /ANSI_C_Programming/chapter4.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 'ANSI_C_Programming/chapter4.ipynb')
-rw-r--r--ANSI_C_Programming/chapter4.ipynb777
1 files changed, 389 insertions, 388 deletions
diff --git a/ANSI_C_Programming/chapter4.ipynb b/ANSI_C_Programming/chapter4.ipynb
index a3d738f1..3a15cce2 100644
--- a/ANSI_C_Programming/chapter4.ipynb
+++ b/ANSI_C_Programming/chapter4.ipynb
@@ -1,389 +1,390 @@
-{
- "metadata": {
- "name": "chapter4.ipynb"
- },
- "nbformat": 3,
- "nbformat_minor": 0,
- "worksheets": [
- {
- "cells": [
- {
- "cell_type": "heading",
- "level": 1,
- "metadata": {},
- "source": [
- "CHAPTER 4: THE CASE CONTROL STRUCTURE"
- ]
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:126"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Switch case statement using dictionary\n",
- "i=2\n",
- "def switch(i):\n",
- " return {True: 'I am in default\\n', #default case\n",
- " i==1: 'I am in case1\\n',\n",
- " i==2: 'I am in case2\\n',\n",
- " i==3: 'I am in case3\\n',\n",
- " }[True]\n",
- "print switch(i)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "I am in case2\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 1
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:128"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Switch case using scrambled case order\n",
- "i=22\n",
- "def switch(i):\n",
- " return{True: 'I am in default\\n',\n",
- " i==121: 'I am in case 121\\n',\n",
- " i==7: 'I am in case 7\\n',\n",
- " i==22: 'I am in case 22\\n',\n",
- " }[True]\n",
- "print switch(i)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "I am in case 22\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 2
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:128-129"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Switch case using char values in case\n",
- "c='x'\n",
- "def switch(c):\n",
- " return {True: 'I am in default\\n',\n",
- " c=='v': 'I am in case v\\n',\n",
- " c=='a': 'I am in case a\\n',\n",
- " c=='x': 'I am in case x\\n',\n",
- " }[True]\n",
- "print switch(c)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "I am in case x\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 3
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:129-130"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Executing common set of statements for multiple cases\n",
- "print \"Enter any one of the alphabets a,b,or c\"\n",
- "ch=raw_input()\n",
- "def casea():\n",
- " print \"\"\n",
- "def caseb():\n",
- " print \"\"\n",
- "def casec():\n",
- " print \"\"\n",
- "def switch(ch):\n",
- " return {True: 'wish you knew what are alphabets\\n',\n",
- " ch=='a' or ch=='A': 'a as in ashar\\n',\n",
- " ch=='b' or ch=='B': 'b as in brain\\n',\n",
- " ch=='c' or ch=='C': 'c as in cookie\\n',\n",
- " }[True]\n",
- "print switch(ch)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter any one of the alphabets a,b,or c\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "B\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "b as in brain\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 6
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:130"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Iniatialisation according to cases\n",
- "print \"Enter value of i\"\n",
- "i=eval(raw_input())\n",
- "def switch(i):\n",
- " print \"Hello\\n\" #It's python,not C :)\n",
- " try:\n",
- " return {1: a,\n",
- " 2: b,\n",
- " }[i]()\n",
- " except:\n",
- " print \"wrong choice\" #print message in default case\n",
- "def a():\n",
- " j=10\n",
- " print j #print j as 10 if i=1\n",
- "def b():\n",
- " j=20 \n",
- " print j #print j as 20 if i=2\n",
- "switch(i)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter value of i\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Hello\n",
- "\n",
- "10\n"
- ]
- }
- ],
- "prompt_number": 10
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:133"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Conditional printing of messages\n",
- "import sys\n",
- "print \"Enter the number of goals scored against india\"\n",
- "goals=eval(raw_input())\n",
- "if goals<=5:\n",
- " print \"To err is human!\\n\"\n",
- "else:\n",
- " print \"About time soccer players learnt C\\n\"\n",
- " print \"and said goodbye! adieu! to soccer\\n\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter the number of goals scored against india\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "7\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "About time soccer players learnt C\n",
- "\n",
- "and said goodbye! adieu! to soccer\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 11
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:134-135"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Program to conditional print of numbers\n",
- "for i in range(1,4,1):\n",
- " for j in range(1,4,1):\n",
- " for k in range(1,4,1):\n",
- " if (i==3 and j==3 and k==3):\n",
- " print \"Out of the loop at last!\\n\"\n",
- " else:\n",
- " print \"%d %d %d\\n\" % (i,j,k)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1 1 1\n",
- "\n",
- "1 1 2\n",
- "\n",
- "1 1 3\n",
- "\n",
- "1 2 1\n",
- "\n",
- "1 2 2\n",
- "\n",
- "1 2 3\n",
- "\n",
- "1 3 1\n",
- "\n",
- "1 3 2\n",
- "\n",
- "1 3 3\n",
- "\n",
- "2 1 1\n",
- "\n",
- "2 1 2\n",
- "\n",
- "2 1 3\n",
- "\n",
- "2 2 1\n",
- "\n",
- "2 2 2\n",
- "\n",
- "2 2 3\n",
- "\n",
- "2 3 1\n",
- "\n",
- "2 3 2\n",
- "\n",
- "2 3 3\n",
- "\n",
- "3 1 1\n",
- "\n",
- "3 1 2\n",
- "\n",
- "3 1 3\n",
- "\n",
- "3 2 1\n",
- "\n",
- "3 2 2\n",
- "\n",
- "3 2 3\n",
- "\n",
- "3 3 1\n",
- "\n",
- "3 3 2\n",
- "\n",
- "Out of the loop at last!\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 12
- }
- ],
- "metadata": {}
- }
- ]
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:c88e4872fc4418c2962652967a7ca0a93c345eb9f3bd38488ba817f74e5fc2eb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "CHAPTER 4: THE CASE CONTROL STRUCTURE"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:126"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "i=2\n",
+ "def switch(i):\n",
+ " return {True: 'I am in default\\n', #default case\n",
+ " i==1: 'I am in case1\\n',\n",
+ " i==2: 'I am in case2\\n',\n",
+ " i==3: 'I am in case3\\n',\n",
+ " }[True]\n",
+ "print switch(i)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "I am in case2\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:128"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "i=22\n",
+ "def switch(i):\n",
+ " return{True: 'I am in default\\n',\n",
+ " i==121: 'I am in case 121\\n',\n",
+ " i==7: 'I am in case 7\\n',\n",
+ " i==22: 'I am in case 22\\n',\n",
+ " }[True]\n",
+ "print switch(i)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "I am in case 22\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:128-129"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "c='x'\n",
+ "def switch(c):\n",
+ " return {True: 'I am in default\\n',\n",
+ " c=='v': 'I am in case v\\n',\n",
+ " c=='a': 'I am in case a\\n',\n",
+ " c=='x': 'I am in case x\\n',\n",
+ " }[True]\n",
+ "print switch(c)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "I am in case x\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:129-130"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "print \"Enter any one of the alphabets a,b,or c\"\n",
+ "ch=raw_input()\n",
+ "def casea():\n",
+ " print \"\"\n",
+ "def caseb():\n",
+ " print \"\"\n",
+ "def casec():\n",
+ " print \"\"\n",
+ "def switch(ch):\n",
+ " return {True: 'wish you knew what are alphabets\\n',\n",
+ " ch=='a' or ch=='A': 'a as in ashar\\n',\n",
+ " ch=='b' or ch=='B': 'b as in brain\\n',\n",
+ " ch=='c' or ch=='C': 'c as in cookie\\n',\n",
+ " }[True]\n",
+ "print switch(ch)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any one of the alphabets a,b,or c\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "B\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "b as in brain\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:130"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "print \"Enter value of i\"\n",
+ "i=eval(raw_input())\n",
+ "def switch(i):\n",
+ " print \"Hello\\n\" #It's python,not C :)\n",
+ " try:\n",
+ " return {1: a,\n",
+ " 2: b,\n",
+ " }[i]()\n",
+ " except:\n",
+ " print \"wrong choice\" #print message in default case\n",
+ "def a():\n",
+ " j=10\n",
+ " print j #print j as 10 if i=1\n",
+ "def b():\n",
+ " j=20 \n",
+ " print j #print j as 20 if i=2\n",
+ "switch(i)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value of i\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Hello\n",
+ "\n",
+ "10\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:133"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "import sys\n",
+ "print \"Enter the number of goals scored against india\"\n",
+ "goals=eval(raw_input())\n",
+ "if goals<=5:\n",
+ " print \"To err is human!\\n\"\n",
+ "else:\n",
+ " print \"About time soccer players learnt C\\n\"\n",
+ " print \"and said goodbye! adieu! to soccer\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the number of goals scored against india\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "7\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "About time soccer players learnt C\n",
+ "\n",
+ "and said goodbye! adieu! to soccer\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:134-135"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "for i in range(1,4,1):\n",
+ " for j in range(1,4,1):\n",
+ " for k in range(1,4,1):\n",
+ " if (i==3 and j==3 and k==3):\n",
+ " print \"Out of the loop at last!\\n\"\n",
+ " else:\n",
+ " print \"%d %d %d\\n\" % (i,j,k)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1 1 1\n",
+ "\n",
+ "1 1 2\n",
+ "\n",
+ "1 1 3\n",
+ "\n",
+ "1 2 1\n",
+ "\n",
+ "1 2 2\n",
+ "\n",
+ "1 2 3\n",
+ "\n",
+ "1 3 1\n",
+ "\n",
+ "1 3 2\n",
+ "\n",
+ "1 3 3\n",
+ "\n",
+ "2 1 1\n",
+ "\n",
+ "2 1 2\n",
+ "\n",
+ "2 1 3\n",
+ "\n",
+ "2 2 1\n",
+ "\n",
+ "2 2 2\n",
+ "\n",
+ "2 2 3\n",
+ "\n",
+ "2 3 1\n",
+ "\n",
+ "2 3 2\n",
+ "\n",
+ "2 3 3\n",
+ "\n",
+ "3 1 1\n",
+ "\n",
+ "3 1 2\n",
+ "\n",
+ "3 1 3\n",
+ "\n",
+ "3 2 1\n",
+ "\n",
+ "3 2 2\n",
+ "\n",
+ "3 2 3\n",
+ "\n",
+ "3 3 1\n",
+ "\n",
+ "3 3 2\n",
+ "\n",
+ "Out of the loop at last!\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ }
+ ],
+ "metadata": {}
+ }
+ ]
} \ No newline at end of file