{ "metadata": { "name": "", "signature": "sha256:a0a1c93dba0a6936203c3f01d39f2697247a7b54141760d78392371a2c5620f6" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Hour 18: Using Special Data Types and Functions" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 18.1, Page No 297" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# There is no proper concept of Enum(Enumaration) in python so i have implemented in this manner\n", "def enum(*sequential, **named):\n", " enums = dict(zip(sequential, range(len(sequential))), **named)\n", " return type('Enum', (), enums)\n", "\n", "\n", "language = enum(human=100,animal=50,computer=51)\n", "\n", "days = enum('SUN','MON','TUE','WED','THU','FRI','SAT')\n", "\n", "print \"human=%d, animal=%d, computer=%d\" % (language.human,language.animal,language.computer)\n", "print \"SUN: %d\" % days.SUN\n", "print \"MON: %d\" % days.MON\n", "print \"TUE: %d\" % days.TUE\n", "print \"WED: %d\" % days.WED\n", "print \"THU: %d\" % days.THU\n", "print \"FRI: %d\" % days.FRI\n", "print \"SAT: %d\" % days.SAT" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "human=100, animal=50, computer=51\n", "SUN: 0\n", "MON: 1\n", "TUE: 2\n", "WED: 3\n", "THU: 4\n", "FRI: 5\n", "SAT: 6\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 18.2, Page No 298" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# There is no proper concept of Enum(Enumaration) in python so i have implemented in this manner\n", "def enum(*sequential, **named):\n", " enums = dict(zip(sequential, range(len(sequential))), **named)\n", " return type('Enum', (), enums)\n", "\n", "units= enum(penny=1,nickel=5,dime=10,quarter=25,dollar=100)\n", "money_unit=[units.dollar,units.quarter,units.dime,units.nickel,units.penny]\n", "unit_name=[\"dollar(s)\",\"quarter(s)\",\"dime(s)\",\"nickel(s)\",\"penny(s)\"]\n", "\n", "cent=int(raw_input(\"Enter a monetary value in cents:\"))\n", "\n", "print \"Which is equivalent to:\"\n", "tmp=0\n", "for i in range(5):\n", " tmp=cent / money_unit[i]\n", " cent = cent-(tmp*money_unit[i])\n", " if(tmp):\n", " print \"%d %s\" % (tmp,unit_name[i])," ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a monetary value in cents:141\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Which is equivalent to:\n", "1 dollar(s) 1 quarter(s) 1 dime(s) 1 nickel(s) 1 penny(s)\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 18.3, Page No 301" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# There is no proper concept of Enum(Enumaration) in python so i have implemented in this manner\n", "def enum(*sequential, **named):\n", " enums = dict(zip(sequential, range(len(sequential))), **named)\n", " return type('Enum', (), enums)\n", "\n", "constants=enum(ITEM_NUM=3,DELT=(ord('a')-ord('A')))\n", "\n", "def Convert2Upper(str1,str2):\n", " str1=list(str1)\n", " str2=list(i for i in range(len(str1)+1))\n", " for i in range(len(str1)):\n", " if ((ord(str1[i])>=97) and (ord(str1[i])<=122)):\n", " str2[i]=chr(ord(str1[i])-constants.DELT)\n", " else:\n", " str2[i]=str1[i]\n", " str2[i+1]='\\0'\n", " return str2\n", " \n", "moon=[\"Whatever we wear\",\"we become beautiful\",\"moon viewing!\"]\n", "str1=\"\"\n", "term=0\n", "str3=[\"\",\"\",\"\"]\n", "\n", "for i in range(constants.ITEM_NUM):\n", " if(str1==None):\n", " print \"malloc() failed.\"\n", " term=1\n", " i=constants.ITEM_NUM\n", " str1=Convert2Upper(moon[i],str1)\n", " print moon[i]\n", " str3[i]=str1\n", "print \"\"\n", "for i in range(len(str3)):\n", " print \"\".join(str3[i])" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Whatever we wear\n", "we become beautiful\n", "moon viewing!\n", "\n", "WHATEVER WE WEAR\u0000\n", "WE BECOME BEAUTIFUL\u0000\n", "MOON VIEWING!\u0000\n" ] } ], "prompt_number": 20 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 18.4, Page No 303" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# There is no proper concept of Enum(Enumaration) in python so i have implemented in this manner\n", "def enum(*sequential, **named):\n", " enums = dict(zip(sequential, range(len(sequential))), **named)\n", " return type('Enum', (), enums)\n", "\n", "con=enum(MIN_NUM=0,MAX_NUM=100)\n", "\n", "def fRecur(n):\n", " if(n==con.MIN_NUM):\n", " return 0\n", " return (fRecur(n-1)+n)\n", "\n", "sum1=sum2=0\n", "for i in range(1,(con.MAX_NUM)+1):\n", " sum1+=i\n", " sum2=fRecur(con.MAX_NUM)\n", "\n", "print \"The value of sum1 is %d.\" % sum1\n", "print \"The value returned by fRecur() is %d.\" % sum2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The value of sum1 is 5050.\n", "The value returned by fRecur() is 5050.\n" ] } ], "prompt_number": 34 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 18.5, Page No 306" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#The output is correct as it takes the CMD LINES from respective terminal.\n", "import sys\n", "print \"The value received by argc is \" ,len(sys.argv),\".\\n\"\n", "print \"There are \",len(sys.argv), \"command-line arguments passed to main().\\n\"\n", "print \"The first command-line argument is: \",sys.argv[0],\"\\n\"\n", "print \"The rest of the command-line arguments are:\\n\"\n", "for i in range(1,len(sys.argv)):\n", " print sys.argv[i]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The value received by argc is 8 .\n", "\n", "There are 8 command-line arguments passed to main().\n", "\n", "The first command-line argument is: -c \n", "\n", "The rest of the command-line arguments are:\n", "\n", "-f\n", "C:\\Users\\Vaibhav\\.ipython\\profile_default\\security\\kernel-683917c3-368a-425a-93a3-9782f6d369b5.json\n", "--IPKernelApp.parent_appname='ipython-notebook'\n", "--profile-dir\n", "C:\\Users\\Vaibhav\\.ipython\\profile_default\n", "--interrupt=852\n", "--parent=1008\n" ] } ], "prompt_number": 1 } ], "metadata": {} } ] }