diff options
Diffstat (limited to 'Practical_C_Programming/Chapter_17_1.ipynb')
-rw-r--r-- | Practical_C_Programming/Chapter_17_1.ipynb | 128 |
1 files changed, 115 insertions, 13 deletions
diff --git a/Practical_C_Programming/Chapter_17_1.ipynb b/Practical_C_Programming/Chapter_17_1.ipynb index c9e7bf83..3d179b54 100644 --- a/Practical_C_Programming/Chapter_17_1.ipynb +++ b/Practical_C_Programming/Chapter_17_1.ipynb @@ -1,6 +1,7 @@ { "metadata": { - "name": "Chapter 17" + "name": "", + "signature": "sha256:359ba0990b58235e80b2e0192de2b1c02844fb6d8275025bc2e3f3cf33cfcdf1" }, "nbformat": 3, "nbformat_minor": 0, @@ -11,18 +12,29 @@ "cell_type": "heading", "level": 1, "metadata": {}, - "source": "Chapter 17: Advanced Pointers " + "source": [ + "Chapter 17: Advanced Pointers " + ] }, { "cell_type": "heading", "level": 3, "metadata": {}, - "source": "Example 17.1, Page number: 331" + "source": [ + "Example 17.1, Page number: 331" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# Example 17.1\n# To declare a structure\n\n\n# Structure declaration\nfrom ctypes import *\n\nclass person (Structure) :\n_fields_ = [(\"name\", c_wchar_p), (\"address\", c_wchar_p), (\"city_state_zip\", c_wchar_p), (\"age\", c_int), (\"height\", c_float)]", + "input": [ + "\n", + "# Structure declaration\n", + "from ctypes import *\n", + "\n", + "class person (Structure) :\n", + "_fields_ = [(\"name\", c_wchar_p), (\"address\", c_wchar_p), (\"city_state_zip\", c_wchar_p), (\"age\", c_int), (\"height\", c_float)]" + ], "language": "python", "metadata": {}, "outputs": [] @@ -31,19 +43,45 @@ "cell_type": "heading", "level": 3, "metadata": {}, - "source": "Example 17.2, Page number: 336" + "source": [ + "Example 17.2, Page number: 336" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# Example 17.2\n# To look for a data item in a list\n\n\n# Function declaration\ndef lookup (name) :\n lists = ['John', 'Jim', 'Jane', 'Clyde']\n result = 0\n\n for index in range (0, 4) :\n if (lists[index] == name) :\n result = 1\n break\n \n return result\n\n# Calculation and result\nname = 'Jane'\n\nif (lookup (name)) :\n print ('%s is in the list\\n' % name)\nelse :\n print ('%s is not in the list\\n' % name)", + "input": [ + "\n", + "# Function declaration\n", + "def lookup (name) :\n", + " lists = ['John', 'Jim', 'Jane', 'Clyde']\n", + " result = 0\n", + "\n", + " for index in range (0, 4) :\n", + " if (lists[index] == name) :\n", + " result = 1\n", + " break\n", + " \n", + " return result\n", + "\n", + "# Calculation and result\n", + "name = 'Jane'\n", + "\n", + "if (lookup (name)) :\n", + " print ('%s is in the list\\n' % name)\n", + "else :\n", + " print ('%s is not in the list\\n' % name)" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Jane is in the list\n\n" + "text": [ + "Jane is in the list\n", + "\n" + ] } ], "prompt_number": 2 @@ -52,19 +90,31 @@ "cell_type": "heading", "level": 3, "metadata": {}, - "source": "Example 17.3, Page number: 338" + "source": [ + "Example 17.3, Page number: 338" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# Example 17.3\n# To insert an element at an index in a list\n\n\n# Declaration and result\naList = [45, 89, 123]\n\naList.insert(1, 53)\n\nprint 'Final List : ', aList", + "input": [ + "\n", + "# Declaration and result\n", + "aList = [45, 89, 123]\n", + "\n", + "aList.insert(1, 53)\n", + "\n", + "print 'Final List : ', aList" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Final List : [45, 53, 89, 123]\n" + "text": [ + "Final List : [45, 53, 89, 123]\n" + ] } ], "prompt_number": 3 @@ -73,19 +123,71 @@ "cell_type": "heading", "level": 3, "metadata": {}, - "source": "Example 17.4, Page number: 348" + "source": [ + "Example 17.4, Page number: 348" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# Example 17.4\n# To print the contents of a binary search tree in ASCII order\n\n\n# Class declaration\nclass Node:\n def __init__(self, val):\n self.l_child = None\n self.r_child = None\n self.data = val\n\ndef binary_insert(root, node):\n if root is None:\n root = node\n else:\n if root.data > node.data:\n if root.l_child == None:\n root.l_child = node\n else:\n binary_insert(root.l_child, node)\n else:\n if root.r_child == None:\n root.r_child = node\n else:\n binary_insert(root.r_child, node)\n\ndef in_order_print(root):\n if not root:\n return\n in_order_print(root.l_child)\n print root.data\n in_order_print(root.r_child)\n\nr = Node('Lemon')\nbinary_insert(r, Node('Plum'))\nbinary_insert(r, Node('Apple'))\nbinary_insert(r, Node('Orange'))\nbinary_insert(r, Node('Pear'))\nbinary_insert(r, Node('Grape'))\n\n\n# Result\nprint \"List of words in ASCII order:\"\nin_order_print(r)", + "input": [ + "\n", + "# Class declaration\n", + "class Node:\n", + " def __init__(self, val):\n", + " self.l_child = None\n", + " self.r_child = None\n", + " self.data = val\n", + "\n", + "def binary_insert(root, node):\n", + " if root is None:\n", + " root = node\n", + " else:\n", + " if root.data > node.data:\n", + " if root.l_child == None:\n", + " root.l_child = node\n", + " else:\n", + " binary_insert(root.l_child, node)\n", + " else:\n", + " if root.r_child == None:\n", + " root.r_child = node\n", + " else:\n", + " binary_insert(root.r_child, node)\n", + "\n", + "def in_order_print(root):\n", + " if not root:\n", + " return\n", + " in_order_print(root.l_child)\n", + " print root.data\n", + " in_order_print(root.r_child)\n", + "\n", + "r = Node('Lemon')\n", + "binary_insert(r, Node('Plum'))\n", + "binary_insert(r, Node('Apple'))\n", + "binary_insert(r, Node('Orange'))\n", + "binary_insert(r, Node('Pear'))\n", + "binary_insert(r, Node('Grape'))\n", + "\n", + "\n", + "# Result\n", + "print \"List of words in ASCII order:\"\n", + "in_order_print(r)" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "List of words in ASCII order:\nApple\nGrape\nLemon\nOrange\nPear\nPlum\n" + "text": [ + "List of words in ASCII order:\n", + "Apple\n", + "Grape\n", + "Lemon\n", + "Orange\n", + "Pear\n", + "Plum\n" + ] } ], "prompt_number": 4 |