{ "metadata": { "name": "", "signature": "sha256:6212a1681da4032325cbede0a51614855911a11a6baad9f5e6fd0a0ecd4b5da9" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 9: Functions" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example average.c, Page 185" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def average(a,b): #function to calculate average\n", " return (a+b)/2\n", "nums = raw_input(\"Enter three numbers: \") #input numbers from user\n", "list1 = map(float, nums.split()) \n", "x=list1[0]\n", "y=list1[1]\n", "z=list1[2]\n", "print \"Average of %.1f and %.1f: %.2f\" % (x,y,average(x,y)) #print average using function\n", "print \"Average of %.1f and %.1f: %.2f\" % (y,z,average(y,z))\n", "print \"Average of %.1f and %.1f: %.2f\" % (x,z,average(x,z))\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter three numbers: 3.5 9.6 10.2\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Average of 3.5 and 9.6: 6.55\n", "Average of 9.6 and 10.2: 9.90\n", "Average of 3.5 and 10.2: 6.85\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example countdown.c, Page 186" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def print_count(n): #function definition\n", " print \"T minus %d and counting\" % n\n", "for i in range (10,0,-1):\n", " print_count(i) #print using function\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "T minus 10 and counting\n", "T minus 9 and counting\n", "T minus 8 and counting\n", "T minus 7 and counting\n", "T minus 6 and counting\n", "T minus 5 and counting\n", "T minus 4 and counting\n", "T minus 3 and counting\n", "T minus 2 and counting\n", "T minus 1 and counting\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example pun2.c, Page 187" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def print_pun(): #function definition\n", " print \"To C, or not to C: that is the question.\"\n", "print_pun() #function call" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "To C, or not to C: that is the question.\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example prime.c, Page 190" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def is_prime(n): #function to check for prime number\n", " if (n<=1):\n", " return False\n", " divisor=2\n", " while(divisor*divisor<=n):\n", " if(n%divisor==0):\n", " return False\n", " return True\n", "n=int(raw_input(\"Enter a number: \")) #input number\n", "if(is_prime(n)): #check if prime using function\n", " print \"Prime\" #print result of check\n", "else:\n", " print \"Not prime\"" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a number: 34\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Not prime\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example on Page 192" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def main():\n", " nums = raw_input(\"Enter three numbers: \") #input numbers from user\n", " list1 = map(float, nums.split()) \n", " x=list1[0]\n", " y=list1[1]\n", " z=list1[2]\n", " \n", " #function usage before definition\n", " \n", " print \"Average of %.1f and %.1f: %.2f\" % (x,y,average(x,y)) #print average using function\n", " print \"Average of %.1f and %.1f: %.2f\" % (y,z,average(y,z))\n", " print \"Average of %.1f and %.1f: %.2f\" % (x,z,average(x,z))\n", " \n", "if __name__==\"__main__\":\n", " main()\n", " \n", "def average(a,b): #function to calculate average\n", " return (a+b)/2" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter three numbers: 3.5 9.6 10.2\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Average of 3.5 and 9.6: 6.55\n", "Average of 9.6 and 10.2: 9.90\n", "Average of 3.5 and 10.2: 6.85\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example on Page 195" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x=3.0\n", "print \"Square: %d\" % square(x) #gives error since function prototype doesnt exist before\n", "def square(n):\n", " return n*n\n", "\n", "\"\"\"\n", "def main():\n", " x=3.0\n", " print \"Square: %d\" % square(x) \n", " \n", "#Now wouldn't give an error since writing the line below loads up all functions before starting main()\n", "\n", "if __name__==\"__main__\":\n", " main()\n", " \n", "def square(n):\n", " return n*n\n", "\"\"\"" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'square' is not defined", "output_type": "pyerr", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m3.0\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"Square: %d\"\u001b[0m \u001b[1;33m%\u001b[0m \u001b[0msquare\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m#gives error since function prototype doesnt exist before\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0msquare\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mn\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mn\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0mn\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mNameError\u001b[0m: name 'square' is not defined" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example on Page 197" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def store_zeroes(a,n):\n", " for i in range(0,n):\n", " a[i]=0\n", "b=[None]*200\n", "store_zeroes(b,100) #First 100 elements of b are now 100.\n", "print b" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example qsort.c, Page 207" ] }, { "cell_type": "code", "collapsed": false, "input": [ "N=10 #number of elements to sort\n", "def split(a,low,high): #function to split list while sorting\n", " part_element=a[low]\n", " while(1):\n", " while(low=high):\n", " break\n", " a[low+1]=a[high]\n", " \n", " while(low=high):\n", " break\n", " a[high-1]=a[low]\n", " \n", " a[high]=part_element\n", " return high\n", " \n", "def quicksort(a,low,high): #recursive function for quicksort\n", " if(low>=high):\n", " return\n", " middle=split(a,low,high)\n", " quicksort(a,low,(middle-1))\n", " quicksort(a,(middle+1),high)\n", " \n", "nums = raw_input(\"Enter %d numbers to be sorted: \"%N) #input numbers to sort\n", "a = map(int, nums.split())\n", "a.sort()\n", "quicksort(a,0,(N-1)) #call quicksort function\n", "print \"In sorted order: \", #print sorted result\n", "for i in a:\n", " print i,\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter 10 numbers to be sorted: 9 16 47 82 4 66 12 3 25 51\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "In sorted order: 3 4 9 12 16 25 47 51 66 82\n" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }