├── .ipynb_checkpoints ├── 18:3:1 Binary Search Trees-checkpoint.ipynb ├── 18:3:6 Randomized BST-checkpoint.ipynb ├── Binary Search Trees-checkpoint.ipynb ├── Counting Bloom Filter - Assignment-checkpoint.ipynb ├── Dynamic Programming - Currency trade-checkpoint.ipynb ├── Dynamic Programming - Rod cutting problem-checkpoint.ipynb ├── Hash tables and functions-checkpoint.ipynb ├── Median Heap and Quickselect-checkpoint.ipynb ├── Randomized BST-checkpoint.ipynb └── Viterbi algorithm-checkpoint.ipynb ├── 18:01:09 Algorithm-collection.ipynb ├── 18:01:18 Plotting with Matplotlib.ipynb ├── 18:01:23 Max subarray.ipynb ├── 18:01:26 K-way Mergesort - assignment.ipynb ├── 18:02:01 Hire-assistant.ipynb ├── 18:02:06 Worst sorting function: median finder.ipynb ├── 18:02:10 Expanded k-way.ipynb ├── 18:02:15 Quicksort.ipynb ├── 18:02:20 Median Heap and Quickselect.ipynb ├── 18:02:22 Hash tables and functions.ipynb ├── 18:03:01 Binary Search Trees.ipynb ├── 18:03:06 Randomized BST.ipynb ├── 18:03:11 Counting Bloom Filter - Assignment.ipynb ├── 18:03:23 Dynamic Programming - Rod cutting problem.ipynb ├── 18:03:27 Dynamic Programming - Currency trade.ipynb ├── 18:03:30 Viterbi algorithm.ipynb ├── 18:04:02 Aquarium problem.ipynb ├── 18:04:08 Genetic Heritage Assignment.ipynb └── 18:04:20 Final Project - Counting Bloom filters.ipynb /.ipynb_checkpoints/18:3:1 Binary Search Trees-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 4, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | " import random" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 11, 17 | "metadata": { 18 | "collapsed": true 19 | }, 20 | "outputs": [], 21 | "source": [ 22 | "class Node:\n", 23 | " def __init__(self, val):\n", 24 | " self.l_child = None\n", 25 | " self.r_child = None\n", 26 | " self.data = val\n", 27 | " self.parent = None\n", 28 | " \n", 29 | "def insert(root, node):\n", 30 | " if root is None:\n", 31 | " root = node\n", 32 | " else:\n", 33 | " if root.data > node.data:\n", 34 | " if root.l_child is None:\n", 35 | " root.l_child = node\n", 36 | " node.parent = root\n", 37 | " else:\n", 38 | " insert(root.l_child, node)\n", 39 | " else:\n", 40 | " if root.r_child is None:\n", 41 | " root.r_child = node\n", 42 | " node.parent = root\n", 43 | " else:\n", 44 | " insert(root.r_child, node)\n", 45 | " return root\n", 46 | " \n", 47 | "def search(root, value):\n", 48 | " if root == None or root.data == value:\n", 49 | " return root\n", 50 | " elif value < root.data:\n", 51 | " return search(root.l_child, value)\n", 52 | " else:\n", 53 | " return search(root.r_child, value)\n", 54 | "\n", 55 | "def transplant(root, u1, u2):\n", 56 | " if u1 == root:\n", 57 | " root = u2\n", 58 | " elif u1 == u1.parent.l_child:\n", 59 | " u1.parent.l_child = u2\n", 60 | " else:\n", 61 | " u1.parent.r_child = u2 \n", 62 | " if u2 != None:\n", 63 | " u2.parent = u1.parent\n", 64 | " \n", 65 | "def delete_node(root, node): \n", 66 | " if node.l_child == None:\n", 67 | " transplant(root, node, node.r_child)\n", 68 | " elif node.r_child == None:\n", 69 | " transplant(root, node, node.l_child)\n", 70 | " else:\n", 71 | " y = tree_min(node.r_child)\n", 72 | " if y.parent != node:\n", 73 | " transplant(root, y, y.r_child)\n", 74 | " y.r_child = node.r_child\n", 75 | " y.r_child.parent = y\n", 76 | " transplant(root, node, y)\n", 77 | " y.l_child = node.l_child\n", 78 | " y.l_child.parent = y\n", 79 | " \n", 80 | "def tree_min(root):\n", 81 | " while root.l_child:\n", 82 | " root = root.l_child\n", 83 | " return root\n", 84 | "\n", 85 | "def traversal(root):\n", 86 | " if root != None:\n", 87 | " traversal(root.l_child)\n", 88 | " print root.data\n", 89 | " traversal(root.r_child)" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 95, 95 | "metadata": {}, 96 | "outputs": [ 97 | { 98 | "name": "stdout", 99 | "output_type": "stream", 100 | "text": [ 101 | "Treemin 10\n", 102 | "Searching for 10 10\n", 103 | "---------------\n", 104 | "10\n", 105 | "26\n", 106 | "27\n", 107 | "30\n", 108 | "30\n", 109 | "31\n", 110 | "35\n", 111 | "35\n", 112 | "37\n", 113 | "41\n", 114 | "41\n", 115 | "43\n", 116 | "43\n", 117 | "44\n", 118 | "45\n", 119 | "51\n", 120 | "51\n", 121 | "52\n", 122 | "54\n", 123 | "57\n", 124 | "59\n", 125 | "60\n", 126 | "61\n", 127 | "63\n", 128 | "67\n", 129 | "67\n", 130 | "67\n", 131 | "68\n", 132 | "68\n", 133 | "69\n", 134 | "70\n", 135 | "71\n", 136 | "72\n", 137 | "73\n", 138 | "74\n", 139 | "78\n", 140 | "78\n", 141 | "79\n", 142 | "80\n", 143 | "82\n", 144 | "83\n", 145 | "84\n", 146 | "90\n", 147 | "91\n", 148 | "92\n", 149 | "96\n", 150 | "96\n", 151 | "99\n", 152 | "100\n", 153 | "100\n", 154 | "100\n", 155 | "Traversal None\n", 156 | "---------------\n", 157 | "deleting 10\n", 158 | "---------------\n", 159 | "10\n", 160 | "26\n", 161 | "27\n", 162 | "30\n", 163 | "30\n", 164 | "31\n", 165 | "35\n", 166 | "35\n", 167 | "37\n", 168 | "41\n", 169 | "41\n", 170 | "43\n", 171 | "43\n", 172 | "44\n", 173 | "45\n", 174 | "51\n", 175 | "51\n", 176 | "52\n", 177 | "54\n", 178 | "57\n", 179 | "59\n", 180 | "60\n", 181 | "61\n", 182 | "63\n", 183 | "67\n", 184 | "67\n", 185 | "67\n", 186 | "68\n", 187 | "68\n", 188 | "69\n", 189 | "70\n", 190 | "71\n", 191 | "72\n", 192 | "73\n", 193 | "74\n", 194 | "78\n", 195 | "78\n", 196 | "79\n", 197 | "80\n", 198 | "82\n", 199 | "83\n", 200 | "84\n", 201 | "90\n", 202 | "91\n", 203 | "92\n", 204 | "96\n", 205 | "96\n", 206 | "99\n", 207 | "100\n", 208 | "100\n", 209 | "100\n", 210 | "Traversal after deletion None\n", 211 | "---------------\n", 212 | "10\n" 213 | ] 214 | } 215 | ], 216 | "source": [ 217 | "tree = Node(10)\n", 218 | "for i in range(50):\n", 219 | " insert(tree, Node(random.randint(15,100)))\n", 220 | "\n", 221 | "print \"Treemin \" + str(tree_min(tree).data)\n", 222 | "print \"Searching for 10 \" + str(search(tree, 10).data)\n", 223 | "print \"---------------\"\n", 224 | "print \"Traversal \" + str(traversal(tree))\n", 225 | "print \"---------------\"\n", 226 | "print \"deleting 10\"\n", 227 | "delete_node(tree, search(tree,10))\n", 228 | "print \"---------------\"\n", 229 | "print \"Traversal after deletion \" + str(traversal(tree))\n", 230 | "print \"---------------\"\n", 231 | "print search(tree, 10).data" 232 | ] 233 | }, 234 | { 235 | "cell_type": "code", 236 | "execution_count": null, 237 | "metadata": { 238 | "collapsed": true 239 | }, 240 | "outputs": [], 241 | "source": [] 242 | } 243 | ], 244 | "metadata": { 245 | "kernelspec": { 246 | "display_name": "Python 2", 247 | "language": "python", 248 | "name": "python2" 249 | }, 250 | "language_info": { 251 | "codemirror_mode": { 252 | "name": "ipython", 253 | "version": 2 254 | }, 255 | "file_extension": ".py", 256 | "mimetype": "text/x-python", 257 | "name": "python", 258 | "nbconvert_exporter": "python", 259 | "pygments_lexer": "ipython2", 260 | "version": "2.7.14" 261 | } 262 | }, 263 | "nbformat": 4, 264 | "nbformat_minor": 2 265 | } 266 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/18:3:6 Randomized BST-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import random" 12 | ] 13 | }, 14 | { 15 | "cell_type": "markdown", 16 | "metadata": {}, 17 | "source": [ 18 | "## Question 1 - Randomized BST" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 4, 24 | "metadata": { 25 | "collapsed": true 26 | }, 27 | "outputs": [], 28 | "source": [ 29 | "class Node:\n", 30 | " def __init__(self, val):\n", 31 | " self.l_child = None\n", 32 | " self.r_child = None\n", 33 | " self.data = val\n", 34 | " self.parent = None\n", 35 | " self.left_of = 0\n", 36 | " \n", 37 | "def insert(root, node):\n", 38 | " if root is None:\n", 39 | " root = node\n", 40 | " else:\n", 41 | " if root.data > node.data:\n", 42 | " root.left_of += 1\n", 43 | " if root.l_child is None:\n", 44 | " root.l_child = node\n", 45 | " node.parent = root\n", 46 | " else:\n", 47 | " insert(root.l_child, node)\n", 48 | " else:\n", 49 | " if root.r_child is None:\n", 50 | " root.r_child = node\n", 51 | " node.parent = root\n", 52 | " else:\n", 53 | " insert(root.r_child, node)\n", 54 | " return root\n", 55 | " \n", 56 | "def search(root, value):\n", 57 | " if root == None or root.data == value:\n", 58 | " return root\n", 59 | " elif value < root.data:\n", 60 | " return search(root.l_child, value)\n", 61 | " else:\n", 62 | " return search(root.r_child, value)\n", 63 | "\n", 64 | "def transplant(root, u1, u2):\n", 65 | " if u1 == root:\n", 66 | " root = u2\n", 67 | " elif u1 == u1.parent.l_child:\n", 68 | " u1.parent.l_child = u2\n", 69 | " else:\n", 70 | " u1.parent.r_child = u2 \n", 71 | " if u2 != None:\n", 72 | " u2.parent = u1.parent\n", 73 | " \n", 74 | "def delete_node(root, node): \n", 75 | " if node.l_child == None:\n", 76 | " transplant(root, node, node.r_child)\n", 77 | " elif node.r_child == None:\n", 78 | " transplant(root, node, node.l_child)\n", 79 | " else:\n", 80 | " y = tree_min(node.r_child)\n", 81 | " if y.parent != node:\n", 82 | " transplant(root, y, y.r_child)\n", 83 | " y.r_child = node.r_child\n", 84 | " y.r_child.parent = y\n", 85 | " transplant(root, node, y)\n", 86 | " y.l_child = node.l_child\n", 87 | " y.l_child.parent = y\n", 88 | " \n", 89 | "def tree_min(root):\n", 90 | " while root.l_child:\n", 91 | " root = root.l_child\n", 92 | " return root\n", 93 | "\n", 94 | "def traversal(root):\n", 95 | " if root != None:\n", 96 | " traversal(root.l_child)\n", 97 | " print root.data\n", 98 | " traversal(root.r_child)" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 2, 104 | "metadata": {}, 105 | "outputs": [ 106 | { 107 | "ename": "NameError", 108 | "evalue": "name 'random' is not defined", 109 | "output_type": "error", 110 | "traceback": [ 111 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 112 | "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", 113 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m901\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m \u001b[0minsert\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnonorder_tree\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mNode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrandom\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrandint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m901\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 9\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", 114 | "\u001b[0;31mNameError\u001b[0m: name 'random' is not defined" 115 | ] 116 | } 117 | ], 118 | "source": [ 119 | "order_tree = Node(0)\n", 120 | "nonorder_tree = Node(0)\n", 121 | "counter = 0\n", 122 | "for i in range(1,901):\n", 123 | " insert(order_tree, Node(i))\n", 124 | " \n", 125 | "for i in range(1,901):\n", 126 | " insert(nonorder_tree, Node(random.randint(1,901)))\n", 127 | " \n", 128 | "\n", 129 | "\n", 130 | "print \"Treemin \" + str(tree_min(order_tree).data)\n", 131 | "print \"Searching for 10 \" + str(search(order_tree, 10).data)\n", 132 | "print \"---------------\"\n", 133 | "# print \"Traversal \" + str(traversal(tree))" 134 | ] 135 | }, 136 | { 137 | "cell_type": "markdown", 138 | "metadata": {}, 139 | "source": [ 140 | "## Question 2 - Average number of comparisons" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 88, 146 | "metadata": { 147 | "collapsed": true 148 | }, 149 | "outputs": [], 150 | "source": [ 151 | "def search_avg_comparisons(root, value, counter): \n", 152 | " if root == None or root.data == value:\n", 153 | " counter += 1\n", 154 | " return counter\n", 155 | " elif value < root.data:\n", 156 | " counter += 1\n", 157 | " return search_avg_comparisons(root.l_child, value, counter)\n", 158 | " else:\n", 159 | " counter += 1\n", 160 | " return search_avg_comparisons(root.r_child, value, counter)" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": 90, 166 | "metadata": {}, 167 | "outputs": [ 168 | { 169 | "name": "stdout", 170 | "output_type": "stream", 171 | "text": [ 172 | "Calculating average number of comparisons for tree: \n", 173 | "Average num of comparisons: \n", 174 | "12.615\n", 175 | "This is something around the order of log_2(n).\n" 176 | ] 177 | } 178 | ], 179 | "source": [ 180 | "print \"Calculating average number of comparisons for tree: \"\n", 181 | "avg_counter = 0\n", 182 | "for i in range(200):\n", 183 | " search_item = random.randint(0,901)\n", 184 | " avg_counter = search_avg_comparisons(nonorder_tree, search_item, avg_counter)\n", 185 | "print \"Average num of comparisons: \"\n", 186 | "print avg_counter/200.0\n", 187 | "print \"This is something around the order of log_2(n).\"" 188 | ] 189 | }, 190 | { 191 | "cell_type": "markdown", 192 | "metadata": {}, 193 | "source": [ 194 | "## Question 3 - Max and average tree height" 195 | ] 196 | }, 197 | { 198 | "cell_type": "code", 199 | "execution_count": 91, 200 | "metadata": { 201 | "collapsed": true 202 | }, 203 | "outputs": [], 204 | "source": [ 205 | "def max_height(bst):\n", 206 | " if bst is None:\n", 207 | " return 0\n", 208 | " return 1+max(max_height(bst.l_child), max_height(bst.r_child))\n", 209 | "\n", 210 | "\n", 211 | "def min_height(bst):\n", 212 | " if bst is None:\n", 213 | " return 0\n", 214 | " return 1+ min(min_height(bst.l_child), min_height(bst.r_child)) \n", 215 | "\n", 216 | "\n", 217 | "def avg_height(root, trials): \n", 218 | " direction = [\"left\", \"right\"]\n", 219 | " height = 0\n", 220 | "\n", 221 | " for i in range(trials):\n", 222 | " temp_root = root\n", 223 | " while temp_root:\n", 224 | " dir_pick = direction[random.randint(0, 1)]\n", 225 | " if dir_pick == \"right\" and temp_root.r_child != None:\n", 226 | " temp_root = temp_root.r_child\n", 227 | " height += 1\n", 228 | " elif dir_pick == \"left\" and temp_root.l_child != None:\n", 229 | " temp_root = temp_root.l_child\n", 230 | " height += 1\n", 231 | " else:\n", 232 | " temp_root = None\n", 233 | "\n", 234 | " return float(height)/trials" 235 | ] 236 | }, 237 | { 238 | "cell_type": "code", 239 | "execution_count": 94, 240 | "metadata": {}, 241 | "outputs": [ 242 | { 243 | "name": "stdout", 244 | "output_type": "stream", 245 | "text": [ 246 | "Calculating max height of tree: \n", 247 | "Max height order_tree: 900\n", 248 | "Max height nonorder_tree: 10\n", 249 | "Calculating average height of tree out of 50 trials: \n", 250 | "Average height order_tree: 1.132\n", 251 | "Average height nonorder_tree: 3.9\n" 252 | ] 253 | } 254 | ], 255 | "source": [ 256 | "print \"Calculating max height of tree: \"\n", 257 | "max_order = 0\n", 258 | "max_order = max_height(order_tree)\n", 259 | "max_nonorder = 0\n", 260 | "max_nonorder = max_height(nonorder_tree)\n", 261 | "\n", 262 | "print \"Max height order_tree: \" + str(max_order)\n", 263 | "print \"Max height nonorder_tree: \" + str(max_nonorder)\n", 264 | "\n", 265 | "print \"Calculating average height of tree out of 50 trials: \"\n", 266 | "trials = 500\n", 267 | "avg_order = 0\n", 268 | "avg_order = avg_height(order_tree, trials)\n", 269 | "avg_nonorder = 0\n", 270 | "avg_nonorder = avg_height(nonorder_tree, trials)\n", 271 | "\n", 272 | "print \"Average height order_tree: \" + str(avg_order)\n", 273 | "print \"Average height nonorder_tree: \" + str(avg_nonorder)" 274 | ] 275 | }, 276 | { 277 | "cell_type": "markdown", 278 | "metadata": {}, 279 | "source": [ 280 | "## Question 4 - Select/Rank " 281 | ] 282 | }, 283 | { 284 | "cell_type": "code", 285 | "execution_count": 17, 286 | "metadata": { 287 | "collapsed": true 288 | }, 289 | "outputs": [], 290 | "source": [ 291 | "def select(root, k):\n", 292 | " if root.left_of > k:\n", 293 | " return select(root.l_child, k)\n", 294 | " elif root.left_of < k:\n", 295 | " return select(root.r_child, k - root.left_of - 1)\n", 296 | " else:\n", 297 | " return root.data\n", 298 | " \n", 299 | "def rank(root, value):\n", 300 | " if root is None:\n", 301 | " raise ValueError\n", 302 | " if root.data == value:\n", 303 | " return root.left_of\n", 304 | " if root.data < value:\n", 305 | " return rank(root.r_child, value) + 1 + root.left_of\n", 306 | " if root.data > val:\n", 307 | " return rank(root.l_child, value)" 308 | ] 309 | }, 310 | { 311 | "cell_type": "code", 312 | "execution_count": 18, 313 | "metadata": {}, 314 | "outputs": [ 315 | { 316 | "name": "stdout", 317 | "output_type": "stream", 318 | "text": [ 319 | "63\n", 320 | "9\n", 321 | "10\n", 322 | "27\n", 323 | "33\n", 324 | "34\n", 325 | "37\n", 326 | "42\n", 327 | "47\n", 328 | "50\n", 329 | "60\n", 330 | "63\n", 331 | "67\n", 332 | "79\n", 333 | "89\n", 334 | "91\n", 335 | "92\n", 336 | "96\n", 337 | "None\n" 338 | ] 339 | } 340 | ], 341 | "source": [ 342 | "tree_new = Node(63)\n", 343 | "for i in range(15):\n", 344 | " insert(tree_new, Node(random.randint(0,100)))\n", 345 | "\n", 346 | "print tree_new.data\n", 347 | "print(tree_new.left_of)\n", 348 | "print traversal(tree_new)" 349 | ] 350 | }, 351 | { 352 | "cell_type": "code", 353 | "execution_count": 19, 354 | "metadata": {}, 355 | "outputs": [ 356 | { 357 | "name": "stdout", 358 | "output_type": "stream", 359 | "text": [ 360 | "67\n", 361 | "10\n" 362 | ] 363 | } 364 | ], 365 | "source": [ 366 | "print select(tree_new, 10)\n", 367 | "print rank(tree_new, select(tree_new, 10))" 368 | ] 369 | } 370 | ], 371 | "metadata": { 372 | "kernelspec": { 373 | "display_name": "Python 2", 374 | "language": "python", 375 | "name": "python2" 376 | }, 377 | "language_info": { 378 | "codemirror_mode": { 379 | "name": "ipython", 380 | "version": 2 381 | }, 382 | "file_extension": ".py", 383 | "mimetype": "text/x-python", 384 | "name": "python", 385 | "nbconvert_exporter": "python", 386 | "pygments_lexer": "ipython2", 387 | "version": "2.7.14" 388 | } 389 | }, 390 | "nbformat": 4, 391 | "nbformat_minor": 2 392 | } 393 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Binary Search Trees-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | " import random" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 40, 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "class Node:\n", 21 | " def __init__(self, val):\n", 22 | " self.l_child = None\n", 23 | " self.r_child = None\n", 24 | " self.data = val\n", 25 | " self.parent = None\n", 26 | " \n", 27 | "def insert(root, node):\n", 28 | " if root is None:\n", 29 | " root = node\n", 30 | " else:\n", 31 | " if root.data > node.data:\n", 32 | " if root.l_child is None:\n", 33 | " root.l_child = node\n", 34 | " node.parent = root\n", 35 | " else:\n", 36 | " insert(root.l_child, node)\n", 37 | " else:\n", 38 | " if root.r_child is None:\n", 39 | " root.r_child = node\n", 40 | " node.parent = root\n", 41 | " else:\n", 42 | " insert(root.r_child, node)\n", 43 | " return root\n", 44 | " \n", 45 | "def search(root, value):\n", 46 | " if root == None or root.data == value:\n", 47 | " return root\n", 48 | " elif value < root.data:\n", 49 | " return search(root.l_child, value)\n", 50 | " else:\n", 51 | " return search(root.r_child, value)\n", 52 | "\n", 53 | "def transplant(root, u1, u2):\n", 54 | " if u1 == root:\n", 55 | " root = u2\n", 56 | " elif u1 == u1.parent.l_child:\n", 57 | " u1.parent.l_child = u2\n", 58 | " else:\n", 59 | " u1.parent.r_child = u2 \n", 60 | " if u2 != None:\n", 61 | " u2.parent = u1.parent\n", 62 | " \n", 63 | "def delete_node(root, node): \n", 64 | " if node.l_child == None:\n", 65 | " transplant(root, node, node.r_child)\n", 66 | " elif node.r_child == None:\n", 67 | " transplant(root, node, node.l_child)\n", 68 | " else:\n", 69 | " y = tree_min(node.r_child)\n", 70 | " if y.parent != node:\n", 71 | " transplant(root, y, y.r_child)\n", 72 | " y.r_child = node.r_child\n", 73 | " y.r_child.parent = y\n", 74 | " transplant(root, node, y)\n", 75 | " y.l_child = node.l_child\n", 76 | " y.l_child.parent = y\n", 77 | " \n", 78 | "def tree_min(root):\n", 79 | " while root.l_child:\n", 80 | " root = root.l_child\n", 81 | " return root\n", 82 | "\n", 83 | "def traversal(root):\n", 84 | " if root != None:\n", 85 | " traversal(root.l_child)\n", 86 | " print root.data\n", 87 | " traversal(root.r_child)" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": 41, 93 | "metadata": {}, 94 | "outputs": [ 95 | { 96 | "name": "stdout", 97 | "output_type": "stream", 98 | "text": [ 99 | "Treemin 10\n", 100 | "Searching for 10 10\n", 101 | "---------------\n", 102 | "10\n", 103 | "15\n", 104 | "15\n", 105 | "16\n", 106 | "17\n", 107 | "19\n", 108 | "19\n", 109 | "25\n", 110 | "26\n", 111 | "27\n", 112 | "28\n", 113 | "30\n", 114 | "32\n", 115 | "34\n", 116 | "35\n", 117 | "37\n", 118 | "40\n", 119 | "45\n", 120 | "45\n", 121 | "49\n", 122 | "51\n", 123 | "52\n", 124 | "54\n", 125 | "56\n", 126 | "58\n", 127 | "59\n", 128 | "62\n", 129 | "64\n", 130 | "65\n", 131 | "65\n", 132 | "65\n", 133 | "68\n", 134 | "69\n", 135 | "72\n", 136 | "73\n", 137 | "77\n", 138 | "79\n", 139 | "80\n", 140 | "82\n", 141 | "83\n", 142 | "84\n", 143 | "86\n", 144 | "87\n", 145 | "89\n", 146 | "89\n", 147 | "90\n", 148 | "90\n", 149 | "93\n", 150 | "93\n", 151 | "99\n", 152 | "100\n", 153 | "Traversal None\n", 154 | "---------------\n", 155 | "deleting 10\n", 156 | "---------------\n", 157 | "10\n", 158 | "15\n", 159 | "15\n", 160 | "16\n", 161 | "17\n", 162 | "19\n", 163 | "19\n", 164 | "25\n", 165 | "26\n", 166 | "27\n", 167 | "28\n", 168 | "30\n", 169 | "32\n", 170 | "34\n", 171 | "35\n", 172 | "37\n", 173 | "40\n", 174 | "45\n", 175 | "45\n", 176 | "49\n", 177 | "51\n", 178 | "52\n", 179 | "54\n", 180 | "56\n", 181 | "58\n", 182 | "59\n", 183 | "62\n", 184 | "64\n", 185 | "65\n", 186 | "65\n", 187 | "65\n", 188 | "68\n", 189 | "69\n", 190 | "72\n", 191 | "73\n", 192 | "77\n", 193 | "79\n", 194 | "80\n", 195 | "82\n", 196 | "83\n", 197 | "84\n", 198 | "86\n", 199 | "87\n", 200 | "89\n", 201 | "89\n", 202 | "90\n", 203 | "90\n", 204 | "93\n", 205 | "93\n", 206 | "99\n", 207 | "100\n", 208 | "Traversal after deletion None\n", 209 | "---------------\n", 210 | "10\n" 211 | ] 212 | } 213 | ], 214 | "source": [ 215 | "tree = Node(10)\n", 216 | "for i in range(50):\n", 217 | " insert(tree, Node(random.randint(15,100)))\n", 218 | "\n", 219 | "print \"Treemin \" + str(tree_min(tree).data)\n", 220 | "print \"Searching for 10 \" + str(search(tree, 10).data)\n", 221 | "print \"---------------\"\n", 222 | "print \"Traversal \" + str(traversal(tree))\n", 223 | "print \"---------------\"\n", 224 | "print \"deleting 10\"\n", 225 | "delete_node(tree, search(tree,10))\n", 226 | "print \"---------------\"\n", 227 | "print \"Traversal after deletion \" + str(traversal(tree))\n", 228 | "print \"---------------\"\n", 229 | "print search(tree, 10).data" 230 | ] 231 | }, 232 | { 233 | "cell_type": "code", 234 | "execution_count": null, 235 | "metadata": { 236 | "collapsed": true 237 | }, 238 | "outputs": [], 239 | "source": [] 240 | } 241 | ], 242 | "metadata": { 243 | "kernelspec": { 244 | "display_name": "Python 2", 245 | "language": "python", 246 | "name": "python2" 247 | }, 248 | "language_info": { 249 | "codemirror_mode": { 250 | "name": "ipython", 251 | "version": 2 252 | }, 253 | "file_extension": ".py", 254 | "mimetype": "text/x-python", 255 | "name": "python", 256 | "nbconvert_exporter": "python", 257 | "pygments_lexer": "ipython2", 258 | "version": "2.7.14" 259 | } 260 | }, 261 | "nbformat": 4, 262 | "nbformat_minor": 2 263 | } 264 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Dynamic Programming - Currency trade-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Currency Trading" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Imagine that you wish to exchange one currency for another. You realize that instead of directly exchanging one currency for another, you might be better off making a series of trades through other currencies, winding up with the currency you want. Suppose that you can trade n different currencies, numbered 1,2,… ,*n*, where you start with currency 1 and wish to wind up with currency n. You are given, for each pair of currencies *i* and *j* , an exchange rate *rij* , meaning that if you start with *d* units of currency *i* , you can trade for *drij* units of currency *j*.\n", 15 | "\n", 16 | "1. Assuming there is no commission, write python code to solve this problem.\n", 17 | "1. Look up the exchange rates for 3-4 currencies online. What solution does your code find?\n", 18 | "\n", 19 | "Here is some example data:\n", 20 | "\n", 21 | "`USD 1 0.741 0.657 1.061 1.00`\n", 22 | "\n", 23 | "`EUR 1.349 1 0.888 1.433 1.366`\n", 24 | "\n", 25 | "`GBP 1.521 1.126 1 1.614 1.538`\n", 26 | "\n", 27 | "`CHF 0.942 0.698 0.619 1 0.953`\n", 28 | "\n", 29 | "`CAD 0.995 0.732 0.650 1.049 1`" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 72, 35 | "metadata": { 36 | "collapsed": true 37 | }, 38 | "outputs": [], 39 | "source": [ 40 | "# Python program for Bellman-Ford's single source \n", 41 | "# shortest path algorithm.\n", 42 | " \n", 43 | "from collections import defaultdict\n", 44 | " \n", 45 | "#Class to represent a graph\n", 46 | "class Graph:\n", 47 | " \n", 48 | " def __init__(self,vertices):\n", 49 | " self.V= vertices #No. of vertices\n", 50 | " self.graph = [] # default dictionary to store graph\n", 51 | " \n", 52 | " # function to add an edge to graph\n", 53 | " def addEdge(self,u,v,w):\n", 54 | " self.graph.append([u, v, w])\n", 55 | " \n", 56 | " # utility function used to print the solution\n", 57 | " def printArr(self, dist):\n", 58 | " print(\"Vertex Distance from Source\")\n", 59 | " for i in range(self.V):\n", 60 | " print(\"%f \\t\\t %f\" % (i, dist[i]))\n", 61 | " \n", 62 | " # The main function that finds shortest distances from src to\n", 63 | " # all other vertices using Bellman-Ford algorithm. The function\n", 64 | " # also detects negative weight cycle\n", 65 | " def BellmanFord(self, src):\n", 66 | " \n", 67 | " # Step 1: Initialize distances from src to all other vertices\n", 68 | " # as INFINITE\n", 69 | " dist = [float(\"Inf\")] * self.V\n", 70 | " dist[src] = 0\n", 71 | " \n", 72 | " \n", 73 | " # Step 2: Relax all edges |V| - 1 times. A simple shortest \n", 74 | " # path from src to any other vertex can have at-most |V| - 1 \n", 75 | " # edges\n", 76 | " for i in range(self.V - 1):\n", 77 | " # Update dist value and parent index of the adjacent vertices of\n", 78 | " # the picked vertex. Consider only those vertices which are still in\n", 79 | " # queue\n", 80 | " for u, v, w in self.graph:\n", 81 | " if dist[u] != float(\"Inf\") and dist[u] * float(w) < dist[v]:\n", 82 | " dist[v] = dist[u] * float(w)\n", 83 | " \n", 84 | " # Step 3: check for negative-weight cycles. The above step \n", 85 | " # guarantees shortest distances if graph doesn't contain \n", 86 | " # negative weight cycle. If we get a shorter path, then there\n", 87 | " # is a cycle.\n", 88 | " \n", 89 | " for u, v, w in self.graph:\n", 90 | " if dist[u] != float(\"Inf\") and dist[u] * float(w) < dist[v]:\n", 91 | " print \"Graph contains negative weight cycle\"\n", 92 | " return\n", 93 | " \n", 94 | " # print all distance\n", 95 | " self.printArr(dist)" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": 73, 101 | "metadata": {}, 102 | "outputs": [ 103 | { 104 | "name": "stdout", 105 | "output_type": "stream", 106 | "text": [ 107 | "Vertex Distance from Source\n", 108 | "0.000000 \t\t 0.000000\n", 109 | "1.000000 \t\t 0.000000\n", 110 | "2.000000 \t\t 0.000000\n" 111 | ] 112 | } 113 | ], 114 | "source": [ 115 | "peter = Graph(3)\n", 116 | "\"\"\"\n", 117 | "0 = USD\n", 118 | "1 = EUR\n", 119 | "2 = GBP\n", 120 | "\"\"\"\n", 121 | "\n", 122 | "peter.addEdge(0, 1, 0.741)\n", 123 | "peter.addEdge(0, 2, 0.657)\n", 124 | "peter.addEdge(1, 0, 1.349)\n", 125 | "peter.addEdge(1, 2, 0.888)\n", 126 | "peter.addEdge(2, 0, 1.521)\n", 127 | "peter.addEdge(2, 1, 1.126)\n", 128 | "\n", 129 | "peter.BellmanFord(0)\n" 130 | ] 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "metadata": {}, 135 | "source": [ 136 | "## Optimal Strategy for the Money Game" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": {}, 142 | "source": [ 143 | "Consider a row of n coins of values v(1) ... v(n), where n is even. We play a game against an opponent by alternating turns. In each turn, a player selects either the first or last coin from the row, removes it from the row permanently, and receives the value of the coin. Determine the maximum possible amount of money we can definitely win if we move first.\n", 144 | "\n", 145 | "For example, the game:\n", 146 | "\n", 147 | " $2, $10, $1, $5\n", 148 | "\n", 149 | "By moving first and playing optimally one can be guaranteed of 15. The first move is to take 5. This forces your opponent to take either 2 or 1, and then allows you to take 10." 150 | ] 151 | }, 152 | { 153 | "cell_type": "markdown", 154 | "metadata": {}, 155 | "source": [ 156 | "#### Possibility 1: Opponent picks randomly" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 53, 162 | "metadata": {}, 163 | "outputs": [], 164 | "source": [ 165 | "import random\n", 166 | "\n", 167 | "def money_game(prices):\n", 168 | " \"\"\"\n", 169 | " Assumes either that my opponent picks randomly from the\n", 170 | " front or back of the line, not trying to maximize\n", 171 | " his gains or has the same maximization strategy as I do.\n", 172 | " Assumes an even number of prices.\n", 173 | " \"\"\"\n", 174 | " p1_price, p2_price = 0, 0\n", 175 | " for i in range(len(prices)/2):\n", 176 | " prices, p1_price = player_1_pick(prices, p1_price)\n", 177 | " \n", 178 | " # Player 2 can either pick randomly or max.\n", 179 | "# prices, p2_price = player_2_pick_random(prices, p2_price)\n", 180 | " prices, p2_price = player_2_pick_max(prices, p2_price)\n", 181 | "\n", 182 | " print \"Player 1 wins %d Dollars!\" %p1_price\n", 183 | " print \"Player 2 wins %d Dollars!\" %p2_price\n", 184 | " \n", 185 | " \n", 186 | "def player_1_pick(array, money):\n", 187 | " if array[0] >= array[-1]:\n", 188 | " money += array.pop(0)\n", 189 | " else: money += array.pop(-1)\n", 190 | " return array, money\n", 191 | "\n", 192 | "def player_2_pick_max(array, money):\n", 193 | " array, money = player_1_pick(array, money)\n", 194 | " return array, money\n", 195 | " \n", 196 | "def player_2_pick_random(array, money):\n", 197 | " money += array.pop(random.choice([0, -1]))\n", 198 | " return array, money" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": 54, 204 | "metadata": {}, 205 | "outputs": [ 206 | { 207 | "name": "stdout", 208 | "output_type": "stream", 209 | "text": [ 210 | "Player 1 wins 9 Dollars!\n", 211 | "Player 2 wins 16 Dollars!\n" 212 | ] 213 | } 214 | ], 215 | "source": [ 216 | "prices = [2, 10, 1, 5, 6, 1]\n", 217 | "money_game(prices)" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": null, 223 | "metadata": { 224 | "collapsed": true 225 | }, 226 | "outputs": [], 227 | "source": [] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "execution_count": null, 232 | "metadata": { 233 | "collapsed": true 234 | }, 235 | "outputs": [], 236 | "source": [] 237 | } 238 | ], 239 | "metadata": { 240 | "kernelspec": { 241 | "display_name": "Python 2", 242 | "language": "python", 243 | "name": "python2" 244 | }, 245 | "language_info": { 246 | "codemirror_mode": { 247 | "name": "ipython", 248 | "version": 2 249 | }, 250 | "file_extension": ".py", 251 | "mimetype": "text/x-python", 252 | "name": "python", 253 | "nbconvert_exporter": "python", 254 | "pygments_lexer": "ipython2", 255 | "version": "2.7.14" 256 | } 257 | }, 258 | "nbformat": 4, 259 | "nbformat_minor": 2 260 | } 261 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Dynamic Programming - Rod cutting problem-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import timeit\n", 12 | "import matplotlib.pyplot as plt" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 2, 18 | "metadata": { 19 | "collapsed": true 20 | }, 21 | "outputs": [], 22 | "source": [ 23 | "def cut_rod(price_list, rod_len):\n", 24 | "\t\"\"\"\n", 25 | "\tStandard rod cutting algorithm without dynamic programming.\n", 26 | "\t\"\"\"\n", 27 | "\tif rod_len == 0:\n", 28 | "\t\treturn 0\n", 29 | "\tcurrent_price = -10\n", 30 | "\tfor i in range(1, rod_len+1):\n", 31 | "\t\tcurrent_price = max(current_price, price_list[i] + cut_rod(price_list, rod_len - 1))\n", 32 | "\treturn current_price" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 3, 38 | "metadata": {}, 39 | "outputs": [ 40 | { 41 | "ename": "KeyboardInterrupt", 42 | "evalue": "", 43 | "output_type": "error", 44 | "traceback": [ 45 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 46 | "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", 47 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0mtemp_avg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0mstart\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtimeit\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtimeit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 11\u001b[0;31m \u001b[0m_\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcut_rod\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprice_list\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 12\u001b[0m \u001b[0mend\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtimeit\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtimeit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0mtime_stamp\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mend\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0mstart\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 48 | "\u001b[0;32m\u001b[0m in \u001b[0;36mcut_rod\u001b[0;34m(price_list, rod_len)\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcurrent_price\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mprice_list\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mcut_rod\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprice_list\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 10\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mcurrent_price\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 49 | "\u001b[0;32m\u001b[0m in \u001b[0;36mcut_rod\u001b[0;34m(price_list, rod_len)\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcurrent_price\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mprice_list\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mcut_rod\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprice_list\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 10\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mcurrent_price\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 50 | "\u001b[0;32m\u001b[0m in \u001b[0;36mcut_rod\u001b[0;34m(price_list, rod_len)\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcurrent_price\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mprice_list\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mcut_rod\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprice_list\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 10\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mcurrent_price\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 51 | "\u001b[0;32m\u001b[0m in \u001b[0;36mcut_rod\u001b[0;34m(price_list, rod_len)\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcurrent_price\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mprice_list\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mcut_rod\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprice_list\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 10\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mcurrent_price\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 52 | "\u001b[0;32m\u001b[0m in \u001b[0;36mcut_rod\u001b[0;34m(price_list, rod_len)\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcurrent_price\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mprice_list\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mcut_rod\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprice_list\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 10\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mcurrent_price\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 53 | "\u001b[0;32m\u001b[0m in \u001b[0;36mcut_rod\u001b[0;34m(price_list, rod_len)\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcurrent_price\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mprice_list\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mcut_rod\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprice_list\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 10\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mcurrent_price\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 54 | "\u001b[0;32m\u001b[0m in \u001b[0;36mcut_rod\u001b[0;34m(price_list, rod_len)\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcurrent_price\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mprice_list\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mcut_rod\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprice_list\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 10\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mcurrent_price\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 55 | "\u001b[0;32m\u001b[0m in \u001b[0;36mcut_rod\u001b[0;34m(price_list, rod_len)\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcurrent_price\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mprice_list\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mcut_rod\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprice_list\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 10\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mcurrent_price\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 56 | "\u001b[0;32m\u001b[0m in \u001b[0;36mcut_rod\u001b[0;34m(price_list, rod_len)\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcurrent_price\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mprice_list\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mcut_rod\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprice_list\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 10\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mcurrent_price\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 57 | "\u001b[0;32m\u001b[0m in \u001b[0;36mcut_rod\u001b[0;34m(price_list, rod_len)\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mStandard\u001b[0m \u001b[0mrod\u001b[0m \u001b[0mcutting\u001b[0m \u001b[0malgorithm\u001b[0m \u001b[0mwithout\u001b[0m \u001b[0mdynamic\u001b[0m \u001b[0mprogramming\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \t\"\"\"\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0mrod_len\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 58 | "\u001b[0;31mKeyboardInterrupt\u001b[0m: " 59 | ] 60 | } 61 | ], 62 | "source": [ 63 | "time = []\n", 64 | "n = range(11)\n", 65 | "price_list = [0, 1, 5, 8, 9, 10, 17, 17, 20, 24, 30, 33, 37, 38, 38, 41,\n", 66 | "\t\t\t 45, 47, 51, 62, 66]\n", 67 | "\n", 68 | "\n", 69 | "for i in range(11):\n", 70 | "\tfor j in range(10):\n", 71 | "\t\ttemp_avg = 0\n", 72 | "\t\tstart = timeit.timeit()\n", 73 | "\t\t_ = cut_rod(price_list, i)\n", 74 | "\t\tend = timeit.timeit()\n", 75 | "\t\ttime_stamp = end - start\n", 76 | "\t\t# print \"Starting point: \" + str(start)\n", 77 | "\t\t# print \"End point: \" + str(end)\n", 78 | "\t\ttemp_avg += time_stamp\n", 79 | "\ttime.append(temp_avg / 10)\n", 80 | "\n", 81 | "print n, time\n", 82 | "plt.plot(n, time)\n", 83 | "plt.show()" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": null, 89 | "metadata": { 90 | "collapsed": true 91 | }, 92 | "outputs": [], 93 | "source": [ 94 | "def bottom_up_cut_rod_with_solution(price_list, rod_len):\n", 95 | "\t\"\"\"\n", 96 | "\tStandard rod cutting algorithm without dynamic programming \n", 97 | "\tnow with the ability to print solutions.\n", 98 | "\t\"\"\"\n", 99 | "\tmax_rev, opt_size = [], range(rod_len)\n", 100 | "\tmax_rev.append(0)\n", 101 | "\tfor i in range(rod_len):\n", 102 | "\t\tcurrent_price = -10\n", 103 | "\t\tfor j in range(rod_len):\n", 104 | "\t\t\ttemp_price = price_list[i] + max_rev[i-j]\n", 105 | "\t\t\tif current_price < temp_price:\n", 106 | "\t\t\t\tcurrent_price = temp_price\n", 107 | "\t\t\t\topt_size[i] = j\n", 108 | "\t\tmax_rev[i] = current_price\n", 109 | "\treturn max_rev, opt_size\n", 110 | "\n", 111 | "def print_cut_rod(price_list, rod_len):\n", 112 | "\tmax_rev, opt_size = bottom_up_cut_rod_with_solution(price_list, rod_len)\n", 113 | "\twhile rod_len > 0:\n", 114 | "\t\tprint opt_size[rod_len]\n", 115 | "\t\trod_len = rod_len - opt_size[n]" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": null, 121 | "metadata": { 122 | "collapsed": true 123 | }, 124 | "outputs": [], 125 | "source": [ 126 | "def fib_memoized(n):\n", 127 | " if n < 2:\n", 128 | " r = [-float(\"inf\"),1,1]\n", 129 | " else:\n", 130 | " r = [-float(\"inf\")] * (n+1)\n", 131 | " r[1] = 1\n", 132 | " r[2] = 1\n", 133 | " return fib_memoized_aux(n,r)\n", 134 | "\n", 135 | "def fib_memoized_aux(n,r):\n", 136 | " if r[n] >= 0:\n", 137 | " return r[n]\n", 138 | " else:\n", 139 | " temp = fib_memoized_aux(n-2,r) + fib_memoized_aux(n-1,r)\n", 140 | " r[n] = temp\n", 141 | " return r[n]" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": null, 147 | "metadata": { 148 | "collapsed": true 149 | }, 150 | "outputs": [], 151 | "source": [ 152 | "def fibonacci_bottom_up(n):\n", 153 | "\tfibs = []\n", 154 | "\tfibs.append(0)\n", 155 | "\tfibs.append(1)\n", 156 | "\tfor i in range (2, n+1):\n", 157 | "\t\tfibs.append(fibs[i-1] + fibs[i-2])\n", 158 | "\treturn fibs[n]\n", 159 | "\n", 160 | "print fib_memoized(10)\n", 161 | "print fibonacci_bottom_up(10)" 162 | ] 163 | } 164 | ], 165 | "metadata": { 166 | "kernelspec": { 167 | "display_name": "Python 2", 168 | "language": "python", 169 | "name": "python2" 170 | }, 171 | "language_info": { 172 | "codemirror_mode": { 173 | "name": "ipython", 174 | "version": 2 175 | }, 176 | "file_extension": ".py", 177 | "mimetype": "text/x-python", 178 | "name": "python", 179 | "nbconvert_exporter": "python", 180 | "pygments_lexer": "ipython2", 181 | "version": "2.7.14" 182 | } 183 | }, 184 | "nbformat": 4, 185 | "nbformat_minor": 2 186 | } 187 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Hash tables and functions-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import string\n", 10 | "import random\n", 11 | "import numpy\n", 12 | "import time\n", 13 | "\n", 14 | "\n", 15 | "def da_search(T,k):\n", 16 | " return T[k]\n", 17 | "\n", 18 | "def da_insert(T,x):\n", 19 | " # T[key[x]] = x\n", 20 | " T[x] = x\n", 21 | " \n", 22 | "def da_delete(T,x):\n", 23 | " T[x] = None\n", 24 | " \n", 25 | "def word_generator(array, arr_len, size = 4, chars=string.ascii_lowercase):\n", 26 | " for i in range(0,arr_len-1):\n", 27 | " array[i] = ''.join(random.choice(chars) for _ in range(size))\n", 28 | " return array\n", 29 | "\n", 30 | "def empty_guesses(n):\n", 31 | " array = numpy.zeros(shape = (n,1))\n", 32 | " return array" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": {}, 38 | "source": [ 39 | "**Direct Address Tables:**\\ As the first step in setting up a crossword solving algorithm you need to create 2 direct address tables, one to store all \\ the “up” answers-whether correct or not-and one to store all the “across” answers. Write some code to create a direct address table that allows you:\n", 40 | "\n", 41 | "1. initialize N empty guesses\n", 42 | "1. set a guess for the i-th entry\n", 43 | "1. clear an incorrect guess for the i-th entry\n", 44 | "\n", 45 | "**Social Security:**\n", 46 | "\n", 47 | "Could we use a direct address table to store a country's entire set of social\\ security numbers (aka id numbers)? Why or why not?" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": null, 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [ 56 | "up_answers = empty_guesses(5)\n", 57 | "across_answers = empty_guesses(5)\n", 58 | "\n", 59 | "up_answers = word_generator(up_answers, 5)\n", 60 | "across_answers = word_generator(across_answers, 5)\n", 61 | "\n", 62 | "up_guess = \"helo\"\n", 63 | "def check_guess(table, i, guess):\n", 64 | " if table[i] == guess:\n", 65 | " return True\n", 66 | " \n", 67 | "check_guess(up_answers, 4, up_guess)" 68 | ] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "metadata": {}, 73 | "source": [ 74 | "**Chained Hash-table**\n", 75 | "\n", 76 | "Using the code from: https://gist.github.com/philip-sterne/47ff8a8e38e6f7b4ecc5733d13d86372, do the following:\n", 77 | "\n", 78 | "1. Complete the missing sections of code\n", 79 | "1. Using the code, create 100,000 words of 10 characters each.\n", 80 | "1. Create four chained hash-tables with 5000 slots.\n", 81 | "1. Store all the words in each chained hash table using each of the different hash functions.\n", 82 | "1. Measure the number of collisions for each hash function.\n", 83 | "1. For each of the hash functions, how many elements are in a bucket on average (if it is not empty)?\n", 84 | "1. Time how long it take to find elements that are in each hash table.\n", 85 | "1. For each hash table, time how long it takes to find 10,000 elements that have not been stored." 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": null, 91 | "metadata": { 92 | "collapsed": true 93 | }, 94 | "outputs": [], 95 | "source": [ 96 | "import random\n", 97 | "import string\n", 98 | "\n", 99 | "\n", 100 | "def randomword(length):\n", 101 | " return ''.join(random.choice(string.lowercase) for i in range(length))\n", 102 | "\n", 103 | "\n", 104 | "def empty_hash_table(N):\n", 105 | " return [[] for n in range(N)]\n", 106 | "\n", 107 | "\n", 108 | "def add_to_hash_table(hash_table, item, hash_function):\n", 109 | " N = len(hash_table)\n", 110 | " hash_table[hash_function(item)%N].append(item)\n", 111 | " \n", 112 | " return hash_table\n", 113 | "\n", 114 | "\n", 115 | "def contains(hash_table, item, hash_function):\n", 116 | " N = len(hash_table)\n", 117 | " \n", 118 | " key_to_check = hash_function(item)%N\n", 119 | " \n", 120 | " #this allows for multiple items to be checked\n", 121 | " for i in hash_table[key_to_check]:\n", 122 | " if i == item:\n", 123 | " return True\n", 124 | " else:\n", 125 | " return False\n", 126 | "\n", 127 | "def remove(hash_table, item, hash_function):\n", 128 | " if not contains(hash_table, item, hash_function):\n", 129 | " raise ValueError()\n", 130 | " N = len(hash_table)\n", 131 | " \n", 132 | " key_to_check = hash_function(item)%N\n", 133 | " \n", 134 | " for i in hash_table[key_to_check]:\n", 135 | " if i == item:\n", 136 | " hash_table[key_to_check[i]] = None\n", 137 | "\n", 138 | " return hash_table\n", 139 | "\n", 140 | "\n", 141 | "def hash_str1(string):\n", 142 | " ans = 0\n", 143 | " for chr in string:\n", 144 | " ans += ord(chr)\n", 145 | " return ans\n", 146 | "\n", 147 | "\n", 148 | "def hash_str2(string):\n", 149 | " ans = 0\n", 150 | " for chr in string:\n", 151 | " ans = ans ^ ord(chr)\n", 152 | " return ans\n", 153 | "\n", 154 | "\n", 155 | "def hash_str3(string):\n", 156 | " ans = 0\n", 157 | " for chr in string:\n", 158 | " ans = ans * 128 + ord(chr)\n", 159 | " return ans\n", 160 | "\n", 161 | "\n", 162 | "def hash_str4(string):\n", 163 | " random.seed(ord(string[0]))\n", 164 | " return random.getrandbits(32)" 165 | ] 166 | }, 167 | { 168 | "cell_type": "markdown", 169 | "metadata": {}, 170 | "source": [ 171 | "Question 2: Generate list" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": null, 177 | "metadata": {}, 178 | "outputs": [], 179 | "source": [ 180 | "list_of_words = [randomword(100) for i in range(100000)]" 181 | ] 182 | }, 183 | { 184 | "cell_type": "markdown", 185 | "metadata": {}, 186 | "source": [ 187 | "Question 3: Create four chained hash-tables with 5000 slots." 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": null, 193 | "metadata": { 194 | "collapsed": true 195 | }, 196 | "outputs": [], 197 | "source": [ 198 | "hash_tables = [chained_hash_table_1, chained_hash_table_2, \n", 199 | " chained_hash_table_3, chained_hash_table_4]\n", 200 | "\n", 201 | "for table in hash_tables:\n", 202 | " table = empty_hash_table(5000)" 203 | ] 204 | }, 205 | { 206 | "cell_type": "markdown", 207 | "metadata": {}, 208 | "source": [ 209 | "Question 4: Store all the words in each chained hash table using each of the different hash functions." 210 | ] 211 | }, 212 | { 213 | "cell_type": "code", 214 | "execution_count": null, 215 | "metadata": {}, 216 | "outputs": [], 217 | "source": [ 218 | "hash_functions = [hash_str1, hash_str2, hash_str3, hash_str4]\n", 219 | "\n", 220 | "function_counter = -1\n", 221 | "for table in hash_tables:\n", 222 | " function_counter += 1\n", 223 | " for item in list_of_words:\n", 224 | " table = add_to_hash_table(table, item,\n", 225 | " hash_functions[function_counter])\n" 226 | ] 227 | }, 228 | { 229 | "cell_type": "markdown", 230 | "metadata": {}, 231 | "source": [ 232 | "Question 5: Measure the number of collisions for each hash function." 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": null, 238 | "metadata": {}, 239 | "outputs": [], 240 | "source": [ 241 | "def collision_counter(hash_table):\n", 242 | " collisions = 0\n", 243 | " for bucket in hash_table:\n", 244 | " bucket_len = len(bucket)\n", 245 | " if bucket_len > 1:\n", 246 | " collisions += bucket_len -1\n", 247 | " print collisions\n", 248 | " \n", 249 | "collision_counter(chained_hash_table_1)\n", 250 | "collision_counter(chained_hash_table_2)\n", 251 | "collision_counter(chained_hash_table_3)\n", 252 | "collision_counter(chained_hash_table_4)" 253 | ] 254 | }, 255 | { 256 | "cell_type": "markdown", 257 | "metadata": {}, 258 | "source": [ 259 | "Question 6: For each of the hash functions, how many elements are in a bucket on average (if it is not empty)?" 260 | ] 261 | }, 262 | { 263 | "cell_type": "code", 264 | "execution_count": null, 265 | "metadata": {}, 266 | "outputs": [], 267 | "source": [ 268 | "def average_element_counter(hash_table):\n", 269 | " average_len = numpy.average([len(bucket) for bucket in hash_table])\n", 270 | " print average_len\n", 271 | " \n", 272 | "average_element_counter(chained_hash_table_1)\n", 273 | "average_element_counter(chained_hash_table_2)\n", 274 | "average_element_counter(chained_hash_table_3)\n", 275 | "average_element_counter(chained_hash_table_4)" 276 | ] 277 | }, 278 | { 279 | "cell_type": "markdown", 280 | "metadata": {}, 281 | "source": [ 282 | "Question 7: Time how long it take to find elements that are in each hash table." 283 | ] 284 | }, 285 | { 286 | "cell_type": "code", 287 | "execution_count": null, 288 | "metadata": {}, 289 | "outputs": [], 290 | "source": [ 291 | "function_counter = -1\n", 292 | "for table in hash_tables:\n", 293 | " start = time.time()\n", 294 | " function_counter += 1\n", 295 | " for item in list_of_words:\n", 296 | " contains(table, item, hash_functions[function_counter])\n", 297 | " \n", 298 | " end = time.time()\n", 299 | " print end-start\n" 300 | ] 301 | }, 302 | { 303 | "cell_type": "markdown", 304 | "metadata": {}, 305 | "source": [ 306 | "Question 8: For each hash table, time how long it takes to find 10,000 elements that have not been stored." 307 | ] 308 | }, 309 | { 310 | "cell_type": "code", 311 | "execution_count": null, 312 | "metadata": {}, 313 | "outputs": [], 314 | "source": [ 315 | "wrong_guess = []\n", 316 | "\n", 317 | "for i in range(0,100000):\n", 318 | " wrong_guess.append(randomword(99))\n", 319 | "\n", 320 | "function_counter = -1\n", 321 | "for table in hash_tables:\n", 322 | " start = time.time()\n", 323 | " function_counter += 1\n", 324 | " for item in wrong_guess:\n", 325 | " contains(table, item, hash_functions[function_counter])\n", 326 | " \n", 327 | " end = time.time()\n", 328 | " print end-start" 329 | ] 330 | }, 331 | { 332 | "cell_type": "code", 333 | "execution_count": null, 334 | "metadata": { 335 | "collapsed": true 336 | }, 337 | "outputs": [], 338 | "source": [] 339 | } 340 | ], 341 | "metadata": { 342 | "kernelspec": { 343 | "display_name": "Python 2", 344 | "language": "python", 345 | "name": "python2" 346 | }, 347 | "language_info": { 348 | "codemirror_mode": { 349 | "name": "ipython", 350 | "version": 2 351 | }, 352 | "file_extension": ".py", 353 | "mimetype": "text/x-python", 354 | "name": "python", 355 | "nbconvert_exporter": "python", 356 | "pygments_lexer": "ipython2", 357 | "version": "2.7.14" 358 | } 359 | }, 360 | "nbformat": 4, 361 | "nbformat_minor": 2 362 | } 363 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Median Heap and Quickselect-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import heapq\n", 12 | "import random" 13 | ] 14 | }, 15 | { 16 | "cell_type": "markdown", 17 | "metadata": {}, 18 | "source": [ 19 | "heapq.heappush(heap, item)\n", 20 | "Push the value item onto the heap, maintaining the heap invariant.\n", 21 | "\n", 22 | "\n", 23 | "heapq.heappop(heap)\n", 24 | "Pop and return the smallest item from the heap, maintaining the heap invariant. If the heap is empty, IndexError is raised.\n", 25 | "\n", 26 | "\n", 27 | "heapq.heappushpop(heap, item)\n", 28 | "Push item on the heap, then pop and return the smallest item from the heap. The combined action runs more efficiently than heappush() followed by a separate call to heappop().\n", 29 | "\n", 30 | "\n", 31 | "heapq.heapify(x)\n", 32 | "Transform list x into a heap, in-place, in linear time.\n", 33 | "\n", 34 | "\n", 35 | "heapq.heapreplace(heap, item)\n", 36 | "Pop and return the smallest item from the heap, and also push the new item. The heap size doesn’t change. If the heap is empty, IndexError is raised. This is more efficient than heappop() followed by heappush(), and can be more appropriate when using a fixed-size heap. Note that the value returned may be larger than item! That constrains reasonable uses of this routine unless written as part of a conditional replacement:\n", 37 | "\n", 38 | "if item > heap[0]:\n", 39 | " item = heapreplace(heap, item)" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 2, 45 | "metadata": { 46 | "collapsed": true 47 | }, 48 | "outputs": [], 49 | "source": [ 50 | "def add_to_median_heap(minheap, maxheap, elem_list):\n", 51 | " \n", 52 | " #assume elem to be a list of elements to be added\n", 53 | " #default puts values on the minheap\n", 54 | " for elem in elem_list:\n", 55 | " heapq.heappush(minheap, elem)\n", 56 | " \n", 57 | " # rebalancing (max heap is built by\n", 58 | " # each value being multiplied by -1)\n", 59 | " balance_element = 0\n", 60 | "\n", 61 | " while len(minheap) > len(maxheap):\n", 62 | " balance_element = heapq.heappop(minheap)\n", 63 | " heapq.heappush(maxheap,(balance_element * -1))\n", 64 | " \n", 65 | " return minheap, maxheap" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 3, 71 | "metadata": { 72 | "collapsed": true 73 | }, 74 | "outputs": [], 75 | "source": [ 76 | "def median(minheap, maxheap):\n", 77 | " median = 0\n", 78 | " \n", 79 | " if len(minheap) == len(maxheap):\n", 80 | " median = ( minheap[0] + (maxheap[0]* -1) ) / 2\n", 81 | " \n", 82 | " # the way we add to the heaps, this should\n", 83 | " # never happen, but we leave it here just in case \n", 84 | " elif len(minheap) > len(maxheap):\n", 85 | " median = minheap[0]\n", 86 | " \n", 87 | " else:\n", 88 | " median = maxheap[0]* -1\n", 89 | " \n", 90 | " return median" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 4, 96 | "metadata": {}, 97 | "outputs": [ 98 | { 99 | "name": "stdout", 100 | "output_type": "stream", 101 | "text": [ 102 | "[8, 8, 16, 13, 22, 23, 25] [-6, -5, -4, -3, -2, -1, -3, 0]\n", 103 | "6\n" 104 | ] 105 | } 106 | ], 107 | "source": [ 108 | "test_min = []\n", 109 | "test_max = []\n", 110 | "items = [3, 3, 2, 13, 8, 16, 1, 23, 0, 22, 4, 6, 8, 25, 5]\n", 111 | "\n", 112 | "test_min, test_max = add_to_median_heap(test_min, test_max, items)\n", 113 | "print test_min, test_max\n", 114 | "\n", 115 | "print median(test_min, test_max)" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": 5, 121 | "metadata": { 122 | "collapsed": true 123 | }, 124 | "outputs": [], 125 | "source": [ 126 | "def quicksort(array, low, high):\n", 127 | " \n", 128 | " # if we reached end of section\n", 129 | " if low < high:\n", 130 | " \n", 131 | " part_index = partition(array, low, high)\n", 132 | " \n", 133 | " # sort the rest of the array recursively\n", 134 | " # and in place\n", 135 | " quicksort(array, low, part_index - 1)\n", 136 | " quicksort(array, part_index + 1, high)\n", 137 | " \n", 138 | " return array\n", 139 | " " 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": 6, 145 | "metadata": { 146 | "collapsed": true 147 | }, 148 | "outputs": [], 149 | "source": [ 150 | "def partition(array, low, high):\n", 151 | " \n", 152 | " pivot_index = high\n", 153 | " i = low - 1\n", 154 | " \n", 155 | " for j in range(low, high):\n", 156 | " \n", 157 | " # if current item is smaller than or equal\n", 158 | " # to pivot (last value), swap them & increment i\n", 159 | " if array[j] <= pivot_index:\n", 160 | " i += 1\n", 161 | " array[i], array[j] = array[j], array[i]\n", 162 | " \n", 163 | " array[i + 1], array[high] = array[high], array[i + 1]\n", 164 | " return i + 1" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": 7, 170 | "metadata": { 171 | "collapsed": true 172 | }, 173 | "outputs": [], 174 | "source": [ 175 | "def quickSelect(A, left, right, k):\n", 176 | " if left == right:\n", 177 | " return A[left]\n", 178 | " pivotIndex = partition(A, left, right)\n", 179 | " \n", 180 | " if k == pivotIndex:\n", 181 | " return A[k]\n", 182 | " elif k < pivotIndex:\n", 183 | " return quickSelect(A, 1, pivotIndex-1,k)\n", 184 | " else:\n", 185 | " return quickSelect(A, pivotIndex+1,right, k - pivotIndex)" 186 | ] 187 | }, 188 | { 189 | "cell_type": "code", 190 | "execution_count": 12, 191 | "metadata": {}, 192 | "outputs": [ 193 | { 194 | "data": { 195 | "text/plain": [ 196 | "4" 197 | ] 198 | }, 199 | "execution_count": 12, 200 | "metadata": {}, 201 | "output_type": "execute_result" 202 | } 203 | ], 204 | "source": [ 205 | "test_list = [8, 3, 6, 0, 1, 2, 7, 4]\n", 206 | "\n", 207 | "quickSelect(test_list[:], 0, len(test_list)-1, 6)" 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "execution_count": null, 213 | "metadata": { 214 | "collapsed": true 215 | }, 216 | "outputs": [], 217 | "source": [] 218 | }, 219 | { 220 | "cell_type": "code", 221 | "execution_count": null, 222 | "metadata": { 223 | "collapsed": true 224 | }, 225 | "outputs": [], 226 | "source": [] 227 | } 228 | ], 229 | "metadata": { 230 | "kernelspec": { 231 | "display_name": "Python 2", 232 | "language": "python", 233 | "name": "python2" 234 | }, 235 | "language_info": { 236 | "codemirror_mode": { 237 | "name": "ipython", 238 | "version": 2 239 | }, 240 | "file_extension": ".py", 241 | "mimetype": "text/x-python", 242 | "name": "python", 243 | "nbconvert_exporter": "python", 244 | "pygments_lexer": "ipython2", 245 | "version": "2.7.14" 246 | } 247 | }, 248 | "nbformat": 4, 249 | "nbformat_minor": 2 250 | } 251 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Randomized BST-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import random" 12 | ] 13 | }, 14 | { 15 | "cell_type": "markdown", 16 | "metadata": {}, 17 | "source": [ 18 | "## Question 1 - Randomized BST" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 3, 24 | "metadata": { 25 | "collapsed": true 26 | }, 27 | "outputs": [], 28 | "source": [ 29 | "class Node:\n", 30 | " def __init__(self, val):\n", 31 | " self.l_child = None\n", 32 | " self.r_child = None\n", 33 | " self.data = val\n", 34 | " self.parent = None\n", 35 | " \n", 36 | "def insert(root, node):\n", 37 | " if root is None:\n", 38 | " root = node\n", 39 | " else:\n", 40 | " if root.data > node.data:\n", 41 | " if root.l_child is None:\n", 42 | " root.l_child = node\n", 43 | " node.parent = root\n", 44 | " else:\n", 45 | " insert(root.l_child, node)\n", 46 | " else:\n", 47 | " if root.r_child is None:\n", 48 | " root.r_child = node\n", 49 | " node.parent = root\n", 50 | " else:\n", 51 | " insert(root.r_child, node)\n", 52 | " return root\n", 53 | " \n", 54 | "def search(root, value):\n", 55 | " if root == None or root.data == value:\n", 56 | " return root\n", 57 | " elif value < root.data:\n", 58 | " return search(root.l_child, value)\n", 59 | " else:\n", 60 | " return search(root.r_child, value)\n", 61 | "\n", 62 | "def transplant(root, u1, u2):\n", 63 | " if u1 == root:\n", 64 | " root = u2\n", 65 | " elif u1 == u1.parent.l_child:\n", 66 | " u1.parent.l_child = u2\n", 67 | " else:\n", 68 | " u1.parent.r_child = u2 \n", 69 | " if u2 != None:\n", 70 | " u2.parent = u1.parent\n", 71 | " \n", 72 | "def delete_node(root, node): \n", 73 | " if node.l_child == None:\n", 74 | " transplant(root, node, node.r_child)\n", 75 | " elif node.r_child == None:\n", 76 | " transplant(root, node, node.l_child)\n", 77 | " else:\n", 78 | " y = tree_min(node.r_child)\n", 79 | " if y.parent != node:\n", 80 | " transplant(root, y, y.r_child)\n", 81 | " y.r_child = node.r_child\n", 82 | " y.r_child.parent = y\n", 83 | " transplant(root, node, y)\n", 84 | " y.l_child = node.l_child\n", 85 | " y.l_child.parent = y\n", 86 | " \n", 87 | "def tree_min(root):\n", 88 | " while root.l_child:\n", 89 | " root = root.l_child\n", 90 | " return root\n", 91 | "\n", 92 | "def traversal(root):\n", 93 | " if root != None:\n", 94 | " traversal(root.l_child)\n", 95 | " print root.data\n", 96 | " traversal(root.r_child)" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 87, 102 | "metadata": {}, 103 | "outputs": [ 104 | { 105 | "name": "stdout", 106 | "output_type": "stream", 107 | "text": [ 108 | "Treemin 0\n", 109 | "Searching for 10 10\n", 110 | "---------------\n" 111 | ] 112 | } 113 | ], 114 | "source": [ 115 | "order_tree = Node(0)\n", 116 | "nonorder_tree = Node(0)\n", 117 | "counter = 0\n", 118 | "for i in range(1,901):\n", 119 | " insert(order_tree, Node(i))\n", 120 | " \n", 121 | "for i in range(1,901):\n", 122 | " insert(nonorder_tree, Node(random.randint(1,901)))\n", 123 | " \n", 124 | "\n", 125 | "\n", 126 | "print \"Treemin \" + str(tree_min(order_tree).data)\n", 127 | "print \"Searching for 10 \" + str(search(order_tree, 10).data)\n", 128 | "print \"---------------\"\n", 129 | "# print \"Traversal \" + str(traversal(tree))" 130 | ] 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "metadata": {}, 135 | "source": [ 136 | "## Question 2 - Average number of comparisons" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 88, 142 | "metadata": { 143 | "collapsed": true 144 | }, 145 | "outputs": [], 146 | "source": [ 147 | "def search_avg_comparisons(root, value, counter): \n", 148 | " if root == None or root.data == value:\n", 149 | " counter += 1\n", 150 | " return counter\n", 151 | " elif value < root.data:\n", 152 | " counter += 1\n", 153 | " return search_avg_comparisons(root.l_child, value, counter)\n", 154 | " else:\n", 155 | " counter += 1\n", 156 | " return search_avg_comparisons(root.r_child, value, counter)" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 90, 162 | "metadata": {}, 163 | "outputs": [ 164 | { 165 | "name": "stdout", 166 | "output_type": "stream", 167 | "text": [ 168 | "Calculating average number of comparisons for tree: \n", 169 | "Average num of comparisons: \n", 170 | "12.615\n", 171 | "This is something around the order of log_2(n).\n" 172 | ] 173 | } 174 | ], 175 | "source": [ 176 | "print \"Calculating average number of comparisons for tree: \"\n", 177 | "avg_counter = 0\n", 178 | "for i in range(200):\n", 179 | " search_item = random.randint(0,901)\n", 180 | " avg_counter = search_avg_comparisons(nonorder_tree, search_item, avg_counter)\n", 181 | "print \"Average num of comparisons: \"\n", 182 | "print avg_counter/200.0\n", 183 | "print \"This is something around the order of log_2(n).\"" 184 | ] 185 | }, 186 | { 187 | "cell_type": "markdown", 188 | "metadata": {}, 189 | "source": [ 190 | "## Question 3 - Max and average tree height" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": 91, 196 | "metadata": { 197 | "collapsed": true 198 | }, 199 | "outputs": [], 200 | "source": [ 201 | "def max_height(root):\n", 202 | " r_height = 0\n", 203 | " while root.r_child:\n", 204 | " root = root.r_child\n", 205 | " r_height += 1\n", 206 | " \n", 207 | " l_height = 0\n", 208 | " while root.l_child:\n", 209 | " root = root.l_child\n", 210 | " l_height += 1 \n", 211 | " return max(r_height, l_height) \n", 212 | "\n", 213 | "\n", 214 | "def avg_height(root, trials): \n", 215 | " direction = [\"left\", \"right\"]\n", 216 | " height = 0\n", 217 | "\n", 218 | " for i in range(trials):\n", 219 | " temp_root = root\n", 220 | " while temp_root:\n", 221 | " dir_pick = direction[random.randint(0, 1)]\n", 222 | " if dir_pick == \"right\" and temp_root.r_child != None:\n", 223 | " temp_root = temp_root.r_child\n", 224 | " height += 1\n", 225 | " elif dir_pick == \"left\" and temp_root.l_child != None:\n", 226 | " temp_root = temp_root.l_child\n", 227 | " height += 1\n", 228 | " else:\n", 229 | " temp_root = None\n", 230 | "\n", 231 | " return float(height)/trials" 232 | ] 233 | }, 234 | { 235 | "cell_type": "code", 236 | "execution_count": 94, 237 | "metadata": {}, 238 | "outputs": [ 239 | { 240 | "name": "stdout", 241 | "output_type": "stream", 242 | "text": [ 243 | "Calculating max height of tree: \n", 244 | "Max height order_tree: 900\n", 245 | "Max height nonorder_tree: 10\n", 246 | "Calculating average height of tree out of 50 trials: \n", 247 | "Average height order_tree: 1.132\n", 248 | "Average height nonorder_tree: 3.9\n" 249 | ] 250 | } 251 | ], 252 | "source": [ 253 | "print \"Calculating max height of tree: \"\n", 254 | "max_order = 0\n", 255 | "max_order = max_height(order_tree)\n", 256 | "max_nonorder = 0\n", 257 | "max_nonorder = max_height(nonorder_tree)\n", 258 | "\n", 259 | "print \"Max height order_tree: \" + str(max_order)\n", 260 | "print \"Max height nonorder_tree: \" + str(max_nonorder)\n", 261 | "\n", 262 | "print \"Calculating average height of tree out of 50 trials: \"\n", 263 | "trials = 500\n", 264 | "avg_order = 0\n", 265 | "avg_order = avg_height(order_tree, trials)\n", 266 | "avg_nonorder = 0\n", 267 | "avg_nonorder = avg_height(nonorder_tree, trials)\n", 268 | "\n", 269 | "print \"Average height order_tree: \" + str(avg_order)\n", 270 | "print \"Average height nonorder_tree: \" + str(avg_nonorder)" 271 | ] 272 | }, 273 | { 274 | "cell_type": "markdown", 275 | "metadata": {}, 276 | "source": [ 277 | "## Question 4 - Select/Rank " 278 | ] 279 | } 280 | ], 281 | "metadata": { 282 | "kernelspec": { 283 | "display_name": "Python 2", 284 | "language": "python", 285 | "name": "python2" 286 | }, 287 | "language_info": { 288 | "codemirror_mode": { 289 | "name": "ipython", 290 | "version": 2 291 | }, 292 | "file_extension": ".py", 293 | "mimetype": "text/x-python", 294 | "name": "python", 295 | "nbconvert_exporter": "python", 296 | "pygments_lexer": "ipython2", 297 | "version": "2.7.14" 298 | } 299 | }, 300 | "nbformat": 4, 301 | "nbformat_minor": 2 302 | } 303 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Viterbi algorithm-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Viterbi Algorithm in Speech Recognition" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "** a. What does the transition_probability table describe?**\n", 15 | "\n", 16 | "The *one-step transition probability* is the probability of transitioning from one state to another for any single step.\n", 17 | "\n", 18 | "** b. What does the emission_probability table describe?**\n", 19 | "\n", 20 | "For each of the *N* possible states, there is a set of emission probabilities governing the distribution of the observed variable at a particular time given the state of the hidden variable at that time (?)\n", 21 | "\n", 22 | "** c. What does the start_probability table describe?**\n", 23 | "\n", 24 | "The probabilities of any given state at the start (for words I guess this would be the general probability of a certain letter occuring at the beginning of a word in the whole of the English language)\n", 25 | "\n", 26 | "** d. What does the Viterbi algorithm do with these tables?**\n", 27 | "\n", 28 | "Use them to predict the likelyhood of the current state and correct it if it's inaccurate.\n", 29 | "\n", 30 | "** e. Describe the optimal substructure found in this problem.**\n", 31 | "\n", 32 | "If we have already identified the changes of the previous 4 letters, we don't need to re-calculate them when we add a 5th. And for language, where many words reoccur over and over, we might not have to solve a new problem every time.\n", 33 | "\n", 34 | "** f. How should one interpret the output of the Viterbi algorithm?**\n", 35 | "\n", 36 | "Gives the most likely word, the shortest path of sorts." 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 1, 42 | "metadata": {}, 43 | "outputs": [ 44 | { 45 | "name": "stdout", 46 | "output_type": "stream", 47 | "text": [ 48 | " 0 1 2 3 4 5 6 7 8 9 10\n", 49 | "a: 1.600E-01 3.750E-04 3.120E-04 2.293E-03 1.365E-06 1.032E-04 2.709E-07 1.393E-06 9.287E-09 2.717E-09 1.997E-08\n", 50 | "c: 5.000E-03 2.400E-03 3.120E-04 5.460E-05 5.160E-04 2.580E-06 1.548E-06 1.857E-07 2.090E-08 2.717E-09 4.754E-10\n", 51 | "b: 5.000E-03 3.120E-02 9.600E-05 5.460E-05 3.440E-05 2.580E-06 6.192E-06 7.430E-07 2.717E-07 8.359E-10 4.754E-10\n", 52 | "r: 5.000E-03 8.000E-04 1.092E-02 2.400E-06 2.293E-05 2.580E-06 5.160E-07 1.548E-07 1.857E-08 9.508E-08 2.090E-11\n", 53 | "d: 1.500E-02 9.600E-03 1.560E-04 5.460E-05 3.440E-05 2.580E-06 1.857E-05 3.715E-07 8.359E-08 1.358E-09 4.754E-10\n", 54 | "The steps of states are a b r a c a d a b r a with highest probability of 1.99668780675e-08\n" 55 | ] 56 | } 57 | ], 58 | "source": [ 59 | "#!/usr/bin/env python\n", 60 | "# -*- coding: utf-8 -*-\n", 61 | "######################\n", 62 | "# Speech recognition #\n", 63 | "######################\n", 64 | "# 1. Work through the following resources:\n", 65 | "# Viterbi algorithm. (n.d.). In Wikipedia. Retrieved November 9, 2016, from\n", 66 | "# https://en.wikipedia.org/wiki/Viterbi_algorithm\n", 67 | "# The 44 Phonemes in English. (n.d.). Retrieved November 9, 2016, from\n", 68 | "# http://www.dyslexia-reading-well.com/44-phonemes-in-english.html\n", 69 | "# 2. Read and run the code given below.\n", 70 | "# 3. Answer the following questions:\n", 71 | "# a. What does the transition_probability table describe?\n", 72 | "# b. What does the emission_probability table describe?\n", 73 | "# c. What does the start_probability table describe?\n", 74 | "# d. What does the Viterbi algorithm do with these tables?\n", 75 | "# e. Describe the optimal substructure found in this problem.\n", 76 | "# f. How should one interpret the output of the Viterbi algorithm?\n", 77 | "\n", 78 | "\n", 79 | "def viterbi(obs, states, start_p, trans_p, emit_p):\n", 80 | " V = [{}]\n", 81 | " for st in states:\n", 82 | " V[0][st] = {\"prob\": start_p[st] * emit_p[st][obs[0]], \"prev\": None}\n", 83 | " # Run Viterbi when t > 0\n", 84 | " for t in range(1, len(obs)):\n", 85 | " V.append({})\n", 86 | " for st in states:\n", 87 | " max_tr_prob = max(V[t - 1][prev_st][\"prob\"] * trans_p[prev_st][st]\n", 88 | " for prev_st in states)\n", 89 | " for prev_st in states:\n", 90 | " if V[t - 1][prev_st][\"prob\"] * trans_p[prev_st][\n", 91 | " st] == max_tr_prob:\n", 92 | " max_prob = max_tr_prob * emit_p[st][obs[t]]\n", 93 | " V[t][st] = {\"prob\": max_prob, \"prev\": prev_st}\n", 94 | " break\n", 95 | " for line in dptable(V):\n", 96 | " print line\n", 97 | " opt = []\n", 98 | " # The highest probability\n", 99 | " max_prob = max(value[\"prob\"] for value in V[-1].values())\n", 100 | " previous = None\n", 101 | " # Get most probable state and its backtrack\n", 102 | " for st, data in V[-1].items():\n", 103 | " if data[\"prob\"] == max_prob:\n", 104 | " opt.append(st)\n", 105 | " previous = st\n", 106 | " break\n", 107 | " # Follow the backtrack till the first observation\n", 108 | " for t in range(len(V) - 2, -1, -1):\n", 109 | " opt.insert(0, V[t + 1][previous][\"prev\"])\n", 110 | " previous = V[t + 1][previous][\"prev\"]\n", 111 | "\n", 112 | " print 'The steps of states are ' + ' '.join(\n", 113 | " opt) + ' with highest probability of %s' % max_prob\n", 114 | "\n", 115 | "\n", 116 | "def dptable(V):\n", 117 | " # Print a table of steps from dictionary\n", 118 | " yield \" \".join((\"%9s\" % i) for i in range(len(V)))\n", 119 | " for state in V[0]:\n", 120 | " yield \"%.9s: \" % state + \" \".join(\"%.9s\" % (\"%.3E\" % v[state][\"prob\"])\n", 121 | " for v in V)\n", 122 | "\n", 123 | "\n", 124 | "states = 'abcdr'\n", 125 | "observations = ('/a/', '/b/', '/r/', '/ã/', '/k/', '/a/', '/d/', '/d/', '/b/',\n", 126 | " '/r/', '/ã/')\n", 127 | "start_probability = {'a': 0.4, 'b': 0.1, 'c': 0.1, 'd': 0.3, 'r': 0.1}\n", 128 | "\n", 129 | "transition_probability = {'a': {'a': 0,\n", 130 | " 'b': 0.3,\n", 131 | " 'c': 0.3,\n", 132 | " 'd': 0.3,\n", 133 | " 'r': 0.1},\n", 134 | " 'b': {'a': 0.2,\n", 135 | " 'b': 0,\n", 136 | " 'c': 0.2,\n", 137 | " 'd': 0.1,\n", 138 | " 'r': 0.5},\n", 139 | " 'c': {'a': 0.5,\n", 140 | " 'b': 0.1,\n", 141 | " 'c': 0.1,\n", 142 | " 'd': 0.1,\n", 143 | " 'r': 0.1},\n", 144 | " 'd': {'a': 0.5,\n", 145 | " 'b': 0.2,\n", 146 | " 'c': 0.2,\n", 147 | " 'd': 0.0,\n", 148 | " 'r': 0.1},\n", 149 | " 'r': {'a': 0.7,\n", 150 | " 'b': 0.1,\n", 151 | " 'c': 0.1,\n", 152 | " 'd': 0.1,\n", 153 | " 'r': 0}}\n", 154 | "emission_probability = {'a': {'/a/': 0.4,\n", 155 | " '/ã/': 0.3,\n", 156 | " '/b/': 0.05,\n", 157 | " '/r/': 0.05,\n", 158 | " '/d/': 0.15,\n", 159 | " '/k/': 0.05},\n", 160 | " 'b': {'/a/': 0.05,\n", 161 | " '/ã/': 0.05,\n", 162 | " '/b/': 0.65,\n", 163 | " '/r/': 0.05,\n", 164 | " '/d/': 0.2,\n", 165 | " '/k/': 0.05},\n", 166 | " 'c': {'/a/': 0.05,\n", 167 | " '/ã/': 0.05,\n", 168 | " '/b/': 0.05,\n", 169 | " '/r/': 0.05,\n", 170 | " '/d/': 0.05,\n", 171 | " '/k/': 0.75},\n", 172 | " 'd': {'/a/': 0.05,\n", 173 | " '/ã/': 0.05,\n", 174 | " '/b/': 0.2,\n", 175 | " '/r/': 0.05,\n", 176 | " '/d/': 0.6,\n", 177 | " '/k/': 0.05},\n", 178 | " 'r': {'/a/': 0.05,\n", 179 | " '/ã/': 0.05,\n", 180 | " '/b/': 0.05,\n", 181 | " '/r/': 0.7,\n", 182 | " '/d/': 0.05,\n", 183 | " '/k/': 0.1}}\n", 184 | "\n", 185 | "viterbi(observations, states, start_probability, transition_probability,\n", 186 | " emission_probability)" 187 | ] 188 | }, 189 | { 190 | "cell_type": "code", 191 | "execution_count": null, 192 | "metadata": { 193 | "collapsed": true 194 | }, 195 | "outputs": [], 196 | "source": [] 197 | } 198 | ], 199 | "metadata": { 200 | "kernelspec": { 201 | "display_name": "Python 2", 202 | "language": "python", 203 | "name": "python2" 204 | }, 205 | "language_info": { 206 | "codemirror_mode": { 207 | "name": "ipython", 208 | "version": 2 209 | }, 210 | "file_extension": ".py", 211 | "mimetype": "text/x-python", 212 | "name": "python", 213 | "nbconvert_exporter": "python", 214 | "pygments_lexer": "ipython2", 215 | "version": "2.7.14" 216 | } 217 | }, 218 | "nbformat": 4, 219 | "nbformat_minor": 2 220 | } 221 | -------------------------------------------------------------------------------- /18:01:09 Algorithm-collection.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import timeit\n", 12 | "import random\n", 13 | "from random import randint\n", 14 | "import math" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": null, 20 | "metadata": { 21 | "collapsed": true 22 | }, 23 | "outputs": [], 24 | "source": [ 25 | "##Repetition function\n", 26 | "def rand_list(m):\n", 27 | " lis = []\n", 28 | " for _ in xrange(m):\n", 29 | " lis.append(randint(-100,100))\n", 30 | " return lis\n", 31 | "\n", 32 | "m = 3\n", 33 | "n = 4\n", 34 | "\n", 35 | "def repetition(f, start_m, repetition_n):\n", 36 | " lis = []\n", 37 | " initial_list = f(start_m)\n", 38 | " for n in range(1,repetition_n):\n", 39 | " lis.append(initial_list*n)\n", 40 | " print lis\n", 41 | " \n", 42 | "repetition(rand_list, m, n)" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "metadata": { 49 | "collapsed": true 50 | }, 51 | "outputs": [], 52 | "source": [ 53 | "##Bubble sort\n", 54 | "def bubble(array):\n", 55 | " counter = 0\n", 56 | " for i in range(len(array)):\n", 57 | " for j in range(0, (len(array))-i-1):\n", 58 | " if array[j] > array[j+1]:\n", 59 | " counter += 1\n", 60 | " array[j], array[j+1] = array[j+1], array[j]\n", 61 | "# print counter\n", 62 | " return array" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": null, 68 | "metadata": { 69 | "collapsed": true 70 | }, 71 | "outputs": [], 72 | "source": [ 73 | "##Insertion sort\n", 74 | "def insertion(array):\n", 75 | " counter = 0\n", 76 | " for j in range(1, len(array)): \n", 77 | " counter += 1\n", 78 | " key = array[j] \n", 79 | " i = j - 1 \n", 80 | " while i >= 0 and array[i] > key: \n", 81 | " counter += 1\n", 82 | " array[i + 1] = array[i] \n", 83 | " i -= 1 \n", 84 | " array[i + 1] = key\n", 85 | "# print counter\n", 86 | " return array" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": null, 92 | "metadata": { 93 | "collapsed": true 94 | }, 95 | "outputs": [], 96 | "source": [ 97 | "##Selection sort\n", 98 | "def selection(array):\n", 99 | " counter = 0\n", 100 | " for slot in range(len(array) - 1, 0, -1):\n", 101 | " maxx = 0 \n", 102 | " counter += 1\n", 103 | " for x in range(1, slot + 1):\n", 104 | " counter += 1\n", 105 | " if array[x] > array[maxx]:\n", 106 | " maxx = x\n", 107 | " counter += 1\n", 108 | " \n", 109 | " temp = array[slot]\n", 110 | " counter += 1\n", 111 | " array[slot] = array[maxx]\n", 112 | " counter += 1\n", 113 | " array[maxx] = temp\n", 114 | " counter += 1\n", 115 | "# print counter\n", 116 | " return array" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": null, 122 | "metadata": { 123 | "collapsed": true 124 | }, 125 | "outputs": [], 126 | "source": [ 127 | "## Shell sort (calling insertion sort)\n", 128 | "def shell_sort(array): \n", 129 | " interval = len(array)/10\n", 130 | " counter = 0\n", 131 | " while interval > 0: \n", 132 | " for count in range(interval):\n", 133 | " array[count::interval] = insertion(array[count::interval])\n", 134 | " counter += 1\n", 135 | " interval = int(interval/2)\n", 136 | "# print counter\n", 137 | " return array" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": null, 143 | "metadata": { 144 | "collapsed": true 145 | }, 146 | "outputs": [], 147 | "source": [ 148 | "## Merge sort (calling the merge function)\n", 149 | "def merge(L,R):\n", 150 | " i = 0\n", 151 | " j = 0\n", 152 | " array = []\n", 153 | " L.append(float('inf'))\n", 154 | " R.append(float('inf'))\n", 155 | " \n", 156 | " while i + j < len(L) + len(R) - 2:\n", 157 | " if L[i] < R[j]:\n", 158 | " array.append(L[i])\n", 159 | " i += 1\n", 160 | " else:\n", 161 | " array.append(R[j])\n", 162 | " j += 1\n", 163 | " return array\n", 164 | "\n", 165 | "def merge_sort(array):\n", 166 | " L = []\n", 167 | " R = []\n", 168 | " \n", 169 | " if len(array) > 3:\n", 170 | " L = merge_sort(array[:len(array)//2])\n", 171 | " R = merge_sort(array[len(array)//2:])\n", 172 | " return merge(L,R)\n", 173 | " \n", 174 | " elif len(array) == 2:\n", 175 | " if array[0] > array[1]:\n", 176 | " array[0], array[1] = array[1], array[0]\n", 177 | " return array \n", 178 | " else: \n", 179 | " return array\n", 180 | "\n", 181 | "random_array = random.sample(range(1, 10000), 100)\n", 182 | "merge_sort(random_array[:])" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": 2, 188 | "metadata": {}, 189 | "outputs": [ 190 | { 191 | "data": { 192 | "text/plain": [ 193 | "[15, 11, 14, 9, 10, 13, 7, 8, 4, 2, 5, 12, 6, 3, 1]" 194 | ] 195 | }, 196 | "execution_count": 2, 197 | "metadata": {}, 198 | "output_type": "execute_result" 199 | } 200 | ], 201 | "source": [ 202 | "## Max Heapify\n", 203 | "\n", 204 | "def parent(i):\n", 205 | " return (i-1)/2\n", 206 | "\n", 207 | "def left(i):\n", 208 | " return 2*i + 1\n", 209 | "\n", 210 | "def right(i):\n", 211 | " return 2*i + 2\n", 212 | "\n", 213 | "def max_heapify(A, i):\n", 214 | " heap_size = len(A)-1\n", 215 | " l = left(i)\n", 216 | " r = right(i)\n", 217 | " \n", 218 | " if l <= heap_size and A[l] > A[i]:\n", 219 | " largest = l\n", 220 | " else:\n", 221 | " largest = i\n", 222 | " \n", 223 | " if r <= heap_size and A[r] > A[largest]:\n", 224 | " largest = r\n", 225 | " \n", 226 | " if largest != i:\n", 227 | " A[i], A[largest] = A[largest], A[i]\n", 228 | " \n", 229 | " A = max_heapify(A, largest)\n", 230 | " \n", 231 | " return A\n", 232 | "\n", 233 | "## Build-Max-Heapify\n", 234 | "\n", 235 | "def build_max_heap(A):\n", 236 | " heap_size = len(A)\n", 237 | " for i in range(heap_size/2, -1, -1):\n", 238 | " A = max_heapify(A, i)\n", 239 | " \n", 240 | " return A\n", 241 | "\n", 242 | "test_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]\n", 243 | "build_max_heap(test_array[:])\n", 244 | "#[7, 15, 16, 21, 28, 29, 30, 31, 39, 49, 49, 76, 85, 85, 92]" 245 | ] 246 | }, 247 | { 248 | "cell_type": "code", 249 | "execution_count": null, 250 | "metadata": { 251 | "collapsed": true 252 | }, 253 | "outputs": [], 254 | "source": [ 255 | "## Heap-push\n", 256 | "def heappush(a, item):\n", 257 | " a.append(item)\n", 258 | " \n", 259 | " a = max_heapify(a, len(a))\n", 260 | " return a\n", 261 | "\n", 262 | "#This should actually take the last item and then refix\n", 263 | "## Heap-pop\n", 264 | "def heap_pop(a):\n", 265 | " max_item = a[0]\n", 266 | " \n", 267 | " a[0] = a.pop()\n", 268 | " a = max_heapify(a, 0)\n", 269 | " return a, max_item\n", 270 | " \n", 271 | "## Heapify" 272 | ] 273 | }, 274 | { 275 | "cell_type": "code", 276 | "execution_count": 46, 277 | "metadata": {}, 278 | "outputs": [ 279 | { 280 | "ename": "NameError", 281 | "evalue": "global name 'bubble' is not defined", 282 | "output_type": "error", 283 | "traceback": [ 284 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 285 | "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", 286 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0mmerge_sort\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrandom_array\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mrand_arr_len\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 16\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0;34m\"Bubble sort: \"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtimeit\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtimeit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbubble_timing\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnumber\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 17\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0;34m\"Insertion sort: \"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtimeit\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtimeit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minsertion_timing\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnumber\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0;34m\"Selection sort: \"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtimeit\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtimeit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mselection_timing\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnumber\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 287 | "\u001b[0;32m/anaconda2/lib/python2.7/timeit.pyc\u001b[0m in \u001b[0;36mtimeit\u001b[0;34m(stmt, setup, timer, number)\u001b[0m\n\u001b[1;32m 235\u001b[0m number=default_number):\n\u001b[1;32m 236\u001b[0m \u001b[0;34m\"\"\"Convenience function to create Timer object and call timeit method.\"\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 237\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mTimer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstmt\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msetup\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtimer\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtimeit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnumber\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 238\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 239\u001b[0m def repeat(stmt=\"pass\", setup=\"pass\", timer=default_timer,\n", 288 | "\u001b[0;32m/anaconda2/lib/python2.7/timeit.pyc\u001b[0m in \u001b[0;36mtimeit\u001b[0;34m(self, number)\u001b[0m\n\u001b[1;32m 200\u001b[0m \u001b[0mgc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdisable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 201\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 202\u001b[0;31m \u001b[0mtiming\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minner\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mit\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtimer\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 203\u001b[0m \u001b[0;32mfinally\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 204\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mgcold\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 289 | "\u001b[0;32m/anaconda2/lib/python2.7/timeit.pyc\u001b[0m in \u001b[0;36minner\u001b[0;34m(_it, _timer, _func)\u001b[0m\n\u001b[1;32m 98\u001b[0m \u001b[0m_t0\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_timer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 99\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0m_i\u001b[0m \u001b[0;32min\u001b[0m \u001b[0m_it\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 100\u001b[0;31m \u001b[0m_func\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 101\u001b[0m \u001b[0m_t1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_timer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 102\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0m_t1\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0m_t0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 290 | "\u001b[0;32m\u001b[0m in \u001b[0;36mbubble_timing\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mbubble_timing\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mbubble\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrandom_array\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0minsertion_timing\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0marr_copy\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mrandom_array\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 291 | "\u001b[0;31mNameError\u001b[0m: global name 'bubble' is not defined" 292 | ] 293 | } 294 | ], 295 | "source": [ 296 | "random_array = random.sample(range(1, 10000), 1000)\n", 297 | "rand_arr_len = len(random_array)\n", 298 | "\n", 299 | "def bubble_timing():\n", 300 | " bubble(random_array[:])\n", 301 | "def insertion_timing():\n", 302 | " arr_copy = random_array[:]\n", 303 | " insertion(arr_copy)\n", 304 | "def selection_timing():\n", 305 | " selection(random_array[:])\n", 306 | "def shell_timing():\n", 307 | " shell_sort(random_array[:])\n", 308 | "def merge_timing():\n", 309 | " merge_sort(random_array[:],0,rand_arr_len)\n", 310 | " \n", 311 | "print \"Bubble sort: \" + str(timeit.timeit(bubble_timing, number=1))\n", 312 | "print \"Insertion sort: \" + str(timeit.timeit(insertion_timing, number=1))\n", 313 | "print \"Selection sort: \" + str(timeit.timeit(selection_timing, number=1))\n", 314 | "print \"Shell sort: \" + str(timeit.timeit(shell_timing, number=1))\n", 315 | "print \"Merge sort: \" + str(timeit.timeit(merge_timing, number=1))" 316 | ] 317 | }, 318 | { 319 | "cell_type": "code", 320 | "execution_count": null, 321 | "metadata": { 322 | "collapsed": true 323 | }, 324 | "outputs": [], 325 | "source": [] 326 | }, 327 | { 328 | "cell_type": "code", 329 | "execution_count": null, 330 | "metadata": { 331 | "collapsed": true 332 | }, 333 | "outputs": [], 334 | "source": [] 335 | }, 336 | { 337 | "cell_type": "code", 338 | "execution_count": null, 339 | "metadata": { 340 | "collapsed": true 341 | }, 342 | "outputs": [], 343 | "source": [ 344 | "##Example of timeit further setup\n", 345 | "\n", 346 | "# compute binary search time\n", 347 | "def bubble_time():\n", 348 | " SETUP_CODE = '''\n", 349 | "from __main__ import bubble'''\n", 350 | " \n", 351 | " TEST_CODE = '''\n", 352 | "test_array = [40, 3, 5, 32, 22, 10, 8, 41, 14, 7, 8,\n", 353 | " 56, 65, 9, 2, 5, 6, 44, 65, 55, 43, 13,\n", 354 | " 32, 43, 22, 11, 54, 55, 37, 86, 79, 44]\n", 355 | "bubble(test_array)'''\n", 356 | " \n", 357 | " # timeit.repeat statement\n", 358 | " times = timeit.timeit(setup = SETUP_CODE,\n", 359 | " stmt = TEST_CODE,\n", 360 | " number = 1)\n", 361 | " \n", 362 | " print('Search time: ' \"%.3G\" % (times)) \n", 363 | " \n", 364 | "bubble_time()" 365 | ] 366 | } 367 | ], 368 | "metadata": { 369 | "kernelspec": { 370 | "display_name": "Python 2", 371 | "language": "python", 372 | "name": "python2" 373 | }, 374 | "language_info": { 375 | "codemirror_mode": { 376 | "name": "ipython", 377 | "version": 2 378 | }, 379 | "file_extension": ".py", 380 | "mimetype": "text/x-python", 381 | "name": "python", 382 | "nbconvert_exporter": "python", 383 | "pygments_lexer": "ipython2", 384 | "version": "2.7.14" 385 | } 386 | }, 387 | "nbformat": 4, 388 | "nbformat_minor": 2 389 | } 390 | -------------------------------------------------------------------------------- /18:01:23 Max subarray.ipynb: -------------------------------------------------------------------------------- 1 | {"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"Max subarray.ipynb","version":"0.3.2","views":{},"default_view":{},"provenance":[]}},"cells":[{"metadata":{"id":"Y2_JvKA6Kcx4","colab_type":"text"},"source":["Write a function  `incremental_max_subarray(x, mx)` , where\n","\n","- x is an array of integers\n","- mx is the max_subarray of x[0:-1],\n","\n","which then returns the max_subarray of the entire list. Give Big-O, Big-Ω, and Big-Θ bounds where possible. If you use any built-in python functions, be sure to take the complexity of those functions into account.\n","\n","def brute_force_max_sub(array):\n","\n","total = 0\n","temp_sum = 0\n","left = 0\n","right = 0\n","\n","for i in range (0, len(array)):\n","\n"," for j in range (i, len(array)):\n","\n"," temp_sum = array[j] - array[i]\n"," \n"," if temp_sum > total:\n","\n"," total = temp_sum\n"," left = i\n"," right = j\n","\n","return total, left, right"],"cell_type":"markdown"},{"metadata":{"id":"bnUbd_0oRQ4S","colab_type":"code","colab":{"autoexec":{"startup":false,"wait_interval":0},"output_extras":[{"item_id":1}],"base_uri":"https://localhost:8080/","height":34},"outputId":"ea6b6de7-1204-425e-b8ca-bcbb1c4bbc87","executionInfo":{"status":"ok","timestamp":1516635300845,"user_tz":-330,"elapsed":971,"user":{"displayName":"Michelle Hackl","photoUrl":"//lh4.googleusercontent.com/-9_FzX-I_fks/AAAAAAAAAAI/AAAAAAAAAAs/KMtAskxSomg/s50-c-k-no/photo.jpg","userId":"116674263760227842671"}}},"source":["from sys import maxint\n","\n","def maxSubArraySum(a):\n"," size = len(a)\n"," total = -maxint - 1\n"," max_so_far = 0 \n"," \n"," for i in range(0, size):\n"," for j in range(i+1, size):\n"," max_so_far = a[j] - a[i]\n"," if (max_so_far > total):\n"," total = max_so_far\n"," left = i\n"," right = j+1\n"," \n"," sub_array = a[left:right]\n"," return sub_array\n"," \n"," \n","test_array = [1, 10, 2, -4, 3, 7, -2, 4, 9, -2, 3] \n","maxSubArraySum(test_array)"],"cell_type":"code","execution_count":15,"outputs":[{"output_type":"execute_result","data":{"text/plain":["[-4, 3, 7, -2, 4, 9]"]},"metadata":{"tags":[]},"execution_count":15}]},{"metadata":{"id":"rPaG4qMmKJ7r","colab_type":"code","colab":{"autoexec":{"startup":false,"wait_interval":0},"output_extras":[{"item_id":1}],"base_uri":"https://localhost:8080/","height":51},"outputId":"77398fde-c44b-49f1-cbf2-3e70eca4bdc3","executionInfo":{"status":"ok","timestamp":1516635670115,"user_tz":-330,"elapsed":857,"user":{"displayName":"Michelle Hackl","photoUrl":"//lh4.googleusercontent.com/-9_FzX-I_fks/AAAAAAAAAAI/AAAAAAAAAAs/KMtAskxSomg/s50-c-k-no/photo.jpg","userId":"116674263760227842671"}}},"source":["def incremental_max_subarray(a, new_array=None):\n"," \n"," if new_array:\n"," a = a + new_array\n"," \n"," size = len(a)\n"," total = -maxint - 1\n"," max_so_far = 0 \n"," \n"," for i in range(0, size):\n"," for j in range(i+1, size):\n"," max_so_far = a[j] - a[i]\n"," if (max_so_far > total):\n"," total = max_so_far\n"," left = i\n"," right = j+1\n"," \n"," sub_array = a[left:right]\n"," return sub_array \n"," \n","test_array = [1, 10, 2, -4, 3, 7, -2, 4, 9, -2, 3] \n","first_solution = incremental_max_subarray(test_array)\n","print first_solution\n","added_values = [22, -5, 3, 5, 6, 30]\n","print incremental_max_subarray(first_solution, added_values)\n"],"cell_type":"code","execution_count":18,"outputs":[{"output_type":"stream","text":["[-4, 3, 7, -2, 4, 9]\n","[-5, 3, 5, 6, 30]\n"],"name":"stdout"}]},{"metadata":{"id":"apjh7sDUbtig","colab_type":"code","colab":{"autoexec":{"startup":false,"wait_interval":0}}},"source":["incremental_max_subarray(x, mx):\n"," new = x[-1] - x[-2] \n"," if mx[-1] - mx[0] > new:\n"," if mx[-1] > x[-1]:\n"," return mx #When the day added didn't offer a better price for the asset\n"," else:\n"," return x[x.index(mx[0]):] #When the day added has a higher value than maxsubarray\n"," else:\n"," return x[-2:] #This is when you buy in the last day of the previous array"],"cell_type":"code","execution_count":0,"outputs":[]}]} -------------------------------------------------------------------------------- /18:01:26 K-way Mergesort - assignment.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 4, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import numpy as np\n", 12 | "import random\n", 13 | "import heapq\n", 14 | "import timeit\n", 15 | "import time" 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": 5, 21 | "metadata": { 22 | "collapsed": true 23 | }, 24 | "outputs": [], 25 | "source": [ 26 | "##Insertion Sort\n", 27 | "\n", 28 | "def insertion_sort(array):\n", 29 | " for j in range(1, len(array)): \n", 30 | " key = array[j] \n", 31 | " i = j - 1 \n", 32 | " \n", 33 | " while i >= 0 and array[i] > key: \n", 34 | " array[i + 1] = array[i] \n", 35 | " i -= 1 \n", 36 | " \n", 37 | " array[i + 1] = key\n", 38 | " \n", 39 | " return array" 40 | ] 41 | }, 42 | { 43 | "cell_type": "markdown", 44 | "metadata": {}, 45 | "source": [ 46 | "Basic merge-sort code taken from https://www.geeksforgeeks.org/merge-sort/\n", 47 | "\n", 48 | "Collaborated on the following functions with Anna Pauxberger." 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 6, 54 | "metadata": { 55 | "collapsed": true 56 | }, 57 | "outputs": [], 58 | "source": [ 59 | "## 3-way Merge Sort\n", 60 | "\n", 61 | "def three_way_merge_sort(array):\n", 62 | " if len(array) >= 3:\n", 63 | " start = 0\n", 64 | " end = len(array) // 3\n", 65 | " \n", 66 | " left = three_merge_sort(array[:end])\n", 67 | " middle = three_merge_sort(array[end:end * 2])\n", 68 | " right = three_merge_sort(array[end * 2:])\n", 69 | " \n", 70 | " array = [x for x in heapq.merge(left, middle, right)]\n", 71 | " \n", 72 | " elif len(array) == 2:\n", 73 | " if array[0] > array[1]:\n", 74 | " array[0], array[1] = array[1], array[0]\n", 75 | " \n", 76 | " else:\n", 77 | " return array\n", 78 | "\n", 79 | " return array" 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "metadata": {}, 85 | "source": [ 86 | "The k-way merge function uses heapq.merge() on the different sublists that are generated in the loop to merge them back in order once they are sorted individually. I used heapq instead of a manually written merge function, since it can easily be expanded to work for any value of k (and writing a k-way merge function proved to be more difficult than I wanted the scope of my optional answer to be).\n", 87 | "\n", 88 | "\n", 89 | "Similarly, I used the heapq.merge() function for my three-way sort, since I could not resist the beauty of such a simple solution (rather than manually comparing all three stacks against each other each time)." 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 8, 95 | "metadata": { 96 | "collapsed": true 97 | }, 98 | "outputs": [], 99 | "source": [ 100 | "## k-way Merge Sort\n", 101 | "\n", 102 | "def k_way_merge_sort(array, k):\n", 103 | " # sublist_end?\n", 104 | " if len(array) > 3*k:\n", 105 | " end = len(array)//k\n", 106 | " \n", 107 | " # for cases where len(a)//k would round to 0\n", 108 | " if end == 0:\n", 109 | " end = 1\n", 110 | "\n", 111 | " list_of_sublists = []\n", 112 | "\n", 113 | " for i in range(k):\n", 114 | " sublist = k_way_merge_sort(array[end*(i):end*(i+1)], k)\n", 115 | " list_of_sublists.append(sublist)\n", 116 | "\n", 117 | " array = [x for x in heapq.merge(*list_of_sublists)]\n", 118 | "\n", 119 | " else:\n", 120 | " array.sort()\n", 121 | "# array = insertion_sort(array)\n", 122 | "\n", 123 | " return array" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 9, 129 | "metadata": {}, 130 | "outputs": [ 131 | { 132 | "name": "stdout", 133 | "output_type": "stream", 134 | "text": [ 135 | "[[ 1.09672546e-05 4.60147858e-05 7.35998154e-04 4.54115868e-03\n", 136 | " 6.24229908e-02]\n", 137 | " [ 1.28746033e-05 3.09944153e-05 4.58955765e-04 6.51597977e-03\n", 138 | " 7.12130070e-02]\n", 139 | " [ 1.19209290e-05 3.98159027e-05 4.70876694e-04 5.84506989e-03\n", 140 | " 6.83619976e-02]\n", 141 | " [ 1.21593475e-05 2.00271606e-05 5.82218170e-04 6.65497780e-03\n", 142 | " 7.96041489e-02]\n", 143 | " [ 1.50203705e-05 4.69684601e-05 8.56876373e-04 5.62500954e-03\n", 144 | " 6.59849644e-02]\n", 145 | " [ 2.69412994e-05 2.00271606e-05 5.08069992e-04 6.30497932e-03\n", 146 | " 6.63280487e-02]\n", 147 | " [ 2.59876251e-05 2.09808350e-05 6.24179840e-04 6.72602654e-03\n", 148 | " 6.74571991e-02]\n", 149 | " [ 2.69412994e-05 2.59876251e-05 5.01871109e-04 5.63406944e-03\n", 150 | " 6.38780594e-02]\n", 151 | " [ 2.50339508e-05 2.21967697e-04 4.42028046e-04 6.07991219e-03\n", 152 | " 7.48710632e-02]\n", 153 | " [ 3.00407410e-05 2.43902206e-04 4.73976135e-04 6.25205040e-03\n", 154 | " 6.96790218e-02]]\n" 155 | ] 156 | } 157 | ], 158 | "source": [ 159 | "## Data collection\n", 160 | "sample_time = np.zeros([10,5])\n", 161 | "\n", 162 | "\n", 163 | "def data_collection(array):\n", 164 | " col = 0\n", 165 | " row = 0\n", 166 | "\n", 167 | " #iterating over a range of k values and the corresponding\n", 168 | " #sample sizes to record the times it takes to sort them\n", 169 | " for k_exp in range(1, 11):\n", 170 | " k = 2 ** k_exp\n", 171 | "\n", 172 | " for s_exp in range(2, 7):\n", 173 | " sample = 10 ** s_exp\n", 174 | " sample_array = random.sample(range(1, sample*10), sample)\n", 175 | " \n", 176 | " #timing function to call in timeit.timeit\n", 177 | " def merge_timing():\n", 178 | " k_way_merge_sort(sample_array[:],k)\n", 179 | "\n", 180 | "\n", 181 | " array[row][col] = timeit.timeit(merge_timing, number=1)\n", 182 | " col += 1 \n", 183 | " \n", 184 | " col = 0\n", 185 | " row += 1\n", 186 | " \n", 187 | " return array\n", 188 | "\n", 189 | "print (data_collection(sample_time))" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": null, 195 | "metadata": { 196 | "collapsed": true 197 | }, 198 | "outputs": [], 199 | "source": [] 200 | } 201 | ], 202 | "metadata": { 203 | "kernelspec": { 204 | "display_name": "Python 2", 205 | "language": "python", 206 | "name": "python2" 207 | }, 208 | "language_info": { 209 | "codemirror_mode": { 210 | "name": "ipython", 211 | "version": 2 212 | }, 213 | "file_extension": ".py", 214 | "mimetype": "text/x-python", 215 | "name": "python", 216 | "nbconvert_exporter": "python", 217 | "pygments_lexer": "ipython2", 218 | "version": "2.7.14" 219 | } 220 | }, 221 | "nbformat": 4, 222 | "nbformat_minor": 2 223 | } 224 | -------------------------------------------------------------------------------- /18:02:06 Worst sorting function: median finder.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 18, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import random as rd\n", 10 | "import numpy as np" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 15, 16 | "metadata": {}, 17 | "outputs": [ 18 | { 19 | "name": "stdout", 20 | "output_type": "stream", 21 | "text": [ 22 | "Trying again!\n", 23 | "Trying again!\n", 24 | "Trying again!\n", 25 | "Trying again!\n", 26 | "[1, 3, 4]\n" 27 | ] 28 | } 29 | ], 30 | "source": [ 31 | "def shuffle_array(array):\n", 32 | " rd.shuffle(array)\n", 33 | " \n", 34 | " return array\n", 35 | "\n", 36 | "def checker(array):\n", 37 | " for i in range(0,len(array)-1):\n", 38 | " if array[i] > array[i+1]:\n", 39 | " return False\n", 40 | " \n", 41 | " return True\n", 42 | "\n", 43 | "\n", 44 | "\n", 45 | "def worst_sorting(array):\n", 46 | " shuffle_array(array)\n", 47 | " check = False\n", 48 | " \n", 49 | " check = checker(array)\n", 50 | " \n", 51 | " if check == True:\n", 52 | " print array\n", 53 | " else:\n", 54 | " print \"Trying again!\"\n", 55 | " worst_sorting(array)\n", 56 | "\n", 57 | "\n", 58 | "\n", 59 | "test_array = [1, 3, 4]\n", 60 | "worst_sorting(test_array)" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 72, 66 | "metadata": { 67 | "collapsed": true 68 | }, 69 | "outputs": [], 70 | "source": [ 71 | "def approx_median(array, delta):\n", 72 | " pick = rd.choice(array)\n", 73 | " \n", 74 | " top = np.max(array)\n", 75 | " bottom = np.min(array)\n", 76 | " range_array = top - bottom\n", 77 | " perc = range_array / 100.0\n", 78 | " \n", 79 | " minus_one_stdev = (bottom+(perc*50))-((delta*perc)/2)\n", 80 | " plus_one_stdev = (bottom+(perc*50))+((delta*perc)/2)\n", 81 | " \n", 82 | " if pick >= minus_one_stdev and pick <= plus_one_stdev:\n", 83 | " print pick\n", 84 | " else:\n", 85 | " approx_median(array,delta)\n", 86 | "\n", 87 | " " 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": 73, 93 | "metadata": {}, 94 | "outputs": [ 95 | { 96 | "name": "stdout", 97 | "output_type": "stream", 98 | "text": [ 99 | "2\n" 100 | ] 101 | } 102 | ], 103 | "source": [ 104 | "test_array = [1, 2, 3]\n", 105 | "approx_median(test_array, 6)" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": null, 111 | "metadata": { 112 | "collapsed": true 113 | }, 114 | "outputs": [], 115 | "source": [] 116 | } 117 | ], 118 | "metadata": { 119 | "kernelspec": { 120 | "display_name": "Python 2", 121 | "language": "python", 122 | "name": "python2" 123 | }, 124 | "language_info": { 125 | "codemirror_mode": { 126 | "name": "ipython", 127 | "version": 2 128 | }, 129 | "file_extension": ".py", 130 | "mimetype": "text/x-python", 131 | "name": "python", 132 | "nbconvert_exporter": "python", 133 | "pygments_lexer": "ipython2", 134 | "version": "2.7.14" 135 | } 136 | }, 137 | "nbformat": 4, 138 | "nbformat_minor": 2 139 | } 140 | -------------------------------------------------------------------------------- /18:02:10 Expanded k-way.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 12, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import numpy as np\n", 12 | "import random as rnd\n", 13 | "import heapq" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": null, 19 | "metadata": { 20 | "collapsed": true 21 | }, 22 | "outputs": [], 23 | "source": [ 24 | "##Insertion Sort\n", 25 | "\n", 26 | "def insertion_sort(array):\n", 27 | " for j in range(1, len(array)): \n", 28 | " key = array[j] \n", 29 | " i = j - 1 \n", 30 | " \n", 31 | " while i >= 0 and array[i] > key: \n", 32 | " array[i + 1] = array[i] \n", 33 | " i -= 1 \n", 34 | " \n", 35 | " array[i + 1] = key\n", 36 | " \n", 37 | " return array" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": null, 43 | "metadata": { 44 | "collapsed": true 45 | }, 46 | "outputs": [], 47 | "source": [ 48 | "## k-way Merge Sort\n", 49 | "\n", 50 | "def k_way_merge_sort(array, k):\n", 51 | " # sublist_end?\n", 52 | " if len(array) > 3*k:\n", 53 | " end = len(array)//k\n", 54 | " \n", 55 | " # for cases where len(a)//k would round to 0\n", 56 | " if end == 0:\n", 57 | " end = 1\n", 58 | "\n", 59 | " list_of_sublists = []\n", 60 | "\n", 61 | " for i in range(k):\n", 62 | " sublist = k_way_merge_sort(array[end*(i):end*(i+1)], k)\n", 63 | " list_of_sublists.append(sublist)\n", 64 | "\n", 65 | " array = [x for x in heapq.merge(*list_of_sublists)]\n", 66 | "\n", 67 | " else:\n", 68 | " array = insertion_sort(array)\n", 69 | "\n", 70 | " return array" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 262, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [ 79 | "def merge_all(list_of_sublists):\n", 80 | " sorted_list = []\n", 81 | " len_list_of_sublists = 0\n", 82 | " \n", 83 | " for lst in list_of_sublists:\n", 84 | " for item in lst:\n", 85 | " len_list_of_sublists += 1\n", 86 | " \n", 87 | " for j in range(0, len_list_of_sublists):\n", 88 | " current_best = 0\n", 89 | " \n", 90 | " for i in range(0, len(list_of_sublists)):\n", 91 | " if list_of_sublists[i][0] < list_of_sublists[current_best][0]:\n", 92 | " current_best = i\n", 93 | "\n", 94 | " sorted_list = np.append(sorted_list, list_of_sublists[current_best][0])\n", 95 | " list_of_sublists[current_best] = np.delete(list_of_sublists[current_best], 0)\n", 96 | "\n", 97 | " if not list_of_sublists[current_best].any():\n", 98 | " list_of_sublists[current_best] = np.append(list_of_sublists[current_best], float('inf'))\n", 99 | " \n", 100 | " return sorted_list" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": 263, 106 | "metadata": { 107 | "collapsed": true 108 | }, 109 | "outputs": [], 110 | "source": [ 111 | "def two_way_merge(L,R):\n", 112 | " i = 0\n", 113 | " j = 0\n", 114 | " array = np.array([])\n", 115 | " L = np.append(L, float('inf'))\n", 116 | " R = np.append(R, float('inf'))\n", 117 | " \n", 118 | " while i + j < len(L) + len(R) - 2:\n", 119 | " if L[i] < R[j]:\n", 120 | " array = np.append(array, L[i])\n", 121 | " i += 1\n", 122 | " \n", 123 | " else:\n", 124 | " array = np.append(array, R[j])\n", 125 | " j += 1\n", 126 | " \n", 127 | " return array" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": 264, 133 | "metadata": { 134 | "collapsed": true 135 | }, 136 | "outputs": [], 137 | "source": [ 138 | "def merge_iterative(list_of_sublists):\n", 139 | " L = list_of_sublists[0]\n", 140 | " R = []\n", 141 | " \n", 142 | " for i in range(len(list_of_sublists) - 1):\n", 143 | " R = list_of_sublists[i + 1]\n", 144 | " L = two_way_merge(L, R)\n", 145 | " \n", 146 | " return L \n" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": 265, 152 | "metadata": {}, 153 | "outputs": [ 154 | { 155 | "name": "stdout", 156 | "output_type": "stream", 157 | "text": [ 158 | "k: [7]\n", 159 | "subl_len: [96]\n", 160 | "The list is the right length.\n", 161 | "The list is sorted.\n" 162 | ] 163 | } 164 | ], 165 | "source": [ 166 | "def merge_test(merge_fun, iterations):\n", 167 | " \n", 168 | " #pick ks & length of sublists\n", 169 | " for i in range(iterations):\n", 170 | " k = np.random.randint(1, 10, 1)\n", 171 | " print \"k: \" + str(k)\n", 172 | " len_subl = np.random.randint(1, 100, 1)\n", 173 | " print \"subl_len: \" + str(len_subl)\n", 174 | " \n", 175 | " total_subl = []\n", 176 | " \n", 177 | " #populate k sublists of length len_subl\n", 178 | " for j in range(k):\n", 179 | " sublist = np.random.randint(0, 1000, len_subl)\n", 180 | " total_subl.append(sublist)\n", 181 | " \n", 182 | " #sort each sublist\n", 183 | " for lst in total_subl:\n", 184 | " lst.sort()\n", 185 | " \n", 186 | " #merge the sublists\n", 187 | " merged_list = merge_fun(total_subl)\n", 188 | "\n", 189 | " #test the function for length and sorted\n", 190 | " merged_list = [x for x in merged_list]\n", 191 | "\n", 192 | " if len(merged_list) == (len_subl * k):\n", 193 | " print \"The list is the right length.\"\n", 194 | " else:\n", 195 | " print \"No right length. No good.\"\n", 196 | " \n", 197 | " if sorted(merged_list) == merged_list:\n", 198 | " print \"The list is sorted.\"\n", 199 | " else:\n", 200 | " print \"No sort. No good.\"\n", 201 | "\n", 202 | "def test_merge(list_of_sublists):\n", 203 | " array = [x for x in heapq.merge(*list_of_sublists)]\n", 204 | " \n", 205 | " return array\n", 206 | " \n", 207 | "merge_test(merge_all, 1)" 208 | ] 209 | } 210 | ], 211 | "metadata": { 212 | "kernelspec": { 213 | "display_name": "Python 2", 214 | "language": "python", 215 | "name": "python2" 216 | }, 217 | "language_info": { 218 | "codemirror_mode": { 219 | "name": "ipython", 220 | "version": 2 221 | }, 222 | "file_extension": ".py", 223 | "mimetype": "text/x-python", 224 | "name": "python", 225 | "nbconvert_exporter": "python", 226 | "pygments_lexer": "ipython2", 227 | "version": "2.7.14" 228 | } 229 | }, 230 | "nbformat": 4, 231 | "nbformat_minor": 2 232 | } 233 | -------------------------------------------------------------------------------- /18:02:15 Quicksort.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import random\n", 12 | "import timeit" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 2, 18 | "metadata": { 19 | "collapsed": true 20 | }, 21 | "outputs": [], 22 | "source": [ 23 | "def quicksort(array, low, high):\n", 24 | " \n", 25 | " # if we reached end of section\n", 26 | " if low < high:\n", 27 | " \n", 28 | " part_index = partition_last_elem(array, low, high)\n", 29 | " \n", 30 | " # sort the rest of the array recursively\n", 31 | " # and in place\n", 32 | " quicksort(array, low, part_index - 1)\n", 33 | " quicksort(array, part_index + 1, high)\n", 34 | " \n", 35 | " return array\n", 36 | " " 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 15, 42 | "metadata": {}, 43 | "outputs": [ 44 | { 45 | "data": { 46 | "text/plain": [ 47 | "[1, 2, 3, 3, 4, 5, 6, 7, 8, 9, 12, 21, 22, 34, 44]" 48 | ] 49 | }, 50 | "execution_count": 15, 51 | "metadata": {}, 52 | "output_type": "execute_result" 53 | } 54 | ], 55 | "source": [ 56 | "def median(low, mid, high, array):\n", 57 | " x1 = array[low]\n", 58 | " x2 = array[mid]\n", 59 | " x3 = array[high]\n", 60 | " \n", 61 | " for a in range(7):\n", 62 | " if x1 <= x2 <= x3:\n", 63 | " if x2 == array[low]:\n", 64 | " return low\n", 65 | " elif x2 == array[mid]:\n", 66 | " return mid\n", 67 | " else:\n", 68 | " return high\n", 69 | " # Every loop I'm shufflin\n", 70 | " (x1, x2, x3) = (x2, x1, x3)\n", 71 | " if a % 2:\n", 72 | " (x1, x2, x3) = (x3, x1, x2)\n", 73 | "\n", 74 | "def rand_quicksort(array, low, high):\n", 75 | " random.shuffle(array)\n", 76 | " # if we reached end of section\n", 77 | " if low < high:\n", 78 | " \n", 79 | " part_index = partition_last_elem(array, low, high)\n", 80 | " \n", 81 | " # sort the rest of the array recursively\n", 82 | " # and in place\n", 83 | " quicksort(array, low, part_index - 1)\n", 84 | " quicksort(array, part_index + 1, high)\n", 85 | " \n", 86 | " return array\n", 87 | "\n", 88 | "rand_test_array = [3, 2, 3, 5, 7, 8, 22, 1, 12, 4, 6, 9, 34, 44, 21]\n", 89 | "rand_quicksort(rand_test_array, 0, (len(rand_test_array)-1))" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 14, 95 | "metadata": { 96 | "collapsed": true 97 | }, 98 | "outputs": [], 99 | "source": [ 100 | "def partition_last_elem(array, low, high):\n", 101 | " \n", 102 | " # picks pivot and if that lies within the array\n", 103 | " # it swaps it for the first element before continuing\n", 104 | " mid = len(array)/2\n", 105 | " pivot_index = median(low, mid, high, array)\n", 106 | " \n", 107 | " if pivot_index != high:\n", 108 | " array[high], array[pivot_index] = array[pivot_index], array[high]\n", 109 | " \n", 110 | " pivot = array[high]\n", 111 | " i = low - 1\n", 112 | " \n", 113 | " for j in range(low, high):\n", 114 | " \n", 115 | " # if current item is smaller than or equal\n", 116 | " # to pivot (last value), swap them & increment i\n", 117 | " if array[j] <= pivot:\n", 118 | " i += 1\n", 119 | " array[i], array[j] = array[j], array[i]\n", 120 | " \n", 121 | " array[i + 1], array[high] = array[high], array[i + 1]\n", 122 | " return i + 1" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 50, 128 | "metadata": { 129 | "collapsed": true 130 | }, 131 | "outputs": [], 132 | "source": [ 133 | "def fib(n):\n", 134 | " if n <= 0:\n", 135 | " return 0\n", 136 | " \n", 137 | " if n == 1:\n", 138 | " return 1\n", 139 | " \n", 140 | " return fib(n-1) + fib(n-2)" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 52, 146 | "metadata": {}, 147 | "outputs": [ 148 | { 149 | "name": "stdout", 150 | "output_type": "stream", 151 | "text": [ 152 | "Average quicksort runtime on a reversed list over 30 trials: 1.15647675991\n", 153 | "Average quicksort runtime on an ordered list over 30 trials: 1.91352689266\n", 154 | "Average quicksort runtime on a random list over 30 trials: 0.0453058719635\n" 155 | ] 156 | } 157 | ], 158 | "source": [ 159 | "lst1 = [random.random() for a in range(900)]\n", 160 | "lst2 = range(900)\n", 161 | "lst3 = lst2[::-1]\n", 162 | "\n", 163 | "n1 = len(lst1)\n", 164 | "n2 = len(lst2)\n", 165 | "\n", 166 | "def quicksort_timing_reverse():\n", 167 | " quicksort(lst3[:], 0, n1 - 1)\n", 168 | " \n", 169 | "def quicksort_timing_ordered():\n", 170 | " quicksort(lst2[:], 0, n1 - 1)\n", 171 | " \n", 172 | "def quicksort_timing_random():\n", 173 | " quicksort(lst1[:], 0, n1 - 1)\n", 174 | " \n", 175 | " \n", 176 | "def fib_timer():\n", 177 | " fib(10)\n", 178 | " \n", 179 | "reverse_time = timeit.timeit(quicksort_timing_reverse, number=30) \n", 180 | "ordered_time = timeit.timeit(quicksort_timing_ordered, number=30)\n", 181 | "random_time = timeit.timeit(quicksort_timing_random, number=30)\n", 182 | "# fib_time = timeit.timeit(fib_timer, number=5)\n", 183 | " \n", 184 | "print \"Average quicksort runtime on a reversed list over 30 trials: \" + str(reverse_time / 30)\n", 185 | "print \"Average quicksort runtime on an ordered list over 30 trials: \" + str(ordered_time / 30)\n", 186 | "print \"Average quicksort runtime on a random list over 30 trials: \" + str(random_time / 30)\n", 187 | "\n", 188 | "# print \"Average fibonacci runtime over 30 trials: \" + str(fib_time)" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": 39, 194 | "metadata": {}, 195 | "outputs": [ 196 | { 197 | "name": "stdout", 198 | "output_type": "stream", 199 | "text": [ 200 | "202050 404550\n" 201 | ] 202 | } 203 | ], 204 | "source": [ 205 | "_, reverse_swaps = quicksort(lst3[:], 0, n1 - 1)\n", 206 | "_, ordered_swaps = quicksort(lst2[:], 0, n1 - 1)\n", 207 | "\n", 208 | "print reverse_swaps, ordered_swaps" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": 47, 214 | "metadata": {}, 215 | "outputs": [ 216 | { 217 | "name": "stdout", 218 | "output_type": "stream", 219 | "text": [ 220 | "('Swapping!', 0, 0, 0, 0, 9)\n", 221 | "('Swapping!', 1, 1, 1, 1, 9)\n", 222 | "('Swapping!', 2, 2, 2, 2, 9)\n", 223 | "('Swapping!', 3, 3, 3, 3, 9)\n", 224 | "('Swapping!', 4, 4, 4, 4, 9)\n", 225 | "('Swapping!', 5, 5, 5, 5, 9)\n", 226 | "('Swapping!', 6, 6, 6, 6, 9)\n", 227 | "('Swapping!', 7, 7, 7, 7, 9)\n", 228 | "('Swapping!', 8, 8, 8, 8, 9)\n", 229 | "0 8 10 9\n", 230 | "('Swapping!', 0, 0, 0, 0, 8)\n", 231 | "('Swapping!', 1, 1, 1, 1, 8)\n", 232 | "('Swapping!', 2, 2, 2, 2, 8)\n", 233 | "('Swapping!', 3, 3, 3, 3, 8)\n", 234 | "('Swapping!', 4, 4, 4, 4, 8)\n", 235 | "('Swapping!', 5, 5, 5, 5, 8)\n", 236 | "('Swapping!', 6, 6, 6, 6, 8)\n", 237 | "('Swapping!', 7, 7, 7, 7, 8)\n", 238 | "0 7 9 8\n", 239 | "('Swapping!', 0, 0, 0, 0, 7)\n", 240 | "('Swapping!', 1, 1, 1, 1, 7)\n", 241 | "('Swapping!', 2, 2, 2, 2, 7)\n", 242 | "('Swapping!', 3, 3, 3, 3, 7)\n", 243 | "('Swapping!', 4, 4, 4, 4, 7)\n", 244 | "('Swapping!', 5, 5, 5, 5, 7)\n", 245 | "('Swapping!', 6, 6, 6, 6, 7)\n", 246 | "0 6 8 7\n", 247 | "('Swapping!', 0, 0, 0, 0, 6)\n", 248 | "('Swapping!', 1, 1, 1, 1, 6)\n", 249 | "('Swapping!', 2, 2, 2, 2, 6)\n", 250 | "('Swapping!', 3, 3, 3, 3, 6)\n", 251 | "('Swapping!', 4, 4, 4, 4, 6)\n", 252 | "('Swapping!', 5, 5, 5, 5, 6)\n", 253 | "0 5 7 6\n", 254 | "('Swapping!', 0, 0, 0, 0, 5)\n", 255 | "('Swapping!', 1, 1, 1, 1, 5)\n", 256 | "('Swapping!', 2, 2, 2, 2, 5)\n", 257 | "('Swapping!', 3, 3, 3, 3, 5)\n", 258 | "('Swapping!', 4, 4, 4, 4, 5)\n", 259 | "0 4 6 5\n", 260 | "('Swapping!', 0, 0, 0, 0, 4)\n", 261 | "('Swapping!', 1, 1, 1, 1, 4)\n", 262 | "('Swapping!', 2, 2, 2, 2, 4)\n", 263 | "('Swapping!', 3, 3, 3, 3, 4)\n", 264 | "0 3 5 4\n", 265 | "('Swapping!', 0, 0, 0, 0, 3)\n", 266 | "('Swapping!', 1, 1, 1, 1, 3)\n", 267 | "('Swapping!', 2, 2, 2, 2, 3)\n", 268 | "0 2 4 3\n", 269 | "('Swapping!', 0, 0, 0, 0, 2)\n", 270 | "('Swapping!', 1, 1, 1, 1, 2)\n", 271 | "0 1 3 2\n", 272 | "('Swapping!', 0, 0, 0, 0, 1)\n", 273 | "0 0 2 1\n" 274 | ] 275 | }, 276 | { 277 | "data": { 278 | "text/plain": [ 279 | "([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 45)" 280 | ] 281 | }, 282 | "execution_count": 47, 283 | "metadata": {}, 284 | "output_type": "execute_result" 285 | } 286 | ], 287 | "source": [ 288 | "import timeit\n", 289 | "import random\n", 290 | "\n", 291 | "eps = 1e-16\n", 292 | "N = 10000\n", 293 | "locations = [0.0, 0.5, 1.0 - eps]\n", 294 | "\n", 295 | "\n", 296 | "def median(x1, x2, x3):\n", 297 | " for a in range(7):\n", 298 | " if x1 <= x2 <= x3:\n", 299 | " return x2\n", 300 | " # Every loop I'm shufflin\n", 301 | " (x1, x2, x3) = (x2, x1, x3)\n", 302 | " if a % 2:\n", 303 | " (x1, x2, x3) = (x3, x1, x2)\n", 304 | "\n", 305 | "\n", 306 | "def qsort(lst):\n", 307 | " indices = [(0, len(lst))]\n", 308 | "\n", 309 | " while indices:\n", 310 | " (frm, to) = indices.pop()\n", 311 | " if frm == to:\n", 312 | " continue\n", 313 | "\n", 314 | " # Find the partition:\n", 315 | " N = to - frm\n", 316 | " inds = [frm + int(N * n) for n in locations]\n", 317 | " values = [lst[ind] for ind in inds]\n", 318 | " partition = median(*values)\n", 319 | "\n", 320 | " # Split into lists:\n", 321 | " lower = [a for a in lst[frm:to] if a < partition]\n", 322 | " upper = [a for a in lst[frm:to] if a > partition]\n", 323 | " counts = sum([1 for a in lst[frm:to] if a == partition])\n", 324 | "\n", 325 | " ind1 = frm + len(lower)\n", 326 | " ind2 = ind1 + counts\n", 327 | "\n", 328 | " # Push back into correct place:\n", 329 | " lst[frm:ind1] = lower\n", 330 | " lst[ind1:ind2] = [partition] * counts\n", 331 | " lst[ind2:to] = upper\n", 332 | "\n", 333 | " # Enqueue other locations\n", 334 | " indices.append((frm, ind1))\n", 335 | " indices.append((ind2, to))\n", 336 | " return lst\n", 337 | "\n", 338 | "\n", 339 | "def randomized_quicksort():\n", 340 | " lst = range(N)\n", 341 | " random.shuffle(lst)\n", 342 | " return qsort(lst)\n", 343 | "\n", 344 | "\n", 345 | "def test_quicksort():\n", 346 | " lst = randomized_quicksort()\n", 347 | " assert (lst == range(N))\n", 348 | "\n", 349 | "\n", 350 | "# Is our algorithm correct\n", 351 | "test_quicksort()\n", 352 | "\n", 353 | "# How fast is our algorithm\n", 354 | "print timeit.timeit(randomized_quicksort, number=1)" 355 | ] 356 | }, 357 | { 358 | "cell_type": "code", 359 | "execution_count": null, 360 | "metadata": { 361 | "collapsed": true 362 | }, 363 | "outputs": [], 364 | "source": [] 365 | } 366 | ], 367 | "metadata": { 368 | "kernelspec": { 369 | "display_name": "Python 2", 370 | "language": "python", 371 | "name": "python2" 372 | }, 373 | "language_info": { 374 | "codemirror_mode": { 375 | "name": "ipython", 376 | "version": 2 377 | }, 378 | "file_extension": ".py", 379 | "mimetype": "text/x-python", 380 | "name": "python", 381 | "nbconvert_exporter": "python", 382 | "pygments_lexer": "ipython2", 383 | "version": "2.7.14" 384 | } 385 | }, 386 | "nbformat": 4, 387 | "nbformat_minor": 2 388 | } 389 | -------------------------------------------------------------------------------- /18:02:20 Median Heap and Quickselect.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import heapq\n", 12 | "import random" 13 | ] 14 | }, 15 | { 16 | "cell_type": "markdown", 17 | "metadata": {}, 18 | "source": [ 19 | "heapq.heappush(heap, item)\n", 20 | "Push the value item onto the heap, maintaining the heap invariant.\n", 21 | "\n", 22 | "\n", 23 | "heapq.heappop(heap)\n", 24 | "Pop and return the smallest item from the heap, maintaining the heap invariant. If the heap is empty, IndexError is raised.\n", 25 | "\n", 26 | "\n", 27 | "heapq.heappushpop(heap, item)\n", 28 | "Push item on the heap, then pop and return the smallest item from the heap. The combined action runs more efficiently than heappush() followed by a separate call to heappop().\n", 29 | "\n", 30 | "\n", 31 | "heapq.heapify(x)\n", 32 | "Transform list x into a heap, in-place, in linear time.\n", 33 | "\n", 34 | "\n", 35 | "heapq.heapreplace(heap, item)\n", 36 | "Pop and return the smallest item from the heap, and also push the new item. The heap size doesn’t change. If the heap is empty, IndexError is raised. This is more efficient than heappop() followed by heappush(), and can be more appropriate when using a fixed-size heap. Note that the value returned may be larger than item! That constrains reasonable uses of this routine unless written as part of a conditional replacement:\n", 37 | "\n", 38 | "if item > heap[0]:\n", 39 | " item = heapreplace(heap, item)" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 2, 45 | "metadata": { 46 | "collapsed": true 47 | }, 48 | "outputs": [], 49 | "source": [ 50 | "def add_to_median_heap(minheap, maxheap, elem_list):\n", 51 | " \n", 52 | " #assume elem to be a list of elements to be added\n", 53 | " #default puts values on the minheap\n", 54 | " for elem in elem_list:\n", 55 | " heapq.heappush(minheap, elem)\n", 56 | " \n", 57 | " # rebalancing (max heap is built by\n", 58 | " # each value being multiplied by -1)\n", 59 | " balance_element = 0\n", 60 | "\n", 61 | " while len(minheap) > len(maxheap):\n", 62 | " balance_element = heapq.heappop(minheap)\n", 63 | " heapq.heappush(maxheap,(balance_element * -1))\n", 64 | " \n", 65 | " return minheap, maxheap" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 3, 71 | "metadata": { 72 | "collapsed": true 73 | }, 74 | "outputs": [], 75 | "source": [ 76 | "def median(minheap, maxheap):\n", 77 | " median = 0\n", 78 | " \n", 79 | " if len(minheap) == len(maxheap):\n", 80 | " median = ( minheap[0] + (maxheap[0]* -1) ) / 2\n", 81 | " \n", 82 | " # the way we add to the heaps, this should\n", 83 | " # never happen, but we leave it here just in case \n", 84 | " elif len(minheap) > len(maxheap):\n", 85 | " median = minheap[0]\n", 86 | " \n", 87 | " else:\n", 88 | " median = maxheap[0]* -1\n", 89 | " \n", 90 | " return median" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 4, 96 | "metadata": {}, 97 | "outputs": [ 98 | { 99 | "name": "stdout", 100 | "output_type": "stream", 101 | "text": [ 102 | "[8, 8, 16, 13, 22, 23, 25] [-6, -5, -4, -3, -2, -1, -3, 0]\n", 103 | "6\n" 104 | ] 105 | } 106 | ], 107 | "source": [ 108 | "test_min = []\n", 109 | "test_max = []\n", 110 | "items = [3, 3, 2, 13, 8, 16, 1, 23, 0, 22, 4, 6, 8, 25, 5]\n", 111 | "\n", 112 | "test_min, test_max = add_to_median_heap(test_min, test_max, items)\n", 113 | "print test_min, test_max\n", 114 | "\n", 115 | "print median(test_min, test_max)" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": 5, 121 | "metadata": { 122 | "collapsed": true 123 | }, 124 | "outputs": [], 125 | "source": [ 126 | "def quicksort(array, low, high):\n", 127 | " \n", 128 | " # if we reached end of section\n", 129 | " if low < high:\n", 130 | " \n", 131 | " part_index = partition(array, low, high)\n", 132 | " \n", 133 | " # sort the rest of the array recursively\n", 134 | " # and in place\n", 135 | " quicksort(array, low, part_index - 1)\n", 136 | " quicksort(array, part_index + 1, high)\n", 137 | " \n", 138 | " return array\n", 139 | " " 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": 6, 145 | "metadata": { 146 | "collapsed": true 147 | }, 148 | "outputs": [], 149 | "source": [ 150 | "def partition(array, low, high):\n", 151 | " \n", 152 | " pivot_index = high\n", 153 | " i = low - 1\n", 154 | " \n", 155 | " for j in range(low, high):\n", 156 | " \n", 157 | " # if current item is smaller than or equal\n", 158 | " # to pivot (last value), swap them & increment i\n", 159 | " if array[j] <= pivot_index:\n", 160 | " i += 1\n", 161 | " array[i], array[j] = array[j], array[i]\n", 162 | " \n", 163 | " array[i + 1], array[high] = array[high], array[i + 1]\n", 164 | " return i + 1" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": 7, 170 | "metadata": { 171 | "collapsed": true 172 | }, 173 | "outputs": [], 174 | "source": [ 175 | "def quickSelect(A, left, right, k):\n", 176 | " if left == right:\n", 177 | " return A[left]\n", 178 | " pivotIndex = partition(A, left, right)\n", 179 | " \n", 180 | " if k == pivotIndex:\n", 181 | " return A[k]\n", 182 | " elif k < pivotIndex:\n", 183 | " return quickSelect(A, 1, pivotIndex-1,k)\n", 184 | " else:\n", 185 | " return quickSelect(A, pivotIndex+1,right, k - pivotIndex)" 186 | ] 187 | }, 188 | { 189 | "cell_type": "code", 190 | "execution_count": 12, 191 | "metadata": {}, 192 | "outputs": [ 193 | { 194 | "data": { 195 | "text/plain": [ 196 | "4" 197 | ] 198 | }, 199 | "execution_count": 12, 200 | "metadata": {}, 201 | "output_type": "execute_result" 202 | } 203 | ], 204 | "source": [ 205 | "test_list = [8, 3, 6, 0, 1, 2, 7, 4]\n", 206 | "\n", 207 | "quickSelect(test_list[:], 0, len(test_list)-1, 6)" 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "execution_count": null, 213 | "metadata": { 214 | "collapsed": true 215 | }, 216 | "outputs": [], 217 | "source": [] 218 | }, 219 | { 220 | "cell_type": "code", 221 | "execution_count": null, 222 | "metadata": { 223 | "collapsed": true 224 | }, 225 | "outputs": [], 226 | "source": [] 227 | } 228 | ], 229 | "metadata": { 230 | "kernelspec": { 231 | "display_name": "Python 2", 232 | "language": "python", 233 | "name": "python2" 234 | }, 235 | "language_info": { 236 | "codemirror_mode": { 237 | "name": "ipython", 238 | "version": 2 239 | }, 240 | "file_extension": ".py", 241 | "mimetype": "text/x-python", 242 | "name": "python", 243 | "nbconvert_exporter": "python", 244 | "pygments_lexer": "ipython2", 245 | "version": "2.7.14" 246 | } 247 | }, 248 | "nbformat": 4, 249 | "nbformat_minor": 2 250 | } 251 | -------------------------------------------------------------------------------- /18:02:22 Hash tables and functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import string\n", 10 | "import random\n", 11 | "import numpy\n", 12 | "import time\n", 13 | "\n", 14 | "\n", 15 | "def da_search(T,k):\n", 16 | " return T[k]\n", 17 | "\n", 18 | "def da_insert(T,x):\n", 19 | " # T[key[x]] = x\n", 20 | " T[x] = x\n", 21 | " \n", 22 | "def da_delete(T,x):\n", 23 | " T[x] = None\n", 24 | " \n", 25 | "def word_generator(array, arr_len, size = 4, chars=string.ascii_lowercase):\n", 26 | " for i in range(0,arr_len-1):\n", 27 | " array[i] = ''.join(random.choice(chars) for _ in range(size))\n", 28 | " return array\n", 29 | "\n", 30 | "def empty_guesses(n):\n", 31 | " array = numpy.zeros(shape = (n,1))\n", 32 | " return array" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": {}, 38 | "source": [ 39 | "**Direct Address Tables:**\\ As the first step in setting up a crossword solving algorithm you need to create 2 direct address tables, one to store all \\ the “up” answers-whether correct or not-and one to store all the “across” answers. Write some code to create a direct address table that allows you:\n", 40 | "\n", 41 | "1. initialize N empty guesses\n", 42 | "1. set a guess for the i-th entry\n", 43 | "1. clear an incorrect guess for the i-th entry\n", 44 | "\n", 45 | "**Social Security:**\n", 46 | "\n", 47 | "Could we use a direct address table to store a country's entire set of social\\ security numbers (aka id numbers)? Why or why not?" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": null, 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [ 56 | "up_answers = empty_guesses(5)\n", 57 | "across_answers = empty_guesses(5)\n", 58 | "\n", 59 | "up_answers = word_generator(up_answers, 5)\n", 60 | "across_answers = word_generator(across_answers, 5)\n", 61 | "\n", 62 | "up_guess = \"helo\"\n", 63 | "def check_guess(table, i, guess):\n", 64 | " if table[i] == guess:\n", 65 | " return True\n", 66 | " \n", 67 | "check_guess(up_answers, 4, up_guess)" 68 | ] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "metadata": {}, 73 | "source": [ 74 | "**Chained Hash-table**\n", 75 | "\n", 76 | "Using the code from: https://gist.github.com/philip-sterne/47ff8a8e38e6f7b4ecc5733d13d86372, do the following:\n", 77 | "\n", 78 | "1. Complete the missing sections of code\n", 79 | "1. Using the code, create 100,000 words of 10 characters each.\n", 80 | "1. Create four chained hash-tables with 5000 slots.\n", 81 | "1. Store all the words in each chained hash table using each of the different hash functions.\n", 82 | "1. Measure the number of collisions for each hash function.\n", 83 | "1. For each of the hash functions, how many elements are in a bucket on average (if it is not empty)?\n", 84 | "1. Time how long it take to find elements that are in each hash table.\n", 85 | "1. For each hash table, time how long it takes to find 10,000 elements that have not been stored." 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": null, 91 | "metadata": { 92 | "collapsed": true 93 | }, 94 | "outputs": [], 95 | "source": [ 96 | "import random\n", 97 | "import string\n", 98 | "\n", 99 | "\n", 100 | "def randomword(length):\n", 101 | " return ''.join(random.choice(string.lowercase) for i in range(length))\n", 102 | "\n", 103 | "\n", 104 | "def empty_hash_table(N):\n", 105 | " return [[] for n in range(N)]\n", 106 | "\n", 107 | "\n", 108 | "def add_to_hash_table(hash_table, item, hash_function):\n", 109 | " N = len(hash_table)\n", 110 | " hash_table[hash_function(item)%N].append(item)\n", 111 | " \n", 112 | " return hash_table\n", 113 | "\n", 114 | "\n", 115 | "def contains(hash_table, item, hash_function):\n", 116 | " N = len(hash_table)\n", 117 | " \n", 118 | " key_to_check = hash_function(item)%N\n", 119 | " \n", 120 | " #this allows for multiple items to be checked\n", 121 | " for i in hash_table[key_to_check]:\n", 122 | " if i == item:\n", 123 | " return True\n", 124 | " else:\n", 125 | " return False\n", 126 | "\n", 127 | "def remove(hash_table, item, hash_function):\n", 128 | " if not contains(hash_table, item, hash_function):\n", 129 | " raise ValueError()\n", 130 | " N = len(hash_table)\n", 131 | " \n", 132 | " key_to_check = hash_function(item)%N\n", 133 | " \n", 134 | " for i in hash_table[key_to_check]:\n", 135 | " if i == item:\n", 136 | " hash_table[key_to_check[i]] = None\n", 137 | "\n", 138 | " return hash_table\n", 139 | "\n", 140 | "\n", 141 | "def hash_str1(string):\n", 142 | " ans = 0\n", 143 | " for chr in string:\n", 144 | " ans += ord(chr)\n", 145 | " return ans\n", 146 | "\n", 147 | "\n", 148 | "def hash_str2(string):\n", 149 | " ans = 0\n", 150 | " for chr in string:\n", 151 | " ans = ans ^ ord(chr)\n", 152 | " return ans\n", 153 | "\n", 154 | "\n", 155 | "def hash_str3(string):\n", 156 | " ans = 0\n", 157 | " for chr in string:\n", 158 | " ans = ans * 128 + ord(chr)\n", 159 | " return ans\n", 160 | "\n", 161 | "\n", 162 | "def hash_str4(string):\n", 163 | " random.seed(ord(string[0]))\n", 164 | " return random.getrandbits(32)" 165 | ] 166 | }, 167 | { 168 | "cell_type": "markdown", 169 | "metadata": {}, 170 | "source": [ 171 | "Question 2: Generate list" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": null, 177 | "metadata": {}, 178 | "outputs": [], 179 | "source": [ 180 | "list_of_words = [randomword(100) for i in range(100000)]" 181 | ] 182 | }, 183 | { 184 | "cell_type": "markdown", 185 | "metadata": {}, 186 | "source": [ 187 | "Question 3: Create four chained hash-tables with 5000 slots." 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": null, 193 | "metadata": { 194 | "collapsed": true 195 | }, 196 | "outputs": [], 197 | "source": [ 198 | "hash_tables = [chained_hash_table_1, chained_hash_table_2, \n", 199 | " chained_hash_table_3, chained_hash_table_4]\n", 200 | "\n", 201 | "for table in hash_tables:\n", 202 | " table = empty_hash_table(5000)" 203 | ] 204 | }, 205 | { 206 | "cell_type": "markdown", 207 | "metadata": {}, 208 | "source": [ 209 | "Question 4: Store all the words in each chained hash table using each of the different hash functions." 210 | ] 211 | }, 212 | { 213 | "cell_type": "code", 214 | "execution_count": null, 215 | "metadata": {}, 216 | "outputs": [], 217 | "source": [ 218 | "hash_functions = [hash_str1, hash_str2, hash_str3, hash_str4]\n", 219 | "\n", 220 | "function_counter = -1\n", 221 | "for table in hash_tables:\n", 222 | " function_counter += 1\n", 223 | " for item in list_of_words:\n", 224 | " table = add_to_hash_table(table, item,\n", 225 | " hash_functions[function_counter])\n" 226 | ] 227 | }, 228 | { 229 | "cell_type": "markdown", 230 | "metadata": {}, 231 | "source": [ 232 | "Question 5: Measure the number of collisions for each hash function." 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": null, 238 | "metadata": {}, 239 | "outputs": [], 240 | "source": [ 241 | "def collision_counter(hash_table):\n", 242 | " collisions = 0\n", 243 | " for bucket in hash_table:\n", 244 | " bucket_len = len(bucket)\n", 245 | " if bucket_len > 1:\n", 246 | " collisions += bucket_len -1\n", 247 | " print collisions\n", 248 | " \n", 249 | "collision_counter(chained_hash_table_1)\n", 250 | "collision_counter(chained_hash_table_2)\n", 251 | "collision_counter(chained_hash_table_3)\n", 252 | "collision_counter(chained_hash_table_4)" 253 | ] 254 | }, 255 | { 256 | "cell_type": "markdown", 257 | "metadata": {}, 258 | "source": [ 259 | "Question 6: For each of the hash functions, how many elements are in a bucket on average (if it is not empty)?" 260 | ] 261 | }, 262 | { 263 | "cell_type": "code", 264 | "execution_count": null, 265 | "metadata": {}, 266 | "outputs": [], 267 | "source": [ 268 | "def average_element_counter(hash_table):\n", 269 | " average_len = numpy.average([len(bucket) for bucket in hash_table])\n", 270 | " print average_len\n", 271 | " \n", 272 | "average_element_counter(chained_hash_table_1)\n", 273 | "average_element_counter(chained_hash_table_2)\n", 274 | "average_element_counter(chained_hash_table_3)\n", 275 | "average_element_counter(chained_hash_table_4)" 276 | ] 277 | }, 278 | { 279 | "cell_type": "markdown", 280 | "metadata": {}, 281 | "source": [ 282 | "Question 7: Time how long it take to find elements that are in each hash table." 283 | ] 284 | }, 285 | { 286 | "cell_type": "code", 287 | "execution_count": null, 288 | "metadata": {}, 289 | "outputs": [], 290 | "source": [ 291 | "function_counter = -1\n", 292 | "for table in hash_tables:\n", 293 | " start = time.time()\n", 294 | " function_counter += 1\n", 295 | " for item in list_of_words:\n", 296 | " contains(table, item, hash_functions[function_counter])\n", 297 | " \n", 298 | " end = time.time()\n", 299 | " print end-start\n" 300 | ] 301 | }, 302 | { 303 | "cell_type": "markdown", 304 | "metadata": {}, 305 | "source": [ 306 | "Question 8: For each hash table, time how long it takes to find 10,000 elements that have not been stored." 307 | ] 308 | }, 309 | { 310 | "cell_type": "code", 311 | "execution_count": null, 312 | "metadata": {}, 313 | "outputs": [], 314 | "source": [ 315 | "wrong_guess = []\n", 316 | "\n", 317 | "for i in range(0,100000):\n", 318 | " wrong_guess.append(randomword(99))\n", 319 | "\n", 320 | "function_counter = -1\n", 321 | "for table in hash_tables:\n", 322 | " start = time.time()\n", 323 | " function_counter += 1\n", 324 | " for item in wrong_guess:\n", 325 | " contains(table, item, hash_functions[function_counter])\n", 326 | " \n", 327 | " end = time.time()\n", 328 | " print end-start" 329 | ] 330 | }, 331 | { 332 | "cell_type": "code", 333 | "execution_count": null, 334 | "metadata": { 335 | "collapsed": true 336 | }, 337 | "outputs": [], 338 | "source": [] 339 | } 340 | ], 341 | "metadata": { 342 | "kernelspec": { 343 | "display_name": "Python 2", 344 | "language": "python", 345 | "name": "python2" 346 | }, 347 | "language_info": { 348 | "codemirror_mode": { 349 | "name": "ipython", 350 | "version": 2 351 | }, 352 | "file_extension": ".py", 353 | "mimetype": "text/x-python", 354 | "name": "python", 355 | "nbconvert_exporter": "python", 356 | "pygments_lexer": "ipython2", 357 | "version": "2.7.14" 358 | } 359 | }, 360 | "nbformat": 4, 361 | "nbformat_minor": 2 362 | } 363 | -------------------------------------------------------------------------------- /18:03:01 Binary Search Trees.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 4, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | " import random" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 11, 17 | "metadata": { 18 | "collapsed": true 19 | }, 20 | "outputs": [], 21 | "source": [ 22 | "class Node:\n", 23 | " def __init__(self, val):\n", 24 | " self.l_child = None\n", 25 | " self.r_child = None\n", 26 | " self.data = val\n", 27 | " self.parent = None\n", 28 | " \n", 29 | "def insert(root, node):\n", 30 | " if root is None:\n", 31 | " root = node\n", 32 | " else:\n", 33 | " if root.data > node.data:\n", 34 | " if root.l_child is None:\n", 35 | " root.l_child = node\n", 36 | " node.parent = root\n", 37 | " else:\n", 38 | " insert(root.l_child, node)\n", 39 | " else:\n", 40 | " if root.r_child is None:\n", 41 | " root.r_child = node\n", 42 | " node.parent = root\n", 43 | " else:\n", 44 | " insert(root.r_child, node)\n", 45 | " return root\n", 46 | " \n", 47 | "def search(root, value):\n", 48 | " if root == None or root.data == value:\n", 49 | " return root\n", 50 | " elif value < root.data:\n", 51 | " return search(root.l_child, value)\n", 52 | " else:\n", 53 | " return search(root.r_child, value)\n", 54 | "\n", 55 | "def transplant(root, u1, u2):\n", 56 | " if u1 == root:\n", 57 | " root = u2\n", 58 | " elif u1 == u1.parent.l_child:\n", 59 | " u1.parent.l_child = u2\n", 60 | " else:\n", 61 | " u1.parent.r_child = u2 \n", 62 | " if u2 != None:\n", 63 | " u2.parent = u1.parent\n", 64 | " \n", 65 | "def delete_node(root, node): \n", 66 | " if node.l_child == None:\n", 67 | " transplant(root, node, node.r_child)\n", 68 | " elif node.r_child == None:\n", 69 | " transplant(root, node, node.l_child)\n", 70 | " else:\n", 71 | " y = tree_min(node.r_child)\n", 72 | " if y.parent != node:\n", 73 | " transplant(root, y, y.r_child)\n", 74 | " y.r_child = node.r_child\n", 75 | " y.r_child.parent = y\n", 76 | " transplant(root, node, y)\n", 77 | " y.l_child = node.l_child\n", 78 | " y.l_child.parent = y\n", 79 | " \n", 80 | "def tree_min(root):\n", 81 | " while root.l_child:\n", 82 | " root = root.l_child\n", 83 | " return root\n", 84 | "\n", 85 | "def traversal(root):\n", 86 | " if root != None:\n", 87 | " traversal(root.l_child)\n", 88 | " print root.data\n", 89 | " traversal(root.r_child)" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 95, 95 | "metadata": {}, 96 | "outputs": [ 97 | { 98 | "name": "stdout", 99 | "output_type": "stream", 100 | "text": [ 101 | "Treemin 10\n", 102 | "Searching for 10 10\n", 103 | "---------------\n", 104 | "10\n", 105 | "26\n", 106 | "27\n", 107 | "30\n", 108 | "30\n", 109 | "31\n", 110 | "35\n", 111 | "35\n", 112 | "37\n", 113 | "41\n", 114 | "41\n", 115 | "43\n", 116 | "43\n", 117 | "44\n", 118 | "45\n", 119 | "51\n", 120 | "51\n", 121 | "52\n", 122 | "54\n", 123 | "57\n", 124 | "59\n", 125 | "60\n", 126 | "61\n", 127 | "63\n", 128 | "67\n", 129 | "67\n", 130 | "67\n", 131 | "68\n", 132 | "68\n", 133 | "69\n", 134 | "70\n", 135 | "71\n", 136 | "72\n", 137 | "73\n", 138 | "74\n", 139 | "78\n", 140 | "78\n", 141 | "79\n", 142 | "80\n", 143 | "82\n", 144 | "83\n", 145 | "84\n", 146 | "90\n", 147 | "91\n", 148 | "92\n", 149 | "96\n", 150 | "96\n", 151 | "99\n", 152 | "100\n", 153 | "100\n", 154 | "100\n", 155 | "Traversal None\n", 156 | "---------------\n", 157 | "deleting 10\n", 158 | "---------------\n", 159 | "10\n", 160 | "26\n", 161 | "27\n", 162 | "30\n", 163 | "30\n", 164 | "31\n", 165 | "35\n", 166 | "35\n", 167 | "37\n", 168 | "41\n", 169 | "41\n", 170 | "43\n", 171 | "43\n", 172 | "44\n", 173 | "45\n", 174 | "51\n", 175 | "51\n", 176 | "52\n", 177 | "54\n", 178 | "57\n", 179 | "59\n", 180 | "60\n", 181 | "61\n", 182 | "63\n", 183 | "67\n", 184 | "67\n", 185 | "67\n", 186 | "68\n", 187 | "68\n", 188 | "69\n", 189 | "70\n", 190 | "71\n", 191 | "72\n", 192 | "73\n", 193 | "74\n", 194 | "78\n", 195 | "78\n", 196 | "79\n", 197 | "80\n", 198 | "82\n", 199 | "83\n", 200 | "84\n", 201 | "90\n", 202 | "91\n", 203 | "92\n", 204 | "96\n", 205 | "96\n", 206 | "99\n", 207 | "100\n", 208 | "100\n", 209 | "100\n", 210 | "Traversal after deletion None\n", 211 | "---------------\n", 212 | "10\n" 213 | ] 214 | } 215 | ], 216 | "source": [ 217 | "tree = Node(10)\n", 218 | "for i in range(50):\n", 219 | " insert(tree, Node(random.randint(15,100)))\n", 220 | "\n", 221 | "print \"Treemin \" + str(tree_min(tree).data)\n", 222 | "print \"Searching for 10 \" + str(search(tree, 10).data)\n", 223 | "print \"---------------\"\n", 224 | "print \"Traversal \" + str(traversal(tree))\n", 225 | "print \"---------------\"\n", 226 | "print \"deleting 10\"\n", 227 | "delete_node(tree, search(tree,10))\n", 228 | "print \"---------------\"\n", 229 | "print \"Traversal after deletion \" + str(traversal(tree))\n", 230 | "print \"---------------\"\n", 231 | "print search(tree, 10).data" 232 | ] 233 | }, 234 | { 235 | "cell_type": "code", 236 | "execution_count": null, 237 | "metadata": { 238 | "collapsed": true 239 | }, 240 | "outputs": [], 241 | "source": [] 242 | } 243 | ], 244 | "metadata": { 245 | "kernelspec": { 246 | "display_name": "Python 2", 247 | "language": "python", 248 | "name": "python2" 249 | }, 250 | "language_info": { 251 | "codemirror_mode": { 252 | "name": "ipython", 253 | "version": 2 254 | }, 255 | "file_extension": ".py", 256 | "mimetype": "text/x-python", 257 | "name": "python", 258 | "nbconvert_exporter": "python", 259 | "pygments_lexer": "ipython2", 260 | "version": "2.7.14" 261 | } 262 | }, 263 | "nbformat": 4, 264 | "nbformat_minor": 2 265 | } 266 | -------------------------------------------------------------------------------- /18:03:06 Randomized BST.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import random" 12 | ] 13 | }, 14 | { 15 | "cell_type": "markdown", 16 | "metadata": {}, 17 | "source": [ 18 | "## Question 1 - Randomized BST" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 4, 24 | "metadata": { 25 | "collapsed": true 26 | }, 27 | "outputs": [], 28 | "source": [ 29 | "class Node:\n", 30 | " def __init__(self, val):\n", 31 | " self.l_child = None\n", 32 | " self.r_child = None\n", 33 | " self.data = val\n", 34 | " self.parent = None\n", 35 | " self.left_of = 0\n", 36 | " \n", 37 | "def insert(root, node):\n", 38 | " if root is None:\n", 39 | " root = node\n", 40 | " else:\n", 41 | " if root.data > node.data:\n", 42 | " root.left_of += 1\n", 43 | " if root.l_child is None:\n", 44 | " root.l_child = node\n", 45 | " node.parent = root\n", 46 | " else:\n", 47 | " insert(root.l_child, node)\n", 48 | " else:\n", 49 | " if root.r_child is None:\n", 50 | " root.r_child = node\n", 51 | " node.parent = root\n", 52 | " else:\n", 53 | " insert(root.r_child, node)\n", 54 | " return root\n", 55 | " \n", 56 | "def search(root, value):\n", 57 | " if root == None or root.data == value:\n", 58 | " return root\n", 59 | " elif value < root.data:\n", 60 | " return search(root.l_child, value)\n", 61 | " else:\n", 62 | " return search(root.r_child, value)\n", 63 | "\n", 64 | "def transplant(root, u1, u2):\n", 65 | " if u1 == root:\n", 66 | " root = u2\n", 67 | " elif u1 == u1.parent.l_child:\n", 68 | " u1.parent.l_child = u2\n", 69 | " else:\n", 70 | " u1.parent.r_child = u2 \n", 71 | " if u2 != None:\n", 72 | " u2.parent = u1.parent\n", 73 | " \n", 74 | "def delete_node(root, node): \n", 75 | " if node.l_child == None:\n", 76 | " transplant(root, node, node.r_child)\n", 77 | " elif node.r_child == None:\n", 78 | " transplant(root, node, node.l_child)\n", 79 | " else:\n", 80 | " y = tree_min(node.r_child)\n", 81 | " if y.parent != node:\n", 82 | " transplant(root, y, y.r_child)\n", 83 | " y.r_child = node.r_child\n", 84 | " y.r_child.parent = y\n", 85 | " transplant(root, node, y)\n", 86 | " y.l_child = node.l_child\n", 87 | " y.l_child.parent = y\n", 88 | " \n", 89 | "def tree_min(root):\n", 90 | " while root.l_child:\n", 91 | " root = root.l_child\n", 92 | " return root\n", 93 | "\n", 94 | "def traversal(root):\n", 95 | " if root != None:\n", 96 | " traversal(root.l_child)\n", 97 | " print root.data\n", 98 | " traversal(root.r_child)" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 2, 104 | "metadata": {}, 105 | "outputs": [ 106 | { 107 | "ename": "NameError", 108 | "evalue": "name 'random' is not defined", 109 | "output_type": "error", 110 | "traceback": [ 111 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 112 | "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", 113 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m901\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m \u001b[0minsert\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnonorder_tree\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mNode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrandom\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrandint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m901\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 9\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", 114 | "\u001b[0;31mNameError\u001b[0m: name 'random' is not defined" 115 | ] 116 | } 117 | ], 118 | "source": [ 119 | "order_tree = Node(0)\n", 120 | "nonorder_tree = Node(0)\n", 121 | "counter = 0\n", 122 | "for i in range(1,901):\n", 123 | " insert(order_tree, Node(i))\n", 124 | " \n", 125 | "for i in range(1,901):\n", 126 | " insert(nonorder_tree, Node(random.randint(1,901)))\n", 127 | " \n", 128 | "\n", 129 | "\n", 130 | "print \"Treemin \" + str(tree_min(order_tree).data)\n", 131 | "print \"Searching for 10 \" + str(search(order_tree, 10).data)\n", 132 | "print \"---------------\"\n", 133 | "# print \"Traversal \" + str(traversal(tree))" 134 | ] 135 | }, 136 | { 137 | "cell_type": "markdown", 138 | "metadata": {}, 139 | "source": [ 140 | "## Question 2 - Average number of comparisons" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 88, 146 | "metadata": { 147 | "collapsed": true 148 | }, 149 | "outputs": [], 150 | "source": [ 151 | "def search_avg_comparisons(root, value, counter): \n", 152 | " if root == None or root.data == value:\n", 153 | " counter += 1\n", 154 | " return counter\n", 155 | " elif value < root.data:\n", 156 | " counter += 1\n", 157 | " return search_avg_comparisons(root.l_child, value, counter)\n", 158 | " else:\n", 159 | " counter += 1\n", 160 | " return search_avg_comparisons(root.r_child, value, counter)" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": 90, 166 | "metadata": {}, 167 | "outputs": [ 168 | { 169 | "name": "stdout", 170 | "output_type": "stream", 171 | "text": [ 172 | "Calculating average number of comparisons for tree: \n", 173 | "Average num of comparisons: \n", 174 | "12.615\n", 175 | "This is something around the order of log_2(n).\n" 176 | ] 177 | } 178 | ], 179 | "source": [ 180 | "print \"Calculating average number of comparisons for tree: \"\n", 181 | "avg_counter = 0\n", 182 | "for i in range(200):\n", 183 | " search_item = random.randint(0,901)\n", 184 | " avg_counter = search_avg_comparisons(nonorder_tree, search_item, avg_counter)\n", 185 | "print \"Average num of comparisons: \"\n", 186 | "print avg_counter/200.0\n", 187 | "print \"This is something around the order of log_2(n).\"" 188 | ] 189 | }, 190 | { 191 | "cell_type": "markdown", 192 | "metadata": {}, 193 | "source": [ 194 | "## Question 3 - Max and average tree height" 195 | ] 196 | }, 197 | { 198 | "cell_type": "code", 199 | "execution_count": 91, 200 | "metadata": { 201 | "collapsed": true 202 | }, 203 | "outputs": [], 204 | "source": [ 205 | "def max_height(bst):\n", 206 | " if bst is None:\n", 207 | " return 0\n", 208 | " return 1+max(max_height(bst.l_child), max_height(bst.r_child))\n", 209 | "\n", 210 | "\n", 211 | "def min_height(bst):\n", 212 | " if bst is None:\n", 213 | " return 0\n", 214 | " return 1+ min(min_height(bst.l_child), min_height(bst.r_child)) \n", 215 | "\n", 216 | "\n", 217 | "def avg_height(root, trials): \n", 218 | " direction = [\"left\", \"right\"]\n", 219 | " height = 0\n", 220 | "\n", 221 | " for i in range(trials):\n", 222 | " temp_root = root\n", 223 | " while temp_root:\n", 224 | " dir_pick = direction[random.randint(0, 1)]\n", 225 | " if dir_pick == \"right\" and temp_root.r_child != None:\n", 226 | " temp_root = temp_root.r_child\n", 227 | " height += 1\n", 228 | " elif dir_pick == \"left\" and temp_root.l_child != None:\n", 229 | " temp_root = temp_root.l_child\n", 230 | " height += 1\n", 231 | " else:\n", 232 | " temp_root = None\n", 233 | "\n", 234 | " return float(height)/trials" 235 | ] 236 | }, 237 | { 238 | "cell_type": "code", 239 | "execution_count": 94, 240 | "metadata": {}, 241 | "outputs": [ 242 | { 243 | "name": "stdout", 244 | "output_type": "stream", 245 | "text": [ 246 | "Calculating max height of tree: \n", 247 | "Max height order_tree: 900\n", 248 | "Max height nonorder_tree: 10\n", 249 | "Calculating average height of tree out of 50 trials: \n", 250 | "Average height order_tree: 1.132\n", 251 | "Average height nonorder_tree: 3.9\n" 252 | ] 253 | } 254 | ], 255 | "source": [ 256 | "print \"Calculating max height of tree: \"\n", 257 | "max_order = 0\n", 258 | "max_order = max_height(order_tree)\n", 259 | "max_nonorder = 0\n", 260 | "max_nonorder = max_height(nonorder_tree)\n", 261 | "\n", 262 | "print \"Max height order_tree: \" + str(max_order)\n", 263 | "print \"Max height nonorder_tree: \" + str(max_nonorder)\n", 264 | "\n", 265 | "print \"Calculating average height of tree out of 50 trials: \"\n", 266 | "trials = 500\n", 267 | "avg_order = 0\n", 268 | "avg_order = avg_height(order_tree, trials)\n", 269 | "avg_nonorder = 0\n", 270 | "avg_nonorder = avg_height(nonorder_tree, trials)\n", 271 | "\n", 272 | "print \"Average height order_tree: \" + str(avg_order)\n", 273 | "print \"Average height nonorder_tree: \" + str(avg_nonorder)" 274 | ] 275 | }, 276 | { 277 | "cell_type": "markdown", 278 | "metadata": {}, 279 | "source": [ 280 | "## Question 4 - Select/Rank " 281 | ] 282 | }, 283 | { 284 | "cell_type": "code", 285 | "execution_count": 17, 286 | "metadata": { 287 | "collapsed": true 288 | }, 289 | "outputs": [], 290 | "source": [ 291 | "def select(root, k):\n", 292 | " if root.left_of > k:\n", 293 | " return select(root.l_child, k)\n", 294 | " elif root.left_of < k:\n", 295 | " return select(root.r_child, k - root.left_of - 1)\n", 296 | " else:\n", 297 | " return root.data\n", 298 | " \n", 299 | "def rank(root, value):\n", 300 | " if root is None:\n", 301 | " raise ValueError\n", 302 | " if root.data == value:\n", 303 | " return root.left_of\n", 304 | " if root.data < value:\n", 305 | " return rank(root.r_child, value) + 1 + root.left_of\n", 306 | " if root.data > val:\n", 307 | " return rank(root.l_child, value)" 308 | ] 309 | }, 310 | { 311 | "cell_type": "code", 312 | "execution_count": 18, 313 | "metadata": {}, 314 | "outputs": [ 315 | { 316 | "name": "stdout", 317 | "output_type": "stream", 318 | "text": [ 319 | "63\n", 320 | "9\n", 321 | "10\n", 322 | "27\n", 323 | "33\n", 324 | "34\n", 325 | "37\n", 326 | "42\n", 327 | "47\n", 328 | "50\n", 329 | "60\n", 330 | "63\n", 331 | "67\n", 332 | "79\n", 333 | "89\n", 334 | "91\n", 335 | "92\n", 336 | "96\n", 337 | "None\n" 338 | ] 339 | } 340 | ], 341 | "source": [ 342 | "tree_new = Node(63)\n", 343 | "for i in range(15):\n", 344 | " insert(tree_new, Node(random.randint(0,100)))\n", 345 | "\n", 346 | "print tree_new.data\n", 347 | "print(tree_new.left_of)\n", 348 | "print traversal(tree_new)" 349 | ] 350 | }, 351 | { 352 | "cell_type": "code", 353 | "execution_count": 19, 354 | "metadata": {}, 355 | "outputs": [ 356 | { 357 | "name": "stdout", 358 | "output_type": "stream", 359 | "text": [ 360 | "67\n", 361 | "10\n" 362 | ] 363 | } 364 | ], 365 | "source": [ 366 | "print select(tree_new, 10)\n", 367 | "print rank(tree_new, select(tree_new, 10))" 368 | ] 369 | } 370 | ], 371 | "metadata": { 372 | "kernelspec": { 373 | "display_name": "Python 2", 374 | "language": "python", 375 | "name": "python2" 376 | }, 377 | "language_info": { 378 | "codemirror_mode": { 379 | "name": "ipython", 380 | "version": 2 381 | }, 382 | "file_extension": ".py", 383 | "mimetype": "text/x-python", 384 | "name": "python", 385 | "nbconvert_exporter": "python", 386 | "pygments_lexer": "ipython2", 387 | "version": "2.7.14" 388 | } 389 | }, 390 | "nbformat": 4, 391 | "nbformat_minor": 2 392 | } 393 | -------------------------------------------------------------------------------- /18:03:23 Dynamic Programming - Rod cutting problem.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import timeit\n", 12 | "import matplotlib.pyplot as plt" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 2, 18 | "metadata": { 19 | "collapsed": true 20 | }, 21 | "outputs": [], 22 | "source": [ 23 | "def cut_rod(price_list, rod_len):\n", 24 | "\t\"\"\"\n", 25 | "\tStandard rod cutting algorithm without dynamic programming.\n", 26 | "\t\"\"\"\n", 27 | "\tif rod_len == 0:\n", 28 | "\t\treturn 0\n", 29 | "\tcurrent_price = -10\n", 30 | "\tfor i in range(1, rod_len+1):\n", 31 | "\t\tcurrent_price = max(current_price, price_list[i] + cut_rod(price_list, rod_len - 1))\n", 32 | "\treturn current_price" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 3, 38 | "metadata": {}, 39 | "outputs": [ 40 | { 41 | "ename": "KeyboardInterrupt", 42 | "evalue": "", 43 | "output_type": "error", 44 | "traceback": [ 45 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 46 | "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", 47 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0mtemp_avg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0mstart\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtimeit\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtimeit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 11\u001b[0;31m \u001b[0m_\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcut_rod\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprice_list\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 12\u001b[0m \u001b[0mend\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtimeit\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtimeit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0mtime_stamp\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mend\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0mstart\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 48 | "\u001b[0;32m\u001b[0m in \u001b[0;36mcut_rod\u001b[0;34m(price_list, rod_len)\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcurrent_price\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mprice_list\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mcut_rod\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprice_list\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 10\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mcurrent_price\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 49 | "\u001b[0;32m\u001b[0m in \u001b[0;36mcut_rod\u001b[0;34m(price_list, rod_len)\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcurrent_price\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mprice_list\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mcut_rod\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprice_list\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 10\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mcurrent_price\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 50 | "\u001b[0;32m\u001b[0m in \u001b[0;36mcut_rod\u001b[0;34m(price_list, rod_len)\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcurrent_price\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mprice_list\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mcut_rod\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprice_list\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 10\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mcurrent_price\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 51 | "\u001b[0;32m\u001b[0m in \u001b[0;36mcut_rod\u001b[0;34m(price_list, rod_len)\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcurrent_price\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mprice_list\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mcut_rod\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprice_list\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 10\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mcurrent_price\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 52 | "\u001b[0;32m\u001b[0m in \u001b[0;36mcut_rod\u001b[0;34m(price_list, rod_len)\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcurrent_price\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mprice_list\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mcut_rod\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprice_list\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 10\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mcurrent_price\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 53 | "\u001b[0;32m\u001b[0m in \u001b[0;36mcut_rod\u001b[0;34m(price_list, rod_len)\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcurrent_price\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mprice_list\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mcut_rod\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprice_list\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 10\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mcurrent_price\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 54 | "\u001b[0;32m\u001b[0m in \u001b[0;36mcut_rod\u001b[0;34m(price_list, rod_len)\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcurrent_price\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mprice_list\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mcut_rod\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprice_list\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 10\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mcurrent_price\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 55 | "\u001b[0;32m\u001b[0m in \u001b[0;36mcut_rod\u001b[0;34m(price_list, rod_len)\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcurrent_price\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mprice_list\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mcut_rod\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprice_list\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 10\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mcurrent_price\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 56 | "\u001b[0;32m\u001b[0m in \u001b[0;36mcut_rod\u001b[0;34m(price_list, rod_len)\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcurrent_price\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mprice_list\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mcut_rod\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprice_list\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrod_len\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 10\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mcurrent_price\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 57 | "\u001b[0;32m\u001b[0m in \u001b[0;36mcut_rod\u001b[0;34m(price_list, rod_len)\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mStandard\u001b[0m \u001b[0mrod\u001b[0m \u001b[0mcutting\u001b[0m \u001b[0malgorithm\u001b[0m \u001b[0mwithout\u001b[0m \u001b[0mdynamic\u001b[0m \u001b[0mprogramming\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \t\"\"\"\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0mrod_len\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mcurrent_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 58 | "\u001b[0;31mKeyboardInterrupt\u001b[0m: " 59 | ] 60 | } 61 | ], 62 | "source": [ 63 | "time = []\n", 64 | "n = range(11)\n", 65 | "price_list = [0, 1, 5, 8, 9, 10, 17, 17, 20, 24, 30, 33, 37, 38, 38, 41,\n", 66 | "\t\t\t 45, 47, 51, 62, 66]\n", 67 | "\n", 68 | "\n", 69 | "for i in range(11):\n", 70 | "\tfor j in range(10):\n", 71 | "\t\ttemp_avg = 0\n", 72 | "\t\tstart = timeit.timeit()\n", 73 | "\t\t_ = cut_rod(price_list, i)\n", 74 | "\t\tend = timeit.timeit()\n", 75 | "\t\ttime_stamp = end - start\n", 76 | "\t\t# print \"Starting point: \" + str(start)\n", 77 | "\t\t# print \"End point: \" + str(end)\n", 78 | "\t\ttemp_avg += time_stamp\n", 79 | "\ttime.append(temp_avg / 10)\n", 80 | "\n", 81 | "print n, time\n", 82 | "plt.plot(n, time)\n", 83 | "plt.show()" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": null, 89 | "metadata": { 90 | "collapsed": true 91 | }, 92 | "outputs": [], 93 | "source": [ 94 | "def bottom_up_cut_rod_with_solution(price_list, rod_len):\n", 95 | "\t\"\"\"\n", 96 | "\tStandard rod cutting algorithm without dynamic programming \n", 97 | "\tnow with the ability to print solutions.\n", 98 | "\t\"\"\"\n", 99 | "\tmax_rev, opt_size = [], range(rod_len)\n", 100 | "\tmax_rev.append(0)\n", 101 | "\tfor i in range(rod_len):\n", 102 | "\t\tcurrent_price = -10\n", 103 | "\t\tfor j in range(rod_len):\n", 104 | "\t\t\ttemp_price = price_list[i] + max_rev[i-j]\n", 105 | "\t\t\tif current_price < temp_price:\n", 106 | "\t\t\t\tcurrent_price = temp_price\n", 107 | "\t\t\t\topt_size[i] = j\n", 108 | "\t\tmax_rev[i] = current_price\n", 109 | "\treturn max_rev, opt_size\n", 110 | "\n", 111 | "def print_cut_rod(price_list, rod_len):\n", 112 | "\tmax_rev, opt_size = bottom_up_cut_rod_with_solution(price_list, rod_len)\n", 113 | "\twhile rod_len > 0:\n", 114 | "\t\tprint opt_size[rod_len]\n", 115 | "\t\trod_len = rod_len - opt_size[n]" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": null, 121 | "metadata": { 122 | "collapsed": true 123 | }, 124 | "outputs": [], 125 | "source": [ 126 | "def fib_memoized(n):\n", 127 | " if n < 2:\n", 128 | " r = [-float(\"inf\"),1,1]\n", 129 | " else:\n", 130 | " r = [-float(\"inf\")] * (n+1)\n", 131 | " r[1] = 1\n", 132 | " r[2] = 1\n", 133 | " return fib_memoized_aux(n,r)\n", 134 | "\n", 135 | "def fib_memoized_aux(n,r):\n", 136 | " if r[n] >= 0:\n", 137 | " return r[n]\n", 138 | " else:\n", 139 | " temp = fib_memoized_aux(n-2,r) + fib_memoized_aux(n-1,r)\n", 140 | " r[n] = temp\n", 141 | " return r[n]" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": null, 147 | "metadata": { 148 | "collapsed": true 149 | }, 150 | "outputs": [], 151 | "source": [ 152 | "def fibonacci_bottom_up(n):\n", 153 | "\tfibs = []\n", 154 | "\tfibs.append(0)\n", 155 | "\tfibs.append(1)\n", 156 | "\tfor i in range (2, n+1):\n", 157 | "\t\tfibs.append(fibs[i-1] + fibs[i-2])\n", 158 | "\treturn fibs[n]\n", 159 | "\n", 160 | "print fib_memoized(10)\n", 161 | "print fibonacci_bottom_up(10)" 162 | ] 163 | } 164 | ], 165 | "metadata": { 166 | "kernelspec": { 167 | "display_name": "Python 2", 168 | "language": "python", 169 | "name": "python2" 170 | }, 171 | "language_info": { 172 | "codemirror_mode": { 173 | "name": "ipython", 174 | "version": 2 175 | }, 176 | "file_extension": ".py", 177 | "mimetype": "text/x-python", 178 | "name": "python", 179 | "nbconvert_exporter": "python", 180 | "pygments_lexer": "ipython2", 181 | "version": "2.7.14" 182 | } 183 | }, 184 | "nbformat": 4, 185 | "nbformat_minor": 2 186 | } 187 | -------------------------------------------------------------------------------- /18:03:27 Dynamic Programming - Currency trade.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Currency Trading" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Imagine that you wish to exchange one currency for another. You realize that instead of directly exchanging one currency for another, you might be better off making a series of trades through other currencies, winding up with the currency you want. Suppose that you can trade n different currencies, numbered 1,2,… ,*n*, where you start with currency 1 and wish to wind up with currency n. You are given, for each pair of currencies *i* and *j* , an exchange rate *rij* , meaning that if you start with *d* units of currency *i* , you can trade for *drij* units of currency *j*.\n", 15 | "\n", 16 | "1. Assuming there is no commission, write python code to solve this problem.\n", 17 | "1. Look up the exchange rates for 3-4 currencies online. What solution does your code find?\n", 18 | "\n", 19 | "Here is some example data:\n", 20 | "\n", 21 | "`USD 1 0.741 0.657 1.061 1.00`\n", 22 | "\n", 23 | "`EUR 1.349 1 0.888 1.433 1.366`\n", 24 | "\n", 25 | "`GBP 1.521 1.126 1 1.614 1.538`\n", 26 | "\n", 27 | "`CHF 0.942 0.698 0.619 1 0.953`\n", 28 | "\n", 29 | "`CAD 0.995 0.732 0.650 1.049 1`" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 72, 35 | "metadata": { 36 | "collapsed": true 37 | }, 38 | "outputs": [], 39 | "source": [ 40 | "# Python program for Bellman-Ford's single source \n", 41 | "# shortest path algorithm.\n", 42 | " \n", 43 | "from collections import defaultdict\n", 44 | " \n", 45 | "#Class to represent a graph\n", 46 | "class Graph:\n", 47 | " \n", 48 | " def __init__(self,vertices):\n", 49 | " self.V= vertices #No. of vertices\n", 50 | " self.graph = [] # default dictionary to store graph\n", 51 | " \n", 52 | " # function to add an edge to graph\n", 53 | " def addEdge(self,u,v,w):\n", 54 | " self.graph.append([u, v, w])\n", 55 | " \n", 56 | " # utility function used to print the solution\n", 57 | " def printArr(self, dist):\n", 58 | " print(\"Vertex Distance from Source\")\n", 59 | " for i in range(self.V):\n", 60 | " print(\"%f \\t\\t %f\" % (i, dist[i]))\n", 61 | " \n", 62 | " # The main function that finds shortest distances from src to\n", 63 | " # all other vertices using Bellman-Ford algorithm. The function\n", 64 | " # also detects negative weight cycle\n", 65 | " def BellmanFord(self, src):\n", 66 | " \n", 67 | " # Step 1: Initialize distances from src to all other vertices\n", 68 | " # as INFINITE\n", 69 | " dist = [float(\"Inf\")] * self.V\n", 70 | " dist[src] = 0\n", 71 | " \n", 72 | " \n", 73 | " # Step 2: Relax all edges |V| - 1 times. A simple shortest \n", 74 | " # path from src to any other vertex can have at-most |V| - 1 \n", 75 | " # edges\n", 76 | " for i in range(self.V - 1):\n", 77 | " # Update dist value and parent index of the adjacent vertices of\n", 78 | " # the picked vertex. Consider only those vertices which are still in\n", 79 | " # queue\n", 80 | " for u, v, w in self.graph:\n", 81 | " if dist[u] != float(\"Inf\") and dist[u] * float(w) < dist[v]:\n", 82 | " dist[v] = dist[u] * float(w)\n", 83 | " \n", 84 | " # Step 3: check for negative-weight cycles. The above step \n", 85 | " # guarantees shortest distances if graph doesn't contain \n", 86 | " # negative weight cycle. If we get a shorter path, then there\n", 87 | " # is a cycle.\n", 88 | " \n", 89 | " for u, v, w in self.graph:\n", 90 | " if dist[u] != float(\"Inf\") and dist[u] * float(w) < dist[v]:\n", 91 | " print \"Graph contains negative weight cycle\"\n", 92 | " return\n", 93 | " \n", 94 | " # print all distance\n", 95 | " self.printArr(dist)" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": 73, 101 | "metadata": {}, 102 | "outputs": [ 103 | { 104 | "name": "stdout", 105 | "output_type": "stream", 106 | "text": [ 107 | "Vertex Distance from Source\n", 108 | "0.000000 \t\t 0.000000\n", 109 | "1.000000 \t\t 0.000000\n", 110 | "2.000000 \t\t 0.000000\n" 111 | ] 112 | } 113 | ], 114 | "source": [ 115 | "peter = Graph(3)\n", 116 | "\"\"\"\n", 117 | "0 = USD\n", 118 | "1 = EUR\n", 119 | "2 = GBP\n", 120 | "\"\"\"\n", 121 | "\n", 122 | "peter.addEdge(0, 1, 0.741)\n", 123 | "peter.addEdge(0, 2, 0.657)\n", 124 | "peter.addEdge(1, 0, 1.349)\n", 125 | "peter.addEdge(1, 2, 0.888)\n", 126 | "peter.addEdge(2, 0, 1.521)\n", 127 | "peter.addEdge(2, 1, 1.126)\n", 128 | "\n", 129 | "peter.BellmanFord(0)\n" 130 | ] 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "metadata": {}, 135 | "source": [ 136 | "## Optimal Strategy for the Money Game" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": {}, 142 | "source": [ 143 | "Consider a row of n coins of values v(1) ... v(n), where n is even. We play a game against an opponent by alternating turns. In each turn, a player selects either the first or last coin from the row, removes it from the row permanently, and receives the value of the coin. Determine the maximum possible amount of money we can definitely win if we move first.\n", 144 | "\n", 145 | "For example, the game:\n", 146 | "\n", 147 | " $2, $10, $1, $5\n", 148 | "\n", 149 | "By moving first and playing optimally one can be guaranteed of 15. The first move is to take 5. This forces your opponent to take either 2 or 1, and then allows you to take 10." 150 | ] 151 | }, 152 | { 153 | "cell_type": "markdown", 154 | "metadata": {}, 155 | "source": [ 156 | "#### Possibility 1: Opponent picks randomly" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 53, 162 | "metadata": { 163 | "collapsed": true 164 | }, 165 | "outputs": [], 166 | "source": [ 167 | "import random\n", 168 | "\n", 169 | "def money_game(prices):\n", 170 | " \"\"\"\n", 171 | " Assumes either that my opponent picks randomly from the\n", 172 | " front or back of the line, not trying to maximize\n", 173 | " his gains or has the same maximization strategy as I do.\n", 174 | " Assumes an even number of prices.\n", 175 | " \"\"\"\n", 176 | " p1_price, p2_price = 0, 0\n", 177 | " for i in range(len(prices)/2):\n", 178 | " prices, p1_price = player_1_pick(prices, p1_price)\n", 179 | " \n", 180 | " # Player 2 can either pick randomly or max.\n", 181 | "# prices, p2_price = player_2_pick_random(prices, p2_price)\n", 182 | " prices, p2_price = player_2_pick_max(prices, p2_price)\n", 183 | "\n", 184 | " print \"Player 1 wins %d Dollars!\" %p1_price\n", 185 | " print \"Player 2 wins %d Dollars!\" %p2_price\n", 186 | " \n", 187 | " \n", 188 | "def player_1_pick(array, money):\n", 189 | " if array[0] >= array[-1]:\n", 190 | " money += array.pop(0)\n", 191 | " else: money += array.pop(-1)\n", 192 | " return array, money\n", 193 | "\n", 194 | "def player_2_pick_max(array, money):\n", 195 | " array, money = player_1_pick(array, money)\n", 196 | " return array, money\n", 197 | " \n", 198 | "def player_2_pick_random(array, money):\n", 199 | " money += array.pop(random.choice([0, -1]))\n", 200 | " return array, money" 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": 54, 206 | "metadata": {}, 207 | "outputs": [ 208 | { 209 | "name": "stdout", 210 | "output_type": "stream", 211 | "text": [ 212 | "Player 1 wins 9 Dollars!\n", 213 | "Player 2 wins 16 Dollars!\n" 214 | ] 215 | } 216 | ], 217 | "source": [ 218 | "prices = [2, 10, 1, 5, 6, 1]\n", 219 | "money_game(prices)" 220 | ] 221 | } 222 | ], 223 | "metadata": { 224 | "kernelspec": { 225 | "display_name": "Python 2", 226 | "language": "python", 227 | "name": "python2" 228 | }, 229 | "language_info": { 230 | "codemirror_mode": { 231 | "name": "ipython", 232 | "version": 2 233 | }, 234 | "file_extension": ".py", 235 | "mimetype": "text/x-python", 236 | "name": "python", 237 | "nbconvert_exporter": "python", 238 | "pygments_lexer": "ipython2", 239 | "version": "2.7.14" 240 | } 241 | }, 242 | "nbformat": 4, 243 | "nbformat_minor": 2 244 | } 245 | -------------------------------------------------------------------------------- /18:03:30 Viterbi algorithm.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Viterbi Algorithm in Speech Recognition" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "** a. What does the transition_probability table describe?**\n", 15 | "\n", 16 | "The *one-step transition probability* is the probability of transitioning from one state to another for any single step.\n", 17 | "\n", 18 | "** b. What does the emission_probability table describe?**\n", 19 | "\n", 20 | "For each of the *N* possible states, there is a set of emission probabilities governing the distribution of the observed variable at a particular time given the state of the hidden variable at that time (?)\n", 21 | "\n", 22 | "** c. What does the start_probability table describe?**\n", 23 | "\n", 24 | "The probabilities of any given state at the start (for words I guess this would be the general probability of a certain letter occuring at the beginning of a word in the whole of the English language)\n", 25 | "\n", 26 | "** d. What does the Viterbi algorithm do with these tables?**\n", 27 | "\n", 28 | "Use them to predict the likelyhood of the current state and correct it if it's inaccurate.\n", 29 | "\n", 30 | "** e. Describe the optimal substructure found in this problem.**\n", 31 | "\n", 32 | "If we have already identified the changes of the previous 4 letters, we don't need to re-calculate them when we add a 5th. And for language, where many words reoccur over and over, we might not have to solve a new problem every time.\n", 33 | "\n", 34 | "** f. How should one interpret the output of the Viterbi algorithm?**\n", 35 | "\n", 36 | "Gives the most likely word, the shortest path of sorts." 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 1, 42 | "metadata": {}, 43 | "outputs": [ 44 | { 45 | "name": "stdout", 46 | "output_type": "stream", 47 | "text": [ 48 | " 0 1 2 3 4 5 6 7 8 9 10\n", 49 | "a: 1.600E-01 3.750E-04 3.120E-04 2.293E-03 1.365E-06 1.032E-04 2.709E-07 1.393E-06 9.287E-09 2.717E-09 1.997E-08\n", 50 | "c: 5.000E-03 2.400E-03 3.120E-04 5.460E-05 5.160E-04 2.580E-06 1.548E-06 1.857E-07 2.090E-08 2.717E-09 4.754E-10\n", 51 | "b: 5.000E-03 3.120E-02 9.600E-05 5.460E-05 3.440E-05 2.580E-06 6.192E-06 7.430E-07 2.717E-07 8.359E-10 4.754E-10\n", 52 | "r: 5.000E-03 8.000E-04 1.092E-02 2.400E-06 2.293E-05 2.580E-06 5.160E-07 1.548E-07 1.857E-08 9.508E-08 2.090E-11\n", 53 | "d: 1.500E-02 9.600E-03 1.560E-04 5.460E-05 3.440E-05 2.580E-06 1.857E-05 3.715E-07 8.359E-08 1.358E-09 4.754E-10\n", 54 | "The steps of states are a b r a c a d a b r a with highest probability of 1.99668780675e-08\n" 55 | ] 56 | } 57 | ], 58 | "source": [ 59 | "#!/usr/bin/env python\n", 60 | "# -*- coding: utf-8 -*-\n", 61 | "######################\n", 62 | "# Speech recognition #\n", 63 | "######################\n", 64 | "# 1. Work through the following resources:\n", 65 | "# Viterbi algorithm. (n.d.). In Wikipedia. Retrieved November 9, 2016, from\n", 66 | "# https://en.wikipedia.org/wiki/Viterbi_algorithm\n", 67 | "# The 44 Phonemes in English. (n.d.). Retrieved November 9, 2016, from\n", 68 | "# http://www.dyslexia-reading-well.com/44-phonemes-in-english.html\n", 69 | "# 2. Read and run the code given below.\n", 70 | "# 3. Answer the following questions:\n", 71 | "# a. What does the transition_probability table describe?\n", 72 | "# b. What does the emission_probability table describe?\n", 73 | "# c. What does the start_probability table describe?\n", 74 | "# d. What does the Viterbi algorithm do with these tables?\n", 75 | "# e. Describe the optimal substructure found in this problem.\n", 76 | "# f. How should one interpret the output of the Viterbi algorithm?\n", 77 | "\n", 78 | "\n", 79 | "def viterbi(obs, states, start_p, trans_p, emit_p):\n", 80 | " V = [{}]\n", 81 | " for st in states:\n", 82 | " V[0][st] = {\"prob\": start_p[st] * emit_p[st][obs[0]], \"prev\": None}\n", 83 | " # Run Viterbi when t > 0\n", 84 | " for t in range(1, len(obs)):\n", 85 | " V.append({})\n", 86 | " for st in states:\n", 87 | " max_tr_prob = max(V[t - 1][prev_st][\"prob\"] * trans_p[prev_st][st]\n", 88 | " for prev_st in states)\n", 89 | " for prev_st in states:\n", 90 | " if V[t - 1][prev_st][\"prob\"] * trans_p[prev_st][\n", 91 | " st] == max_tr_prob:\n", 92 | " max_prob = max_tr_prob * emit_p[st][obs[t]]\n", 93 | " V[t][st] = {\"prob\": max_prob, \"prev\": prev_st}\n", 94 | " break\n", 95 | " for line in dptable(V):\n", 96 | " print line\n", 97 | " opt = []\n", 98 | " # The highest probability\n", 99 | " max_prob = max(value[\"prob\"] for value in V[-1].values())\n", 100 | " previous = None\n", 101 | " # Get most probable state and its backtrack\n", 102 | " for st, data in V[-1].items():\n", 103 | " if data[\"prob\"] == max_prob:\n", 104 | " opt.append(st)\n", 105 | " previous = st\n", 106 | " break\n", 107 | " # Follow the backtrack till the first observation\n", 108 | " for t in range(len(V) - 2, -1, -1):\n", 109 | " opt.insert(0, V[t + 1][previous][\"prev\"])\n", 110 | " previous = V[t + 1][previous][\"prev\"]\n", 111 | "\n", 112 | " print 'The steps of states are ' + ' '.join(\n", 113 | " opt) + ' with highest probability of %s' % max_prob\n", 114 | "\n", 115 | "\n", 116 | "def dptable(V):\n", 117 | " # Print a table of steps from dictionary\n", 118 | " yield \" \".join((\"%9s\" % i) for i in range(len(V)))\n", 119 | " for state in V[0]:\n", 120 | " yield \"%.9s: \" % state + \" \".join(\"%.9s\" % (\"%.3E\" % v[state][\"prob\"])\n", 121 | " for v in V)\n", 122 | "\n", 123 | "\n", 124 | "states = 'abcdr'\n", 125 | "observations = ('/a/', '/b/', '/r/', '/ã/', '/k/', '/a/', '/d/', '/d/', '/b/',\n", 126 | " '/r/', '/ã/')\n", 127 | "start_probability = {'a': 0.4, 'b': 0.1, 'c': 0.1, 'd': 0.3, 'r': 0.1}\n", 128 | "\n", 129 | "transition_probability = {'a': {'a': 0,\n", 130 | " 'b': 0.3,\n", 131 | " 'c': 0.3,\n", 132 | " 'd': 0.3,\n", 133 | " 'r': 0.1},\n", 134 | " 'b': {'a': 0.2,\n", 135 | " 'b': 0,\n", 136 | " 'c': 0.2,\n", 137 | " 'd': 0.1,\n", 138 | " 'r': 0.5},\n", 139 | " 'c': {'a': 0.5,\n", 140 | " 'b': 0.1,\n", 141 | " 'c': 0.1,\n", 142 | " 'd': 0.1,\n", 143 | " 'r': 0.1},\n", 144 | " 'd': {'a': 0.5,\n", 145 | " 'b': 0.2,\n", 146 | " 'c': 0.2,\n", 147 | " 'd': 0.0,\n", 148 | " 'r': 0.1},\n", 149 | " 'r': {'a': 0.7,\n", 150 | " 'b': 0.1,\n", 151 | " 'c': 0.1,\n", 152 | " 'd': 0.1,\n", 153 | " 'r': 0}}\n", 154 | "emission_probability = {'a': {'/a/': 0.4,\n", 155 | " '/ã/': 0.3,\n", 156 | " '/b/': 0.05,\n", 157 | " '/r/': 0.05,\n", 158 | " '/d/': 0.15,\n", 159 | " '/k/': 0.05},\n", 160 | " 'b': {'/a/': 0.05,\n", 161 | " '/ã/': 0.05,\n", 162 | " '/b/': 0.65,\n", 163 | " '/r/': 0.05,\n", 164 | " '/d/': 0.2,\n", 165 | " '/k/': 0.05},\n", 166 | " 'c': {'/a/': 0.05,\n", 167 | " '/ã/': 0.05,\n", 168 | " '/b/': 0.05,\n", 169 | " '/r/': 0.05,\n", 170 | " '/d/': 0.05,\n", 171 | " '/k/': 0.75},\n", 172 | " 'd': {'/a/': 0.05,\n", 173 | " '/ã/': 0.05,\n", 174 | " '/b/': 0.2,\n", 175 | " '/r/': 0.05,\n", 176 | " '/d/': 0.6,\n", 177 | " '/k/': 0.05},\n", 178 | " 'r': {'/a/': 0.05,\n", 179 | " '/ã/': 0.05,\n", 180 | " '/b/': 0.05,\n", 181 | " '/r/': 0.7,\n", 182 | " '/d/': 0.05,\n", 183 | " '/k/': 0.1}}\n", 184 | "\n", 185 | "viterbi(observations, states, start_probability, transition_probability,\n", 186 | " emission_probability)" 187 | ] 188 | }, 189 | { 190 | "cell_type": "code", 191 | "execution_count": null, 192 | "metadata": { 193 | "collapsed": true 194 | }, 195 | "outputs": [], 196 | "source": [] 197 | } 198 | ], 199 | "metadata": { 200 | "kernelspec": { 201 | "display_name": "Python 2", 202 | "language": "python", 203 | "name": "python2" 204 | }, 205 | "language_info": { 206 | "codemirror_mode": { 207 | "name": "ipython", 208 | "version": 2 209 | }, 210 | "file_extension": ".py", 211 | "mimetype": "text/x-python", 212 | "name": "python", 213 | "nbconvert_exporter": "python", 214 | "pygments_lexer": "ipython2", 215 | "version": "2.7.14" 216 | } 217 | }, 218 | "nbformat": 4, 219 | "nbformat_minor": 2 220 | } 221 | --------------------------------------------------------------------------------