├── imgs ├── bigo.png ├── knn.jpg ├── mnm.jpg ├── node.png ├── array.png ├── graph.png ├── numpy.png ├── queue.png ├── queue1.png ├── regex.png ├── stack.png ├── stack1.png ├── ci-plot.png ├── mergesort.png ├── pokemon1.jpg ├── quicksort.png ├── bubblepass.png ├── mergesortB.png ├── python-tips.jpg ├── insertionsort.png ├── reconstruct-me.png ├── regex-example.png ├── selectionsort.png ├── eigenfaces-faculty.png ├── eigenfaces-student.png └── reconstruct-faculty.png ├── README.md └── notebooks ├── 01-programming fundamentals.ipynb ├── 02-hashtable implementation.ipynb └── 04-coding questions for DS interview.ipynb /imgs/bigo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shik3519/programming-concepts-for-data-science/HEAD/imgs/bigo.png -------------------------------------------------------------------------------- /imgs/knn.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shik3519/programming-concepts-for-data-science/HEAD/imgs/knn.jpg -------------------------------------------------------------------------------- /imgs/mnm.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shik3519/programming-concepts-for-data-science/HEAD/imgs/mnm.jpg -------------------------------------------------------------------------------- /imgs/node.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shik3519/programming-concepts-for-data-science/HEAD/imgs/node.png -------------------------------------------------------------------------------- /imgs/array.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shik3519/programming-concepts-for-data-science/HEAD/imgs/array.png -------------------------------------------------------------------------------- /imgs/graph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shik3519/programming-concepts-for-data-science/HEAD/imgs/graph.png -------------------------------------------------------------------------------- /imgs/numpy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shik3519/programming-concepts-for-data-science/HEAD/imgs/numpy.png -------------------------------------------------------------------------------- /imgs/queue.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shik3519/programming-concepts-for-data-science/HEAD/imgs/queue.png -------------------------------------------------------------------------------- /imgs/queue1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shik3519/programming-concepts-for-data-science/HEAD/imgs/queue1.png -------------------------------------------------------------------------------- /imgs/regex.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shik3519/programming-concepts-for-data-science/HEAD/imgs/regex.png -------------------------------------------------------------------------------- /imgs/stack.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shik3519/programming-concepts-for-data-science/HEAD/imgs/stack.png -------------------------------------------------------------------------------- /imgs/stack1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shik3519/programming-concepts-for-data-science/HEAD/imgs/stack1.png -------------------------------------------------------------------------------- /imgs/ci-plot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shik3519/programming-concepts-for-data-science/HEAD/imgs/ci-plot.png -------------------------------------------------------------------------------- /imgs/mergesort.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shik3519/programming-concepts-for-data-science/HEAD/imgs/mergesort.png -------------------------------------------------------------------------------- /imgs/pokemon1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shik3519/programming-concepts-for-data-science/HEAD/imgs/pokemon1.jpg -------------------------------------------------------------------------------- /imgs/quicksort.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shik3519/programming-concepts-for-data-science/HEAD/imgs/quicksort.png -------------------------------------------------------------------------------- /imgs/bubblepass.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shik3519/programming-concepts-for-data-science/HEAD/imgs/bubblepass.png -------------------------------------------------------------------------------- /imgs/mergesortB.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shik3519/programming-concepts-for-data-science/HEAD/imgs/mergesortB.png -------------------------------------------------------------------------------- /imgs/python-tips.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shik3519/programming-concepts-for-data-science/HEAD/imgs/python-tips.jpg -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # programming-for-data-science 2 | Basics of programming: algorithms, data structures, object oriented programming 3 | -------------------------------------------------------------------------------- /imgs/insertionsort.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shik3519/programming-concepts-for-data-science/HEAD/imgs/insertionsort.png -------------------------------------------------------------------------------- /imgs/reconstruct-me.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shik3519/programming-concepts-for-data-science/HEAD/imgs/reconstruct-me.png -------------------------------------------------------------------------------- /imgs/regex-example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shik3519/programming-concepts-for-data-science/HEAD/imgs/regex-example.png -------------------------------------------------------------------------------- /imgs/selectionsort.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shik3519/programming-concepts-for-data-science/HEAD/imgs/selectionsort.png -------------------------------------------------------------------------------- /imgs/eigenfaces-faculty.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shik3519/programming-concepts-for-data-science/HEAD/imgs/eigenfaces-faculty.png -------------------------------------------------------------------------------- /imgs/eigenfaces-student.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shik3519/programming-concepts-for-data-science/HEAD/imgs/eigenfaces-student.png -------------------------------------------------------------------------------- /imgs/reconstruct-faculty.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shik3519/programming-concepts-for-data-science/HEAD/imgs/reconstruct-faculty.png -------------------------------------------------------------------------------- /notebooks/01-programming fundamentals.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 4, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import itertools\n", 10 | "import numpy as np" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 12, 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "l = np.arange(20,100)" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 20, 25 | "metadata": {}, 26 | "outputs": [ 27 | { 28 | "data": { 29 | "text/plain": [ 30 | "array([20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,\n", 31 | " 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,\n", 32 | " 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,\n", 33 | " 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87,\n", 34 | " 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])" 35 | ] 36 | }, 37 | "execution_count": 20, 38 | "metadata": {}, 39 | "output_type": "execute_result" 40 | } 41 | ], 42 | "source": [ 43 | "l" 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "metadata": {}, 49 | "source": [ 50 | "`itertools.islice`" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 23, 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "name": "stdout", 60 | "output_type": "stream", 61 | "text": [ 62 | "23\n", 63 | "24\n", 64 | "25\n", 65 | "26\n", 66 | "27\n", 67 | "28\n", 68 | "29\n" 69 | ] 70 | } 71 | ], 72 | "source": [ 73 | "# you can define start and stop\n", 74 | "for item in itertools.islice(l,3,10):\n", 75 | " print(item)" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 25, 81 | "metadata": {}, 82 | "outputs": [], 83 | "source": [ 84 | "my_list = [1, 3, 6, 10]" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": 31, 90 | "metadata": {}, 91 | "outputs": [], 92 | "source": [ 93 | "# square each term using list comprehension\n", 94 | "# Output: [1, 9, 36, 100]\n", 95 | "j = [x**2 for x in my_list]" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": 28, 101 | "metadata": {}, 102 | "outputs": [ 103 | { 104 | "data": { 105 | "text/plain": [ 106 | "146" 107 | ] 108 | }, 109 | "execution_count": 28, 110 | "metadata": {}, 111 | "output_type": "execute_result" 112 | } 113 | ], 114 | "source": [ 115 | "# same thing can be done using generator expression\n", 116 | "# Output: at 0x0000000002EBDAF8>\n", 117 | "sum(x**2 for x in my_list)" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 37, 123 | "metadata": {}, 124 | "outputs": [], 125 | "source": [ 126 | "a = (x**2 for x in my_list)" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": 38, 132 | "metadata": {}, 133 | "outputs": [ 134 | { 135 | "data": { 136 | "text/plain": [ 137 | "1" 138 | ] 139 | }, 140 | "execution_count": 38, 141 | "metadata": {}, 142 | "output_type": "execute_result" 143 | } 144 | ], 145 | "source": [ 146 | "next(a)" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": 39, 152 | "metadata": {}, 153 | "outputs": [], 154 | "source": [ 155 | "def all_even():\n", 156 | " n = 0\n", 157 | " while True:\n", 158 | " yield n\n", 159 | " n += 2" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": 40, 165 | "metadata": {}, 166 | "outputs": [], 167 | "source": [ 168 | "k = all_even()" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": 44, 174 | "metadata": {}, 175 | "outputs": [ 176 | { 177 | "data": { 178 | "text/plain": [ 179 | "4" 180 | ] 181 | }, 182 | "execution_count": 44, 183 | "metadata": {}, 184 | "output_type": "execute_result" 185 | } 186 | ], 187 | "source": [ 188 | "next(k)" 189 | ] 190 | }, 191 | { 192 | "cell_type": "markdown", 193 | "metadata": {}, 194 | "source": [ 195 | "**Class example**" 196 | ] 197 | }, 198 | { 199 | "cell_type": "code", 200 | "execution_count": 81, 201 | "metadata": {}, 202 | "outputs": [], 203 | "source": [ 204 | "class point:\n", 205 | " def __init__(self, x, y):\n", 206 | " self.x = x\n", 207 | " self.y = y\n", 208 | "\n", 209 | " def distance(self, p):\n", 210 | " return np.sqrt(np.square(p.x - self.x) + np.square(p.y - self.y))\n", 211 | " \n", 212 | " def __str__(self):\n", 213 | " return \"(\"+str(self.x)+\",\"+str(self.y) + \")\"" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": 82, 219 | "metadata": {}, 220 | "outputs": [], 221 | "source": [ 222 | "p = point(3,4)\n", 223 | "q = point(5,6)" 224 | ] 225 | }, 226 | { 227 | "cell_type": "code", 228 | "execution_count": 83, 229 | "metadata": {}, 230 | "outputs": [ 231 | { 232 | "name": "stdout", 233 | "output_type": "stream", 234 | "text": [ 235 | "2.8284271247461903\n" 236 | ] 237 | } 238 | ], 239 | "source": [ 240 | "print(p.distance(q))" 241 | ] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "execution_count": 84, 246 | "metadata": {}, 247 | "outputs": [ 248 | { 249 | "name": "stdout", 250 | "output_type": "stream", 251 | "text": [ 252 | "(3,4)\n" 253 | ] 254 | } 255 | ], 256 | "source": [ 257 | "print(p)" 258 | ] 259 | }, 260 | { 261 | "cell_type": "code", 262 | "execution_count": 86, 263 | "metadata": {}, 264 | "outputs": [], 265 | "source": [ 266 | "class Point3D(point):\n", 267 | " def __init__(self, x, y, z):\n", 268 | " super().__init__(x, y)\n", 269 | " self.z = z\n", 270 | " \n", 271 | " def distance(self, another_point):\n", 272 | " q1 = point(another_point.x,another_point.y)\n", 273 | " q2 = point(another_point.x,another_point.y)\n", 274 | " \n", 275 | " d1 = super().distance(q1)\n", 276 | " \n", 277 | " \n", 278 | " \n", 279 | "# def __str__(self):" 280 | ] 281 | }, 282 | { 283 | "cell_type": "code", 284 | "execution_count": 87, 285 | "metadata": {}, 286 | "outputs": [], 287 | "source": [ 288 | "p = Point3D(3,4,9)" 289 | ] 290 | }, 291 | { 292 | "cell_type": "code", 293 | "execution_count": 90, 294 | "metadata": {}, 295 | "outputs": [], 296 | "source": [ 297 | "q = Point3D(5,6,10)" 298 | ] 299 | }, 300 | { 301 | "cell_type": "code", 302 | "execution_count": 91, 303 | "metadata": {}, 304 | "outputs": [ 305 | { 306 | "data": { 307 | "text/plain": [ 308 | "2.8284271247461903" 309 | ] 310 | }, 311 | "execution_count": 91, 312 | "metadata": {}, 313 | "output_type": "execute_result" 314 | } 315 | ], 316 | "source": [ 317 | "p.distance(q)" 318 | ] 319 | }, 320 | { 321 | "cell_type": "code", 322 | "execution_count": null, 323 | "metadata": {}, 324 | "outputs": [], 325 | "source": [] 326 | } 327 | ], 328 | "metadata": { 329 | "kernelspec": { 330 | "display_name": "Python 3", 331 | "language": "python", 332 | "name": "python3" 333 | }, 334 | "language_info": { 335 | "codemirror_mode": { 336 | "name": "ipython", 337 | "version": 3 338 | }, 339 | "file_extension": ".py", 340 | "mimetype": "text/x-python", 341 | "name": "python", 342 | "nbconvert_exporter": "python", 343 | "pygments_lexer": "ipython3", 344 | "version": "3.6.5" 345 | } 346 | }, 347 | "nbformat": 4, 348 | "nbformat_minor": 2 349 | } 350 | -------------------------------------------------------------------------------- /notebooks/02-hashtable implementation.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Introduction" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "* Understand the logic of hashing\n", 15 | "* Understand the working of how dictionary stores elements\n", 16 | "* Creating a hashtable class" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "A hashtable is a dictionary implementation that relies on this strategy to dramatically speed up key lookups by constraining the size of the search region. A hashtable is a list of buckets and a bucket is a list of associations mapping and arbitrary key to arbitrary value. We compute a function, a hash function, on the key that indicates which bucket (number) potentially contains the search key. With a uniform distribution, we would expect roughly N/B associations in each bucket for B buckets and N total elements in the dictionary. A complexity of N/B is much better than N and, with sufficiently large B, we would say that N/B approaches 1, giving complexity O(1) versus O(n)." 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 20, 29 | "metadata": {}, 30 | "outputs": [], 31 | "source": [ 32 | "class HashTable:\n", 33 | " def __init__(self, nbuckets):\n", 34 | " if isinstance(nbuckets,\n", 35 | " int) and nbuckets > 1 and self.__checkprime__(nbuckets):\n", 36 | " self.nbuckets = nbuckets\n", 37 | " self.table = [[] for i in range(nbuckets)]\n", 38 | " else:\n", 39 | " print(\"Number of bucket should be a postive prime integer\")\n", 40 | "\n", 41 | " def __checkprime__(self, x):\n", 42 | " factors = 0\n", 43 | " for i in range(1, x + 1):\n", 44 | " if x % i == 0: factors += 1\n", 45 | " if factors == 2:\n", 46 | " return True\n", 47 | " else:\n", 48 | " return False\n", 49 | "\n", 50 | " def __hashcode__(self, o):\n", 51 | " \"\"\"\n", 52 | " Return a hashcode for strings and integers; all others return None\n", 53 | " For integers, just return the integer value.\n", 54 | " For strings, perform operation h = h*31 + ord(c) for all characters in the string\n", 55 | " \"\"\"\n", 56 | " if isinstance(o, int): return o\n", 57 | " elif isinstance(o, str) or type(o) == unicode:\n", 58 | " h = 0\n", 59 | " for e in o:\n", 60 | " h = h * 31 + ord(e)\n", 61 | " return h\n", 62 | " else:\n", 63 | " return None\n", 64 | "\n", 65 | " def buckets_str(self):\n", 66 | " \"\"\"\n", 67 | " Return a string representing the various buckets of this table.\n", 68 | " The output looks like:\n", 69 | " 0000->\n", 70 | " 0001->\n", 71 | " 0002->\n", 72 | " 0003->parrt:99\n", 73 | " 0004->\n", 74 | " where parrt:99 indicates an association of (parrt,99) in bucket 3.\n", 75 | " \"\"\"\n", 76 | " s = ''\n", 77 | " for i in range(len(self.table)):\n", 78 | " s = s + '{0:04d}'.format(i) + \"->\"\n", 79 | " if len(self.table[i]) > 0:\n", 80 | " x = ''\n", 81 | " l = len(self.table[i])\n", 82 | " for j in range(len(self.table[i]) - 1):\n", 83 | " x = x + str(self.table[i][j][0]) + \":\" + str(\n", 84 | " self.table[i][j][1]) + \", \"\n", 85 | " x = x + str(self.table[i][l - 1][0]) + \":\" + str(\n", 86 | " self.table[i][l - 1][1])\n", 87 | " s = s + x + \"\\n\"\n", 88 | " else:\n", 89 | " s = s + \"\\n\"\n", 90 | " return s\n", 91 | "\n", 92 | " def bucket_indexof(self, key):\n", 93 | " \"\"\"\n", 94 | " Return the element within a specific bucket; the bucket is:\n", 95 | " table[hashcode(key) % len(table)]. You have to linearly\n", 96 | " search the bucket to find the tuple containing key.\n", 97 | " \"\"\"\n", 98 | " bucket_index = self.__hashcode__(key) % len(self.table)\n", 99 | " bucket = self.table[bucket_index]\n", 100 | " for i, item in enumerate(bucket):\n", 101 | " if item[0] == key: return i\n", 102 | " return None\n", 103 | "\n", 104 | " def put(self, key, value):\n", 105 | " \"\"\"\n", 106 | " Perform the equivalent of table[key] = value\n", 107 | " Find the appropriate bucket indicated by key and then append (key,value)\n", 108 | " to that bucket if the (key,value) pair doesn't exist yet in that bucket.\n", 109 | " If the bucket for key already has a (key,value) pair with that key,\n", 110 | " then replace the tuple with the new (key,value).\n", 111 | " Make sure that you are only adding (key,value) associations to the buckets.\n", 112 | " The type(value) can be anything. Could be a set, list, number, string, anything!\n", 113 | " \"\"\"\n", 114 | " bucket_index = self.__hashcode__(key) % len(self.table)\n", 115 | " bucket = self.table[bucket_index]\n", 116 | "\n", 117 | " for i in range(len(bucket)):\n", 118 | " if bucket[i][0] == key:\n", 119 | " self.table[bucket_index][i] = (key, value)\n", 120 | " self.table[bucket_index].append((key, value))\n", 121 | "\n", 122 | " def get(self, key):\n", 123 | " \"\"\"\n", 124 | " Return the equivalent of table[key].\n", 125 | " Find the appropriate bucket indicated by the key and look for the\n", 126 | " association with the key. Return the value (not the key and not\n", 127 | " the association!). Return None if key not found.\n", 128 | " \"\"\"\n", 129 | "\n", 130 | " bucket_index = self.__hashcode__(key) % len(self.table)\n", 131 | " bucket = self.table[bucket_index]\n", 132 | " for i in bucket:\n", 133 | " if i[0] == key:\n", 134 | " return i[1]\n", 135 | " return None\n", 136 | "\n", 137 | " def __getitem__(self, key):\n", 138 | " return self.get(key)\n", 139 | "\n", 140 | " def __setitem__(self, key, value):\n", 141 | " self.put(key, value)\n", 142 | "\n", 143 | " def __str__(self):\n", 144 | " \"\"\"\n", 145 | " Return what str(table) would return for a regular Python dict\n", 146 | " such as {parrt:99}. The order should be in bucket order and then\n", 147 | " insertion order within each bucket.\n", 148 | " \"\"\"\n", 149 | " bucket_size = []\n", 150 | " for i in range(len(self.table)):\n", 151 | " bucket_size.append(len(self.table[i]))\n", 152 | " if sum(bucket_size) == 1:\n", 153 | " x = '{'\n", 154 | " for i in range(len(self.table)):\n", 155 | " if len(self.table[i]) == 1:\n", 156 | " x = x + str(self.table[i][0][0]) + \":\" + str(\n", 157 | " self.table[i][0][1]) + \"}\"\n", 158 | "\n", 159 | " return x\n", 160 | " elif sum(bucket_size) > 1:\n", 161 | " x = '{'\n", 162 | " element = []\n", 163 | " for i in range(len(self.table)):\n", 164 | " for j in self.table[i]:\n", 165 | " element.append(j)\n", 166 | " for e in element[:-1]:\n", 167 | " x = x + str(e[0]) + \":\" + str(e[1]) + \", \"\n", 168 | "\n", 169 | " x = x + str(element[-1][0]) + \":\" + str(element[-1][1]) + \"}\"\n", 170 | " return x\n", 171 | " else:\n", 172 | " return \"{}\"" 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": 21, 178 | "metadata": {}, 179 | "outputs": [], 180 | "source": [ 181 | "table = HashTable(5)\n", 182 | "\n", 183 | "table.put(\"parrt\", {2, 99, 3942})\n", 184 | "table.put(\"tombu\", {6, 3, 1024, 99, 102342})\n", 185 | "str(table)\n", 186 | "\n", 187 | "table.put(\"shikhar\", 100)\n", 188 | "\n", 189 | "table['shikhar']" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": null, 195 | "metadata": {}, 196 | "outputs": [], 197 | "source": [] 198 | } 199 | ], 200 | "metadata": { 201 | "kernelspec": { 202 | "display_name": "Python 3", 203 | "language": "python", 204 | "name": "python3" 205 | }, 206 | "language_info": { 207 | "codemirror_mode": { 208 | "name": "ipython", 209 | "version": 3 210 | }, 211 | "file_extension": ".py", 212 | "mimetype": "text/x-python", 213 | "name": "python", 214 | "nbconvert_exporter": "python", 215 | "pygments_lexer": "ipython3", 216 | "version": "3.6.5" 217 | } 218 | }, 219 | "nbformat": 4, 220 | "nbformat_minor": 2 221 | } 222 | -------------------------------------------------------------------------------- /notebooks/04-coding questions for DS interview.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Common coding interview questions and their solution for data scientist. Solutions in python. Most of the questions are based on arrays and strings. Source: https://leetcode.com/ " 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 18, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "import numpy as np\n", 17 | "import matplotlib.pyplot as plt\n", 18 | "%matplotlib inline" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": {}, 24 | "source": [ 25 | "Given a string, find the length of the longest substring without repeating characters.\n", 26 | "\n", 27 | "Examples:\n", 28 | "\n", 29 | "Given \"abcabcbb\", the answer is \"abc\", which the length is 3.\n", 30 | "\n", 31 | "Given \"bbbbb\", the answer is \"b\", with the length of 1.\n", 32 | "\n", 33 | "Given \"pwwkew\", the answer is \"wke\", with the length of 3. Note that the answer must be a substring, \"pwke\" is a subsequence and not a substring." 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 39, 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [ 42 | "s = \"abcabcbb\"" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 40, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [ 51 | "def long(s):\n", 52 | " l_max = 0\n", 53 | " l_now = 0\n", 54 | " s_current = ''\n", 55 | " s_max = ''\n", 56 | " for i,e in enumerate(s):\n", 57 | " l_now = len(s_current)\n", 58 | " if l_now >= l_max:\n", 59 | " l_max = l_now\n", 60 | " s_max = s_current\n", 61 | " if e not in s_current:\n", 62 | " s_current = s_current + e\n", 63 | " else:\n", 64 | " s_current = s_current[s_current.index(e)+1:] + e\n", 65 | " return s_max " 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 41, 71 | "metadata": {}, 72 | "outputs": [ 73 | { 74 | "data": { 75 | "text/plain": [ 76 | "'abc'" 77 | ] 78 | }, 79 | "execution_count": 41, 80 | "metadata": {}, 81 | "output_type": "execute_result" 82 | } 83 | ], 84 | "source": [ 85 | "long(s)" 86 | ] 87 | }, 88 | { 89 | "cell_type": "markdown", 90 | "metadata": {}, 91 | "source": [ 92 | "---" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 119, 98 | "metadata": {}, 99 | "outputs": [], 100 | "source": [ 101 | "def lengthOfLongestSubstring(s):\n", 102 | " \"\"\"\n", 103 | " :type s: str\n", 104 | " :rtype: int\n", 105 | " \"\"\"\n", 106 | " str_current = ''\n", 107 | " l_now = 0\n", 108 | " l_max = 0\n", 109 | " for e in s:\n", 110 | " if e not in str_current:\n", 111 | " str_current = str_current + e\n", 112 | " l_now = len(str_current)\n", 113 | " if l_now > l_max:\n", 114 | " l_max = l_now\n", 115 | " else:\n", 116 | " str_current = str_current[str_current.index(e) + 1:] + e\n", 117 | " return l_max" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 120, 123 | "metadata": {}, 124 | "outputs": [], 125 | "source": [ 126 | "s = \"abcabcbb\"" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": 122, 132 | "metadata": {}, 133 | "outputs": [ 134 | { 135 | "data": { 136 | "text/plain": [ 137 | "3" 138 | ] 139 | }, 140 | "execution_count": 122, 141 | "metadata": {}, 142 | "output_type": "execute_result" 143 | } 144 | ], 145 | "source": [ 146 | "lengthOfLongestSubstring(s)" 147 | ] 148 | }, 149 | { 150 | "cell_type": "markdown", 151 | "metadata": {}, 152 | "source": [ 153 | "---" 154 | ] 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "metadata": {}, 159 | "source": [ 160 | "Longest palindrome (need to complete)" 161 | ] 162 | }, 163 | { 164 | "cell_type": "markdown", 165 | "metadata": {}, 166 | "source": [ 167 | "Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000." 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": 125, 173 | "metadata": {}, 174 | "outputs": [], 175 | "source": [ 176 | "def longestPalindrome(s):\n", 177 | " \"\"\"\n", 178 | " :type s: str\n", 179 | " :rtype: int\n", 180 | " \"\"\"\n", 181 | " max_s = ''\n", 182 | " for i in range(len(s)):\n", 183 | " for j in range(i + 1, len(s)):\n", 184 | " if (s[i:j + 1] == s[i:j + 1][::-1]) and len(\n", 185 | " s[i:j + 1]) > len(max_s):\n", 186 | " max_s = s[i:j + 1]\n", 187 | " else:\n", 188 | " continue\n", 189 | " return max_s" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": 127, 195 | "metadata": {}, 196 | "outputs": [ 197 | { 198 | "data": { 199 | "text/plain": [ 200 | "'geeksskeeg'" 201 | ] 202 | }, 203 | "execution_count": 127, 204 | "metadata": {}, 205 | "output_type": "execute_result" 206 | } 207 | ], 208 | "source": [ 209 | "longestPalindrome(\"forgeeksskeegfor\")" 210 | ] 211 | }, 212 | { 213 | "cell_type": "markdown", 214 | "metadata": {}, 215 | "source": [ 216 | "---" 217 | ] 218 | }, 219 | { 220 | "cell_type": "markdown", 221 | "metadata": {}, 222 | "source": [ 223 | "Determine whether an integer is a palindrome. Do this without extra space." 224 | ] 225 | }, 226 | { 227 | "cell_type": "code", 228 | "execution_count": null, 229 | "metadata": {}, 230 | "outputs": [], 231 | "source": [ 232 | "def isPalindrome(self, x):\n", 233 | " \"\"\"\n", 234 | " :type x: int\n", 235 | " :rtype: bool\n", 236 | " \"\"\"\n", 237 | " if x < 0: return False\n", 238 | " s = str(x)\n", 239 | " return s == s[::-1]" 240 | ] 241 | }, 242 | { 243 | "cell_type": "markdown", 244 | "metadata": {}, 245 | "source": [ 246 | "---" 247 | ] 248 | }, 249 | { 250 | "cell_type": "markdown", 251 | "metadata": {}, 252 | "source": [ 253 | "Given a list of numbers, can you return the outliers?" 254 | ] 255 | }, 256 | { 257 | "cell_type": "code", 258 | "execution_count": 18, 259 | "metadata": {}, 260 | "outputs": [], 261 | "source": [ 262 | "import pandas as pd\n", 263 | "\n", 264 | "test = list(np.random.randint(-1000, 1000, 400))\n", 265 | "\n", 266 | "test.extend([-10000, 5000])\n", 267 | "\n", 268 | "df = pd.DataFrame(test)\n", 269 | "\n", 270 | "df.columns = ['num']" 271 | ] 272 | }, 273 | { 274 | "cell_type": "code", 275 | "execution_count": 34, 276 | "metadata": {}, 277 | "outputs": [ 278 | { 279 | "data": { 280 | "image/png": "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\nAAAAAElFTkSuQmCC\n", 281 | "text/plain": [ 282 | "" 283 | ] 284 | }, 285 | "metadata": {}, 286 | "output_type": "display_data" 287 | } 288 | ], 289 | "source": [ 290 | "import seaborn as sns\n", 291 | "sns.set_style(\"whitegrid\")\n", 292 | "ax = sns.boxplot(x=df['num'])" 293 | ] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "execution_count": 53, 298 | "metadata": {}, 299 | "outputs": [], 300 | "source": [ 301 | "def outliers(l):\n", 302 | " median = np.percentile(l, 50)\n", 303 | " low = np.percentile(l, 25)\n", 304 | " high = np.percentile(l, 75)\n", 305 | " iqr = high - low\n", 306 | " lw = low - 1.5 * iqr\n", 307 | " hw = high + 1.5 * iqr\n", 308 | " outliers = [i for i in l if i < lw or i > hw]\n", 309 | " return outliers" 310 | ] 311 | }, 312 | { 313 | "cell_type": "code", 314 | "execution_count": 54, 315 | "metadata": {}, 316 | "outputs": [ 317 | { 318 | "data": { 319 | "text/plain": [ 320 | "[-10000, 5000]" 321 | ] 322 | }, 323 | "execution_count": 54, 324 | "metadata": {}, 325 | "output_type": "execute_result" 326 | } 327 | ], 328 | "source": [ 329 | "outliers(test)" 330 | ] 331 | }, 332 | { 333 | "cell_type": "markdown", 334 | "metadata": {}, 335 | "source": [ 336 | "----" 337 | ] 338 | }, 339 | { 340 | "cell_type": "markdown", 341 | "metadata": {}, 342 | "source": [ 343 | "You're given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in S is a type of stone you have. You want to know how many of the stones you have are also jewels.\n", 344 | "\n", 345 | "The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so \"a\" is considered a different type of stone from \"A\"." 346 | ] 347 | }, 348 | { 349 | "cell_type": "code", 350 | "execution_count": 26, 351 | "metadata": {}, 352 | "outputs": [], 353 | "source": [ 354 | "def numJewelsInStones(J, S):\n", 355 | " s = dict.fromkeys(S, 0)\n", 356 | " for e in S:\n", 357 | " s[e] += 1\n", 358 | " return sum([s[e] for e in s.keys() if e in J])" 359 | ] 360 | }, 361 | { 362 | "cell_type": "code", 363 | "execution_count": 3, 364 | "metadata": {}, 365 | "outputs": [], 366 | "source": [ 367 | "J = \"aA\"\n", 368 | "S = \"aAAbbbb\"" 369 | ] 370 | }, 371 | { 372 | "cell_type": "code", 373 | "execution_count": 28, 374 | "metadata": {}, 375 | "outputs": [ 376 | { 377 | "data": { 378 | "text/plain": [ 379 | "3" 380 | ] 381 | }, 382 | "execution_count": 28, 383 | "metadata": {}, 384 | "output_type": "execute_result" 385 | } 386 | ], 387 | "source": [ 388 | "numJewelsInStones(J,S)" 389 | ] 390 | }, 391 | { 392 | "cell_type": "markdown", 393 | "metadata": {}, 394 | "source": [ 395 | "---" 396 | ] 397 | }, 398 | { 399 | "cell_type": "markdown", 400 | "metadata": {}, 401 | "source": [ 402 | "We are to write the letters of a given string S, from left to right into lines. Each line has maximum width 100 units, and if writing a letter would cause the width of the line to exceed 100 units, it is written on the next line. We are given an array widths, an array where widths[0] is the width of 'a', widths[1] is the width of 'b', ..., and widths[25] is the width of 'z'.\n", 403 | "\n", 404 | "Now answer two questions: how many lines have at least one character from S, and what is the width used by the last such line? Return your answer as an integer list of length 2." 405 | ] 406 | }, 407 | { 408 | "cell_type": "code", 409 | "execution_count": null, 410 | "metadata": {}, 411 | "outputs": [], 412 | "source": [ 413 | "def numberOfLines(widths, S):\n", 414 | " en = 'abcdefghijklmnopqrstuvwxyz'\n", 415 | " s = dict.fromkeys(S,0)\n", 416 | " t = {e:widthds[en.index(e)] for e in s.keys()}\n", 417 | " c = 0\n", 418 | " l = 1\n", 419 | " for e in S:\n", 420 | " c+=t[e]\n", 421 | " if c > 100:\n", 422 | " l+=1\n", 423 | " c = t[e]\n", 424 | " return l,c\n" 425 | ] 426 | }, 427 | { 428 | "cell_type": "code", 429 | "execution_count": 29, 430 | "metadata": {}, 431 | "outputs": [], 432 | "source": [ 433 | "widths = [4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]\n", 434 | "S = \"bbbcccdddaaa\"" 435 | ] 436 | }, 437 | { 438 | "cell_type": "code", 439 | "execution_count": 36, 440 | "metadata": {}, 441 | "outputs": [], 442 | "source": [ 443 | "l = list(S)" 444 | ] 445 | }, 446 | { 447 | "cell_type": "code", 448 | "execution_count": 31, 449 | "metadata": {}, 450 | "outputs": [], 451 | "source": [ 452 | "en = 'abcdefghijklmnopqrstuvwxyz'" 453 | ] 454 | }, 455 | { 456 | "cell_type": "code", 457 | "execution_count": 33, 458 | "metadata": {}, 459 | "outputs": [ 460 | { 461 | "data": { 462 | "text/plain": [ 463 | "1" 464 | ] 465 | }, 466 | "execution_count": 33, 467 | "metadata": {}, 468 | "output_type": "execute_result" 469 | } 470 | ], 471 | "source": [ 472 | "en.index('b')" 473 | ] 474 | }, 475 | { 476 | "cell_type": "code", 477 | "execution_count": 30, 478 | "metadata": {}, 479 | "outputs": [], 480 | "source": [ 481 | "s = dict.fromkeys(S,0)" 482 | ] 483 | }, 484 | { 485 | "cell_type": "markdown", 486 | "metadata": {}, 487 | "source": [ 488 | "---" 489 | ] 490 | }, 491 | { 492 | "cell_type": "markdown", 493 | "metadata": {}, 494 | "source": [ 495 | "Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place.\n", 496 | "\n", 497 | "The move sequence is represented by a string. And each move is represent by a character. The valid robot moves are R (Right), L (Left), U (Up) and D (down). The output should be true or false representing whether the robot makes a circle." 498 | ] 499 | }, 500 | { 501 | "cell_type": "code", 502 | "execution_count": 45, 503 | "metadata": {}, 504 | "outputs": [], 505 | "source": [ 506 | "def judgeCircle(moves):\n", 507 | " return (moves.count('L') == moves.count('R')) and (moves.count('U')==moves.count('D'))\n", 508 | " " 509 | ] 510 | }, 511 | { 512 | "cell_type": "code", 513 | "execution_count": 46, 514 | "metadata": {}, 515 | "outputs": [], 516 | "source": [ 517 | "moves= \"UD\"" 518 | ] 519 | }, 520 | { 521 | "cell_type": "code", 522 | "execution_count": 47, 523 | "metadata": {}, 524 | "outputs": [ 525 | { 526 | "data": { 527 | "text/plain": [ 528 | "True" 529 | ] 530 | }, 531 | "execution_count": 47, 532 | "metadata": {}, 533 | "output_type": "execute_result" 534 | } 535 | ], 536 | "source": [ 537 | "judgeCircle(moves)" 538 | ] 539 | }, 540 | { 541 | "cell_type": "markdown", 542 | "metadata": {}, 543 | "source": [ 544 | "---" 545 | ] 546 | }, 547 | { 548 | "cell_type": "markdown", 549 | "metadata": {}, 550 | "source": [ 551 | "Given a 32-bit signed integer, reverse digits of an integer." 552 | ] 553 | }, 554 | { 555 | "cell_type": "code", 556 | "execution_count": 10, 557 | "metadata": {}, 558 | "outputs": [], 559 | "source": [ 560 | "def reverse(x):\n", 561 | " \"\"\"\n", 562 | " :type x: int\n", 563 | " :rtype: int\n", 564 | " \"\"\"\n", 565 | " if x>=0:\n", 566 | " x = str(x)\n", 567 | " x = int(x[::-1])\n", 568 | " elif x<0:\n", 569 | " x = str(abs(x))\n", 570 | " x = int(x[::-1]) \n", 571 | " x = -x\n", 572 | " if x> 2**31-1 or x< -2**31: return 0\n", 573 | " else: return x\n", 574 | " " 575 | ] 576 | }, 577 | { 578 | "cell_type": "code", 579 | "execution_count": 11, 580 | "metadata": {}, 581 | "outputs": [ 582 | { 583 | "data": { 584 | "text/plain": [ 585 | "-1" 586 | ] 587 | }, 588 | "execution_count": 11, 589 | "metadata": {}, 590 | "output_type": "execute_result" 591 | } 592 | ], 593 | "source": [ 594 | "reverse(-10)" 595 | ] 596 | }, 597 | { 598 | "cell_type": "markdown", 599 | "metadata": {}, 600 | "source": [ 601 | "---" 602 | ] 603 | }, 604 | { 605 | "cell_type": "markdown", 606 | "metadata": {}, 607 | "source": [ 608 | "Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.\n", 609 | "\n", 610 | "An input string is valid if:\n", 611 | "\n", 612 | "Open brackets must be closed by the same type of brackets.\n", 613 | "Open brackets must be closed in the correct order.\n", 614 | "Note that an empty string is also considered valid." 615 | ] 616 | }, 617 | { 618 | "cell_type": "code", 619 | "execution_count": 52, 620 | "metadata": {}, 621 | "outputs": [], 622 | "source": [ 623 | "def isValid(s):\n", 624 | " \"\"\"\n", 625 | " :type s: str\n", 626 | " :rtype: bool\n", 627 | " \"\"\"\n", 628 | " bracket_dict = {'(': ')', ')': '(', '{': '}', '}': '{', ']': '[', '[': ']'}\n", 629 | " open_br = ['(', '{', '[']\n", 630 | " current_open = []\n", 631 | " l = list(s)\n", 632 | " for e in l:\n", 633 | " if e in open_br:\n", 634 | " current_open.append(e)\n", 635 | " elif len(current_open) > 0 and e == bracket_dict[current_open[-1]]:\n", 636 | " current_open.pop(-1)\n", 637 | " elif len(current_open) > 0 and e != bracket_dict[current_open[-1]]:\n", 638 | " return False\n", 639 | " else:\n", 640 | " return False\n", 641 | " if len(current_open)>0: return False\n", 642 | " else: return True" 643 | ] 644 | }, 645 | { 646 | "cell_type": "code", 647 | "execution_count": 53, 648 | "metadata": {}, 649 | "outputs": [], 650 | "source": [ 651 | "s1 = \"[\"" 652 | ] 653 | }, 654 | { 655 | "cell_type": "code", 656 | "execution_count": 54, 657 | "metadata": {}, 658 | "outputs": [ 659 | { 660 | "data": { 661 | "text/plain": [ 662 | "False" 663 | ] 664 | }, 665 | "execution_count": 54, 666 | "metadata": {}, 667 | "output_type": "execute_result" 668 | } 669 | ], 670 | "source": [ 671 | "isValid(s1)" 672 | ] 673 | }, 674 | { 675 | "cell_type": "markdown", 676 | "metadata": {}, 677 | "source": [ 678 | "Better solution using stack" 679 | ] 680 | }, 681 | { 682 | "cell_type": "code", 683 | "execution_count": null, 684 | "metadata": {}, 685 | "outputs": [], 686 | "source": [ 687 | "def parChecker(string):\n", 688 | " s = myStack()\n", 689 | " balanced = True\n", 690 | " idx = 0\n", 691 | " open_br = '{(['\n", 692 | " close_br = '})]'\n", 693 | " while idx < len(string) and balanced:\n", 694 | " if string[idx] in open_br:\n", 695 | " s.push(string[idx])\n", 696 | " else:\n", 697 | " if s.isEmpty(): balanced = False\n", 698 | " else:\n", 699 | " if close_br.index(string[idx]) != open_br.index(s.pop()):\n", 700 | " balanced = False\n", 701 | " idx += 1\n", 702 | " if balanced and s.isEmpty(): return True\n", 703 | " else: return False" 704 | ] 705 | }, 706 | { 707 | "cell_type": "markdown", 708 | "metadata": {}, 709 | "source": [ 710 | "---" 711 | ] 712 | }, 713 | { 714 | "cell_type": "markdown", 715 | "metadata": {}, 716 | "source": [ 717 | "Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.\n", 718 | "\n", 719 | "Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory." 720 | ] 721 | }, 722 | { 723 | "cell_type": "code", 724 | "execution_count": 163, 725 | "metadata": {}, 726 | "outputs": [], 727 | "source": [ 728 | "def removeDuplicates(nums):\n", 729 | " \"\"\"\n", 730 | " :type nums: List[int]\n", 731 | " :rtype: int\n", 732 | " \"\"\"\n", 733 | " sub = 0\n", 734 | " for i in range(len(nums)):\n", 735 | " e = nums[i-sub]\n", 736 | " if i>0 and e == nums[i-1-sub]:\n", 737 | " nums.pop(i-sub)\n", 738 | " sub+=1\n", 739 | " return len(nums)" 740 | ] 741 | }, 742 | { 743 | "cell_type": "code", 744 | "execution_count": 164, 745 | "metadata": {}, 746 | "outputs": [], 747 | "source": [ 748 | "nums = [0,0,1,1,1,2,2,3,3,4]" 749 | ] 750 | }, 751 | { 752 | "cell_type": "code", 753 | "execution_count": 165, 754 | "metadata": {}, 755 | "outputs": [ 756 | { 757 | "data": { 758 | "text/plain": [ 759 | "5" 760 | ] 761 | }, 762 | "execution_count": 165, 763 | "metadata": {}, 764 | "output_type": "execute_result" 765 | } 766 | ], 767 | "source": [ 768 | "removeDuplicates(nums)" 769 | ] 770 | }, 771 | { 772 | "cell_type": "code", 773 | "execution_count": 166, 774 | "metadata": {}, 775 | "outputs": [ 776 | { 777 | "data": { 778 | "text/plain": [ 779 | "[0, 1, 2, 3, 4]" 780 | ] 781 | }, 782 | "execution_count": 166, 783 | "metadata": {}, 784 | "output_type": "execute_result" 785 | } 786 | ], 787 | "source": [ 788 | "nums" 789 | ] 790 | }, 791 | { 792 | "cell_type": "markdown", 793 | "metadata": {}, 794 | "source": [ 795 | "---" 796 | ] 797 | }, 798 | { 799 | "cell_type": "markdown", 800 | "metadata": {}, 801 | "source": [ 802 | "Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.\n", 803 | "\n", 804 | "Note:\n", 805 | "\n", 806 | "The number of elements initialized in nums1 and nums2 are m and n respectively.\n", 807 | "You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2." 808 | ] 809 | }, 810 | { 811 | "cell_type": "code", 812 | "execution_count": null, 813 | "metadata": {}, 814 | "outputs": [], 815 | "source": [ 816 | "def merge(nums1, m, nums2, n):\n", 817 | " \"\"\"\n", 818 | " :type nums1: List[int]\n", 819 | " :type m: int\n", 820 | " :type nums2: List[int]\n", 821 | " :type n: int\n", 822 | " :rtype: void Do not return anything, modify nums1 in-place instead.\n", 823 | " \"\"\"\n", 824 | " l1max = nums1[m-1]\n", 825 | " k = 0\n", 826 | " for i,e in enumerate(num2):\n", 827 | " if l1max= target:\n", 877 | " return # if we reach the number why bother to continue\n", 878 | "\n", 879 | " for i, e in enumerate(numbers):\n", 880 | " remaining = numbers[i + 1:]\n", 881 | " subset_sum(remaining, target, partial + [e],combination)\n", 882 | " return combination" 883 | ] 884 | }, 885 | { 886 | "cell_type": "code", 887 | "execution_count": 12, 888 | "metadata": {}, 889 | "outputs": [ 890 | { 891 | "data": { 892 | "text/plain": [ 893 | "[[1, 4, 3],\n", 894 | " [1, 4, 2, 1],\n", 895 | " [1, 5, 2],\n", 896 | " [1, 4, 3],\n", 897 | " [1, 4, 2, 1],\n", 898 | " [1, 1, 6],\n", 899 | " [1, 7],\n", 900 | " [4, 4],\n", 901 | " [4, 3, 1],\n", 902 | " [5, 3],\n", 903 | " [5, 2, 1],\n", 904 | " [4, 3, 1],\n", 905 | " [2, 6],\n", 906 | " [1, 7],\n", 907 | " [8]]" 908 | ] 909 | }, 910 | "execution_count": 12, 911 | "metadata": {}, 912 | "output_type": "execute_result" 913 | } 914 | ], 915 | "source": [ 916 | "subset_sum(l,8)" 917 | ] 918 | }, 919 | { 920 | "cell_type": "markdown", 921 | "metadata": {}, 922 | "source": [ 923 | "---" 924 | ] 925 | }, 926 | { 927 | "cell_type": "markdown", 928 | "metadata": {}, 929 | "source": [ 930 | "Merge 2 sorted lists" 931 | ] 932 | }, 933 | { 934 | "cell_type": "code", 935 | "execution_count": 141, 936 | "metadata": {}, 937 | "outputs": [], 938 | "source": [ 939 | "l1 = [1, 3, 7, 8, 99, 100, 111, 2, 34, 55, 34]\n", 940 | "l2 = [2, 4, 5, 7, 80, 88, 23, 31, 23, 40]" 941 | ] 942 | }, 943 | { 944 | "cell_type": "code", 945 | "execution_count": 142, 946 | "metadata": {}, 947 | "outputs": [], 948 | "source": [ 949 | "l1 = sorted(l1)\n", 950 | "l2 = sorted(l2)" 951 | ] 952 | }, 953 | { 954 | "cell_type": "code", 955 | "execution_count": 143, 956 | "metadata": {}, 957 | "outputs": [], 958 | "source": [ 959 | "def merge(l1, l2):\n", 960 | " len1 = len(l1)\n", 961 | " len2 = len(l2)\n", 962 | " l3 = [None] * (len1 + len2)\n", 963 | " idx1 = 0\n", 964 | " idx2 = 0\n", 965 | " idx3 = 0\n", 966 | " while idx1 < len1 and idx2 < len2:\n", 967 | " if l1[idx1] < l2[idx2]:\n", 968 | " l3[idx3] = l1[idx1]\n", 969 | " idx1 += 1\n", 970 | " idx3 += 1\n", 971 | " elif l1[idx1] > l2[idx2]:\n", 972 | " l3[idx3] = l2[idx2]\n", 973 | " idx2 += 1\n", 974 | " idx3 += 1\n", 975 | " else:\n", 976 | " l3[idx3] = l1[idx1]\n", 977 | " l3[idx3 + 1] = l2[idx2]\n", 978 | " idx1 += 1\n", 979 | " idx2 += 1\n", 980 | " idx3 += 2\n", 981 | " if idx1 == len1:\n", 982 | " l3[idx3:] = l2[idx2:]\n", 983 | " else:\n", 984 | " l3[idx3:] = l1[idx1:]\n", 985 | " return l3" 986 | ] 987 | }, 988 | { 989 | "cell_type": "code", 990 | "execution_count": 144, 991 | "metadata": {}, 992 | "outputs": [ 993 | { 994 | "data": { 995 | "text/plain": [ 996 | "[1, 2, 2, 3, 4, 5, 7, 7, 8, 23, 23, 31, 34, 34, 40, 55, 80, 88, 99, 100, 111]" 997 | ] 998 | }, 999 | "execution_count": 144, 1000 | "metadata": {}, 1001 | "output_type": "execute_result" 1002 | } 1003 | ], 1004 | "source": [ 1005 | "merge(l1, l2)" 1006 | ] 1007 | }, 1008 | { 1009 | "cell_type": "markdown", 1010 | "metadata": {}, 1011 | "source": [ 1012 | "----" 1013 | ] 1014 | }, 1015 | { 1016 | "cell_type": "markdown", 1017 | "metadata": {}, 1018 | "source": [ 1019 | "Sum 2" 1020 | ] 1021 | }, 1022 | { 1023 | "cell_type": "code", 1024 | "execution_count": 147, 1025 | "metadata": {}, 1026 | "outputs": [], 1027 | "source": [ 1028 | "l = [2,3,4,8,9]" 1029 | ] 1030 | }, 1031 | { 1032 | "cell_type": "code", 1033 | "execution_count": 148, 1034 | "metadata": {}, 1035 | "outputs": [], 1036 | "source": [ 1037 | "t = 7" 1038 | ] 1039 | }, 1040 | { 1041 | "cell_type": "markdown", 1042 | "metadata": {}, 1043 | "source": [ 1044 | "Given an array of integers, return indices of the two numbers such that they add up to a specific target.\n", 1045 | "\n", 1046 | "You may assume that each input would have exactly one solution, and you may not use the same element twice." 1047 | ] 1048 | }, 1049 | { 1050 | "cell_type": "code", 1051 | "execution_count": 149, 1052 | "metadata": {}, 1053 | "outputs": [], 1054 | "source": [ 1055 | "def twoSum(nums, target):\n", 1056 | " \"\"\"\n", 1057 | " :type nums: List[int]\n", 1058 | " :type target: int\n", 1059 | " :rtype: List[int]\n", 1060 | " \"\"\"\n", 1061 | " lookup = {}\n", 1062 | " for i, num in enumerate(nums):\n", 1063 | " if target - num in lookup:\n", 1064 | " return [lookup[target - num], i]\n", 1065 | " lookup[num] = i " 1066 | ] 1067 | }, 1068 | { 1069 | "cell_type": "code", 1070 | "execution_count": 150, 1071 | "metadata": {}, 1072 | "outputs": [ 1073 | { 1074 | "data": { 1075 | "text/plain": [ 1076 | "[1, 2]" 1077 | ] 1078 | }, 1079 | "execution_count": 150, 1080 | "metadata": {}, 1081 | "output_type": "execute_result" 1082 | } 1083 | ], 1084 | "source": [ 1085 | "twoSum(l,7)" 1086 | ] 1087 | }, 1088 | { 1089 | "cell_type": "markdown", 1090 | "metadata": {}, 1091 | "source": [ 1092 | "---" 1093 | ] 1094 | }, 1095 | { 1096 | "cell_type": "markdown", 1097 | "metadata": {}, 1098 | "source": [ 1099 | "Contain duplicate" 1100 | ] 1101 | }, 1102 | { 1103 | "cell_type": "markdown", 1104 | "metadata": {}, 1105 | "source": [ 1106 | "Given an array of integers, find if the array contains any duplicates.\n", 1107 | "\n", 1108 | "Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct." 1109 | ] 1110 | }, 1111 | { 1112 | "cell_type": "code", 1113 | "execution_count": 162, 1114 | "metadata": {}, 1115 | "outputs": [], 1116 | "source": [ 1117 | "def checkdup(l):\n", 1118 | " return len(set(l))!=len(l)" 1119 | ] 1120 | }, 1121 | { 1122 | "cell_type": "code", 1123 | "execution_count": 163, 1124 | "metadata": {}, 1125 | "outputs": [], 1126 | "source": [ 1127 | "l = [1, 1, 1, 3, 3, 4, 3, 2, 4, 2]" 1128 | ] 1129 | }, 1130 | { 1131 | "cell_type": "code", 1132 | "execution_count": 164, 1133 | "metadata": {}, 1134 | "outputs": [ 1135 | { 1136 | "data": { 1137 | "text/plain": [ 1138 | "True" 1139 | ] 1140 | }, 1141 | "execution_count": 164, 1142 | "metadata": {}, 1143 | "output_type": "execute_result" 1144 | } 1145 | ], 1146 | "source": [ 1147 | "checkdup(l)" 1148 | ] 1149 | }, 1150 | { 1151 | "cell_type": "code", 1152 | "execution_count": 165, 1153 | "metadata": {}, 1154 | "outputs": [ 1155 | { 1156 | "data": { 1157 | "text/plain": [ 1158 | "False" 1159 | ] 1160 | }, 1161 | "execution_count": 165, 1162 | "metadata": {}, 1163 | "output_type": "execute_result" 1164 | } 1165 | ], 1166 | "source": [ 1167 | "l = [1, 2, 3, 4]\n", 1168 | "checkdup(l)" 1169 | ] 1170 | }, 1171 | { 1172 | "cell_type": "markdown", 1173 | "metadata": {}, 1174 | "source": [ 1175 | "---" 1176 | ] 1177 | }, 1178 | { 1179 | "cell_type": "markdown", 1180 | "metadata": {}, 1181 | "source": [ 1182 | "Rotate array" 1183 | ] 1184 | }, 1185 | { 1186 | "cell_type": "markdown", 1187 | "metadata": {}, 1188 | "source": [ 1189 | "Given an array, rotate the array to the right by k steps, where k is non-negative." 1190 | ] 1191 | }, 1192 | { 1193 | "cell_type": "markdown", 1194 | "metadata": {}, 1195 | "source": [ 1196 | "Input: [1,2,3,4,5,6,7] and k = 3\n", 1197 | "Output: [5,6,7,1,2,3,4]" 1198 | ] 1199 | }, 1200 | { 1201 | "cell_type": "code", 1202 | "execution_count": 72, 1203 | "metadata": {}, 1204 | "outputs": [], 1205 | "source": [ 1206 | "def rotateArr(l, k):\n", 1207 | " if k == 0:\n", 1208 | " return l\n", 1209 | " if k > len(l):\n", 1210 | " k = k % len(l)\n", 1211 | " out = l[-k:]\n", 1212 | " out.extend(l[:-k])\n", 1213 | " return out" 1214 | ] 1215 | }, 1216 | { 1217 | "cell_type": "code", 1218 | "execution_count": 73, 1219 | "metadata": {}, 1220 | "outputs": [], 1221 | "source": [ 1222 | "l = [1, 2, 3, 4, 5, 6, 7]\n", 1223 | "k = 4" 1224 | ] 1225 | }, 1226 | { 1227 | "cell_type": "code", 1228 | "execution_count": 74, 1229 | "metadata": {}, 1230 | "outputs": [ 1231 | { 1232 | "data": { 1233 | "text/plain": [ 1234 | "[4, 5, 6, 7, 1, 2, 3]" 1235 | ] 1236 | }, 1237 | "execution_count": 74, 1238 | "metadata": {}, 1239 | "output_type": "execute_result" 1240 | } 1241 | ], 1242 | "source": [ 1243 | "rotateArr(l, 4)" 1244 | ] 1245 | }, 1246 | { 1247 | "cell_type": "markdown", 1248 | "metadata": {}, 1249 | "source": [ 1250 | "---" 1251 | ] 1252 | }, 1253 | { 1254 | "cell_type": "code", 1255 | "execution_count": 112, 1256 | "metadata": {}, 1257 | "outputs": [], 1258 | "source": [ 1259 | "def productExceptSelf(nums):\n", 1260 | " \"\"\"\n", 1261 | " :type nums: List[int]\n", 1262 | " :rtype: List[int]\n", 1263 | " \"\"\"\n", 1264 | " out = [None] * len(nums)\n", 1265 | " for i in range(len(nums)):\n", 1266 | " print(nums)\n", 1267 | " nums_c = nums[:]\n", 1268 | " nums.pop(i)\n", 1269 | " out[i] = np.product(nums)\n", 1270 | " nums = nums_c[:]\n", 1271 | " return out" 1272 | ] 1273 | }, 1274 | { 1275 | "cell_type": "code", 1276 | "execution_count": 117, 1277 | "metadata": {}, 1278 | "outputs": [], 1279 | "source": [ 1280 | "nums = [1, 2, 3]" 1281 | ] 1282 | }, 1283 | { 1284 | "cell_type": "code", 1285 | "execution_count": 118, 1286 | "metadata": {}, 1287 | "outputs": [ 1288 | { 1289 | "name": "stdout", 1290 | "output_type": "stream", 1291 | "text": [ 1292 | "[0]\n" 1293 | ] 1294 | }, 1295 | { 1296 | "data": { 1297 | "text/plain": [ 1298 | "[1.0]" 1299 | ] 1300 | }, 1301 | "execution_count": 118, 1302 | "metadata": {}, 1303 | "output_type": "execute_result" 1304 | } 1305 | ], 1306 | "source": [ 1307 | "productExceptSelf(nums)" 1308 | ] 1309 | }, 1310 | { 1311 | "cell_type": "markdown", 1312 | "metadata": {}, 1313 | "source": [ 1314 | "---" 1315 | ] 1316 | }, 1317 | { 1318 | "cell_type": "markdown", 1319 | "metadata": {}, 1320 | "source": [ 1321 | "Write a program that can determine the height of an arbitrary binary tree" 1322 | ] 1323 | }, 1324 | { 1325 | "cell_type": "markdown", 1326 | "metadata": {}, 1327 | "source": [ 1328 | "1. Find the max depth of the left subtree\n", 1329 | "2. Find the max depth of the right subtree\n", 1330 | "3. Return the larger one\n", 1331 | "4. This has to be done recursively" 1332 | ] 1333 | }, 1334 | { 1335 | "cell_type": "code", 1336 | "execution_count": 135, 1337 | "metadata": {}, 1338 | "outputs": [ 1339 | { 1340 | "name": "stdout", 1341 | "output_type": "stream", 1342 | "text": [ 1343 | "Height of tree is 3\n" 1344 | ] 1345 | } 1346 | ], 1347 | "source": [ 1348 | "# Python program to find the maximum depth of tree\n", 1349 | "#Algorithm\n", 1350 | "\n", 1351 | "# A binary tree node\n", 1352 | "class Node:\n", 1353 | " def __init__(self, data):\n", 1354 | " self.data = data\n", 1355 | " self.left = None\n", 1356 | " self.right = None\n", 1357 | "\n", 1358 | "\n", 1359 | "def maxDepth(node):\n", 1360 | " if node is None: return 0\n", 1361 | " else:\n", 1362 | "\n", 1363 | " ldepth = maxDepth(node.left)\n", 1364 | " rdepth = maxDepth(node.right)\n", 1365 | "\n", 1366 | " if ldepth > rdepth:\n", 1367 | " return ldepth + 1\n", 1368 | " else:\n", 1369 | " return rdepth + 1\n", 1370 | "\n", 1371 | "#tree creation\n", 1372 | "root = Node(1)\n", 1373 | "root.left = Node(2)\n", 1374 | "root.right = Node(3)\n", 1375 | "root.left.left = Node(4)\n", 1376 | "root.left.right = Node(5)\n", 1377 | "\n", 1378 | "print(\"Height of tree is %d\" % (maxDepth(root)))" 1379 | ] 1380 | }, 1381 | { 1382 | "cell_type": "markdown", 1383 | "metadata": {}, 1384 | "source": [ 1385 | "---" 1386 | ] 1387 | }, 1388 | { 1389 | "cell_type": "markdown", 1390 | "metadata": {}, 1391 | "source": [ 1392 | "Calculating a square root" 1393 | ] 1394 | }, 1395 | { 1396 | "cell_type": "code", 1397 | "execution_count": 166, 1398 | "metadata": {}, 1399 | "outputs": [], 1400 | "source": [ 1401 | "def mysqrt(num):\n", 1402 | " if num == 0 or num == 1:\n", 1403 | " return num\n", 1404 | " lower = 0\n", 1405 | " upper = num\n", 1406 | " while lower <= upper:\n", 1407 | " middle = (lower + upper) // 2\n", 1408 | " if middle * middle == num:\n", 1409 | " return middle\n", 1410 | " elif middle * middle < num:\n", 1411 | " lower = middle + 1\n", 1412 | " else:\n", 1413 | " upper = middle - 1\n", 1414 | " return \"No integer square root\"" 1415 | ] 1416 | }, 1417 | { 1418 | "cell_type": "code", 1419 | "execution_count": 167, 1420 | "metadata": {}, 1421 | "outputs": [ 1422 | { 1423 | "data": { 1424 | "text/plain": [ 1425 | "'No integer square root'" 1426 | ] 1427 | }, 1428 | "execution_count": 167, 1429 | "metadata": {}, 1430 | "output_type": "execute_result" 1431 | } 1432 | ], 1433 | "source": [ 1434 | "mysqrt(5)" 1435 | ] 1436 | }, 1437 | { 1438 | "cell_type": "markdown", 1439 | "metadata": {}, 1440 | "source": [ 1441 | "Decimal precision" 1442 | ] 1443 | }, 1444 | { 1445 | "cell_type": "code", 1446 | "execution_count": 158, 1447 | "metadata": {}, 1448 | "outputs": [], 1449 | "source": [ 1450 | "def mysqrt(num):\n", 1451 | " if num == 0 or num == 1:\n", 1452 | " return num\n", 1453 | " lower = 0\n", 1454 | " upper = num\n", 1455 | " while lower <= upper:\n", 1456 | " middle = (lower + upper) / 2.00\n", 1457 | " if abs(middle * middle - num) < .01:\n", 1458 | " return middle\n", 1459 | " elif middle * middle < num:\n", 1460 | " lower = middle + .001\n", 1461 | " else:\n", 1462 | " upper = middle - .001\n", 1463 | " return \"No integer square root\"" 1464 | ] 1465 | }, 1466 | { 1467 | "cell_type": "code", 1468 | "execution_count": 159, 1469 | "metadata": {}, 1470 | "outputs": [ 1471 | { 1472 | "data": { 1473 | "text/plain": [ 1474 | "2.2362226562500007" 1475 | ] 1476 | }, 1477 | "execution_count": 159, 1478 | "metadata": {}, 1479 | "output_type": "execute_result" 1480 | } 1481 | ], 1482 | "source": [ 1483 | "mysqrt(5)" 1484 | ] 1485 | }, 1486 | { 1487 | "cell_type": "code", 1488 | "execution_count": 161, 1489 | "metadata": {}, 1490 | "outputs": [ 1491 | { 1492 | "data": { 1493 | "text/plain": [ 1494 | "2.23606797749979" 1495 | ] 1496 | }, 1497 | "execution_count": 161, 1498 | "metadata": {}, 1499 | "output_type": "execute_result" 1500 | } 1501 | ], 1502 | "source": [ 1503 | "np.sqrt(5)" 1504 | ] 1505 | }, 1506 | { 1507 | "cell_type": "markdown", 1508 | "metadata": {}, 1509 | "source": [ 1510 | "---" 1511 | ] 1512 | }, 1513 | { 1514 | "cell_type": "markdown", 1515 | "metadata": {}, 1516 | "source": [ 1517 | "How do you find the second largest element in a Binary Search Tree?" 1518 | ] 1519 | }, 1520 | { 1521 | "cell_type": "code", 1522 | "execution_count": 162, 1523 | "metadata": {}, 1524 | "outputs": [], 1525 | "source": [ 1526 | "class bstnode():\n", 1527 | " def __init__(self, key, val):\n", 1528 | " self.key = key\n", 1529 | " self.val = val\n", 1530 | " self.left = None\n", 1531 | " self.right = None\n", 1532 | "\n", 1533 | " def get(self):\n", 1534 | " return self.val\n", 1535 | "\n", 1536 | " def set_val(self, val):\n", 1537 | " self.val = val\n", 1538 | "\n", 1539 | "\n", 1540 | "class bst():\n", 1541 | " def __init__(self):\n", 1542 | " self.root = None\n", 1543 | "\n", 1544 | " def preorder_(self, current_node):\n", 1545 | " if current_node is None:\n", 1546 | " return\n", 1547 | " else:\n", 1548 | " print(current_node.val)\n", 1549 | " self.preorder_(current_node.left)\n", 1550 | " self.preorder_(current_node.right)\n", 1551 | "\n", 1552 | " def insert(self, key, val):\n", 1553 | " if self.root is None:\n", 1554 | " self.root = bstnode(key, val)\n", 1555 | " else:\n", 1556 | " self.insert_node(self.root, key, val)\n", 1557 | "\n", 1558 | " def find(self, item):\n", 1559 | " return self.find_node(self.root, item)\n", 1560 | "\n", 1561 | " def find_node(self, current_node, item):\n", 1562 | " if current_node is None: return \"Empty tree\"\n", 1563 | " elif item == current_node.key:\n", 1564 | " return current_node.val\n", 1565 | " elif item < current_node.key:\n", 1566 | " return self.find_node(current_node.left, item)\n", 1567 | " else:\n", 1568 | " return self.find_node(current_node.right, item)\n", 1569 | "\n", 1570 | " def traverse(self):\n", 1571 | " return self.preorder_(self.root)\n", 1572 | "\n", 1573 | " def insert_node(self, current_node, key, val):\n", 1574 | " if key < current_node.key:\n", 1575 | " if current_node.left is None:\n", 1576 | " current_node.left = bstnode(key, val)\n", 1577 | " else:\n", 1578 | " self.insert_node(current_node.left, key, val)\n", 1579 | " elif key > current_node.key:\n", 1580 | " if current_node.right is None:\n", 1581 | " current_node.right = bstnode(key, val)\n", 1582 | " else:\n", 1583 | " self.insert_node(current_node.right, key, val)\n", 1584 | " else:\n", 1585 | " current_node.val = val" 1586 | ] 1587 | }, 1588 | { 1589 | "cell_type": "code", 1590 | "execution_count": 169, 1591 | "metadata": {}, 1592 | "outputs": [], 1593 | "source": [ 1594 | "mybst = bst()\n", 1595 | "\n", 1596 | "mybst.insert(100, \"Shikhar\")\n", 1597 | "\n", 1598 | "mybst.insert(120, \"Neerja\")\n", 1599 | "\n", 1600 | "mybst.insert(90, \"Prince\")\n", 1601 | "\n", 1602 | "mybst.insert(300, \"Alvira\")\n", 1603 | "\n", 1604 | "mybst.insert(420, \"Kerem\")\n", 1605 | "\n", 1606 | "mybst.insert(50, \"Akshay\")\n", 1607 | "\n", 1608 | "mybst.insert(540,\"Devesh\")" 1609 | ] 1610 | }, 1611 | { 1612 | "cell_type": "code", 1613 | "execution_count": 170, 1614 | "metadata": {}, 1615 | "outputs": [], 1616 | "source": [ 1617 | "def scnd_largest(node,parent = None):\n", 1618 | " parent = None\n", 1619 | " while node.right is not None:\n", 1620 | " parent = node\n", 1621 | " node = node.right\n", 1622 | " if node.left is None:\n", 1623 | " return parent.key\n", 1624 | " else:\n", 1625 | " return node.left.key" 1626 | ] 1627 | }, 1628 | { 1629 | "cell_type": "code", 1630 | "execution_count": 171, 1631 | "metadata": {}, 1632 | "outputs": [ 1633 | { 1634 | "data": { 1635 | "text/plain": [ 1636 | "420" 1637 | ] 1638 | }, 1639 | "execution_count": 171, 1640 | "metadata": {}, 1641 | "output_type": "execute_result" 1642 | } 1643 | ], 1644 | "source": [ 1645 | "scnd_largest(mybst.root)" 1646 | ] 1647 | }, 1648 | { 1649 | "cell_type": "markdown", 1650 | "metadata": {}, 1651 | "source": [ 1652 | "---" 1653 | ] 1654 | }, 1655 | { 1656 | "cell_type": "markdown", 1657 | "metadata": {}, 1658 | "source": [ 1659 | "Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero." 1660 | ] 1661 | }, 1662 | { 1663 | "cell_type": "code", 1664 | "execution_count": 179, 1665 | "metadata": {}, 1666 | "outputs": [], 1667 | "source": [ 1668 | "nums = [-1, 0, 1, 2, -1, -4,0,0]" 1669 | ] 1670 | }, 1671 | { 1672 | "cell_type": "code", 1673 | "execution_count": 168, 1674 | "metadata": {}, 1675 | "outputs": [], 1676 | "source": [ 1677 | "def findTriplets(l):\n", 1678 | " triplets = []\n", 1679 | " end = len(l) - 1\n", 1680 | " for i in range(len(l)):\n", 1681 | " e1 = l[i]\n", 1682 | " if i < len(l) - 2:\n", 1683 | " for j in range(i + 1, len(l)):\n", 1684 | " e2 = l[j]\n", 1685 | " if -e1 - e2 in l[j + 1:] and sorted([e1, e2, -e1 - e2\n", 1686 | " ]) not in triplets:\n", 1687 | " triplets.append(sorted([e1, e2, -e1 - e2]))\n", 1688 | " else:\n", 1689 | " continue\n", 1690 | " return triplets" 1691 | ] 1692 | }, 1693 | { 1694 | "cell_type": "code", 1695 | "execution_count": 18, 1696 | "metadata": {}, 1697 | "outputs": [ 1698 | { 1699 | "data": { 1700 | "text/plain": [ 1701 | "[[-1, 0, 1], [-1, -1, 2], [0, 0, 0]]" 1702 | ] 1703 | }, 1704 | "execution_count": 18, 1705 | "metadata": {}, 1706 | "output_type": "execute_result" 1707 | } 1708 | ], 1709 | "source": [ 1710 | "findTriplets(nums)" 1711 | ] 1712 | }, 1713 | { 1714 | "cell_type": "markdown", 1715 | "metadata": {}, 1716 | "source": [ 1717 | "----" 1718 | ] 1719 | }, 1720 | { 1721 | "cell_type": "markdown", 1722 | "metadata": {}, 1723 | "source": [ 1724 | "You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.\n", 1725 | "\n", 1726 | "Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police." 1727 | ] 1728 | }, 1729 | { 1730 | "cell_type": "code", 1731 | "execution_count": 49, 1732 | "metadata": {}, 1733 | "outputs": [], 1734 | "source": [ 1735 | "l = [100,1,2,4,5,123,43,21,123]" 1736 | ] 1737 | }, 1738 | { 1739 | "cell_type": "code", 1740 | "execution_count": 50, 1741 | "metadata": {}, 1742 | "outputs": [], 1743 | "source": [ 1744 | "def findMax(l):\n", 1745 | " if len(l) == 1: return l[0]\n", 1746 | " elif len(l) == 2: return max(l[0], l[1])\n", 1747 | " elif len(l) == 3: return max(l[0] + l[2], l[1])\n", 1748 | " smax = 0\n", 1749 | " for i in range(len(l) - 2):\n", 1750 | " s = l[i]\n", 1751 | " s += findMax(l[i + 2:])\n", 1752 | " if s > smax:\n", 1753 | " smax = s\n", 1754 | " return smax" 1755 | ] 1756 | }, 1757 | { 1758 | "cell_type": "code", 1759 | "execution_count": 51, 1760 | "metadata": {}, 1761 | "outputs": [ 1762 | { 1763 | "data": { 1764 | "text/plain": [ 1765 | "350" 1766 | ] 1767 | }, 1768 | "execution_count": 51, 1769 | "metadata": {}, 1770 | "output_type": "execute_result" 1771 | } 1772 | ], 1773 | "source": [ 1774 | "findMax(l)" 1775 | ] 1776 | }, 1777 | { 1778 | "cell_type": "markdown", 1779 | "metadata": {}, 1780 | "source": [ 1781 | "----" 1782 | ] 1783 | }, 1784 | { 1785 | "cell_type": "markdown", 1786 | "metadata": {}, 1787 | "source": [ 1788 | "https://leetcode.com/problems/move-zeroes/description/" 1789 | ] 1790 | }, 1791 | { 1792 | "cell_type": "markdown", 1793 | "metadata": {}, 1794 | "source": [ 1795 | "Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements." 1796 | ] 1797 | }, 1798 | { 1799 | "cell_type": "code", 1800 | "execution_count": 30, 1801 | "metadata": {}, 1802 | "outputs": [], 1803 | "source": [ 1804 | "Input = [0,1,0,3,12]\n", 1805 | "Output = [1,3,12,0,0]" 1806 | ] 1807 | }, 1808 | { 1809 | "cell_type": "markdown", 1810 | "metadata": {}, 1811 | "source": [ 1812 | "Note:\n", 1813 | "\n", 1814 | "You must do this in-place without making a copy of the array.\n", 1815 | "Minimize the total number of operations." 1816 | ] 1817 | }, 1818 | { 1819 | "cell_type": "code", 1820 | "execution_count": 31, 1821 | "metadata": {}, 1822 | "outputs": [], 1823 | "source": [ 1824 | "def moveZeroes(nums):\n", 1825 | " \"\"\"\n", 1826 | " :type nums: List[int]\n", 1827 | " :rtype: void Do not return anything, modify nums in-place instead.\n", 1828 | " \"\"\"\n", 1829 | " non_zero_ = []\n", 1830 | " non_zero_cnt = 0\n", 1831 | " zero_cnt = 0\n", 1832 | " for i,e in enumerate(nums):\n", 1833 | " if e!=0:\n", 1834 | " non_zero_.append(e)\n", 1835 | " non_zero_cnt+=1\n", 1836 | " else:\n", 1837 | " zero_cnt+=1\n", 1838 | " if non_zero_cnt>0:\n", 1839 | " nums[:non_zero_cnt] = non_zero_\n", 1840 | " if zero_cnt>0:\n", 1841 | " nums[-zero_cnt:] = [0]*zero_cnt" 1842 | ] 1843 | }, 1844 | { 1845 | "cell_type": "code", 1846 | "execution_count": 32, 1847 | "metadata": {}, 1848 | "outputs": [], 1849 | "source": [ 1850 | "moveZeroes(Input)" 1851 | ] 1852 | }, 1853 | { 1854 | "cell_type": "code", 1855 | "execution_count": 33, 1856 | "metadata": {}, 1857 | "outputs": [ 1858 | { 1859 | "data": { 1860 | "text/plain": [ 1861 | "[1, 3, 12, 0, 0]" 1862 | ] 1863 | }, 1864 | "execution_count": 33, 1865 | "metadata": {}, 1866 | "output_type": "execute_result" 1867 | } 1868 | ], 1869 | "source": [ 1870 | "Input" 1871 | ] 1872 | }, 1873 | { 1874 | "cell_type": "markdown", 1875 | "metadata": {}, 1876 | "source": [ 1877 | "----" 1878 | ] 1879 | }, 1880 | { 1881 | "cell_type": "markdown", 1882 | "metadata": {}, 1883 | "source": [ 1884 | "https://leetcode.com/problems/majority-element/description/" 1885 | ] 1886 | }, 1887 | { 1888 | "cell_type": "markdown", 1889 | "metadata": {}, 1890 | "source": [ 1891 | "\n", 1892 | "Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.\n", 1893 | "\n", 1894 | "You may assume that the array is non-empty and the majority element always exist in the array." 1895 | ] 1896 | }, 1897 | { 1898 | "cell_type": "code", 1899 | "execution_count": 75, 1900 | "metadata": {}, 1901 | "outputs": [], 1902 | "source": [ 1903 | "Input = [2,2,1,1,1,2,2]" 1904 | ] 1905 | }, 1906 | { 1907 | "cell_type": "code", 1908 | "execution_count": 78, 1909 | "metadata": {}, 1910 | "outputs": [], 1911 | "source": [ 1912 | "def majorityElement(nums):\n", 1913 | " \"\"\"\n", 1914 | " :type nums: List[int]\n", 1915 | " :rtype: int\n", 1916 | " \"\"\"\n", 1917 | " d = dict.fromkeys(nums, 0)\n", 1918 | " thres = len(nums) // 2\n", 1919 | " for i, e in enumerate(nums):\n", 1920 | " d[e] += 1\n", 1921 | " majority = []\n", 1922 | " for e in d.keys():\n", 1923 | " if d[e] > thres: majority.append(e)\n", 1924 | " return majority[0]" 1925 | ] 1926 | }, 1927 | { 1928 | "cell_type": "code", 1929 | "execution_count": 79, 1930 | "metadata": {}, 1931 | "outputs": [ 1932 | { 1933 | "data": { 1934 | "text/plain": [ 1935 | "2" 1936 | ] 1937 | }, 1938 | "execution_count": 79, 1939 | "metadata": {}, 1940 | "output_type": "execute_result" 1941 | } 1942 | ], 1943 | "source": [ 1944 | "majorityElement(Input)" 1945 | ] 1946 | }, 1947 | { 1948 | "cell_type": "markdown", 1949 | "metadata": {}, 1950 | "source": [ 1951 | "---" 1952 | ] 1953 | }, 1954 | { 1955 | "cell_type": "markdown", 1956 | "metadata": {}, 1957 | "source": [ 1958 | "https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/" 1959 | ] 1960 | }, 1961 | { 1962 | "cell_type": "markdown", 1963 | "metadata": {}, 1964 | "source": [ 1965 | "Say you have an array for which the ith element is the price of a given stock on day i.\n", 1966 | "\n", 1967 | "Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).\n", 1968 | "\n", 1969 | "Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again)." 1970 | ] 1971 | }, 1972 | { 1973 | "cell_type": "markdown", 1974 | "metadata": {}, 1975 | "source": [ 1976 | "Example 1:\n", 1977 | "\n", 1978 | "Input: [7,1,5,3,6,4]\n", 1979 | "Output: 7\n", 1980 | "Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.\n", 1981 | " Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.\n", 1982 | "Example 2:\n", 1983 | "\n", 1984 | "Input: [1,2,3,4,5]\n", 1985 | "Output: 4\n", 1986 | "Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.\n", 1987 | " Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are\n", 1988 | " engaging multiple transactions at the same time. You must sell before buying again.\n", 1989 | "Example 3:\n", 1990 | "\n", 1991 | "Input: [7,6,4,3,1]\n", 1992 | "Output: 0\n", 1993 | "Explanation: In this case, no transaction is done, i.e. max profit = 0." 1994 | ] 1995 | }, 1996 | { 1997 | "cell_type": "code", 1998 | "execution_count": 83, 1999 | "metadata": {}, 2000 | "outputs": [], 2001 | "source": [ 2002 | "Input = [7,1,5,3,6,4] " 2003 | ] 2004 | }, 2005 | { 2006 | "cell_type": "code", 2007 | "execution_count": 86, 2008 | "metadata": {}, 2009 | "outputs": [], 2010 | "source": [ 2011 | "def maxProfit(prices):\n", 2012 | " \"\"\"\n", 2013 | " :type prices: List[int]\n", 2014 | " :rtype: int\n", 2015 | " \"\"\"\n", 2016 | " if not prices:\n", 2017 | " return 0\n", 2018 | " low = high = prices[0]\n", 2019 | " profit = 0\n", 2020 | " for i in range(1, len(prices)):\n", 2021 | " if prices[i] >= prices[i - 1]:\n", 2022 | " high = prices[i]\n", 2023 | " else:\n", 2024 | " profit += high - low\n", 2025 | " low = high = prices[i]\n", 2026 | " profit += high - low\n", 2027 | " return profit" 2028 | ] 2029 | }, 2030 | { 2031 | "cell_type": "code", 2032 | "execution_count": 87, 2033 | "metadata": {}, 2034 | "outputs": [ 2035 | { 2036 | "data": { 2037 | "text/plain": [ 2038 | "7" 2039 | ] 2040 | }, 2041 | "execution_count": 87, 2042 | "metadata": {}, 2043 | "output_type": "execute_result" 2044 | } 2045 | ], 2046 | "source": [ 2047 | "maxProfit(Input)" 2048 | ] 2049 | }, 2050 | { 2051 | "cell_type": "markdown", 2052 | "metadata": {}, 2053 | "source": [ 2054 | "---" 2055 | ] 2056 | }, 2057 | { 2058 | "cell_type": "markdown", 2059 | "metadata": {}, 2060 | "source": [ 2061 | "https://leetcode.com/problems/missing-number/description/" 2062 | ] 2063 | }, 2064 | { 2065 | "cell_type": "markdown", 2066 | "metadata": {}, 2067 | "source": [ 2068 | "Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array." 2069 | ] 2070 | }, 2071 | { 2072 | "cell_type": "code", 2073 | "execution_count": 130, 2074 | "metadata": {}, 2075 | "outputs": [], 2076 | "source": [ 2077 | "Input = [0,1]\n", 2078 | "Output = 2" 2079 | ] 2080 | }, 2081 | { 2082 | "cell_type": "code", 2083 | "execution_count": 133, 2084 | "metadata": {}, 2085 | "outputs": [], 2086 | "source": [ 2087 | "def missingNumber(nums):\n", 2088 | " \"\"\"\n", 2089 | " :type nums: List[int]\n", 2090 | " :rtype: int\n", 2091 | " \"\"\"\n", 2092 | " nums = sorted(nums)\n", 2093 | " for i,e in enumerate(nums):\n", 2094 | " if nums[i] != i:\n", 2095 | " return i\n", 2096 | " return nums[i]+1" 2097 | ] 2098 | }, 2099 | { 2100 | "cell_type": "code", 2101 | "execution_count": 134, 2102 | "metadata": {}, 2103 | "outputs": [ 2104 | { 2105 | "data": { 2106 | "text/plain": [ 2107 | "2" 2108 | ] 2109 | }, 2110 | "execution_count": 134, 2111 | "metadata": {}, 2112 | "output_type": "execute_result" 2113 | } 2114 | ], 2115 | "source": [ 2116 | "missingNumber(Input)" 2117 | ] 2118 | }, 2119 | { 2120 | "cell_type": "code", 2121 | "execution_count": 116, 2122 | "metadata": {}, 2123 | "outputs": [ 2124 | { 2125 | "data": { 2126 | "text/plain": [ 2127 | "[0]" 2128 | ] 2129 | }, 2130 | "execution_count": 116, 2131 | "metadata": {}, 2132 | "output_type": "execute_result" 2133 | } 2134 | ], 2135 | "source": [ 2136 | "sorted(Input)" 2137 | ] 2138 | }, 2139 | { 2140 | "cell_type": "markdown", 2141 | "metadata": {}, 2142 | "source": [ 2143 | "---" 2144 | ] 2145 | }, 2146 | { 2147 | "cell_type": "markdown", 2148 | "metadata": {}, 2149 | "source": [ 2150 | "https://leetcode.com/problems/maximum-subarray/description/" 2151 | ] 2152 | }, 2153 | { 2154 | "cell_type": "markdown", 2155 | "metadata": {}, 2156 | "source": [ 2157 | "\n", 2158 | "Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum." 2159 | ] 2160 | }, 2161 | { 2162 | "cell_type": "code", 2163 | "execution_count": 70, 2164 | "metadata": {}, 2165 | "outputs": [], 2166 | "source": [ 2167 | "Input = [1,2,-1,-2,2,1,-2,1,4,-5,4]\n", 2168 | "Output = 6" 2169 | ] 2170 | }, 2171 | { 2172 | "cell_type": "code", 2173 | "execution_count": 77, 2174 | "metadata": {}, 2175 | "outputs": [], 2176 | "source": [ 2177 | "def maxSubArray(nums):\n", 2178 | " \"\"\"\n", 2179 | " :type nums: List[int]\n", 2180 | " :rtype: int\n", 2181 | " \"\"\"\n", 2182 | " if nums == []:\n", 2183 | " return 0\n", 2184 | " elif len(nums) == 1:\n", 2185 | " return nums[0]\n", 2186 | " elif len(nums) == 2:\n", 2187 | " return max(nums[0], nums[1], nums[0] + nums[1])\n", 2188 | " all_neg = True\n", 2189 | " for entries in nums:\n", 2190 | " if entries >= 0:\n", 2191 | " all_neg = False\n", 2192 | " if all_neg == False:\n", 2193 | " current_sum = 0\n", 2194 | " max_sum = sum(nums)\n", 2195 | " for i in range(len(nums)):\n", 2196 | " current_sum += nums[i]\n", 2197 | " if current_sum < 0:\n", 2198 | " current_sum = 0\n", 2199 | "\n", 2200 | " elif current_sum > max_sum:\n", 2201 | " max_sum = current_sum\n", 2202 | " return max_sum\n", 2203 | " else:\n", 2204 | " return max(nums)" 2205 | ] 2206 | }, 2207 | { 2208 | "cell_type": "code", 2209 | "execution_count": 78, 2210 | "metadata": {}, 2211 | "outputs": [ 2212 | { 2213 | "data": { 2214 | "text/plain": [ 2215 | "[1, 2, -1, -2, 2, 1, -2, 1, 4, -5, 4]" 2216 | ] 2217 | }, 2218 | "execution_count": 78, 2219 | "metadata": {}, 2220 | "output_type": "execute_result" 2221 | } 2222 | ], 2223 | "source": [ 2224 | "Input" 2225 | ] 2226 | }, 2227 | { 2228 | "cell_type": "code", 2229 | "execution_count": 79, 2230 | "metadata": {}, 2231 | "outputs": [ 2232 | { 2233 | "data": { 2234 | "text/plain": [ 2235 | "6" 2236 | ] 2237 | }, 2238 | "execution_count": 79, 2239 | "metadata": {}, 2240 | "output_type": "execute_result" 2241 | } 2242 | ], 2243 | "source": [ 2244 | "maxSubArray(Input)" 2245 | ] 2246 | }, 2247 | { 2248 | "cell_type": "markdown", 2249 | "metadata": {}, 2250 | "source": [ 2251 | "----" 2252 | ] 2253 | }, 2254 | { 2255 | "cell_type": "markdown", 2256 | "metadata": {}, 2257 | "source": [ 2258 | "https://leetcode.com/problems/reshape-the-matrix/description/" 2259 | ] 2260 | }, 2261 | { 2262 | "cell_type": "markdown", 2263 | "metadata": {}, 2264 | "source": [ 2265 | "You're given a matrix represented by a two-dimensional array, and two positive integers r and c representing the row number and column number of the wanted reshaped matrix, respectively.\n", 2266 | "\n", 2267 | "The reshaped matrix need to be filled with all the elements of the original matrix in the same row-traversing order as they were.\n", 2268 | "\n", 2269 | "If the 'reshape' operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix." 2270 | ] 2271 | }, 2272 | { 2273 | "cell_type": "code", 2274 | "execution_count": 88, 2275 | "metadata": {}, 2276 | "outputs": [], 2277 | "source": [ 2278 | "nums = [[1,2],[3,4]]\n", 2279 | "r = 1\n", 2280 | "c = 4" 2281 | ] 2282 | }, 2283 | { 2284 | "cell_type": "code", 2285 | "execution_count": 82, 2286 | "metadata": {}, 2287 | "outputs": [], 2288 | "source": [ 2289 | "import numpy as np" 2290 | ] 2291 | }, 2292 | { 2293 | "cell_type": "code", 2294 | "execution_count": 84, 2295 | "metadata": {}, 2296 | "outputs": [], 2297 | "source": [ 2298 | "k = np.random.randint(0,4,(3,4))" 2299 | ] 2300 | }, 2301 | { 2302 | "cell_type": "code", 2303 | "execution_count": 85, 2304 | "metadata": {}, 2305 | "outputs": [ 2306 | { 2307 | "data": { 2308 | "text/plain": [ 2309 | "array([[1, 3, 3, 1],\n", 2310 | " [0, 0, 3, 2],\n", 2311 | " [2, 1, 3, 0]])" 2312 | ] 2313 | }, 2314 | "execution_count": 85, 2315 | "metadata": {}, 2316 | "output_type": "execute_result" 2317 | } 2318 | ], 2319 | "source": [ 2320 | "k" 2321 | ] 2322 | }, 2323 | { 2324 | "cell_type": "code", 2325 | "execution_count": 86, 2326 | "metadata": {}, 2327 | "outputs": [ 2328 | { 2329 | "data": { 2330 | "text/plain": [ 2331 | "array([[1, 3, 3],\n", 2332 | " [1, 0, 0],\n", 2333 | " [3, 2, 2],\n", 2334 | " [1, 3, 0]])" 2335 | ] 2336 | }, 2337 | "execution_count": 86, 2338 | "metadata": {}, 2339 | "output_type": "execute_result" 2340 | } 2341 | ], 2342 | "source": [ 2343 | "k.reshape(4,3)" 2344 | ] 2345 | }, 2346 | { 2347 | "cell_type": "code", 2348 | "execution_count": 75, 2349 | "metadata": {}, 2350 | "outputs": [ 2351 | { 2352 | "data": { 2353 | "text/plain": [ 2354 | "[[1, 2], [3, 4]]" 2355 | ] 2356 | }, 2357 | "execution_count": 75, 2358 | "metadata": {}, 2359 | "output_type": "execute_result" 2360 | } 2361 | ], 2362 | "source": [ 2363 | "nums" 2364 | ] 2365 | }, 2366 | { 2367 | "cell_type": "code", 2368 | "execution_count": 81, 2369 | "metadata": {}, 2370 | "outputs": [ 2371 | { 2372 | "data": { 2373 | "text/plain": [ 2374 | "[[None, None, None, None]]" 2375 | ] 2376 | }, 2377 | "execution_count": 81, 2378 | "metadata": {}, 2379 | "output_type": "execute_result" 2380 | } 2381 | ], 2382 | "source": [ 2383 | "[[None]*4]*1" 2384 | ] 2385 | }, 2386 | { 2387 | "cell_type": "code", 2388 | "execution_count": 89, 2389 | "metadata": {}, 2390 | "outputs": [], 2391 | "source": [ 2392 | "def matrixReshape(nums, r, c):\n", 2393 | " \"\"\"\n", 2394 | " :type nums: List[List[int]]\n", 2395 | " :type r: int\n", 2396 | " :type c: int\n", 2397 | " :rtype: List[List[int]]\n", 2398 | " \"\"\"\n", 2399 | " row = len(nums)\n", 2400 | " col = len(nums[0])\n", 2401 | " if row * col != r*c: return nums\n", 2402 | " \n", 2403 | " else:\n", 2404 | " new_mat = [[]]*r\n", 2405 | " elements = []\n", 2406 | " for i in range(row):\n", 2407 | " elements.extend(nums[i])\n", 2408 | " k = 0\n", 2409 | " for i in range(r):\n", 2410 | " new_mat[i].extend(elements[k*c:(k+1)*c])\n", 2411 | " return new_mat " 2412 | ] 2413 | }, 2414 | { 2415 | "cell_type": "code", 2416 | "execution_count": 90, 2417 | "metadata": {}, 2418 | "outputs": [ 2419 | { 2420 | "data": { 2421 | "text/plain": [ 2422 | "[[1, 2, 3, 4]]" 2423 | ] 2424 | }, 2425 | "execution_count": 90, 2426 | "metadata": {}, 2427 | "output_type": "execute_result" 2428 | } 2429 | ], 2430 | "source": [ 2431 | "matrixReshape(nums,r,c)" 2432 | ] 2433 | }, 2434 | { 2435 | "cell_type": "markdown", 2436 | "metadata": {}, 2437 | "source": [ 2438 | "---" 2439 | ] 2440 | }, 2441 | { 2442 | "cell_type": "markdown", 2443 | "metadata": {}, 2444 | "source": [ 2445 | "Reverse an array without affecting special characters\n" 2446 | ] 2447 | }, 2448 | { 2449 | "cell_type": "markdown", 2450 | "metadata": {}, 2451 | "source": [ 2452 | "Given a string, that contains special character together with alphabets (‘a’ to ‘z’ and ‘A’ to ‘Z’), reverse the string in a way that special characters are not affected." 2453 | ] 2454 | }, 2455 | { 2456 | "cell_type": "code", 2457 | "execution_count": 2, 2458 | "metadata": {}, 2459 | "outputs": [], 2460 | "source": [ 2461 | "import string" 2462 | ] 2463 | }, 2464 | { 2465 | "cell_type": "code", 2466 | "execution_count": 11, 2467 | "metadata": {}, 2468 | "outputs": [], 2469 | "source": [ 2470 | "s = \"Ab,c,de!$\"" 2471 | ] 2472 | }, 2473 | { 2474 | "cell_type": "code", 2475 | "execution_count": 12, 2476 | "metadata": {}, 2477 | "outputs": [], 2478 | "source": [ 2479 | "def swap(s):\n", 2480 | " regular = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'\n", 2481 | " l = list(s)\n", 2482 | " start = 0\n", 2483 | " end = len(l) - 1\n", 2484 | " while start < end:\n", 2485 | " if l[start] not in regular:\n", 2486 | " start += 1\n", 2487 | " elif l[end] not in regular:\n", 2488 | " end -= 1\n", 2489 | " else:\n", 2490 | " l[start], l[end] = l[end], l[start]\n", 2491 | " start += 1\n", 2492 | " end -= 1\n", 2493 | " return ''.join(l)" 2494 | ] 2495 | }, 2496 | { 2497 | "cell_type": "code", 2498 | "execution_count": 13, 2499 | "metadata": {}, 2500 | "outputs": [ 2501 | { 2502 | "data": { 2503 | "text/plain": [ 2504 | "'ed,c,bA!$'" 2505 | ] 2506 | }, 2507 | "execution_count": 13, 2508 | "metadata": {}, 2509 | "output_type": "execute_result" 2510 | } 2511 | ], 2512 | "source": [ 2513 | "swap(s)" 2514 | ] 2515 | }, 2516 | { 2517 | "cell_type": "markdown", 2518 | "metadata": {}, 2519 | "source": [ 2520 | "---" 2521 | ] 2522 | }, 2523 | { 2524 | "cell_type": "markdown", 2525 | "metadata": {}, 2526 | "source": [ 2527 | "https://www.geeksforgeeks.org/minimum-length-subarray-sum-greater-given-value/" 2528 | ] 2529 | }, 2530 | { 2531 | "cell_type": "markdown", 2532 | "metadata": {}, 2533 | "source": [ 2534 | "Smallest subarray with sum greater than a given value" 2535 | ] 2536 | }, 2537 | { 2538 | "cell_type": "code", 2539 | "execution_count": 2, 2540 | "metadata": {}, 2541 | "outputs": [], 2542 | "source": [ 2543 | "def smallestSubWithSum(arr, x):\n", 2544 | " n = len(arr)\n", 2545 | " min_len = n + 1\n", 2546 | " for i, e in enumerate(arr):\n", 2547 | " current_sum = e\n", 2548 | " if current_sum > x:\n", 2549 | " return 1\n", 2550 | "\n", 2551 | " for j in range(i + 1, n):\n", 2552 | " current_sum += arr[j]\n", 2553 | "\n", 2554 | " if current_sum > x and j - i + 1 < min_len:\n", 2555 | " print(i,j)\n", 2556 | " min_len = j - i + 1\n", 2557 | " break\n", 2558 | " return min_len" 2559 | ] 2560 | }, 2561 | { 2562 | "cell_type": "code", 2563 | "execution_count": 3, 2564 | "metadata": {}, 2565 | "outputs": [], 2566 | "source": [ 2567 | "arr1 = [1, 4, 45, 6, 10, 19]" 2568 | ] 2569 | }, 2570 | { 2571 | "cell_type": "code", 2572 | "execution_count": 5, 2573 | "metadata": {}, 2574 | "outputs": [ 2575 | { 2576 | "name": "stdout", 2577 | "output_type": "stream", 2578 | "text": [ 2579 | "0 2\n", 2580 | "1 2\n" 2581 | ] 2582 | }, 2583 | { 2584 | "data": { 2585 | "text/plain": [ 2586 | "1" 2587 | ] 2588 | }, 2589 | "execution_count": 5, 2590 | "metadata": {}, 2591 | "output_type": "execute_result" 2592 | } 2593 | ], 2594 | "source": [ 2595 | "smallestSubWithSum(arr1,5)" 2596 | ] 2597 | }, 2598 | { 2599 | "cell_type": "markdown", 2600 | "metadata": {}, 2601 | "source": [ 2602 | "---" 2603 | ] 2604 | }, 2605 | { 2606 | "cell_type": "code", 2607 | "execution_count": 14, 2608 | "metadata": {}, 2609 | "outputs": [], 2610 | "source": [ 2611 | "def pythagoreantriple(arr):\n", 2612 | " n = len(arr)\n", 2613 | " for i, e in enumerate(arr):\n", 2614 | " arr[i] = e * e\n", 2615 | "\n", 2616 | " arr.sort()\n", 2617 | "\n", 2618 | " for i in range(n - 1, 1, -1):\n", 2619 | " j = 0\n", 2620 | " k = i - 1\n", 2621 | " while j < k:\n", 2622 | " if arr[j] + arr[k] == arr[i]:\n", 2623 | " return True\n", 2624 | " elif arr[j] + arr[k] > arr[i]:\n", 2625 | " k -= 1\n", 2626 | " else:\n", 2627 | " j += 1\n", 2628 | " return False" 2629 | ] 2630 | }, 2631 | { 2632 | "cell_type": "code", 2633 | "execution_count": 17, 2634 | "metadata": {}, 2635 | "outputs": [], 2636 | "source": [ 2637 | "ar = [3, 1, 4, 6, 1]" 2638 | ] 2639 | }, 2640 | { 2641 | "cell_type": "code", 2642 | "execution_count": 18, 2643 | "metadata": {}, 2644 | "outputs": [ 2645 | { 2646 | "data": { 2647 | "text/plain": [ 2648 | "False" 2649 | ] 2650 | }, 2651 | "execution_count": 18, 2652 | "metadata": {}, 2653 | "output_type": "execute_result" 2654 | } 2655 | ], 2656 | "source": [ 2657 | "pythagoreantriple(ar)" 2658 | ] 2659 | }, 2660 | { 2661 | "cell_type": "markdown", 2662 | "metadata": {}, 2663 | "source": [ 2664 | "----" 2665 | ] 2666 | }, 2667 | { 2668 | "cell_type": "markdown", 2669 | "metadata": {}, 2670 | "source": [ 2671 | "https://www.geeksforgeeks.org/count-triplets-with-sum-smaller-that-a-given-value/" 2672 | ] 2673 | }, 2674 | { 2675 | "cell_type": "markdown", 2676 | "metadata": {}, 2677 | "source": [ 2678 | "Count triplets with sum smaller than a given value" 2679 | ] 2680 | }, 2681 | { 2682 | "cell_type": "code", 2683 | "execution_count": 227, 2684 | "metadata": {}, 2685 | "outputs": [], 2686 | "source": [ 2687 | "def countTriplets(arr, target):\n", 2688 | "# print(arr)\n", 2689 | " arr = sorted(arr)\n", 2690 | "# print(arr)\n", 2691 | " n = len(arr)\n", 2692 | " triplets = []\n", 2693 | " count = 0\n", 2694 | " \n", 2695 | " for i, e in enumerate(arr):\n", 2696 | " j = i+1\n", 2697 | " k = n-1\n", 2698 | " while j= target:\n", 2703 | " k-=1\n", 2704 | " return count\n", 2705 | " " 2706 | ] 2707 | }, 2708 | { 2709 | "cell_type": "code", 2710 | "execution_count": 228, 2711 | "metadata": {}, 2712 | "outputs": [], 2713 | "source": [ 2714 | "nums = [5, 1, 3, 4, 7]" 2715 | ] 2716 | }, 2717 | { 2718 | "cell_type": "code", 2719 | "execution_count": 229, 2720 | "metadata": {}, 2721 | "outputs": [ 2722 | { 2723 | "data": { 2724 | "text/plain": [ 2725 | "4" 2726 | ] 2727 | }, 2728 | "execution_count": 229, 2729 | "metadata": {}, 2730 | "output_type": "execute_result" 2731 | } 2732 | ], 2733 | "source": [ 2734 | "countTriplets(nums,12)" 2735 | ] 2736 | }, 2737 | { 2738 | "cell_type": "markdown", 2739 | "metadata": {}, 2740 | "source": [ 2741 | "---" 2742 | ] 2743 | }, 2744 | { 2745 | "cell_type": "markdown", 2746 | "metadata": {}, 2747 | "source": [ 2748 | "https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/" 2749 | ] 2750 | }, 2751 | { 2752 | "cell_type": "markdown", 2753 | "metadata": {}, 2754 | "source": [ 2755 | "Say you have an array for which the ith element is the price of a given stock on day i.\n", 2756 | "\n", 2757 | "If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.\n", 2758 | "\n", 2759 | "Note that you cannot sell a stock before you buy one." 2760 | ] 2761 | }, 2762 | { 2763 | "cell_type": "code", 2764 | "execution_count": 5, 2765 | "metadata": {}, 2766 | "outputs": [], 2767 | "source": [ 2768 | "def maxProfit(prices):\n", 2769 | " n = len(prices)\n", 2770 | " max_profit = 0\n", 2771 | " for i in range(n):\n", 2772 | " for j in range(i + 1, n):\n", 2773 | " if prices[j] - prices[i] > max_profit:\n", 2774 | " max_profit = prices[j] - prices[i]\n", 2775 | " else:\n", 2776 | " continue\n", 2777 | " return max_profit" 2778 | ] 2779 | }, 2780 | { 2781 | "cell_type": "code", 2782 | "execution_count": 6, 2783 | "metadata": {}, 2784 | "outputs": [], 2785 | "source": [ 2786 | "prices = [7,1,5,3,6,4]" 2787 | ] 2788 | }, 2789 | { 2790 | "cell_type": "code", 2791 | "execution_count": 7, 2792 | "metadata": {}, 2793 | "outputs": [ 2794 | { 2795 | "data": { 2796 | "text/plain": [ 2797 | "5" 2798 | ] 2799 | }, 2800 | "execution_count": 7, 2801 | "metadata": {}, 2802 | "output_type": "execute_result" 2803 | } 2804 | ], 2805 | "source": [ 2806 | "maxProfit(prices)" 2807 | ] 2808 | }, 2809 | { 2810 | "cell_type": "code", 2811 | "execution_count": 8, 2812 | "metadata": {}, 2813 | "outputs": [], 2814 | "source": [ 2815 | "prices = [7,6,4,3,1]" 2816 | ] 2817 | }, 2818 | { 2819 | "cell_type": "code", 2820 | "execution_count": 9, 2821 | "metadata": {}, 2822 | "outputs": [ 2823 | { 2824 | "data": { 2825 | "text/plain": [ 2826 | "0" 2827 | ] 2828 | }, 2829 | "execution_count": 9, 2830 | "metadata": {}, 2831 | "output_type": "execute_result" 2832 | } 2833 | ], 2834 | "source": [ 2835 | "maxProfit(prices)" 2836 | ] 2837 | }, 2838 | { 2839 | "cell_type": "markdown", 2840 | "metadata": {}, 2841 | "source": [ 2842 | "A better solution" 2843 | ] 2844 | }, 2845 | { 2846 | "cell_type": "code", 2847 | "execution_count": 10, 2848 | "metadata": {}, 2849 | "outputs": [], 2850 | "source": [ 2851 | "def maxProfit(prices):\n", 2852 | " n = len(prices)\n", 2853 | " max_Profit = 0\n", 2854 | " if n < 2:\n", 2855 | " return 0\n", 2856 | " else:\n", 2857 | " min_price = prices[0]\n", 2858 | " for i in range(n):\n", 2859 | " profit = prices[i] - min_price\n", 2860 | " max_Profit = max(profit, max_Profit)\n", 2861 | " min_price = min(min_price, prices[i])\n", 2862 | " return max_Profit" 2863 | ] 2864 | }, 2865 | { 2866 | "cell_type": "code", 2867 | "execution_count": 12, 2868 | "metadata": {}, 2869 | "outputs": [], 2870 | "source": [ 2871 | "prices = [7,1,5,3,6,4]" 2872 | ] 2873 | }, 2874 | { 2875 | "cell_type": "code", 2876 | "execution_count": 13, 2877 | "metadata": {}, 2878 | "outputs": [ 2879 | { 2880 | "data": { 2881 | "text/plain": [ 2882 | "5" 2883 | ] 2884 | }, 2885 | "execution_count": 13, 2886 | "metadata": {}, 2887 | "output_type": "execute_result" 2888 | } 2889 | ], 2890 | "source": [ 2891 | "maxProfit(prices)" 2892 | ] 2893 | }, 2894 | { 2895 | "cell_type": "markdown", 2896 | "metadata": {}, 2897 | "source": [ 2898 | "----" 2899 | ] 2900 | }, 2901 | { 2902 | "cell_type": "markdown", 2903 | "metadata": {}, 2904 | "source": [ 2905 | "https://leetcode.com/problems/summary-ranges/" 2906 | ] 2907 | }, 2908 | { 2909 | "cell_type": "markdown", 2910 | "metadata": {}, 2911 | "source": [ 2912 | "Given a sorted integer array without duplicates, return the summary of its ranges.\n", 2913 | "\n", 2914 | "For example, given[0,1,2,4,5,7], return[\"0->2\",\"4->5\",\"7\"]." 2915 | ] 2916 | }, 2917 | { 2918 | "cell_type": "code", 2919 | "execution_count": 42, 2920 | "metadata": {}, 2921 | "outputs": [], 2922 | "source": [ 2923 | "def summaryRanges(nums):\n", 2924 | " \"\"\"\n", 2925 | " :type nums: List[int]\n", 2926 | " :rtype: List[str]\n", 2927 | " \"\"\"\n", 2928 | " summary = []\n", 2929 | " n = len(nums)\n", 2930 | " if n == 0:\n", 2931 | " return nums\n", 2932 | " elif n == 1:\n", 2933 | " return [f\"{nums[0]}\"]\n", 2934 | "\n", 2935 | " start = 0\n", 2936 | " end = 0\n", 2937 | "\n", 2938 | " for i in range(1, n):\n", 2939 | " if nums[i] - nums[i - 1] == 1:\n", 2940 | " end = i\n", 2941 | " else:\n", 2942 | " if end == start:\n", 2943 | " summary.append(f\"{nums[start]}\")\n", 2944 | " start = end = i\n", 2945 | " else:\n", 2946 | " summary.append(f\"{nums[start]}->{nums[end]}\")\n", 2947 | " start = i\n", 2948 | " end = i\n", 2949 | " if end == start:\n", 2950 | " summary.append(str(nums[start]))\n", 2951 | " start = end = i\n", 2952 | " else:\n", 2953 | " summary.append(str(nums[start]) + \"->\" + str(nums[end]))\n", 2954 | " start = i\n", 2955 | " end = i\n", 2956 | " return summary" 2957 | ] 2958 | }, 2959 | { 2960 | "cell_type": "code", 2961 | "execution_count": 43, 2962 | "metadata": {}, 2963 | "outputs": [], 2964 | "source": [ 2965 | "nums= [0,1,2,4,5,7]" 2966 | ] 2967 | }, 2968 | { 2969 | "cell_type": "code", 2970 | "execution_count": 44, 2971 | "metadata": {}, 2972 | "outputs": [ 2973 | { 2974 | "data": { 2975 | "text/plain": [ 2976 | "['0->2', '4->5', '7']" 2977 | ] 2978 | }, 2979 | "execution_count": 44, 2980 | "metadata": {}, 2981 | "output_type": "execute_result" 2982 | } 2983 | ], 2984 | "source": [ 2985 | "summaryRanges(nums)" 2986 | ] 2987 | }, 2988 | { 2989 | "cell_type": "markdown", 2990 | "metadata": {}, 2991 | "source": [ 2992 | "----" 2993 | ] 2994 | }, 2995 | { 2996 | "cell_type": "markdown", 2997 | "metadata": {}, 2998 | "source": [ 2999 | "https://dxmahata.gitbooks.io/leetcode-python-solutions/content/intersection_of_two_arrays.html" 3000 | ] 3001 | }, 3002 | { 3003 | "cell_type": "markdown", 3004 | "metadata": {}, 3005 | "source": [ 3006 | "Given two arrays, write a function to compute their intersection.\n", 3007 | "\n", 3008 | "Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].\n", 3009 | "\n", 3010 | "Note: Each element in the result must be unique. The result can be in any order." 3011 | ] 3012 | }, 3013 | { 3014 | "cell_type": "code", 3015 | "execution_count": 80, 3016 | "metadata": {}, 3017 | "outputs": [], 3018 | "source": [ 3019 | "def intersection(nums1, nums2):\n", 3020 | " \"\"\"\n", 3021 | " :type nums1: List[int]\n", 3022 | " :type nums2: List[int]\n", 3023 | " :rtype: List[int]\n", 3024 | " \"\"\"\n", 3025 | " return list(set(nums1).intersection(set(nums2)))" 3026 | ] 3027 | }, 3028 | { 3029 | "cell_type": "markdown", 3030 | "metadata": {}, 3031 | "source": [ 3032 | "---" 3033 | ] 3034 | }, 3035 | { 3036 | "cell_type": "markdown", 3037 | "metadata": {}, 3038 | "source": [ 3039 | "https://dxmahata.gitbooks.io/leetcode-python-solutions/content/reverse_vowels_of_a_string.html" 3040 | ] 3041 | }, 3042 | { 3043 | "cell_type": "markdown", 3044 | "metadata": {}, 3045 | "source": [ 3046 | "Write a function that takes a string as input and reverse only the vowels of a string.\n", 3047 | "\n", 3048 | "Example 1: Given s = \"hello\", return \"holle\".\n", 3049 | "\n", 3050 | "Example 2: Given s = \"leetcode\", return \"leotcede\".\n", 3051 | "\n", 3052 | "Note: The vowels does not include the letter \"y\"." 3053 | ] 3054 | }, 3055 | { 3056 | "cell_type": "code", 3057 | "execution_count": 89, 3058 | "metadata": {}, 3059 | "outputs": [], 3060 | "source": [ 3061 | "def reverseVowels(s):\n", 3062 | " \"\"\"\n", 3063 | " :type s: str\n", 3064 | " :rtype: str\n", 3065 | " \"\"\"\n", 3066 | " vowel = 'aeiouAEIOU'\n", 3067 | " s = list(s)\n", 3068 | " vowel_idx = []\n", 3069 | " for i,e in enumerate(s):\n", 3070 | " if e in vowel:\n", 3071 | " vowel_idx.append(i)\n", 3072 | " else:\n", 3073 | " continue\n", 3074 | " itr = len(vowel_idx)//2\n", 3075 | " for i in range(itr):\n", 3076 | " s[vowel_idx[i]],s[vowel_idx[-1-i]] = s[vowel_idx[-1-i]],s[vowel_idx[i]]\n", 3077 | " return ''.join(s)" 3078 | ] 3079 | }, 3080 | { 3081 | "cell_type": "code", 3082 | "execution_count": 85, 3083 | "metadata": {}, 3084 | "outputs": [], 3085 | "source": [ 3086 | "s = \"hello\"" 3087 | ] 3088 | }, 3089 | { 3090 | "cell_type": "code", 3091 | "execution_count": 86, 3092 | "metadata": {}, 3093 | "outputs": [ 3094 | { 3095 | "data": { 3096 | "text/plain": [ 3097 | "'holle'" 3098 | ] 3099 | }, 3100 | "execution_count": 86, 3101 | "metadata": {}, 3102 | "output_type": "execute_result" 3103 | } 3104 | ], 3105 | "source": [ 3106 | "reverseVowels(s)" 3107 | ] 3108 | }, 3109 | { 3110 | "cell_type": "code", 3111 | "execution_count": 87, 3112 | "metadata": {}, 3113 | "outputs": [], 3114 | "source": [ 3115 | "s = \"leetcode\"" 3116 | ] 3117 | }, 3118 | { 3119 | "cell_type": "code", 3120 | "execution_count": 88, 3121 | "metadata": {}, 3122 | "outputs": [ 3123 | { 3124 | "data": { 3125 | "text/plain": [ 3126 | "'leotcede'" 3127 | ] 3128 | }, 3129 | "execution_count": 88, 3130 | "metadata": {}, 3131 | "output_type": "execute_result" 3132 | } 3133 | ], 3134 | "source": [ 3135 | "reverseVowels(s)" 3136 | ] 3137 | }, 3138 | { 3139 | "cell_type": "markdown", 3140 | "metadata": {}, 3141 | "source": [ 3142 | "---" 3143 | ] 3144 | }, 3145 | { 3146 | "cell_type": "markdown", 3147 | "metadata": {}, 3148 | "source": [ 3149 | "https://dxmahata.gitbooks.io/leetcode-python-solutions/content/intersection_of_two_arrays_ii.html" 3150 | ] 3151 | }, 3152 | { 3153 | "cell_type": "markdown", 3154 | "metadata": {}, 3155 | "source": [ 3156 | "Given two arrays, write a function to compute their intersection.\n", 3157 | "\n", 3158 | "Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].\n", 3159 | "\n", 3160 | "Note: Each element in the result should appear as many times as it shows in both arrays. The result can be in any order. Follow up: What if the given array is already sorted? How would you optimize your algorithm? What if nums1's size is small compared to nums2's size? Which algorithm is better? What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?" 3161 | ] 3162 | }, 3163 | { 3164 | "cell_type": "code", 3165 | "execution_count": 115, 3166 | "metadata": {}, 3167 | "outputs": [], 3168 | "source": [ 3169 | "def intersect(nums1, nums2):\n", 3170 | " \"\"\"\n", 3171 | " :type nums1: List[int]\n", 3172 | " :type nums2: List[int]\n", 3173 | " :rtype: List[int]\n", 3174 | " \"\"\"\n", 3175 | " n1 = len(nums1)\n", 3176 | " n2 = len(nums2)\n", 3177 | " if n1 < n2:\n", 3178 | " nums_small = nums1\n", 3179 | " nums_big = nums2\n", 3180 | " else:\n", 3181 | " nums_small = nums2\n", 3182 | " nums_big = nums1\n", 3183 | " intersection = []\n", 3184 | " sub = 0\n", 3185 | " n = len(nums_small)\n", 3186 | " for i in range(n):\n", 3187 | " e = nums_small[i]\n", 3188 | " if e in nums_big:\n", 3189 | " intersection.append(e)\n", 3190 | " nums_big.pop(nums_big.index(e))\n", 3191 | " else:\n", 3192 | " continue\n", 3193 | " return intersection" 3194 | ] 3195 | }, 3196 | { 3197 | "cell_type": "code", 3198 | "execution_count": 116, 3199 | "metadata": {}, 3200 | "outputs": [], 3201 | "source": [ 3202 | "nums1 = [1, 2, 2, 1]\n", 3203 | "nums2 = [2, 2]" 3204 | ] 3205 | }, 3206 | { 3207 | "cell_type": "code", 3208 | "execution_count": 117, 3209 | "metadata": {}, 3210 | "outputs": [ 3211 | { 3212 | "data": { 3213 | "text/plain": [ 3214 | "[2, 2]" 3215 | ] 3216 | }, 3217 | "execution_count": 117, 3218 | "metadata": {}, 3219 | "output_type": "execute_result" 3220 | } 3221 | ], 3222 | "source": [ 3223 | "intersect(nums1,nums2)" 3224 | ] 3225 | }, 3226 | { 3227 | "cell_type": "markdown", 3228 | "metadata": {}, 3229 | "source": [ 3230 | "---" 3231 | ] 3232 | }, 3233 | { 3234 | "cell_type": "markdown", 3235 | "metadata": {}, 3236 | "source": [ 3237 | "Write a function to determine whether 2 strings are anagrams" 3238 | ] 3239 | }, 3240 | { 3241 | "cell_type": "code", 3242 | "execution_count": 5, 3243 | "metadata": {}, 3244 | "outputs": [], 3245 | "source": [ 3246 | "def anagram(s1,s2):\n", 3247 | " n1 = len(s1)\n", 3248 | " n2 = len(s2)\n", 3249 | " if n1!=n2:\n", 3250 | " return False\n", 3251 | " d1 = dict.fromkeys(s1)\n", 3252 | " d2 = dict.fromkeys(s2)\n", 3253 | " common = set(d1.keys()).union(set(d2.keys()))\n", 3254 | " for e in common:\n", 3255 | " if e not in d1 or e not in d2:\n", 3256 | " return False\n", 3257 | " elif d1[e] != d2[e]:\n", 3258 | " return False\n", 3259 | " return True" 3260 | ] 3261 | }, 3262 | { 3263 | "cell_type": "code", 3264 | "execution_count": 8, 3265 | "metadata": {}, 3266 | "outputs": [], 3267 | "source": [ 3268 | "str1 = \"test\"\n", 3269 | "str2 = \"ttes\"" 3270 | ] 3271 | }, 3272 | { 3273 | "cell_type": "code", 3274 | "execution_count": 9, 3275 | "metadata": {}, 3276 | "outputs": [ 3277 | { 3278 | "data": { 3279 | "text/plain": [ 3280 | "True" 3281 | ] 3282 | }, 3283 | "execution_count": 9, 3284 | "metadata": {}, 3285 | "output_type": "execute_result" 3286 | } 3287 | ], 3288 | "source": [ 3289 | "anagram(str1,str2)" 3290 | ] 3291 | }, 3292 | { 3293 | "cell_type": "code", 3294 | "execution_count": 10, 3295 | "metadata": {}, 3296 | "outputs": [], 3297 | "source": [ 3298 | "str1 = \"test\"\n", 3299 | "str2 = \"ttew\"" 3300 | ] 3301 | }, 3302 | { 3303 | "cell_type": "code", 3304 | "execution_count": 11, 3305 | "metadata": {}, 3306 | "outputs": [ 3307 | { 3308 | "data": { 3309 | "text/plain": [ 3310 | "False" 3311 | ] 3312 | }, 3313 | "execution_count": 11, 3314 | "metadata": {}, 3315 | "output_type": "execute_result" 3316 | } 3317 | ], 3318 | "source": [ 3319 | "anagram(str1,str2)" 3320 | ] 3321 | }, 3322 | { 3323 | "cell_type": "markdown", 3324 | "metadata": {}, 3325 | "source": [ 3326 | "----" 3327 | ] 3328 | }, 3329 | { 3330 | "cell_type": "markdown", 3331 | "metadata": {}, 3332 | "source": [ 3333 | "Fibonacci" 3334 | ] 3335 | }, 3336 | { 3337 | "cell_type": "code", 3338 | "execution_count": 12, 3339 | "metadata": {}, 3340 | "outputs": [], 3341 | "source": [ 3342 | "def fibonacci(n):\n", 3343 | " if n<2:\n", 3344 | " return n\n", 3345 | " return fibonacci(n-1)+fibonacci(n-2)" 3346 | ] 3347 | }, 3348 | { 3349 | "cell_type": "code", 3350 | "execution_count": 13, 3351 | "metadata": {}, 3352 | "outputs": [ 3353 | { 3354 | "data": { 3355 | "text/plain": [ 3356 | "3" 3357 | ] 3358 | }, 3359 | "execution_count": 13, 3360 | "metadata": {}, 3361 | "output_type": "execute_result" 3362 | } 3363 | ], 3364 | "source": [ 3365 | "fibonacci(4)" 3366 | ] 3367 | }, 3368 | { 3369 | "cell_type": "code", 3370 | "execution_count": 17, 3371 | "metadata": {}, 3372 | "outputs": [], 3373 | "source": [ 3374 | "d = {0: 0,1:1}\n", 3375 | "for i in range(2,50):\n", 3376 | " d[i] = d[i-1]+d[i-2] " 3377 | ] 3378 | }, 3379 | { 3380 | "cell_type": "code", 3381 | "execution_count": 18, 3382 | "metadata": {}, 3383 | "outputs": [ 3384 | { 3385 | "data": { 3386 | "text/plain": [ 3387 | "{0: 0,\n", 3388 | " 1: 1,\n", 3389 | " 2: 1,\n", 3390 | " 3: 2,\n", 3391 | " 4: 3,\n", 3392 | " 5: 5,\n", 3393 | " 6: 8,\n", 3394 | " 7: 13,\n", 3395 | " 8: 21,\n", 3396 | " 9: 34,\n", 3397 | " 10: 55,\n", 3398 | " 11: 89,\n", 3399 | " 12: 144,\n", 3400 | " 13: 233,\n", 3401 | " 14: 377,\n", 3402 | " 15: 610,\n", 3403 | " 16: 987,\n", 3404 | " 17: 1597,\n", 3405 | " 18: 2584,\n", 3406 | " 19: 4181,\n", 3407 | " 20: 6765,\n", 3408 | " 21: 10946,\n", 3409 | " 22: 17711,\n", 3410 | " 23: 28657,\n", 3411 | " 24: 46368,\n", 3412 | " 25: 75025,\n", 3413 | " 26: 121393,\n", 3414 | " 27: 196418,\n", 3415 | " 28: 317811,\n", 3416 | " 29: 514229,\n", 3417 | " 30: 832040,\n", 3418 | " 31: 1346269,\n", 3419 | " 32: 2178309,\n", 3420 | " 33: 3524578,\n", 3421 | " 34: 5702887,\n", 3422 | " 35: 9227465,\n", 3423 | " 36: 14930352,\n", 3424 | " 37: 24157817,\n", 3425 | " 38: 39088169,\n", 3426 | " 39: 63245986,\n", 3427 | " 40: 102334155,\n", 3428 | " 41: 165580141,\n", 3429 | " 42: 267914296,\n", 3430 | " 43: 433494437,\n", 3431 | " 44: 701408733,\n", 3432 | " 45: 1134903170,\n", 3433 | " 46: 1836311903,\n", 3434 | " 47: 2971215073,\n", 3435 | " 48: 4807526976,\n", 3436 | " 49: 7778742049}" 3437 | ] 3438 | }, 3439 | "execution_count": 18, 3440 | "metadata": {}, 3441 | "output_type": "execute_result" 3442 | } 3443 | ], 3444 | "source": [ 3445 | "d" 3446 | ] 3447 | }, 3448 | { 3449 | "cell_type": "markdown", 3450 | "metadata": {}, 3451 | "source": [ 3452 | "---" 3453 | ] 3454 | }, 3455 | { 3456 | "cell_type": "markdown", 3457 | "metadata": {}, 3458 | "source": [ 3459 | "Implement argsort without using numpy" 3460 | ] 3461 | }, 3462 | { 3463 | "cell_type": "code", 3464 | "execution_count": 65, 3465 | "metadata": {}, 3466 | "outputs": [], 3467 | "source": [ 3468 | "nums = [1,42,56,246,783,32,12,45]" 3469 | ] 3470 | }, 3471 | { 3472 | "cell_type": "code", 3473 | "execution_count": 20, 3474 | "metadata": {}, 3475 | "outputs": [ 3476 | { 3477 | "data": { 3478 | "text/plain": [ 3479 | "array([0, 6, 5, 1, 7, 2, 3, 4])" 3480 | ] 3481 | }, 3482 | "execution_count": 20, 3483 | "metadata": {}, 3484 | "output_type": "execute_result" 3485 | } 3486 | ], 3487 | "source": [ 3488 | "np.argsort(nums)" 3489 | ] 3490 | }, 3491 | { 3492 | "cell_type": "code", 3493 | "execution_count": 21, 3494 | "metadata": {}, 3495 | "outputs": [], 3496 | "source": [ 3497 | "def argsort(nums):\n", 3498 | " arg_idx = []\n", 3499 | " nums_c = nums.copy()\n", 3500 | " for i in range(len(nums)):\n", 3501 | " min_ = min(nums_c)\n", 3502 | " arg_idx.append(nums.index(min_))\n", 3503 | " nums_c.pop(nums_c.index(min_))\n", 3504 | " return arg_idx" 3505 | ] 3506 | }, 3507 | { 3508 | "cell_type": "code", 3509 | "execution_count": 22, 3510 | "metadata": {}, 3511 | "outputs": [ 3512 | { 3513 | "data": { 3514 | "text/plain": [ 3515 | "[0, 6, 5, 1, 7, 2, 3, 4]" 3516 | ] 3517 | }, 3518 | "execution_count": 22, 3519 | "metadata": {}, 3520 | "output_type": "execute_result" 3521 | } 3522 | ], 3523 | "source": [ 3524 | "argsort(nums)" 3525 | ] 3526 | }, 3527 | { 3528 | "cell_type": "markdown", 3529 | "metadata": {}, 3530 | "source": [ 3531 | "Assume that there are duplicates" 3532 | ] 3533 | }, 3534 | { 3535 | "cell_type": "code", 3536 | "execution_count": 89, 3537 | "metadata": {}, 3538 | "outputs": [], 3539 | "source": [ 3540 | "nums = [1,4,3,2,4]" 3541 | ] 3542 | }, 3543 | { 3544 | "cell_type": "code", 3545 | "execution_count": 90, 3546 | "metadata": {}, 3547 | "outputs": [], 3548 | "source": [ 3549 | "def rank(nums):\n", 3550 | " nums_s = sorted(set(nums))\n", 3551 | " d ={}\n", 3552 | " for i,e in enumerate(nums_s):\n", 3553 | " d[e] = i\n", 3554 | " rank = [d[e] for e in nums]\n", 3555 | " return rank" 3556 | ] 3557 | }, 3558 | { 3559 | "cell_type": "code", 3560 | "execution_count": 91, 3561 | "metadata": {}, 3562 | "outputs": [ 3563 | { 3564 | "data": { 3565 | "text/plain": [ 3566 | "[0, 3, 2, 1, 3]" 3567 | ] 3568 | }, 3569 | "execution_count": 91, 3570 | "metadata": {}, 3571 | "output_type": "execute_result" 3572 | } 3573 | ], 3574 | "source": [ 3575 | "rank(nums)" 3576 | ] 3577 | }, 3578 | { 3579 | "cell_type": "markdown", 3580 | "metadata": {}, 3581 | "source": [ 3582 | "----" 3583 | ] 3584 | }, 3585 | { 3586 | "cell_type": "markdown", 3587 | "metadata": {}, 3588 | "source": [ 3589 | "Product of Array Except Self: O(n)" 3590 | ] 3591 | }, 3592 | { 3593 | "cell_type": "code", 3594 | "execution_count": 5, 3595 | "metadata": {}, 3596 | "outputs": [], 3597 | "source": [ 3598 | "def productExceptSelf(nums):\n", 3599 | " \"\"\"\n", 3600 | " :type nums: List[int]\n", 3601 | " :rtype: List[int]\n", 3602 | " \"\"\"\n", 3603 | " size = len(nums)\n", 3604 | " output = [1] * size\n", 3605 | " left = 1\n", 3606 | " for x in range(size - 1):\n", 3607 | " left *= nums[x]\n", 3608 | " output[x + 1] *= left\n", 3609 | " right = 1\n", 3610 | " for x in range(size - 1, 0, -1):\n", 3611 | " right *= nums[x]\n", 3612 | " output[x - 1] *= right\n", 3613 | " return output" 3614 | ] 3615 | }, 3616 | { 3617 | "cell_type": "code", 3618 | "execution_count": 6, 3619 | "metadata": {}, 3620 | "outputs": [], 3621 | "source": [ 3622 | "nums = [1,2,3,4]" 3623 | ] 3624 | }, 3625 | { 3626 | "cell_type": "code", 3627 | "execution_count": 7, 3628 | "metadata": {}, 3629 | "outputs": [ 3630 | { 3631 | "data": { 3632 | "text/plain": [ 3633 | "[24, 12, 8, 6]" 3634 | ] 3635 | }, 3636 | "execution_count": 7, 3637 | "metadata": {}, 3638 | "output_type": "execute_result" 3639 | } 3640 | ], 3641 | "source": [ 3642 | "productExceptSelf(nums)" 3643 | ] 3644 | }, 3645 | { 3646 | "cell_type": "markdown", 3647 | "metadata": {}, 3648 | "source": [ 3649 | "----" 3650 | ] 3651 | }, 3652 | { 3653 | "cell_type": "markdown", 3654 | "metadata": {}, 3655 | "source": [ 3656 | "Permute a element of a set" 3657 | ] 3658 | }, 3659 | { 3660 | "cell_type": "code", 3661 | "execution_count": 8, 3662 | "metadata": {}, 3663 | "outputs": [], 3664 | "source": [ 3665 | "nums = [1,2,3]" 3666 | ] 3667 | }, 3668 | { 3669 | "cell_type": "code", 3670 | "execution_count": 9, 3671 | "metadata": {}, 3672 | "outputs": [], 3673 | "source": [ 3674 | "def permute(nums):\n", 3675 | " if len(nums) == 0:\n", 3676 | " return []\n", 3677 | " if len(nums) == 1:\n", 3678 | " return [nums]\n", 3679 | " l = []\n", 3680 | "\n", 3681 | " for i in range(len(nums)):\n", 3682 | " e = nums[i]\n", 3683 | " rem_list = nums[:i] + nums[i + 1:]\n", 3684 | " for a in permute(rem_list):\n", 3685 | " l.append([e] + a)\n", 3686 | " return l" 3687 | ] 3688 | }, 3689 | { 3690 | "cell_type": "code", 3691 | "execution_count": 10, 3692 | "metadata": {}, 3693 | "outputs": [ 3694 | { 3695 | "data": { 3696 | "text/plain": [ 3697 | "[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]" 3698 | ] 3699 | }, 3700 | "execution_count": 10, 3701 | "metadata": {}, 3702 | "output_type": "execute_result" 3703 | } 3704 | ], 3705 | "source": [ 3706 | "permute(nums)" 3707 | ] 3708 | }, 3709 | { 3710 | "cell_type": "markdown", 3711 | "metadata": {}, 3712 | "source": [ 3713 | "### Resources" 3714 | ] 3715 | }, 3716 | { 3717 | "cell_type": "markdown", 3718 | "metadata": {}, 3719 | "source": [ 3720 | "* https://www.geeksforgeeks.org/top-10-algorithms-in-interview-questions/#algo5\n", 3721 | "* https://dxmahata.gitbooks.io/leetcode-python-solutions/content/3_sum.html" 3722 | ] 3723 | } 3724 | ], 3725 | "metadata": { 3726 | "kernelspec": { 3727 | "display_name": "Python 3", 3728 | "language": "python", 3729 | "name": "python3" 3730 | }, 3731 | "language_info": { 3732 | "codemirror_mode": { 3733 | "name": "ipython", 3734 | "version": 3 3735 | }, 3736 | "file_extension": ".py", 3737 | "mimetype": "text/x-python", 3738 | "name": "python", 3739 | "nbconvert_exporter": "python", 3740 | "pygments_lexer": "ipython3", 3741 | "version": "3.6.5" 3742 | }, 3743 | "toc": { 3744 | "nav_menu": {}, 3745 | "number_sections": true, 3746 | "sideBar": true, 3747 | "skip_h1_title": false, 3748 | "toc_cell": false, 3749 | "toc_position": {}, 3750 | "toc_section_display": "block", 3751 | "toc_window_display": false 3752 | } 3753 | }, 3754 | "nbformat": 4, 3755 | "nbformat_minor": 2 3756 | } 3757 | --------------------------------------------------------------------------------