diff options
Diffstat (limited to 'The_C_Book/Chapter6.ipynb')
-rwxr-xr-x | The_C_Book/Chapter6.ipynb | 749 |
1 files changed, 749 insertions, 0 deletions
diff --git a/The_C_Book/Chapter6.ipynb b/The_C_Book/Chapter6.ipynb new file mode 100755 index 00000000..f0f634c3 --- /dev/null +++ b/The_C_Book/Chapter6.ipynb @@ -0,0 +1,749 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:899f4d53f2b8fecda4de14fb928c82b270754cca6abc5b32e76cbdd24e164f18" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 6: Structured Data Types" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.1, page no. 173" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Example 6.1 and example 6.2 are same for python.\n", + "'''\n", + "\n", + "\n", + "class wp_char:\n", + " wp_cval = ''\n", + " wp_font = 0\n", + " wp_psize = 0\n", + "\n", + " def __init__(self):\n", + " wp_cval = ''\n", + " wp_font = 0\n", + " wp_psize = 0\n", + "\n", + "\n", + "def infun():\n", + " w = wp_char()\n", + " #w.wp_cval = raw_input(\"Enter any Character.. :\")\n", + " w.wp_font = 2\n", + " w.wp_psize = 10\n", + " return w\n", + "\n", + "ARSIZE = 10\n", + "icount=0\n", + "lo_indx=0\n", + "hi_indx=0\n", + "ar = []\n", + "for icount in range(ARSIZE):\n", + " ar.append(infun())\n", + " if(ar[icount].wp_cval == '\\n'):\n", + " ''' /*\n", + " * Leave the loop.\n", + " * not incrementing icount means that the\n", + " * '\\n' is ignored in the sort\n", + " */\n", + " '''\n", + " break;\n", + "\n", + "\n", + "#/* now a simple exchange sort */\n", + "for lo_indx in range(icount-1):\n", + " for hi_indx in range(icount):\n", + " if (ar[lo_indx].wp_cval > ar[hi_indx].wp_cval):\n", + " #Swap the two structures.\n", + " wp_tmp = ar[lo_indx]\n", + " ar[lo_indx] = ar[hi_indx]\n", + " ar[hi_indx] = wp_tmp\n", + "\n", + "\n", + "for lo_indx in range(icount+1):\n", + " print \"%s %d %d\\n\" % (ar[lo_indx].wp_cval,ar[lo_indx].wp_font,ar[lo_indx].wp_psize)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " 2 10\n", + "\n", + " 2 10\n", + "\n", + " 2 10\n", + "\n", + " 2 10\n", + "\n", + " 2 10\n", + "\n", + " 2 10\n", + "\n", + " 2 10\n", + "\n", + " 2 10\n", + "\n", + " 2 10\n", + "\n", + " 2 10\n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.3, page no. 183" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "This wont give any output.\n", + "\n", + "Note: while compiling this program. comment either of the s_1 class structure.\n", + "'''\n", + "\n", + "\n", + "class s_1: #incomplete type\n", + " pass\n", + "\n", + "class s_2:\n", + " something = 0\n", + " s = s_1()\n", + "\n", + "class s_1:\n", + " something = 0.0\n", + " s = s_2()" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.4, page no. 183" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "This wont give any output.\n", + "'''\n", + "\n", + "class x:\n", + " f = 0.0\n", + "\n", + "def func():\n", + " a = x()\n", + " a.f = 0 \n", + " return a\n", + "\n", + "\n", + "def f2():\n", + " o = func()" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.5, page no. 185" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "this wont give any output.\n", + "'''\n", + "\n", + "class list_ele:\n", + " def __init__(self,d=0,p=None):\n", + " data=d\n", + " pointer = p\n", + "ar=[]\n", + "\n", + "ar.append(list_ele())\n", + "ar.append(list_ele())\n", + "ar.append(list_ele())\n", + "ar[0].data = 5;\n", + "ar[0].pointer = ar[2]\n", + "ar[1].data = 99\n", + "ar[1].pointer = ar[2]\n", + "ar[2].data = -7\n", + "ar[2].pointer = None" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.6, page no. 185" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "class list_ele:\n", + " def __init__(self,d=0,p=None):\n", + " data=d\n", + " pointer = p\n", + "ar=[]\n", + "\n", + "ar.append(list_ele())\n", + "ar.append(list_ele())\n", + "ar.append(list_ele())\n", + "ar[0].data = 5;\n", + "ar[0].pointer = ar[2]\n", + "ar[1].data = 99\n", + "ar[1].pointer = ar[2]\n", + "ar[2].data = -7\n", + "ar[2].pointer = None\n", + "\n", + "for i in ar:\n", + " print \"contents %d\\n\" %(i.data)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "contents 5\n", + "\n", + "contents 99\n", + "\n", + "contents -7\n", + "\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.7, page no. 187" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "This wont compile as we do not have list_ele class here.\n", + "'''\n", + "\n", + "def sortfun(list ):\n", + " exchange = True\n", + " nextp = []\n", + " thisp = None\n", + " dummy = list_ele()\n", + " '''\n", + " * Algorithm is this:\n", + " * Repeatedly scan list.\n", + " * If two list items are out of order,\n", + " * link them in the other way round.\n", + " * Stop if a full pass is made and no\n", + " * exchanges are required.\n", + " * The whole business is confused by\n", + " * working one element behind the\n", + " * first one of interest.\n", + " * This is because of the simple mechanics of\n", + " * linking and unlinking elements.\n", + " */ '''\n", + " dummy.pointer = list;\n", + " while(exchange):\n", + " exchange = False\n", + " thisp=dummy\n", + " while( (nextp == thisp.pointer) and nextp.pointer):\n", + " if(nextp.data < nextp.pointer.data):\n", + " #/* exchange */\n", + " exchange = 1\n", + " thisp.pointer = nextp.pointer\n", + " nextp.pointer =thisp.pointer.pointer\n", + " thisp.pointer.pointer = nextp\n", + "\n", + " thisp = thisp.pointer;\n", + "\n", + " return(dummy.pointer);" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.8, page no. 190" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Examples 6.8 and Example 6.9\n", + "Both are same for Python as we do not have any concept of pointer.\n", + "Python will go same for both.\n", + "'''\n", + "\n", + "\n", + "class Tree:\n", + " def __init__(self, cargo, left=None, right=None):\n", + " self.cargo = cargo\n", + " self.left = left\n", + " self.right = right\n", + "\n", + " def __str__(self):\n", + " return str(self.cargo)\n", + "'''\n", + "/*\n", + "* Tree search algorithm.\n", + "* Searches for value 'v' in tree,\n", + "* returns pointer to first node found containing\n", + "* the value otherwise 0.\n", + "*/\n", + "\n", + "'''\n", + "def t_search(root,v):\n", + " while(root):\n", + " if(root.cargo == v):\n", + " return(root)\n", + " if(root.cargo > v):\n", + " root = root.left\n", + " else:\n", + " root = root.right\n", + "\n", + "\n", + "\n", + "#/* construct tree by hand */\n", + "tp = None\n", + "root_p = None\n", + "tree=[]\n", + "for i in range(7):\n", + " j = 0\n", + " j = i+1\n", + " tree.append(Tree(j))\n", + " \n", + " if(j == 2 or j == 6):\n", + " tree[i].left = tree[i-1]\n", + " tree[i-1].right = tree[i]\n", + "\n", + "\n", + "root_p = tree[3]\n", + "root_p.left = tree[1]\n", + "root_p.right = tree[5]\n", + "# /* try the search */\n", + "tp = t_search(root_p, 9)\n", + "if(tp!=None):\n", + " print \"found at position %d\\n\" %(tp-tree)\n", + "else:\n", + " print \"value not found\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "value not found\n", + "\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.10, page no. 192" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Note : this wont give any output.\n", + "'''\n", + "\n", + "\n", + "def t_walk(r):\n", + " if(root_p == 0):\n", + " return\n", + " t_walk(root_p.left)\n", + " print \"%d\\n\" %(root_p.data)\n", + " t_walk(root_p.right)" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.11, page no. 193" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "class A:\n", + " def __init__(self):\n", + " u_f = 0.0\n", + " u_i = 0\n", + "\n", + "var = A()\n", + "var.u_f = 23.5\n", + "print \"value is %f\\n\" %(var.u_f)\n", + "var.u_i = 5\n", + "print \"value is %d\\n\" %(var.u_i)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "value is 23.500000\n", + "\n", + "value is 5\n", + "\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.12, page no. 194" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "FLOAT_TYPE = 1\n", + "CHAR_TYPE = 2\n", + "INT_TYPE = 3\n", + "class var_type:\n", + " def __init__(self):\n", + " type_in_union = 0\n", + " un_float = 0.0\n", + " un_char = '0'\n", + " un_int = 0\n", + "\n", + " \n", + "\n", + "v = var_type()\n", + "def print_vt():\n", + " if(v.type_in_union == FLOAT_TYPE):\n", + " print \"%f\\n\" %(v.un_float)\n", + " else:\n", + " if(v.type_in_union == CHAR_TYPE):\n", + " print \"%s\\n\" %(v.un_char)\n", + " else:\n", + " if(v.type_in_union == INT_TYPE):\n", + " print \"%d\\n\" %(v.un_int)\n", + " else:\n", + " print \"Unknown type in union\\n\"\n", + "\n", + "\n", + "v.type_in_union = FLOAT_TYPE \n", + "v.un_float = 3.5 \n", + "print_vt() \n", + "v.type_in_union = CHAR_TYPE \n", + "v.un_char = 'a' \n", + "print_vt() " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "3.500000\n", + "\n", + "a\n", + "\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.13, page no. 195" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "This wont give any output.\n", + "'''\n", + "\n", + "\n", + "class Abc:\n", + " field1 = 4\n", + " a = 3\n", + " field2 = 1\n", + " igned = 0\n", + " field3 = 6" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.14, page no. 200" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "NMONTHS=12\n", + "month = 0\n", + "month_days =[31,28,31,30,31,30,31,31,30,31,30,31]\n", + "mnames=[\"January\", \"February\",\"March\", \"April\",\"May\", \"June\",\"July\", \"August\",\"September\", \"October\",\"November\", \"December\"]\n", + "\n", + "day_count = month;\n", + "for day_count in range(NMONTHS):\n", + " print \"%d days in %s\\n\" %(month_days[day_count],mnames[day_count])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "31 days in January\n", + "\n", + "28 days in February\n", + "\n", + "31 days in March\n", + "\n", + "30 days in April\n", + "\n", + "31 days in May\n", + "\n", + "30 days in June\n", + "\n", + "31 days in July\n", + "\n", + "31 days in August\n", + "\n", + "30 days in September\n", + "\n", + "31 days in October\n", + "\n", + "30 days in November\n", + "\n", + "31 days in December\n", + "\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.15, page no. 201" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "class s:\n", + " a = 0\n", + " b = '0'\n", + " cp = '0'\n", + " def set(self,aa,bb,c):\n", + " a = aa\n", + " b = bb\n", + " cp = c\n", + "ex_s = s()\n", + "ex_s.set(1, 'a', \"hello\")\n", + "\n", + "first = ex_s\n", + "second = s()\n", + "second.set(2, 'b', \"byebye\")\n", + "\n", + "print first\n", + "\n", + "print second" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "<__main__.s instance at 0x7f29180251b8>\n", + "<__main__.s instance at 0x7f29180252d8>\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.16, page no. 202" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "class s:\n", + " a = 0\n", + " class ss:\n", + " c = 0\n", + " d = '0'\n", + "\n", + "e = s()\n", + "e.a = 1\n", + "e.c = 2\n", + "e.d ='a'\n", + "\n", + "print e" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "<__main__.s instance at 0x7f29180253b0>\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.18, page no. 202" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "y = [[1, 3, 5],[2, 4, 6],[3, 5, 7]]\n", + "\n", + "print y" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[[1, 3, 5], [2, 4, 6], [3, 5, 7]]\n" + ] + } + ], + "prompt_number": 11 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file |