├── Calculus_Basics.ipynb ├── Exceptions.ipynb ├── File_Handling ├── .DS_Store ├── file_handling.ipynb ├── image.jpeg ├── image_copy.jpeg ├── image_copy_2.jpeg ├── image_copy_3.jpeg ├── sample.txt ├── sample2.txt ├── sample3.txt └── untitled.txt ├── Functional_Programming.ipynb ├── Functional_Programming_2.ipynb ├── Modules.ipynb ├── Modules ├── A.py ├── B.py ├── arithmetic.py ├── importer.py ├── main.py └── my_package │ ├── __init__.py │ ├── file1.py │ └── file2.py ├── OOPS_2.ipynb ├── OOP_101.ipynb ├── OOP_21st_September.ipynb ├── Problem_Solving_3.ipynb ├── Python_Refresher_1.ipynb ├── Python_Refresher_2.ipynb ├── Python_Refresher_3.ipynb ├── Python_Refresher_4.ipynb ├── README.md ├── Searching.ipynb ├── Sorting_1.ipynb └── Sorting_3.ipynb /Calculus_Basics.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "2e2767d2", 6 | "metadata": {}, 7 | "source": [ 8 | "## Calculus" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "d88ac559", 14 | "metadata": {}, 15 | "source": [ 16 | "## Functions" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 1, 22 | "id": "d85b02cd", 23 | "metadata": {}, 24 | "outputs": [], 25 | "source": [ 26 | "def f(x):\n", 27 | " return x**2" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 8, 33 | "id": "ebf0d7cb", 34 | "metadata": {}, 35 | "outputs": [ 36 | { 37 | "name": "stdout", 38 | "output_type": "stream", 39 | "text": [ 40 | "Inputs: [0, 1, 2, 3, 4, 5]\n", 41 | "Outputs: [0, 1, 4, 9, 16, 25]\n" 42 | ] 43 | } 44 | ], 45 | "source": [ 46 | "inputs = [0, 1, 2, 3, 4, 5]\n", 47 | "outputs = []\n", 48 | "\n", 49 | "# method 1\n", 50 | "for x in inputs:\n", 51 | " outputs.append(f(x))\n", 52 | "\n", 53 | "print(\"Inputs: \", inputs)\n", 54 | "print(\"Outputs: \", outputs)" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 9, 60 | "id": "ee8c5993", 61 | "metadata": {}, 62 | "outputs": [ 63 | { 64 | "name": "stdout", 65 | "output_type": "stream", 66 | "text": [ 67 | "[0, 1, 4, 9, 16, 25]\n" 68 | ] 69 | } 70 | ], 71 | "source": [ 72 | "# method 2 - using list comprehension\n", 73 | "outputs1 = [f(x) for x in inputs]\n", 74 | "print(outputs1)" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 10, 80 | "id": "05227426", 81 | "metadata": {}, 82 | "outputs": [ 83 | { 84 | "name": "stdout", 85 | "output_type": "stream", 86 | "text": [ 87 | "[0, 1, 4, 9, 16, 25]\n" 88 | ] 89 | } 90 | ], 91 | "source": [ 92 | "# method 3 - using functional programming - map\n", 93 | "outputs2 = list(map(f, inputs))\n", 94 | "print(outputs2)" 95 | ] 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "id": "463a4724", 100 | "metadata": {}, 101 | "source": [ 102 | "## Limits" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 11, 108 | "id": "a2f3df46", 109 | "metadata": {}, 110 | "outputs": [], 111 | "source": [ 112 | "def f(x):\n", 113 | " if x > 2:\n", 114 | " return x**2\n", 115 | " else:\n", 116 | " return x" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 20, 122 | "id": "387f00a2", 123 | "metadata": {}, 124 | "outputs": [ 125 | { 126 | "name": "stdout", 127 | "output_type": "stream", 128 | "text": [ 129 | "[1.5, 1.9, 1.99, 1.9999]\n" 130 | ] 131 | } 132 | ], 133 | "source": [ 134 | "a = 2\n", 135 | "left_hand_inputs = [1.5, 1.9, 1.99, 1.9999]\n", 136 | "right_hand_inputs = [2.5, 2.1, 2.01, 2.0001]\n", 137 | "\n", 138 | "print(left_hand_inputs) # as close to 2 as possible from left side" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 18, 144 | "id": "7ed66499", 145 | "metadata": {}, 146 | "outputs": [ 147 | { 148 | "name": "stdout", 149 | "output_type": "stream", 150 | "text": [ 151 | "[2.5, 2.1, 2.01, 2.0001]\n" 152 | ] 153 | } 154 | ], 155 | "source": [ 156 | "print(right_hand_inputs) # as close to 2 as possible from right side" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 19, 162 | "id": "b779bf26", 163 | "metadata": {}, 164 | "outputs": [ 165 | { 166 | "name": "stdout", 167 | "output_type": "stream", 168 | "text": [ 169 | "Left Hand outputs: [1.5, 1.9, 1.99, 1.9999]\n", 170 | "Right hand outputs: [6.25, 4.41, 4.040099999999999, 4.000400010000001]\n" 171 | ] 172 | } 173 | ], 174 | "source": [ 175 | "left_hand_outputs = [f(x) for x in left_hand_inputs]\n", 176 | "right_hand_outputs = [f(x) for x in right_hand_inputs]\n", 177 | "\n", 178 | "print(\"Left Hand outputs: \", left_hand_outputs)\n", 179 | "print(\"Right hand outputs: \", right_hand_outputs)" 180 | ] 181 | }, 182 | { 183 | "cell_type": "markdown", 184 | "id": "fc3aa479", 185 | "metadata": {}, 186 | "source": [ 187 | "## Limits again" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": 31, 193 | "id": "a96dc3c9", 194 | "metadata": {}, 195 | "outputs": [], 196 | "source": [ 197 | "def f(x):\n", 198 | " if x > 2:\n", 199 | " return x**2\n", 200 | " else:\n", 201 | " return x" 202 | ] 203 | }, 204 | { 205 | "cell_type": "code", 206 | "execution_count": 33, 207 | "id": "88aac453", 208 | "metadata": {}, 209 | "outputs": [ 210 | { 211 | "name": "stdout", 212 | "output_type": "stream", 213 | "text": [ 214 | "[0.5, 0.9, 0.999, 0.9999]\n", 215 | "[1.5, 1.1, 1.001, 1.0001]\n" 216 | ] 217 | } 218 | ], 219 | "source": [ 220 | "a = 1\n", 221 | "left_hand_inputs = [a - i for i in [0.5, 0.1, 0.001, 0.0001]]\n", 222 | "right_hand_inputs = [a + i for i in [0.5, 0.1, 0.001, 0.0001]]\n", 223 | "\n", 224 | "print(left_hand_inputs) # as close to 2 as possible from left side\n", 225 | "print(right_hand_inputs)" 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "execution_count": 34, 231 | "id": "89cd38bd", 232 | "metadata": {}, 233 | "outputs": [ 234 | { 235 | "name": "stdout", 236 | "output_type": "stream", 237 | "text": [ 238 | "Left Hand outputs: [0.5, 0.9, 0.999, 0.9999]\n", 239 | "Right hand outputs: [1.5, 1.1, 1.001, 1.0001]\n" 240 | ] 241 | } 242 | ], 243 | "source": [ 244 | "left_hand_outputs = [f(x) for x in left_hand_inputs]\n", 245 | "right_hand_outputs = [f(x) for x in right_hand_inputs]\n", 246 | "\n", 247 | "print(\"Left Hand outputs: \", left_hand_outputs)\n", 248 | "print(\"Right hand outputs: \", right_hand_outputs)" 249 | ] 250 | }, 251 | { 252 | "cell_type": "markdown", 253 | "id": "98cfeeab", 254 | "metadata": {}, 255 | "source": [ 256 | "## Doubts" 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "execution_count": 57, 262 | "id": "9cecc564", 263 | "metadata": {}, 264 | "outputs": [], 265 | "source": [ 266 | "class ThirdPartyAPI:\n", 267 | " def send_message(self, phone_num):\n", 268 | " if (phone_num[-1] == \"0\"):\n", 269 | " return False\n", 270 | " return True\n", 271 | " " 272 | ] 273 | }, 274 | { 275 | "cell_type": "code", 276 | "execution_count": 58, 277 | "id": "31776be2", 278 | "metadata": {}, 279 | "outputs": [], 280 | "source": [ 281 | "thirdPartyAPI = ThirdPartyAPI()\n", 282 | "\n", 283 | "def send_sms(phone_num):\n", 284 | " res = thirdPartyAPI.send_message(phone_num)\n", 285 | " if res == False:\n", 286 | " raise Exception(\"Message was not sent\")\n", 287 | " return res" 288 | ] 289 | }, 290 | { 291 | "cell_type": "code", 292 | "execution_count": 59, 293 | "id": "6e40af37", 294 | "metadata": {}, 295 | "outputs": [ 296 | { 297 | "data": { 298 | "text/plain": [ 299 | "True" 300 | ] 301 | }, 302 | "execution_count": 59, 303 | "metadata": {}, 304 | "output_type": "execute_result" 305 | } 306 | ], 307 | "source": [ 308 | "send_sms(\"9034138061\")" 309 | ] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "execution_count": 60, 314 | "id": "36144f0c", 315 | "metadata": {}, 316 | "outputs": [ 317 | { 318 | "ename": "Exception", 319 | "evalue": "Message was not sent", 320 | "output_type": "error", 321 | "traceback": [ 322 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 323 | "\u001b[0;31mException\u001b[0m Traceback (most recent call last)", 324 | "Input \u001b[0;32mIn [60]\u001b[0m, in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43msend_sms\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m9034138060\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n", 325 | "Input \u001b[0;32mIn [58]\u001b[0m, in \u001b[0;36msend_sms\u001b[0;34m(phone_num)\u001b[0m\n\u001b[1;32m 4\u001b[0m res \u001b[38;5;241m=\u001b[39m thirdPartyAPI\u001b[38;5;241m.\u001b[39msend_message(phone_num)\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m res \u001b[38;5;241m==\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m:\n\u001b[0;32m----> 6\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mMessage was not sent\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 7\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m res\n", 326 | "\u001b[0;31mException\u001b[0m: Message was not sent" 327 | ] 328 | } 329 | ], 330 | "source": [ 331 | "send_sms(\"9034138060\")" 332 | ] 333 | }, 334 | { 335 | "cell_type": "code", 336 | "execution_count": null, 337 | "id": "8fabd787", 338 | "metadata": {}, 339 | "outputs": [], 340 | "source": [] 341 | }, 342 | { 343 | "cell_type": "markdown", 344 | "id": "5e8d303c", 345 | "metadata": {}, 346 | "source": [] 347 | }, 348 | { 349 | "cell_type": "code", 350 | "execution_count": 69, 351 | "id": "aa9be3f0", 352 | "metadata": {}, 353 | "outputs": [], 354 | "source": [ 355 | "class ThirdPartyAPI:\n", 356 | " def send_message(self, phone_num):\n", 357 | " if (phone_num[-1] == \"0\"):\n", 358 | " raise Exception(\"Bug in 3rd party code\")\n", 359 | " return True" 360 | ] 361 | }, 362 | { 363 | "cell_type": "code", 364 | "execution_count": 79, 365 | "id": "3a1d7554", 366 | "metadata": {}, 367 | "outputs": [], 368 | "source": [ 369 | "thirdPartyAPI = ThirdPartyAPI()\n", 370 | "\n", 371 | "def send_sms(phone_num):\n", 372 | " try:\n", 373 | " res = thirdPartyAPI.send_message(phone_num)\n", 374 | " return 20\n", 375 | " except Exception as e:\n", 376 | " print(e)\n", 377 | " finally:\n", 378 | " print(\"All done\")" 379 | ] 380 | }, 381 | { 382 | "cell_type": "code", 383 | "execution_count": 80, 384 | "id": "957e8277", 385 | "metadata": {}, 386 | "outputs": [ 387 | { 388 | "name": "stdout", 389 | "output_type": "stream", 390 | "text": [ 391 | "All done\n" 392 | ] 393 | }, 394 | { 395 | "data": { 396 | "text/plain": [ 397 | "20" 398 | ] 399 | }, 400 | "execution_count": 80, 401 | "metadata": {}, 402 | "output_type": "execute_result" 403 | } 404 | ], 405 | "source": [ 406 | "send_sms(\"9034138061\")" 407 | ] 408 | }, 409 | { 410 | "cell_type": "code", 411 | "execution_count": 81, 412 | "id": "b5ba0ccb", 413 | "metadata": {}, 414 | "outputs": [ 415 | { 416 | "name": "stdout", 417 | "output_type": "stream", 418 | "text": [ 419 | "Bug in 3rd party code\n", 420 | "All done\n" 421 | ] 422 | } 423 | ], 424 | "source": [ 425 | "send_sms(\"9034138060\")" 426 | ] 427 | }, 428 | { 429 | "cell_type": "code", 430 | "execution_count": 89, 431 | "id": "cc7406cb", 432 | "metadata": {}, 433 | "outputs": [], 434 | "source": [ 435 | "\n", 436 | "def f():\n", 437 | " try:\n", 438 | " x = 0\n", 439 | " y = 1\n", 440 | " return x + y\n", 441 | " except:\n", 442 | " print('exception')\n", 443 | " finally:\n", 444 | " x += 2\n", 445 | " print(x)\n", 446 | " y += 3\n", 447 | " print(y)" 448 | ] 449 | }, 450 | { 451 | "cell_type": "code", 452 | "execution_count": 90, 453 | "id": "0f05fe06", 454 | "metadata": {}, 455 | "outputs": [ 456 | { 457 | "name": "stdout", 458 | "output_type": "stream", 459 | "text": [ 460 | "2\n", 461 | "4\n" 462 | ] 463 | }, 464 | { 465 | "data": { 466 | "text/plain": [ 467 | "1" 468 | ] 469 | }, 470 | "execution_count": 90, 471 | "metadata": {}, 472 | "output_type": "execute_result" 473 | } 474 | ], 475 | "source": [ 476 | "f()" 477 | ] 478 | }, 479 | { 480 | "cell_type": "code", 481 | "execution_count": null, 482 | "id": "30a27c4c", 483 | "metadata": {}, 484 | "outputs": [], 485 | "source": [] 486 | } 487 | ], 488 | "metadata": { 489 | "kernelspec": { 490 | "display_name": "Python 3 (ipykernel)", 491 | "language": "python", 492 | "name": "python3" 493 | }, 494 | "language_info": { 495 | "codemirror_mode": { 496 | "name": "ipython", 497 | "version": 3 498 | }, 499 | "file_extension": ".py", 500 | "mimetype": "text/x-python", 501 | "name": "python", 502 | "nbconvert_exporter": "python", 503 | "pygments_lexer": "ipython3", 504 | "version": "3.9.12" 505 | } 506 | }, 507 | "nbformat": 4, 508 | "nbformat_minor": 5 509 | } 510 | -------------------------------------------------------------------------------- /File_Handling/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/scaleracademy/dsml-jun-22-beginner-intermediate/2b2b21ab72368da52a4113f18a8e3f8544cf34d2/File_Handling/.DS_Store -------------------------------------------------------------------------------- /File_Handling/file_handling.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "80c54348", 6 | "metadata": {}, 7 | "source": [ 8 | "## Working with files" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 1, 14 | "id": "4f10047d", 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "open?" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 2, 24 | "id": "447e7ac4", 25 | "metadata": {}, 26 | "outputs": [], 27 | "source": [ 28 | "f = open(\"untitled.txt\", \"r\") # in the current folder" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 19, 34 | "id": "40eba3d1", 35 | "metadata": {}, 36 | "outputs": [ 37 | { 38 | "ename": "FileNotFoundError", 39 | "evalue": "[Errno 2] No such file or directory: 'sample2.txt'", 40 | "output_type": "error", 41 | "traceback": [ 42 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 43 | "\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", 44 | "Input \u001b[0;32mIn [19]\u001b[0m, in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m f \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mopen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43msample2.txt\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mr\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n", 45 | "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'sample2.txt'" 46 | ] 47 | } 48 | ], 49 | "source": [ 50 | "f = open(\"sample2.txt\", \"r\") # gives an error if file is not present" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 3, 56 | "id": "08882425", 57 | "metadata": {}, 58 | "outputs": [ 59 | { 60 | "data": { 61 | "text/plain": [ 62 | "_io.TextIOWrapper" 63 | ] 64 | }, 65 | "execution_count": 3, 66 | "metadata": {}, 67 | "output_type": "execute_result" 68 | } 69 | ], 70 | "source": [ 71 | "type(f)" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 5, 77 | "id": "c5d5018f", 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [ 81 | "open?" 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "id": "fa126889", 87 | "metadata": {}, 88 | "source": [ 89 | "### Opening a file" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 10, 95 | "id": "c2a4a069", 96 | "metadata": {}, 97 | "outputs": [], 98 | "source": [ 99 | "f = open(\"untitled.txt\", \"r\") # r is the read mode" 100 | ] 101 | }, 102 | { 103 | "cell_type": "markdown", 104 | "id": "e4c967ea", 105 | "metadata": {}, 106 | "source": [ 107 | "### Reading from a file" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": 7, 113 | "id": "bf303200", 114 | "metadata": {}, 115 | "outputs": [ 116 | { 117 | "data": { 118 | "text/plain": [ 119 | "'Hello this is our first file\\n\\nthat we are going to open\\n\\nThis is some magic:\\n1\\n2\\n3\\n4\\n'" 120 | ] 121 | }, 122 | "execution_count": 7, 123 | "metadata": {}, 124 | "output_type": "execute_result" 125 | } 126 | ], 127 | "source": [ 128 | "f.read()" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 8, 134 | "id": "f98a6857", 135 | "metadata": {}, 136 | "outputs": [ 137 | { 138 | "data": { 139 | "text/plain": [ 140 | "''" 141 | ] 142 | }, 143 | "execution_count": 8, 144 | "metadata": {}, 145 | "output_type": "execute_result" 146 | } 147 | ], 148 | "source": [ 149 | "f.read()" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": 21, 155 | "id": "59d1af60", 156 | "metadata": {}, 157 | "outputs": [], 158 | "source": [ 159 | "# f.close() # close once done reading / writing" 160 | ] 161 | }, 162 | { 163 | "cell_type": "markdown", 164 | "id": "8624ade1", 165 | "metadata": {}, 166 | "source": [ 167 | "### Writing to a file" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": 9, 173 | "id": "0d4708db", 174 | "metadata": {}, 175 | "outputs": [ 176 | { 177 | "ename": "UnsupportedOperation", 178 | "evalue": "not writable", 179 | "output_type": "error", 180 | "traceback": [ 181 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 182 | "\u001b[0;31mUnsupportedOperation\u001b[0m Traceback (most recent call last)", 183 | "Input \u001b[0;32mIn [9]\u001b[0m, in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mf\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mwrite\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mI am writing to this file!\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n", 184 | "\u001b[0;31mUnsupportedOperation\u001b[0m: not writable" 185 | ] 186 | } 187 | ], 188 | "source": [ 189 | "f.write('I am writing to this file!')" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": 11, 195 | "id": "bc1de677", 196 | "metadata": {}, 197 | "outputs": [], 198 | "source": [ 199 | "f2 = open('sample.txt', 'w') # only the write mode" 200 | ] 201 | }, 202 | { 203 | "cell_type": "code", 204 | "execution_count": 22, 205 | "id": "9eb11873", 206 | "metadata": {}, 207 | "outputs": [], 208 | "source": [ 209 | "f3 = open(\"sample2.txt\", \"w\") # it will automatically create a new file" 210 | ] 211 | }, 212 | { 213 | "cell_type": "code", 214 | "execution_count": 12, 215 | "id": "b921cc60", 216 | "metadata": {}, 217 | "outputs": [ 218 | { 219 | "data": { 220 | "text/plain": [ 221 | "_io.TextIOWrapper" 222 | ] 223 | }, 224 | "execution_count": 12, 225 | "metadata": {}, 226 | "output_type": "execute_result" 227 | } 228 | ], 229 | "source": [ 230 | "type(f2)" 231 | ] 232 | }, 233 | { 234 | "cell_type": "code", 235 | "execution_count": 13, 236 | "id": "37bddc69", 237 | "metadata": {}, 238 | "outputs": [ 239 | { 240 | "data": { 241 | "text/plain": [ 242 | "51" 243 | ] 244 | }, 245 | "execution_count": 13, 246 | "metadata": {}, 247 | "output_type": "execute_result" 248 | } 249 | ], 250 | "source": [ 251 | "f2.write('Hello everyone talking from python program to you!!')\n", 252 | "# it returns the no of bytes written to the file" 253 | ] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": 23, 258 | "id": "ce63a9af", 259 | "metadata": {}, 260 | "outputs": [ 261 | { 262 | "data": { 263 | "text/plain": [ 264 | "6" 265 | ] 266 | }, 267 | "execution_count": 23, 268 | "metadata": {}, 269 | "output_type": "execute_result" 270 | } 271 | ], 272 | "source": [ 273 | "f3.write('1\\n2\\n3\\n')" 274 | ] 275 | }, 276 | { 277 | "cell_type": "code", 278 | "execution_count": 24, 279 | "id": "1f29064d", 280 | "metadata": {}, 281 | "outputs": [], 282 | "source": [ 283 | "f3.close()" 284 | ] 285 | }, 286 | { 287 | "cell_type": "code", 288 | "execution_count": 14, 289 | "id": "dcc0d936", 290 | "metadata": {}, 291 | "outputs": [ 292 | { 293 | "ename": "UnsupportedOperation", 294 | "evalue": "not readable", 295 | "output_type": "error", 296 | "traceback": [ 297 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 298 | "\u001b[0;31mUnsupportedOperation\u001b[0m Traceback (most recent call last)", 299 | "Input \u001b[0;32mIn [14]\u001b[0m, in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mf2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mread\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n", 300 | "\u001b[0;31mUnsupportedOperation\u001b[0m: not readable" 301 | ] 302 | } 303 | ], 304 | "source": [ 305 | "f2.read()" 306 | ] 307 | }, 308 | { 309 | "cell_type": "code", 310 | "execution_count": 15, 311 | "id": "0a4583f0", 312 | "metadata": {}, 313 | "outputs": [], 314 | "source": [ 315 | "f2.close() # why is closing important?" 316 | ] 317 | }, 318 | { 319 | "cell_type": "code", 320 | "execution_count": 16, 321 | "id": "7b4be5e8", 322 | "metadata": {}, 323 | "outputs": [], 324 | "source": [ 325 | "f2 = open('sample.txt', 'r')" 326 | ] 327 | }, 328 | { 329 | "cell_type": "code", 330 | "execution_count": 17, 331 | "id": "fd4b99fc", 332 | "metadata": {}, 333 | "outputs": [ 334 | { 335 | "data": { 336 | "text/plain": [ 337 | "'Hello everyone talking from python program to you!!'" 338 | ] 339 | }, 340 | "execution_count": 17, 341 | "metadata": {}, 342 | "output_type": "execute_result" 343 | } 344 | ], 345 | "source": [ 346 | "f2.read()" 347 | ] 348 | }, 349 | { 350 | "cell_type": "code", 351 | "execution_count": null, 352 | "id": "a1cbc779", 353 | "metadata": {}, 354 | "outputs": [], 355 | "source": [] 356 | }, 357 | { 358 | "cell_type": "markdown", 359 | "id": "f96d3e6f", 360 | "metadata": {}, 361 | "source": [ 362 | "## Some other important modes: file access modes" 363 | ] 364 | }, 365 | { 366 | "cell_type": "code", 367 | "execution_count": 25, 368 | "id": "02d57f0e", 369 | "metadata": {}, 370 | "outputs": [], 371 | "source": [ 372 | "open?" 373 | ] 374 | }, 375 | { 376 | "cell_type": "code", 377 | "execution_count": 26, 378 | "id": "b3e51b6c", 379 | "metadata": {}, 380 | "outputs": [], 381 | "source": [ 382 | "f = open('sample2.txt', 'r')" 383 | ] 384 | }, 385 | { 386 | "cell_type": "code", 387 | "execution_count": 27, 388 | "id": "e7aa9c18", 389 | "metadata": {}, 390 | "outputs": [ 391 | { 392 | "data": { 393 | "text/plain": [ 394 | "'1\\n2\\n3\\n'" 395 | ] 396 | }, 397 | "execution_count": 27, 398 | "metadata": {}, 399 | "output_type": "execute_result" 400 | } 401 | ], 402 | "source": [ 403 | "f.read()" 404 | ] 405 | }, 406 | { 407 | "cell_type": "code", 408 | "execution_count": 28, 409 | "id": "a97f55c4", 410 | "metadata": {}, 411 | "outputs": [], 412 | "source": [ 413 | "f.close()" 414 | ] 415 | }, 416 | { 417 | "cell_type": "code", 418 | "execution_count": 29, 419 | "id": "5047011d", 420 | "metadata": {}, 421 | "outputs": [ 422 | { 423 | "data": { 424 | "text/plain": [ 425 | "3" 426 | ] 427 | }, 428 | "execution_count": 29, 429 | "metadata": {}, 430 | "output_type": "execute_result" 431 | } 432 | ], 433 | "source": [ 434 | "f = open('sample2.txt', 'w')\n", 435 | "f.write('abc')" 436 | ] 437 | }, 438 | { 439 | "cell_type": "code", 440 | "execution_count": 30, 441 | "id": "b8402199", 442 | "metadata": {}, 443 | "outputs": [], 444 | "source": [ 445 | "f.close()" 446 | ] 447 | }, 448 | { 449 | "cell_type": "code", 450 | "execution_count": 34, 451 | "id": "6235762a", 452 | "metadata": {}, 453 | "outputs": [ 454 | { 455 | "data": { 456 | "text/plain": [ 457 | "'abc'" 458 | ] 459 | }, 460 | "execution_count": 34, 461 | "metadata": {}, 462 | "output_type": "execute_result" 463 | } 464 | ], 465 | "source": [ 466 | "f = open('sample2.txt', 'r')\n", 467 | "f.read()\n", 468 | "\n", 469 | "# A. 1\\n2\\n3\\nabc\n", 470 | "# B. abc -------- previous content was deleted\n", 471 | "# C. 1\\n2\\n3\\n" 472 | ] 473 | }, 474 | { 475 | "cell_type": "code", 476 | "execution_count": 35, 477 | "id": "b57a64ee", 478 | "metadata": {}, 479 | "outputs": [], 480 | "source": [ 481 | "open?" 482 | ] 483 | }, 484 | { 485 | "cell_type": "code", 486 | "execution_count": 36, 487 | "id": "70775fc8", 488 | "metadata": {}, 489 | "outputs": [], 490 | "source": [ 491 | "f = open('sample2.txt', 'a')" 492 | ] 493 | }, 494 | { 495 | "cell_type": "code", 496 | "execution_count": 38, 497 | "id": "7fecc77e", 498 | "metadata": {}, 499 | "outputs": [ 500 | { 501 | "data": { 502 | "text/plain": [ 503 | "6" 504 | ] 505 | }, 506 | "execution_count": 38, 507 | "metadata": {}, 508 | "output_type": "execute_result" 509 | } 510 | ], 511 | "source": [ 512 | "f.write('1\\n2\\n3\\n')" 513 | ] 514 | }, 515 | { 516 | "cell_type": "code", 517 | "execution_count": 39, 518 | "id": "3297972b", 519 | "metadata": {}, 520 | "outputs": [], 521 | "source": [ 522 | "f.close()" 523 | ] 524 | }, 525 | { 526 | "cell_type": "code", 527 | "execution_count": 41, 528 | "id": "0b399de7", 529 | "metadata": {}, 530 | "outputs": [ 531 | { 532 | "data": { 533 | "text/plain": [ 534 | "'abc1\\n2\\n3\\n'" 535 | ] 536 | }, 537 | "execution_count": 41, 538 | "metadata": {}, 539 | "output_type": "execute_result" 540 | } 541 | ], 542 | "source": [ 543 | "f = open('sample2.txt', 'r')\n", 544 | "f.read()\n", 545 | "\n", 546 | "# A. 1\\n2\\n3\\nabc\n", 547 | "# B. abc\n", 548 | "# C. 1\\n2\\n3\\n\n", 549 | "# D. abc1\\n2\\n3\\n ---- it appends to the end" 550 | ] 551 | }, 552 | { 553 | "cell_type": "code", 554 | "execution_count": 42, 555 | "id": "0b1c23f1", 556 | "metadata": {}, 557 | "outputs": [], 558 | "source": [ 559 | "# read (r)\n", 560 | "# write (w)\n", 561 | "# append (a)" 562 | ] 563 | }, 564 | { 565 | "cell_type": "markdown", 566 | "id": "9c3d1c67", 567 | "metadata": {}, 568 | "source": [ 569 | "### read+ or write+ modes\n", 570 | "\n", 571 | "You have to experiment so that you can become a master of them!!" 572 | ] 573 | }, 574 | { 575 | "cell_type": "code", 576 | "execution_count": 53, 577 | "id": "2b15fdce", 578 | "metadata": {}, 579 | "outputs": [], 580 | "source": [ 581 | "open?" 582 | ] 583 | }, 584 | { 585 | "cell_type": "code", 586 | "execution_count": 54, 587 | "id": "fd50c9e3", 588 | "metadata": {}, 589 | "outputs": [], 590 | "source": [ 591 | "f = open('sample2.txt', 'r+') # read + mode => read and write" 592 | ] 593 | }, 594 | { 595 | "cell_type": "code", 596 | "execution_count": 55, 597 | "id": "b85f4040", 598 | "metadata": {}, 599 | "outputs": [ 600 | { 601 | "data": { 602 | "text/plain": [ 603 | "'abc1\\n2\\n3\\nthis is new contentthis is new content'" 604 | ] 605 | }, 606 | "execution_count": 55, 607 | "metadata": {}, 608 | "output_type": "execute_result" 609 | } 610 | ], 611 | "source": [ 612 | "f.read() # the cursor came to end" 613 | ] 614 | }, 615 | { 616 | "cell_type": "code", 617 | "execution_count": 56, 618 | "id": "15ff8c02", 619 | "metadata": {}, 620 | "outputs": [ 621 | { 622 | "data": { 623 | "text/plain": [ 624 | "39" 625 | ] 626 | }, 627 | "execution_count": 56, 628 | "metadata": {}, 629 | "output_type": "execute_result" 630 | } 631 | ], 632 | "source": [ 633 | "f.write(' this is going to be added at the end') # when is a write completed?" 634 | ] 635 | }, 636 | { 637 | "cell_type": "code", 638 | "execution_count": 57, 639 | "id": "cb4286eb", 640 | "metadata": {}, 641 | "outputs": [ 642 | { 643 | "data": { 644 | "text/plain": [ 645 | "''" 646 | ] 647 | }, 648 | "execution_count": 57, 649 | "metadata": {}, 650 | "output_type": "execute_result" 651 | } 652 | ], 653 | "source": [ 654 | "f.read()" 655 | ] 656 | }, 657 | { 658 | "cell_type": "code", 659 | "execution_count": 58, 660 | "id": "49910f6b", 661 | "metadata": {}, 662 | "outputs": [], 663 | "source": [ 664 | "f.close()" 665 | ] 666 | }, 667 | { 668 | "cell_type": "code", 669 | "execution_count": 59, 670 | "id": "986e3d9c", 671 | "metadata": {}, 672 | "outputs": [], 673 | "source": [ 674 | "f = open('sample2.txt', 'r+') # read + mode => read and write" 675 | ] 676 | }, 677 | { 678 | "cell_type": "code", 679 | "execution_count": 60, 680 | "id": "616fe39b", 681 | "metadata": {}, 682 | "outputs": [ 683 | { 684 | "data": { 685 | "text/plain": [ 686 | "'abc1\\n2\\n3\\nthis is new contentthis is new content this is going to be added at the end'" 687 | ] 688 | }, 689 | "execution_count": 60, 690 | "metadata": {}, 691 | "output_type": "execute_result" 692 | } 693 | ], 694 | "source": [ 695 | "f.read()" 696 | ] 697 | }, 698 | { 699 | "cell_type": "code", 700 | "execution_count": 61, 701 | "id": "01787981", 702 | "metadata": {}, 703 | "outputs": [], 704 | "source": [ 705 | "f.close()" 706 | ] 707 | }, 708 | { 709 | "cell_type": "markdown", 710 | "id": "5c2e0533", 711 | "metadata": {}, 712 | "source": [ 713 | "'abc1\\n2\\n3\\nthis is new contentthis is new content this is going to be added at the end'" 714 | ] 715 | }, 716 | { 717 | "cell_type": "markdown", 718 | "id": "9eb4380e", 719 | "metadata": {}, 720 | "source": [ 721 | "### Example 2" 722 | ] 723 | }, 724 | { 725 | "cell_type": "code", 726 | "execution_count": 63, 727 | "id": "31d8486f", 728 | "metadata": {}, 729 | "outputs": [], 730 | "source": [ 731 | "f = open('sample2.txt', 'r+') # cursor is the beginning" 732 | ] 733 | }, 734 | { 735 | "cell_type": "code", 736 | "execution_count": 64, 737 | "id": "6a02cb01", 738 | "metadata": {}, 739 | "outputs": [ 740 | { 741 | "data": { 742 | "text/plain": [ 743 | "22" 744 | ] 745 | }, 746 | "execution_count": 64, 747 | "metadata": {}, 748 | "output_type": "execute_result" 749 | } 750 | ], 751 | "source": [ 752 | "f.write('this will get replaced')" 753 | ] 754 | }, 755 | { 756 | "cell_type": "code", 757 | "execution_count": 65, 758 | "id": "0b21f8e0", 759 | "metadata": {}, 760 | "outputs": [], 761 | "source": [ 762 | "f.close()" 763 | ] 764 | }, 765 | { 766 | "cell_type": "code", 767 | "execution_count": 66, 768 | "id": "c335f583", 769 | "metadata": {}, 770 | "outputs": [], 771 | "source": [ 772 | "f = open('sample2.txt', 'r+') # cursor is the beginning" 773 | ] 774 | }, 775 | { 776 | "cell_type": "code", 777 | "execution_count": 67, 778 | "id": "eecc1607", 779 | "metadata": {}, 780 | "outputs": [ 781 | { 782 | "data": { 783 | "text/plain": [ 784 | "'this will get replacedontentthis is new content this is going to be added at the end'" 785 | ] 786 | }, 787 | "execution_count": 67, 788 | "metadata": {}, 789 | "output_type": "execute_result" 790 | } 791 | ], 792 | "source": [ 793 | "f.read()" 794 | ] 795 | }, 796 | { 797 | "cell_type": "code", 798 | "execution_count": null, 799 | "id": "46fe6cac", 800 | "metadata": {}, 801 | "outputs": [], 802 | "source": [] 803 | }, 804 | { 805 | "cell_type": "markdown", 806 | "id": "c4007680", 807 | "metadata": {}, 808 | "source": [ 809 | "## Example 3" 810 | ] 811 | }, 812 | { 813 | "cell_type": "code", 814 | "execution_count": 68, 815 | "id": "c02a3e84", 816 | "metadata": {}, 817 | "outputs": [ 818 | { 819 | "ename": "FileNotFoundError", 820 | "evalue": "[Errno 2] No such file or directory: 'sample3.txt'", 821 | "output_type": "error", 822 | "traceback": [ 823 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 824 | "\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", 825 | "Input \u001b[0;32mIn [68]\u001b[0m, in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m f \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mopen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43msample3.txt\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mr+\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n", 826 | "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'sample3.txt'" 827 | ] 828 | } 829 | ], 830 | "source": [ 831 | "f = open('sample3.txt', 'r+') # does not create a new file" 832 | ] 833 | }, 834 | { 835 | "cell_type": "code", 836 | "execution_count": 113, 837 | "id": "b74dc277", 838 | "metadata": {}, 839 | "outputs": [], 840 | "source": [ 841 | "f = open('sample3.txt', 'w+') # it will create a new file, in the truncate mode" 842 | ] 843 | }, 844 | { 845 | "cell_type": "code", 846 | "execution_count": 114, 847 | "id": "e3848e9a", 848 | "metadata": {}, 849 | "outputs": [ 850 | { 851 | "data": { 852 | "text/plain": [ 853 | "4" 854 | ] 855 | }, 856 | "execution_count": 114, 857 | "metadata": {}, 858 | "output_type": "execute_result" 859 | } 860 | ], 861 | "source": [ 862 | "f.write('1234')" 863 | ] 864 | }, 865 | { 866 | "cell_type": "code", 867 | "execution_count": 115, 868 | "id": "4467f617", 869 | "metadata": {}, 870 | "outputs": [], 871 | "source": [ 872 | "f.close()" 873 | ] 874 | }, 875 | { 876 | "cell_type": "code", 877 | "execution_count": 116, 878 | "id": "08f273b6", 879 | "metadata": {}, 880 | "outputs": [], 881 | "source": [ 882 | "f = open('sample3.txt', 'r+')" 883 | ] 884 | }, 885 | { 886 | "cell_type": "code", 887 | "execution_count": 117, 888 | "id": "f586c452", 889 | "metadata": {}, 890 | "outputs": [ 891 | { 892 | "data": { 893 | "text/plain": [ 894 | "'1234'" 895 | ] 896 | }, 897 | "execution_count": 117, 898 | "metadata": {}, 899 | "output_type": "execute_result" 900 | } 901 | ], 902 | "source": [ 903 | "f.read() # cursor moved to the end" 904 | ] 905 | }, 906 | { 907 | "cell_type": "code", 908 | "execution_count": 118, 909 | "id": "1b60b596", 910 | "metadata": {}, 911 | "outputs": [], 912 | "source": [ 913 | "f.close()" 914 | ] 915 | }, 916 | { 917 | "cell_type": "code", 918 | "execution_count": 119, 919 | "id": "1d06ddaa", 920 | "metadata": {}, 921 | "outputs": [ 922 | { 923 | "data": { 924 | "text/plain": [ 925 | "1" 926 | ] 927 | }, 928 | "execution_count": 119, 929 | "metadata": {}, 930 | "output_type": "execute_result" 931 | } 932 | ], 933 | "source": [ 934 | "f = open('sample3.txt', 'r+')\n", 935 | "f.write('5')" 936 | ] 937 | }, 938 | { 939 | "cell_type": "code", 940 | "execution_count": 120, 941 | "id": "44bd59b1", 942 | "metadata": {}, 943 | "outputs": [], 944 | "source": [ 945 | "f.close()" 946 | ] 947 | }, 948 | { 949 | "cell_type": "code", 950 | "execution_count": 121, 951 | "id": "97ef81b9", 952 | "metadata": {}, 953 | "outputs": [ 954 | { 955 | "data": { 956 | "text/plain": [ 957 | "'5234'" 958 | ] 959 | }, 960 | "execution_count": 121, 961 | "metadata": {}, 962 | "output_type": "execute_result" 963 | } 964 | ], 965 | "source": [ 966 | "f = open('sample3.txt', 'r+')\n", 967 | "f.read() # 5234" 968 | ] 969 | }, 970 | { 971 | "cell_type": "code", 972 | "execution_count": 122, 973 | "id": "8c5481ac", 974 | "metadata": {}, 975 | "outputs": [], 976 | "source": [ 977 | "f.close()" 978 | ] 979 | }, 980 | { 981 | "cell_type": "markdown", 982 | "id": "06c6c73d", 983 | "metadata": {}, 984 | "source": [ 985 | "### read specific no of bytes" 986 | ] 987 | }, 988 | { 989 | "cell_type": "code", 990 | "execution_count": 126, 991 | "id": "36588787", 992 | "metadata": {}, 993 | "outputs": [ 994 | { 995 | "data": { 996 | "text/plain": [ 997 | "'52'" 998 | ] 999 | }, 1000 | "execution_count": 126, 1001 | "metadata": {}, 1002 | "output_type": "execute_result" 1003 | } 1004 | ], 1005 | "source": [ 1006 | "# 5234\n", 1007 | "\n", 1008 | "f = open('sample3.txt', 'r+')\n", 1009 | "f.read(2) # it will read only 2 bytes of memory" 1010 | ] 1011 | }, 1012 | { 1013 | "cell_type": "code", 1014 | "execution_count": 127, 1015 | "id": "f85cb9cd", 1016 | "metadata": {}, 1017 | "outputs": [ 1018 | { 1019 | "data": { 1020 | "text/plain": [ 1021 | "'34'" 1022 | ] 1023 | }, 1024 | "execution_count": 127, 1025 | "metadata": {}, 1026 | "output_type": "execute_result" 1027 | } 1028 | ], 1029 | "source": [ 1030 | "f.read() # what do you expect --- 34?" 1031 | ] 1032 | }, 1033 | { 1034 | "cell_type": "code", 1035 | "execution_count": 128, 1036 | "id": "924dc3e0", 1037 | "metadata": {}, 1038 | "outputs": [], 1039 | "source": [ 1040 | "f.close()" 1041 | ] 1042 | }, 1043 | { 1044 | "cell_type": "markdown", 1045 | "id": "47fd8291", 1046 | "metadata": {}, 1047 | "source": [ 1048 | "## Replace 34 with 197" 1049 | ] 1050 | }, 1051 | { 1052 | "cell_type": "code", 1053 | "execution_count": 129, 1054 | "id": "9fa540b5", 1055 | "metadata": {}, 1056 | "outputs": [ 1057 | { 1058 | "data": { 1059 | "text/plain": [ 1060 | "'52'" 1061 | ] 1062 | }, 1063 | "execution_count": 129, 1064 | "metadata": {}, 1065 | "output_type": "execute_result" 1066 | } 1067 | ], 1068 | "source": [ 1069 | "f = open('sample3.txt', 'r+')\n", 1070 | "f.read(2) # it will read only 2 bytes of memory" 1071 | ] 1072 | }, 1073 | { 1074 | "cell_type": "code", 1075 | "execution_count": 130, 1076 | "id": "e44e293d", 1077 | "metadata": {}, 1078 | "outputs": [ 1079 | { 1080 | "data": { 1081 | "text/plain": [ 1082 | "3" 1083 | ] 1084 | }, 1085 | "execution_count": 130, 1086 | "metadata": {}, 1087 | "output_type": "execute_result" 1088 | } 1089 | ], 1090 | "source": [ 1091 | "f.write('197')" 1092 | ] 1093 | }, 1094 | { 1095 | "cell_type": "code", 1096 | "execution_count": 131, 1097 | "id": "ae8b781e", 1098 | "metadata": {}, 1099 | "outputs": [], 1100 | "source": [ 1101 | "f.close()" 1102 | ] 1103 | }, 1104 | { 1105 | "cell_type": "code", 1106 | "execution_count": 136, 1107 | "id": "0d4c7c38", 1108 | "metadata": {}, 1109 | "outputs": [ 1110 | { 1111 | "data": { 1112 | "text/plain": [ 1113 | "'5234197'" 1114 | ] 1115 | }, 1116 | "execution_count": 136, 1117 | "metadata": {}, 1118 | "output_type": "execute_result" 1119 | } 1120 | ], 1121 | "source": [ 1122 | "f = open('sample3.txt', 'r+')\n", 1123 | "f.read() # why it is not replacing? Take this as a HW\n", 1124 | "# Let's take it as a HW and discuss next file." 1125 | ] 1126 | }, 1127 | { 1128 | "cell_type": "markdown", 1129 | "id": "5000a5d4", 1130 | "metadata": {}, 1131 | "source": [ 1132 | "### HW: Why adding at the end?" 1133 | ] 1134 | }, 1135 | { 1136 | "cell_type": "code", 1137 | "execution_count": 137, 1138 | "id": "2b073662", 1139 | "metadata": {}, 1140 | "outputs": [], 1141 | "source": [ 1142 | "f.close()" 1143 | ] 1144 | }, 1145 | { 1146 | "cell_type": "markdown", 1147 | "id": "1128d867", 1148 | "metadata": {}, 1149 | "source": [ 1150 | "## Smarter way of opening files" 1151 | ] 1152 | }, 1153 | { 1154 | "cell_type": "code", 1155 | "execution_count": 141, 1156 | "id": "c90cb125", 1157 | "metadata": {}, 1158 | "outputs": [ 1159 | { 1160 | "name": "stdout", 1161 | "output_type": "stream", 1162 | "text": [ 1163 | "523\n", 1164 | "419\n", 1165 | "7\n" 1166 | ] 1167 | } 1168 | ], 1169 | "source": [ 1170 | "# housekeeping of open and close to be handled by python\n", 1171 | "\n", 1172 | "# 5234197\n", 1173 | "with open('sample3.txt', 'w+') as f:\n", 1174 | " # truncate\n", 1175 | " f.write('5234197')\n", 1176 | "\n", 1177 | "\n", 1178 | "with open('sample3.txt', 'r+') as f:\n", 1179 | " # inside here you can do all the file operations\n", 1180 | " print(f.read(3))\n", 1181 | " print(f.read(3))\n", 1182 | " print(f.read(3))\n", 1183 | " f.write('Here we write!!')\n", 1184 | "\n", 1185 | "# outside the with block => file f is automatically closed" 1186 | ] 1187 | }, 1188 | { 1189 | "cell_type": "code", 1190 | "execution_count": 142, 1191 | "id": "db25bb16", 1192 | "metadata": {}, 1193 | "outputs": [ 1194 | { 1195 | "name": "stdout", 1196 | "output_type": "stream", 1197 | "text": [ 1198 | "5234197Here we write!!\n" 1199 | ] 1200 | } 1201 | ], 1202 | "source": [ 1203 | "with open('sample3.txt', 'r+') as f:\n", 1204 | " print(f.read())" 1205 | ] 1206 | }, 1207 | { 1208 | "cell_type": "code", 1209 | "execution_count": 143, 1210 | "id": "01dfe3dd", 1211 | "metadata": {}, 1212 | "outputs": [ 1213 | { 1214 | "name": "stdout", 1215 | "output_type": "stream", 1216 | "text": [ 1217 | "523\n", 1218 | "419\n", 1219 | "7He\n" 1220 | ] 1221 | } 1222 | ], 1223 | "source": [ 1224 | "with open('sample3.txt', 'r+') as f:\n", 1225 | " # inside here you can do all the file operations\n", 1226 | " print(f.read(3))\n", 1227 | " print(f.read(3))\n", 1228 | " print(f.read(3))\n", 1229 | " f.write('Here we write!!') # it automatically moves to the end" 1230 | ] 1231 | }, 1232 | { 1233 | "cell_type": "markdown", 1234 | "id": "ab86f94d", 1235 | "metadata": {}, 1236 | "source": [ 1237 | "### HW" 1238 | ] 1239 | }, 1240 | { 1241 | "cell_type": "code", 1242 | "execution_count": 144, 1243 | "id": "d619afa8", 1244 | "metadata": {}, 1245 | "outputs": [ 1246 | { 1247 | "name": "stdout", 1248 | "output_type": "stream", 1249 | "text": [ 1250 | "5234197Here we write!!Here we write!!\n" 1251 | ] 1252 | } 1253 | ], 1254 | "source": [ 1255 | "with open('sample3.txt', 'r+') as f:\n", 1256 | " print(f.read())" 1257 | ] 1258 | }, 1259 | { 1260 | "cell_type": "markdown", 1261 | "id": "f8a48c05", 1262 | "metadata": {}, 1263 | "source": [ 1264 | "# open, read, write, close, with" 1265 | ] 1266 | }, 1267 | { 1268 | "cell_type": "markdown", 1269 | "id": "1322e276", 1270 | "metadata": {}, 1271 | "source": [ 1272 | "## Build our own image copy program" 1273 | ] 1274 | }, 1275 | { 1276 | "cell_type": "code", 1277 | "execution_count": 150, 1278 | "id": "ea1f1130", 1279 | "metadata": {}, 1280 | "outputs": [], 1281 | "source": [ 1282 | "# we have to use a binary decoding\n", 1283 | "# rb => read with binary decoding\n", 1284 | "# when you do not know the encoding, directly use the binary\n", 1285 | "with open('image.jpeg', 'rb') as f:\n", 1286 | " file_content = f.read() # prints the hexadecimal format" 1287 | ] 1288 | }, 1289 | { 1290 | "cell_type": "code", 1291 | "execution_count": 147, 1292 | "id": "1d80d819", 1293 | "metadata": {}, 1294 | "outputs": [], 1295 | "source": [ 1296 | "open?" 1297 | ] 1298 | }, 1299 | { 1300 | "cell_type": "code", 1301 | "execution_count": 152, 1302 | "id": "f620b390", 1303 | "metadata": {}, 1304 | "outputs": [], 1305 | "source": [ 1306 | "# file_content --- stored in the memory" 1307 | ] 1308 | }, 1309 | { 1310 | "cell_type": "code", 1311 | "execution_count": 154, 1312 | "id": "aebb460c", 1313 | "metadata": {}, 1314 | "outputs": [], 1315 | "source": [ 1316 | "# write with binary encoding\n", 1317 | "with open('image_copy.jpeg', 'wb') as f:\n", 1318 | " f.write(file_content) # writing the binary\n", 1319 | " # closing is automatically handled as you go outside the block" 1320 | ] 1321 | }, 1322 | { 1323 | "cell_type": "markdown", 1324 | "id": "2ca1ae22", 1325 | "metadata": {}, 1326 | "source": [ 1327 | "## Doubts" 1328 | ] 1329 | }, 1330 | { 1331 | "cell_type": "code", 1332 | "execution_count": 155, 1333 | "id": "6138d39d", 1334 | "metadata": {}, 1335 | "outputs": [], 1336 | "source": [ 1337 | "f.read?" 1338 | ] 1339 | }, 1340 | { 1341 | "cell_type": "code", 1342 | "execution_count": 158, 1343 | "id": "5a3cbb0a", 1344 | "metadata": {}, 1345 | "outputs": [], 1346 | "source": [ 1347 | "f = open('/Users/sahilbansal/Desktop/new_file.txt', 'w')" 1348 | ] 1349 | }, 1350 | { 1351 | "cell_type": "code", 1352 | "execution_count": 159, 1353 | "id": "3532639e", 1354 | "metadata": {}, 1355 | "outputs": [ 1356 | { 1357 | "data": { 1358 | "text/plain": [ 1359 | "16" 1360 | ] 1361 | }, 1362 | "execution_count": 159, 1363 | "metadata": {}, 1364 | "output_type": "execute_result" 1365 | } 1366 | ], 1367 | "source": [ 1368 | "f.write(\"This is new file\")" 1369 | ] 1370 | }, 1371 | { 1372 | "cell_type": "code", 1373 | "execution_count": 160, 1374 | "id": "2728bc77", 1375 | "metadata": {}, 1376 | "outputs": [], 1377 | "source": [ 1378 | "f.close()" 1379 | ] 1380 | }, 1381 | { 1382 | "cell_type": "code", 1383 | "execution_count": 164, 1384 | "id": "5bf203e1", 1385 | "metadata": {}, 1386 | "outputs": [], 1387 | "source": [ 1388 | "# write with binary encoding\n", 1389 | "with open('image_copy_2.jpeg', 'wb') as f:\n", 1390 | " f.write(file_content)\n", 1391 | " # closing is automatically handled as you go outside the block" 1392 | ] 1393 | }, 1394 | { 1395 | "cell_type": "code", 1396 | "execution_count": 165, 1397 | "id": "be722207", 1398 | "metadata": {}, 1399 | "outputs": [], 1400 | "source": [ 1401 | "\n", 1402 | "f2 = open('image_copy_3.jpeg', 'wb')\n", 1403 | "\n", 1404 | "# we have to use a binary decoding\n", 1405 | "# rb => read with binary decoding\n", 1406 | "# when you do not know the encoding, directly use the binary\n", 1407 | "with open('image.jpeg', 'rb') as f:\n", 1408 | " f2.write(f.read()) # prints the hexadecimal format\n", 1409 | "\n", 1410 | "f2.close()" 1411 | ] 1412 | }, 1413 | { 1414 | "cell_type": "code", 1415 | "execution_count": 166, 1416 | "id": "f13323c1", 1417 | "metadata": {}, 1418 | "outputs": [], 1419 | "source": [ 1420 | "# reading writing csv files => covered in the DSML Adv" 1421 | ] 1422 | }, 1423 | { 1424 | "cell_type": "code", 1425 | "execution_count": null, 1426 | "id": "3ba53444", 1427 | "metadata": {}, 1428 | "outputs": [], 1429 | "source": [] 1430 | }, 1431 | { 1432 | "cell_type": "code", 1433 | "execution_count": null, 1434 | "id": "dc5e5577", 1435 | "metadata": {}, 1436 | "outputs": [], 1437 | "source": [] 1438 | } 1439 | ], 1440 | "metadata": { 1441 | "kernelspec": { 1442 | "display_name": "Python 3 (ipykernel)", 1443 | "language": "python", 1444 | "name": "python3" 1445 | }, 1446 | "language_info": { 1447 | "codemirror_mode": { 1448 | "name": "ipython", 1449 | "version": 3 1450 | }, 1451 | "file_extension": ".py", 1452 | "mimetype": "text/x-python", 1453 | "name": "python", 1454 | "nbconvert_exporter": "python", 1455 | "pygments_lexer": "ipython3", 1456 | "version": "3.9.12" 1457 | } 1458 | }, 1459 | "nbformat": 4, 1460 | "nbformat_minor": 5 1461 | } 1462 | -------------------------------------------------------------------------------- /File_Handling/image.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/scaleracademy/dsml-jun-22-beginner-intermediate/2b2b21ab72368da52a4113f18a8e3f8544cf34d2/File_Handling/image.jpeg -------------------------------------------------------------------------------- /File_Handling/image_copy.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/scaleracademy/dsml-jun-22-beginner-intermediate/2b2b21ab72368da52a4113f18a8e3f8544cf34d2/File_Handling/image_copy.jpeg -------------------------------------------------------------------------------- /File_Handling/image_copy_2.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/scaleracademy/dsml-jun-22-beginner-intermediate/2b2b21ab72368da52a4113f18a8e3f8544cf34d2/File_Handling/image_copy_2.jpeg -------------------------------------------------------------------------------- /File_Handling/image_copy_3.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/scaleracademy/dsml-jun-22-beginner-intermediate/2b2b21ab72368da52a4113f18a8e3f8544cf34d2/File_Handling/image_copy_3.jpeg -------------------------------------------------------------------------------- /File_Handling/sample.txt: -------------------------------------------------------------------------------- 1 | Hello everyone talking from python program to you!! -------------------------------------------------------------------------------- /File_Handling/sample2.txt: -------------------------------------------------------------------------------- 1 | this will get replacedontentthis is new content this is going to be added at the end -------------------------------------------------------------------------------- /File_Handling/sample3.txt: -------------------------------------------------------------------------------- 1 | 5234197Here we write!!Here we write!! -------------------------------------------------------------------------------- /File_Handling/untitled.txt: -------------------------------------------------------------------------------- 1 | Hello this is our first file 2 | 3 | that we are going to open 4 | 5 | This is some magic: 6 | 1 7 | 2 8 | 3 9 | 4 10 | -------------------------------------------------------------------------------- /Functional_Programming.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "id": "view-in-github", 7 | "colab_type": "text" 8 | }, 9 | "source": [ 10 | "\"Open" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "id": "a1f7ba5d", 16 | "metadata": { 17 | "id": "a1f7ba5d" 18 | }, 19 | "source": [ 20 | "## Functional Programming" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": null, 26 | "id": "d211f1db", 27 | "metadata": { 28 | "id": "d211f1db" 29 | }, 30 | "outputs": [], 31 | "source": [ 32 | "def square_number(a):\n", 33 | " return a**2" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": null, 39 | "id": "f4f203c1", 40 | "metadata": { 41 | "id": "f4f203c1", 42 | "outputId": "0a8d5cd3-280b-4e24-efb0-cbd818f370ea" 43 | }, 44 | "outputs": [ 45 | { 46 | "data": { 47 | "text/plain": [ 48 | "16" 49 | ] 50 | }, 51 | "execution_count": 2, 52 | "metadata": {}, 53 | "output_type": "execute_result" 54 | } 55 | ], 56 | "source": [ 57 | "square_number(4)" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": null, 63 | "id": "28185aff", 64 | "metadata": { 65 | "id": "28185aff" 66 | }, 67 | "outputs": [], 68 | "source": [ 69 | "square = lambda x: x**2\n", 70 | "# lambda is creating a function which can be stored in a variable" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": null, 76 | "id": "e72f046e", 77 | "metadata": { 78 | "id": "e72f046e", 79 | "outputId": "9ebd38e8-aa06-42ba-88ce-0e3451f8b15d" 80 | }, 81 | "outputs": [ 82 | { 83 | "data": { 84 | "text/plain": [ 85 | "9" 86 | ] 87 | }, 88 | "execution_count": 4, 89 | "metadata": {}, 90 | "output_type": "execute_result" 91 | } 92 | ], 93 | "source": [ 94 | "square(3)" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": null, 100 | "id": "ec390749", 101 | "metadata": { 102 | "id": "ec390749", 103 | "outputId": "d7e84b20-a154-457c-a00c-3c6f2e652429" 104 | }, 105 | "outputs": [ 106 | { 107 | "data": { 108 | "text/plain": [ 109 | "81" 110 | ] 111 | }, 112 | "execution_count": 5, 113 | "metadata": {}, 114 | "output_type": "execute_result" 115 | } 116 | ], 117 | "source": [ 118 | "square(9)" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": null, 124 | "id": "7ebd023d", 125 | "metadata": { 126 | "id": "7ebd023d" 127 | }, 128 | "outputs": [], 129 | "source": [ 130 | "add = lambda x, y: x + y" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": null, 136 | "id": "a225636a", 137 | "metadata": { 138 | "id": "a225636a", 139 | "outputId": "dc889d47-4e45-4d6a-d2bd-914b90e1ee27" 140 | }, 141 | "outputs": [ 142 | { 143 | "data": { 144 | "text/plain": [ 145 | "8" 146 | ] 147 | }, 148 | "execution_count": 8, 149 | "metadata": {}, 150 | "output_type": "execute_result" 151 | } 152 | ], 153 | "source": [ 154 | "add(3, 5)" 155 | ] 156 | }, 157 | { 158 | "cell_type": "markdown", 159 | "id": "c8e9bb30", 160 | "metadata": { 161 | "id": "c8e9bb30" 162 | }, 163 | "source": [ 164 | "### Anonymous functions\n", 165 | "\n", 166 | "Lambda functions are known as anonymous functions, i.e.\n", 167 | "functions without any name" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": null, 173 | "id": "fcbfdf5a", 174 | "metadata": { 175 | "id": "fcbfdf5a", 176 | "outputId": "e32ae51c-ba06-4e2f-c0aa-2a5365a0162f" 177 | }, 178 | "outputs": [ 179 | { 180 | "data": { 181 | "text/plain": [ 182 | "(x)>" 183 | ] 184 | }, 185 | "execution_count": 9, 186 | "metadata": {}, 187 | "output_type": "execute_result" 188 | } 189 | ], 190 | "source": [ 191 | "(lambda x: x**3)" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": null, 197 | "id": "541f6051", 198 | "metadata": { 199 | "id": "541f6051" 200 | }, 201 | "outputs": [], 202 | "source": [ 203 | "y = (lambda x: x**3)" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": null, 209 | "id": "df63f7e4", 210 | "metadata": { 211 | "id": "df63f7e4", 212 | "outputId": "a987cda8-43c2-4abb-de97-d249afa0995a" 213 | }, 214 | "outputs": [ 215 | { 216 | "data": { 217 | "text/plain": [ 218 | "function" 219 | ] 220 | }, 221 | "execution_count": 11, 222 | "metadata": {}, 223 | "output_type": "execute_result" 224 | } 225 | ], 226 | "source": [ 227 | "type(y)" 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": null, 233 | "id": "7f20d015", 234 | "metadata": { 235 | "id": "7f20d015", 236 | "outputId": "e7f68d03-02a0-46d8-a2d3-d10c5dcd48da" 237 | }, 238 | "outputs": [ 239 | { 240 | "data": { 241 | "text/plain": [ 242 | "8" 243 | ] 244 | }, 245 | "execution_count": 12, 246 | "metadata": {}, 247 | "output_type": "execute_result" 248 | } 249 | ], 250 | "source": [ 251 | "(lambda x: x**3)(2)" 252 | ] 253 | }, 254 | { 255 | "cell_type": "code", 256 | "execution_count": null, 257 | "id": "ae301347", 258 | "metadata": { 259 | "id": "ae301347", 260 | "outputId": "212fa9d1-e4a3-4ad6-90da-8a4937302b97" 261 | }, 262 | "outputs": [ 263 | { 264 | "data": { 265 | "text/plain": [ 266 | "125" 267 | ] 268 | }, 269 | "execution_count": 13, 270 | "metadata": {}, 271 | "output_type": "execute_result" 272 | } 273 | ], 274 | "source": [ 275 | "(lambda x: x**3)(5)" 276 | ] 277 | }, 278 | { 279 | "cell_type": "code", 280 | "execution_count": null, 281 | "id": "d40ef077", 282 | "metadata": { 283 | "id": "d40ef077", 284 | "outputId": "71838cd4-ca6b-4ecf-ec43-e3d80fd5b219" 285 | }, 286 | "outputs": [ 287 | { 288 | "data": { 289 | "text/plain": [ 290 | "(lst)>" 291 | ] 292 | }, 293 | "execution_count": 14, 294 | "metadata": {}, 295 | "output_type": "execute_result" 296 | } 297 | ], 298 | "source": [ 299 | "lambda lst: lst[0]" 300 | ] 301 | }, 302 | { 303 | "cell_type": "code", 304 | "execution_count": null, 305 | "id": "f3f4c416", 306 | "metadata": { 307 | "id": "f3f4c416", 308 | "outputId": "945a22bf-6ee3-42e3-b6cf-d1120e07c4d1" 309 | }, 310 | "outputs": [ 311 | { 312 | "data": { 313 | "text/plain": [ 314 | "9" 315 | ] 316 | }, 317 | "execution_count": 16, 318 | "metadata": {}, 319 | "output_type": "execute_result" 320 | } 321 | ], 322 | "source": [ 323 | "(lambda lst: lst[0])([9, 5, 6])" 324 | ] 325 | }, 326 | { 327 | "cell_type": "code", 328 | "execution_count": null, 329 | "id": "874cd0a1", 330 | "metadata": { 331 | "id": "874cd0a1" 332 | }, 333 | "outputs": [], 334 | "source": [ 335 | "def get_first(lst):\n", 336 | " return lst[0]" 337 | ] 338 | }, 339 | { 340 | "cell_type": "code", 341 | "execution_count": null, 342 | "id": "fdfe4905", 343 | "metadata": { 344 | "id": "fdfe4905", 345 | "outputId": "cfbf393b-d524-4776-e1e5-5aaa450482e1" 346 | }, 347 | "outputs": [ 348 | { 349 | "data": { 350 | "text/plain": [ 351 | "9" 352 | ] 353 | }, 354 | "execution_count": 18, 355 | "metadata": {}, 356 | "output_type": "execute_result" 357 | } 358 | ], 359 | "source": [ 360 | "get_first([9, 5, 6])" 361 | ] 362 | }, 363 | { 364 | "cell_type": "code", 365 | "execution_count": null, 366 | "id": "c6f0b10f", 367 | "metadata": { 368 | "id": "c6f0b10f", 369 | "outputId": "4f4e53fe-b615-4080-8959-51c754bb82f0" 370 | }, 371 | "outputs": [ 372 | { 373 | "data": { 374 | "text/plain": [ 375 | "9" 376 | ] 377 | }, 378 | "execution_count": 19, 379 | "metadata": {}, 380 | "output_type": "execute_result" 381 | } 382 | ], 383 | "source": [ 384 | "(lambda lst: lst[0])([9, 5, 6])" 385 | ] 386 | }, 387 | { 388 | "cell_type": "code", 389 | "execution_count": null, 390 | "id": "ac6c74c5", 391 | "metadata": { 392 | "id": "ac6c74c5" 393 | }, 394 | "outputs": [], 395 | "source": [] 396 | }, 397 | { 398 | "cell_type": "markdown", 399 | "id": "b0bbb8b8", 400 | "metadata": { 401 | "id": "b0bbb8b8" 402 | }, 403 | "source": [ 404 | "### Use case" 405 | ] 406 | }, 407 | { 408 | "cell_type": "code", 409 | "execution_count": null, 410 | "id": "37fe090b", 411 | "metadata": { 412 | "id": "37fe090b" 413 | }, 414 | "outputs": [], 415 | "source": [ 416 | "coordinates = [(1, -1), (-1, 2), (3, 5), (5, 3), (7, 7), (2, 1)]" 417 | ] 418 | }, 419 | { 420 | "cell_type": "code", 421 | "execution_count": null, 422 | "id": "90084594", 423 | "metadata": { 424 | "id": "90084594" 425 | }, 426 | "outputs": [], 427 | "source": [ 428 | "# we want to sort them on the basis of x coordinate" 429 | ] 430 | }, 431 | { 432 | "cell_type": "code", 433 | "execution_count": null, 434 | "id": "b6544206", 435 | "metadata": { 436 | "id": "b6544206" 437 | }, 438 | "outputs": [], 439 | "source": [ 440 | "res = sorted(coordinates)" 441 | ] 442 | }, 443 | { 444 | "cell_type": "code", 445 | "execution_count": null, 446 | "id": "deb1b553", 447 | "metadata": { 448 | "id": "deb1b553", 449 | "outputId": "f1d77f89-1105-4119-f658-8ed7f0120089" 450 | }, 451 | "outputs": [ 452 | { 453 | "name": "stdout", 454 | "output_type": "stream", 455 | "text": [ 456 | "[(-1, 2), (1, -1), (2, 1), (3, 5), (5, 3), (7, 7)]\n" 457 | ] 458 | } 459 | ], 460 | "source": [ 461 | "print(res)" 462 | ] 463 | }, 464 | { 465 | "cell_type": "code", 466 | "execution_count": null, 467 | "id": "e1546487", 468 | "metadata": { 469 | "id": "e1546487" 470 | }, 471 | "outputs": [], 472 | "source": [ 473 | "# we want to sort them on the basis of y coordinate" 474 | ] 475 | }, 476 | { 477 | "cell_type": "markdown", 478 | "id": "78c186fd", 479 | "metadata": { 480 | "id": "78c186fd" 481 | }, 482 | "source": [ 483 | "#### sort vs sorted" 484 | ] 485 | }, 486 | { 487 | "cell_type": "code", 488 | "execution_count": null, 489 | "id": "33e123e0", 490 | "metadata": { 491 | "id": "33e123e0" 492 | }, 493 | "outputs": [], 494 | "source": [ 495 | "l = [1, 4, 3, 2, 5]" 496 | ] 497 | }, 498 | { 499 | "cell_type": "code", 500 | "execution_count": null, 501 | "id": "2d4681fc", 502 | "metadata": { 503 | "id": "2d4681fc", 504 | "outputId": "7f4a6274-348a-4807-a267-46e5c97dfcbf" 505 | }, 506 | "outputs": [ 507 | { 508 | "name": "stdout", 509 | "output_type": "stream", 510 | "text": [ 511 | "[1, 2, 3, 4, 5]\n", 512 | "[1, 2, 3, 4, 5]\n" 513 | ] 514 | } 515 | ], 516 | "source": [ 517 | "x = sorted(l) # creates a copy (new list) of the list which is sorted\n", 518 | "print(x)\n", 519 | "print(l)" 520 | ] 521 | }, 522 | { 523 | "cell_type": "code", 524 | "execution_count": null, 525 | "id": "961fef21", 526 | "metadata": { 527 | "id": "961fef21" 528 | }, 529 | "outputs": [], 530 | "source": [ 531 | "l.sort()" 532 | ] 533 | }, 534 | { 535 | "cell_type": "code", 536 | "execution_count": null, 537 | "id": "da6567c3", 538 | "metadata": { 539 | "id": "da6567c3", 540 | "outputId": "caf9f06d-cbb0-4b11-d450-1e01d654d878" 541 | }, 542 | "outputs": [ 543 | { 544 | "name": "stdout", 545 | "output_type": "stream", 546 | "text": [ 547 | "[1, 2, 3, 4, 5]\n" 548 | ] 549 | } 550 | ], 551 | "source": [ 552 | "print(l)" 553 | ] 554 | }, 555 | { 556 | "cell_type": "markdown", 557 | "id": "52fec12a", 558 | "metadata": { 559 | "id": "52fec12a" 560 | }, 561 | "source": [ 562 | "### Solution" 563 | ] 564 | }, 565 | { 566 | "cell_type": "code", 567 | "execution_count": null, 568 | "id": "1ea9f394", 569 | "metadata": { 570 | "id": "1ea9f394" 571 | }, 572 | "outputs": [], 573 | "source": [ 574 | "sorted?" 575 | ] 576 | }, 577 | { 578 | "cell_type": "code", 579 | "execution_count": null, 580 | "id": "65c57a71", 581 | "metadata": { 582 | "id": "65c57a71" 583 | }, 584 | "outputs": [], 585 | "source": [ 586 | "coordinates = [(1, -1), (-1, 2), (3, 5), (5, 3), (7, 7), (2, 1)]" 587 | ] 588 | }, 589 | { 590 | "cell_type": "code", 591 | "execution_count": null, 592 | "id": "5549bbfc", 593 | "metadata": { 594 | "id": "5549bbfc" 595 | }, 596 | "outputs": [], 597 | "source": [ 598 | "def get_y(lst):\n", 599 | " return lst[1]" 600 | ] 601 | }, 602 | { 603 | "cell_type": "code", 604 | "execution_count": null, 605 | "id": "e95610e2", 606 | "metadata": { 607 | "id": "e95610e2", 608 | "outputId": "23f1647c-fb92-4dec-f251-5d79cbbaa85f" 609 | }, 610 | "outputs": [ 611 | { 612 | "data": { 613 | "text/plain": [ 614 | "[(1, -1), (2, 1), (-1, 2), (5, 3), (3, 5), (7, 7)]" 615 | ] 616 | }, 617 | "execution_count": 38, 618 | "metadata": {}, 619 | "output_type": "execute_result" 620 | } 621 | ], 622 | "source": [ 623 | "sorted(coordinates, key=get_y)" 624 | ] 625 | }, 626 | { 627 | "cell_type": "code", 628 | "execution_count": null, 629 | "id": "100ea4b5", 630 | "metadata": { 631 | "id": "100ea4b5" 632 | }, 633 | "outputs": [], 634 | "source": [ 635 | "def bubble_sort(arr):\n", 636 | " n = len(arr)\n", 637 | " for i in range(n - 1):\n", 638 | " for j in range(n - i - 1):\n", 639 | " if arr[j] > arr[j + 1]:\n", 640 | " arr[j], arr[j + 1] = arr[j + 1], arr[j]\n", 641 | " return arr" 642 | ] 643 | }, 644 | { 645 | "cell_type": "code", 646 | "execution_count": null, 647 | "id": "0279ff03", 648 | "metadata": { 649 | "id": "0279ff03", 650 | "outputId": "f17c9203-ad99-402f-c4fe-bfff237809df" 651 | }, 652 | "outputs": [ 653 | { 654 | "data": { 655 | "text/plain": [ 656 | "[1, 2, 3, 4, 5]" 657 | ] 658 | }, 659 | "execution_count": 44, 660 | "metadata": {}, 661 | "output_type": "execute_result" 662 | } 663 | ], 664 | "source": [ 665 | "l = [5, 1, 2, 3, 4]\n", 666 | "bubble_sort(l)" 667 | ] 668 | }, 669 | { 670 | "cell_type": "code", 671 | "execution_count": null, 672 | "id": "3bcf6a30", 673 | "metadata": { 674 | "id": "3bcf6a30", 675 | "outputId": "38059876-13bc-4357-8a26-d6d6156cb999" 676 | }, 677 | "outputs": [ 678 | { 679 | "data": { 680 | "text/plain": [ 681 | "True" 682 | ] 683 | }, 684 | "execution_count": 45, 685 | "metadata": {}, 686 | "output_type": "execute_result" 687 | } 688 | ], 689 | "source": [ 690 | "(1, -1) > (-1, 2)" 691 | ] 692 | }, 693 | { 694 | "cell_type": "code", 695 | "execution_count": null, 696 | "id": "d286ddeb", 697 | "metadata": { 698 | "id": "d286ddeb", 699 | "outputId": "569fd846-58d1-4fa2-ed68-c64ab49c7ac0" 700 | }, 701 | "outputs": [ 702 | { 703 | "data": { 704 | "text/plain": [ 705 | "[(-1, 2), (1, -1), (2, 1), (3, 5), (5, 3), (7, 7)]" 706 | ] 707 | }, 708 | "execution_count": 46, 709 | "metadata": {}, 710 | "output_type": "execute_result" 711 | } 712 | ], 713 | "source": [ 714 | "bubble_sort(coordinates)" 715 | ] 716 | }, 717 | { 718 | "cell_type": "markdown", 719 | "id": "279c0627", 720 | "metadata": { 721 | "id": "279c0627" 722 | }, 723 | "source": [ 724 | "### Modified bubble sort to sort based on y" 725 | ] 726 | }, 727 | { 728 | "cell_type": "code", 729 | "execution_count": null, 730 | "id": "b3dabde4", 731 | "metadata": { 732 | "id": "b3dabde4" 733 | }, 734 | "outputs": [], 735 | "source": [ 736 | "def get_y(lst):\n", 737 | " return lst[1]" 738 | ] 739 | }, 740 | { 741 | "cell_type": "code", 742 | "execution_count": null, 743 | "id": "42d37446", 744 | "metadata": { 745 | "id": "42d37446" 746 | }, 747 | "outputs": [], 748 | "source": [ 749 | "def bubble_sort(arr):\n", 750 | " n = len(arr)\n", 751 | " for i in range(n - 1):\n", 752 | " for j in range(n - i - 1):\n", 753 | " if get_y(arr[j]) > get_y(arr[j + 1]):\n", 754 | " arr[j], arr[j + 1] = arr[j + 1], arr[j]\n", 755 | " return arr" 756 | ] 757 | }, 758 | { 759 | "cell_type": "code", 760 | "execution_count": null, 761 | "id": "dff7b3ca", 762 | "metadata": { 763 | "id": "dff7b3ca" 764 | }, 765 | "outputs": [], 766 | "source": [ 767 | "coordinates = [(1, -1), (-1, 2), (3, 5), (5, 3), (7, 7), (2, 1)]" 768 | ] 769 | }, 770 | { 771 | "cell_type": "code", 772 | "execution_count": null, 773 | "id": "27b069f4", 774 | "metadata": { 775 | "id": "27b069f4", 776 | "outputId": "0f16f0d0-3b9f-4c7b-c184-1bb3074d41f3" 777 | }, 778 | "outputs": [ 779 | { 780 | "data": { 781 | "text/plain": [ 782 | "[(1, -1), (2, 1), (-1, 2), (5, 3), (3, 5), (7, 7)]" 783 | ] 784 | }, 785 | "execution_count": 50, 786 | "metadata": {}, 787 | "output_type": "execute_result" 788 | } 789 | ], 790 | "source": [ 791 | "bubble_sort(coordinates)" 792 | ] 793 | }, 794 | { 795 | "cell_type": "markdown", 796 | "id": "edb0312d", 797 | "metadata": { 798 | "id": "edb0312d" 799 | }, 800 | "source": [ 801 | "### One more example" 802 | ] 803 | }, 804 | { 805 | "cell_type": "code", 806 | "execution_count": null, 807 | "id": "ccddefc8", 808 | "metadata": { 809 | "id": "ccddefc8" 810 | }, 811 | "outputs": [], 812 | "source": [ 813 | "coordinates = [(1, -1), (-1, 2), (3, 5), (5, 3), (7, 7), (2, 1)]" 814 | ] 815 | }, 816 | { 817 | "cell_type": "code", 818 | "execution_count": null, 819 | "id": "19851736", 820 | "metadata": { 821 | "id": "19851736", 822 | "outputId": "c35de98d-cc75-4d03-bb0b-b2b25e5543f4" 823 | }, 824 | "outputs": [ 825 | { 826 | "data": { 827 | "text/plain": [ 828 | "[(1, -1), (2, 1), (-1, 2), (5, 3), (3, 5), (7, 7)]" 829 | ] 830 | }, 831 | "execution_count": 51, 832 | "metadata": {}, 833 | "output_type": "execute_result" 834 | } 835 | ], 836 | "source": [ 837 | "sorted(coordinates, key= lambda l: l[1])" 838 | ] 839 | }, 840 | { 841 | "cell_type": "code", 842 | "execution_count": null, 843 | "id": "d56ef850", 844 | "metadata": { 845 | "id": "d56ef850" 846 | }, 847 | "outputs": [], 848 | "source": [ 849 | "students = [\n", 850 | " {\"name\": \"A\", \"marks\": 50},\n", 851 | " {\"name\": \"B\", \"marks\": 100},\n", 852 | " {\"name\": \"C\", \"marks\": 40},\n", 853 | " {\"name\": \"D\", \"marks\": 70},\n", 854 | " {\"name\": \"E\", \"marks\": 60},\n", 855 | "]" 856 | ] 857 | }, 858 | { 859 | "cell_type": "code", 860 | "execution_count": null, 861 | "id": "488ba1ed", 862 | "metadata": { 863 | "id": "488ba1ed" 864 | }, 865 | "outputs": [], 866 | "source": [ 867 | "## HW : Sort based on the marks" 868 | ] 869 | }, 870 | { 871 | "cell_type": "code", 872 | "execution_count": null, 873 | "id": "d1a9bae8", 874 | "metadata": { 875 | "id": "d1a9bae8", 876 | "outputId": "839aff0c-f118-42ac-cb71-04116d1403ea" 877 | }, 878 | "outputs": [ 879 | { 880 | "data": { 881 | "text/plain": [ 882 | "[{'name': 'C', 'marks': 40},\n", 883 | " {'name': 'A', 'marks': 50},\n", 884 | " {'name': 'E', 'marks': 60},\n", 885 | " {'name': 'D', 'marks': 70},\n", 886 | " {'name': 'B', 'marks': 100}]" 887 | ] 888 | }, 889 | "execution_count": 54, 890 | "metadata": {}, 891 | "output_type": "execute_result" 892 | } 893 | ], 894 | "source": [ 895 | "sorted(students, key = lambda x: x['marks'])" 896 | ] 897 | }, 898 | { 899 | "cell_type": "code", 900 | "execution_count": null, 901 | "id": "f9f1fa6c", 902 | "metadata": { 903 | "id": "f9f1fa6c", 904 | "outputId": "e2cfda9f-505d-4ead-d2c8-e68772860eab" 905 | }, 906 | "outputs": [ 907 | { 908 | "data": { 909 | "text/plain": [ 910 | "[{'name': 'B', 'marks': 100},\n", 911 | " {'name': 'D', 'marks': 70},\n", 912 | " {'name': 'E', 'marks': 60},\n", 913 | " {'name': 'A', 'marks': 50},\n", 914 | " {'name': 'C', 'marks': 40}]" 915 | ] 916 | }, 917 | "execution_count": 56, 918 | "metadata": {}, 919 | "output_type": "execute_result" 920 | } 921 | ], 922 | "source": [ 923 | "sorted(students, key = lambda x: x['marks'], reverse=True)" 924 | ] 925 | }, 926 | { 927 | "cell_type": "code", 928 | "execution_count": null, 929 | "id": "5f5b0fe5", 930 | "metadata": { 931 | "id": "5f5b0fe5" 932 | }, 933 | "outputs": [], 934 | "source": [ 935 | "sorted?" 936 | ] 937 | }, 938 | { 939 | "cell_type": "markdown", 940 | "id": "6156fbc1", 941 | "metadata": { 942 | "id": "6156fbc1" 943 | }, 944 | "source": [ 945 | "### Higher Order Functions" 946 | ] 947 | }, 948 | { 949 | "cell_type": "code", 950 | "execution_count": null, 951 | "id": "111eeb42", 952 | "metadata": { 953 | "id": "111eeb42" 954 | }, 955 | "outputs": [], 956 | "source": [ 957 | "# a function that returns another functions" 958 | ] 959 | }, 960 | { 961 | "cell_type": "code", 962 | "execution_count": null, 963 | "id": "7bcb8e72", 964 | "metadata": { 965 | "id": "7bcb8e72" 966 | }, 967 | "outputs": [], 968 | "source": [ 969 | "def gen_exp(n):\n", 970 | " def exp(x):\n", 971 | " return x**n\n", 972 | " \n", 973 | " return exp" 974 | ] 975 | }, 976 | { 977 | "cell_type": "code", 978 | "execution_count": null, 979 | "id": "8da30ef1", 980 | "metadata": { 981 | "id": "8da30ef1" 982 | }, 983 | "outputs": [], 984 | "source": [ 985 | "exp_5 = gen_exp(5)" 986 | ] 987 | }, 988 | { 989 | "cell_type": "code", 990 | "execution_count": null, 991 | "id": "520f13a8", 992 | "metadata": { 993 | "id": "520f13a8", 994 | "outputId": "82b1f53f-35ba-4dac-ef2e-e97ac19518e8" 995 | }, 996 | "outputs": [ 997 | { 998 | "data": { 999 | "text/plain": [ 1000 | "function" 1001 | ] 1002 | }, 1003 | "execution_count": 60, 1004 | "metadata": {}, 1005 | "output_type": "execute_result" 1006 | } 1007 | ], 1008 | "source": [ 1009 | "type(exp_5)" 1010 | ] 1011 | }, 1012 | { 1013 | "cell_type": "code", 1014 | "execution_count": null, 1015 | "id": "4f99915c", 1016 | "metadata": { 1017 | "id": "4f99915c", 1018 | "outputId": "79795bda-a194-4e90-c409-175d23e7c953" 1019 | }, 1020 | "outputs": [ 1021 | { 1022 | "data": { 1023 | "text/plain": [ 1024 | "32" 1025 | ] 1026 | }, 1027 | "execution_count": 61, 1028 | "metadata": {}, 1029 | "output_type": "execute_result" 1030 | } 1031 | ], 1032 | "source": [ 1033 | "exp_5(2)" 1034 | ] 1035 | }, 1036 | { 1037 | "cell_type": "markdown", 1038 | "id": "1724697d", 1039 | "metadata": { 1040 | "id": "1724697d" 1041 | }, 1042 | "source": [ 1043 | "### Quiz" 1044 | ] 1045 | }, 1046 | { 1047 | "cell_type": "code", 1048 | "execution_count": null, 1049 | "id": "68d38045", 1050 | "metadata": { 1051 | "id": "68d38045", 1052 | "outputId": "9dea83e2-9012-4ce9-d05d-b0345d32d0e5" 1053 | }, 1054 | "outputs": [ 1055 | { 1056 | "name": "stdout", 1057 | "output_type": "stream", 1058 | "text": [ 1059 | "(1, 4)\n" 1060 | ] 1061 | } 1062 | ], 1063 | "source": [ 1064 | "def func2(c, d):\n", 1065 | " return c, d\n", 1066 | "\n", 1067 | "def func1(a, b):\n", 1068 | " c = a**1\n", 1069 | " d = b**2\n", 1070 | " x = lambda: func2(c,d)\n", 1071 | " return x\n", 1072 | "\n", 1073 | "result = func1(1, 2)\n", 1074 | "\n", 1075 | "print(result())" 1076 | ] 1077 | }, 1078 | { 1079 | "cell_type": "code", 1080 | "execution_count": null, 1081 | "id": "98c64814", 1082 | "metadata": { 1083 | "id": "98c64814", 1084 | "outputId": "c669fbad-a6d8-4a66-e230-05f007531832" 1085 | }, 1086 | "outputs": [ 1087 | { 1088 | "ename": "TypeError", 1089 | "evalue": "'tuple' object is not callable", 1090 | "output_type": "error", 1091 | "traceback": [ 1092 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 1093 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 1094 | "Input \u001b[0;32mIn [66]\u001b[0m, in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m func2(c,d)\n\u001b[1;32m 9\u001b[0m result \u001b[38;5;241m=\u001b[39m func1(\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m)\n\u001b[0;32m---> 11\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43mresult\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m)\n", 1095 | "\u001b[0;31mTypeError\u001b[0m: 'tuple' object is not callable" 1096 | ] 1097 | } 1098 | ], 1099 | "source": [ 1100 | "def func2(c, d):\n", 1101 | " return c, d\n", 1102 | "\n", 1103 | "def func1(a, b):\n", 1104 | " c = a**1\n", 1105 | " d = b**2\n", 1106 | " return func2(c,d)\n", 1107 | "\n", 1108 | "result = func1(1, 2)\n", 1109 | "\n", 1110 | "print(result())" 1111 | ] 1112 | }, 1113 | { 1114 | "cell_type": "markdown", 1115 | "id": "b4c7b0e0", 1116 | "metadata": { 1117 | "id": "b4c7b0e0" 1118 | }, 1119 | "source": [ 1120 | "### Example 2" 1121 | ] 1122 | }, 1123 | { 1124 | "cell_type": "code", 1125 | "execution_count": null, 1126 | "id": "49894aee", 1127 | "metadata": { 1128 | "id": "49894aee" 1129 | }, 1130 | "outputs": [], 1131 | "source": [ 1132 | "def multiply_n(n):\n", 1133 | " def multiply(x):\n", 1134 | " return x*n\n", 1135 | " \n", 1136 | " return multiply" 1137 | ] 1138 | }, 1139 | { 1140 | "cell_type": "code", 1141 | "execution_count": null, 1142 | "id": "941987e9", 1143 | "metadata": { 1144 | "id": "941987e9" 1145 | }, 1146 | "outputs": [], 1147 | "source": [ 1148 | "mul_5 = multiply_n(5)" 1149 | ] 1150 | }, 1151 | { 1152 | "cell_type": "code", 1153 | "execution_count": null, 1154 | "id": "64d6fde5", 1155 | "metadata": { 1156 | "id": "64d6fde5", 1157 | "outputId": "0739aee4-d9de-4b25-dae2-2450c7f232fc" 1158 | }, 1159 | "outputs": [ 1160 | { 1161 | "data": { 1162 | "text/plain": [ 1163 | "50" 1164 | ] 1165 | }, 1166 | "execution_count": 71, 1167 | "metadata": {}, 1168 | "output_type": "execute_result" 1169 | } 1170 | ], 1171 | "source": [ 1172 | "mul_5(10)" 1173 | ] 1174 | }, 1175 | { 1176 | "cell_type": "code", 1177 | "execution_count": null, 1178 | "id": "ddffe1bf", 1179 | "metadata": { 1180 | "id": "ddffe1bf" 1181 | }, 1182 | "outputs": [], 1183 | "source": [ 1184 | "mul_6 = multiply_n(6)" 1185 | ] 1186 | }, 1187 | { 1188 | "cell_type": "code", 1189 | "execution_count": null, 1190 | "id": "af61821d", 1191 | "metadata": { 1192 | "id": "af61821d", 1193 | "outputId": "78d04856-a292-472a-b3ed-1e63a4fc4fb9" 1194 | }, 1195 | "outputs": [ 1196 | { 1197 | "data": { 1198 | "text/plain": [ 1199 | "60" 1200 | ] 1201 | }, 1202 | "execution_count": 72, 1203 | "metadata": {}, 1204 | "output_type": "execute_result" 1205 | } 1206 | ], 1207 | "source": [ 1208 | "mul_6(10)" 1209 | ] 1210 | }, 1211 | { 1212 | "cell_type": "code", 1213 | "execution_count": null, 1214 | "id": "868fac17", 1215 | "metadata": { 1216 | "id": "868fac17" 1217 | }, 1218 | "outputs": [], 1219 | "source": [] 1220 | }, 1221 | { 1222 | "cell_type": "code", 1223 | "execution_count": null, 1224 | "id": "3fc1cc6d", 1225 | "metadata": { 1226 | "id": "3fc1cc6d" 1227 | }, 1228 | "outputs": [], 1229 | "source": [] 1230 | }, 1231 | { 1232 | "cell_type": "code", 1233 | "execution_count": null, 1234 | "id": "114d0367", 1235 | "metadata": { 1236 | "id": "114d0367" 1237 | }, 1238 | "outputs": [], 1239 | "source": [] 1240 | }, 1241 | { 1242 | "cell_type": "markdown", 1243 | "id": "dd6e3812", 1244 | "metadata": { 1245 | "id": "dd6e3812" 1246 | }, 1247 | "source": [ 1248 | "### Decorators" 1249 | ] 1250 | }, 1251 | { 1252 | "cell_type": "code", 1253 | "execution_count": null, 1254 | "id": "6fc2383a", 1255 | "metadata": { 1256 | "id": "6fc2383a" 1257 | }, 1258 | "outputs": [], 1259 | "source": [ 1260 | "def foo():\n", 1261 | " print('-'*50)\n", 1262 | " print('Hello Everyone! How are you doing?')\n", 1263 | " print('-'*50)" 1264 | ] 1265 | }, 1266 | { 1267 | "cell_type": "code", 1268 | "execution_count": null, 1269 | "id": "e7a14b18", 1270 | "metadata": { 1271 | "id": "e7a14b18", 1272 | "outputId": "45380b74-b789-44ad-fae9-551437df0986" 1273 | }, 1274 | "outputs": [ 1275 | { 1276 | "name": "stdout", 1277 | "output_type": "stream", 1278 | "text": [ 1279 | "--------------------------------------------------\n", 1280 | "Hello Everyone! How are you doing?\n", 1281 | "--------------------------------------------------\n" 1282 | ] 1283 | } 1284 | ], 1285 | "source": [ 1286 | "foo()" 1287 | ] 1288 | }, 1289 | { 1290 | "cell_type": "code", 1291 | "execution_count": null, 1292 | "id": "12a428fc", 1293 | "metadata": { 1294 | "id": "12a428fc" 1295 | }, 1296 | "outputs": [], 1297 | "source": [ 1298 | "def bar():\n", 1299 | " print('-'*50)\n", 1300 | " print('Random cliche print statement!')\n", 1301 | " print('-'*50)" 1302 | ] 1303 | }, 1304 | { 1305 | "cell_type": "code", 1306 | "execution_count": null, 1307 | "id": "5654ee83", 1308 | "metadata": { 1309 | "id": "5654ee83", 1310 | "outputId": "c67c2d53-f294-4f43-9381-1305d6bfdd89" 1311 | }, 1312 | "outputs": [ 1313 | { 1314 | "name": "stdout", 1315 | "output_type": "stream", 1316 | "text": [ 1317 | "--------------------------------------------------\n", 1318 | "Random cliche print statement!\n", 1319 | "--------------------------------------------------\n" 1320 | ] 1321 | } 1322 | ], 1323 | "source": [ 1324 | "bar()" 1325 | ] 1326 | }, 1327 | { 1328 | "cell_type": "code", 1329 | "execution_count": null, 1330 | "id": "e87935b3", 1331 | "metadata": { 1332 | "id": "e87935b3" 1333 | }, 1334 | "outputs": [], 1335 | "source": [ 1336 | "# can I pass a function as argument also?" 1337 | ] 1338 | }, 1339 | { 1340 | "cell_type": "code", 1341 | "execution_count": null, 1342 | "id": "a0574f0e", 1343 | "metadata": { 1344 | "id": "a0574f0e" 1345 | }, 1346 | "outputs": [], 1347 | "source": [ 1348 | "def pretty(func):\n", 1349 | " def inner():\n", 1350 | " print('-'*50)\n", 1351 | " func()\n", 1352 | " print('-'*50)\n", 1353 | " return inner" 1354 | ] 1355 | }, 1356 | { 1357 | "cell_type": "code", 1358 | "execution_count": null, 1359 | "id": "b8a6c977", 1360 | "metadata": { 1361 | "id": "b8a6c977" 1362 | }, 1363 | "outputs": [], 1364 | "source": [ 1365 | "# is pretty a Higher order function? Yes because it returns another func" 1366 | ] 1367 | }, 1368 | { 1369 | "cell_type": "code", 1370 | "execution_count": null, 1371 | "id": "56115b0e", 1372 | "metadata": { 1373 | "id": "56115b0e" 1374 | }, 1375 | "outputs": [], 1376 | "source": [ 1377 | "def foo():\n", 1378 | " print(\"Hello Everyone! How are you doing?\")" 1379 | ] 1380 | }, 1381 | { 1382 | "cell_type": "code", 1383 | "execution_count": null, 1384 | "id": "8ab48cf1", 1385 | "metadata": { 1386 | "id": "8ab48cf1" 1387 | }, 1388 | "outputs": [], 1389 | "source": [ 1390 | "new_foo = pretty(foo)" 1391 | ] 1392 | }, 1393 | { 1394 | "cell_type": "code", 1395 | "execution_count": null, 1396 | "id": "0d088f0d", 1397 | "metadata": { 1398 | "id": "0d088f0d", 1399 | "outputId": "208feefd-b5fe-4923-8704-46ee93b0c6ae" 1400 | }, 1401 | "outputs": [ 1402 | { 1403 | "name": "stdout", 1404 | "output_type": "stream", 1405 | "text": [ 1406 | "--------------------------------------------------\n", 1407 | "Hello Everyon! How are you doing?\n", 1408 | "--------------------------------------------------\n" 1409 | ] 1410 | } 1411 | ], 1412 | "source": [ 1413 | "new_foo()" 1414 | ] 1415 | }, 1416 | { 1417 | "cell_type": "code", 1418 | "execution_count": null, 1419 | "id": "59e5a2e3", 1420 | "metadata": { 1421 | "id": "59e5a2e3" 1422 | }, 1423 | "outputs": [], 1424 | "source": [ 1425 | "def bar():\n", 1426 | " print(\"Random Cliche Statement!\")" 1427 | ] 1428 | }, 1429 | { 1430 | "cell_type": "code", 1431 | "execution_count": null, 1432 | "id": "98acb0ba", 1433 | "metadata": { 1434 | "id": "98acb0ba" 1435 | }, 1436 | "outputs": [], 1437 | "source": [ 1438 | "new_bar = pretty(bar)" 1439 | ] 1440 | }, 1441 | { 1442 | "cell_type": "code", 1443 | "execution_count": null, 1444 | "id": "1533dd4c", 1445 | "metadata": { 1446 | "id": "1533dd4c", 1447 | "outputId": "9f2ce05f-ad5a-4ab0-e7ad-fe253258b411" 1448 | }, 1449 | "outputs": [ 1450 | { 1451 | "name": "stdout", 1452 | "output_type": "stream", 1453 | "text": [ 1454 | "--------------------------------------------------\n", 1455 | "Random Cliche Statement!\n", 1456 | "--------------------------------------------------\n" 1457 | ] 1458 | } 1459 | ], 1460 | "source": [ 1461 | "new_bar()" 1462 | ] 1463 | }, 1464 | { 1465 | "cell_type": "markdown", 1466 | "id": "e386878b", 1467 | "metadata": { 1468 | "id": "e386878b" 1469 | }, 1470 | "source": [ 1471 | "## Decorator" 1472 | ] 1473 | }, 1474 | { 1475 | "cell_type": "code", 1476 | "execution_count": null, 1477 | "id": "79e436e5", 1478 | "metadata": { 1479 | "id": "79e436e5" 1480 | }, 1481 | "outputs": [], 1482 | "source": [ 1483 | "@pretty\n", 1484 | "def magical_foo():\n", 1485 | " print(\"WHHAAAATTTT?\")" 1486 | ] 1487 | }, 1488 | { 1489 | "cell_type": "code", 1490 | "execution_count": null, 1491 | "id": "7c6f5053", 1492 | "metadata": { 1493 | "id": "7c6f5053", 1494 | "outputId": "e96a7b58-edf7-409a-b5bd-4f32b1517667" 1495 | }, 1496 | "outputs": [ 1497 | { 1498 | "name": "stdout", 1499 | "output_type": "stream", 1500 | "text": [ 1501 | "--------------------------------------------------\n", 1502 | "WHHAAAATTTT?\n", 1503 | "--------------------------------------------------\n" 1504 | ] 1505 | } 1506 | ], 1507 | "source": [ 1508 | "magical_foo()" 1509 | ] 1510 | }, 1511 | { 1512 | "cell_type": "code", 1513 | "execution_count": null, 1514 | "id": "883e0569", 1515 | "metadata": { 1516 | "id": "883e0569" 1517 | }, 1518 | "outputs": [], 1519 | "source": [ 1520 | "@pretty\n", 1521 | "def magical_bar():\n", 1522 | " print(\"I AM AMAZED RIGHT NOW!! MIGHT JUST CRY!!!\")" 1523 | ] 1524 | }, 1525 | { 1526 | "cell_type": "code", 1527 | "execution_count": null, 1528 | "id": "4ef99d5b", 1529 | "metadata": { 1530 | "id": "4ef99d5b", 1531 | "outputId": "37782d99-d937-41dd-be46-a6bec7d94135" 1532 | }, 1533 | "outputs": [ 1534 | { 1535 | "name": "stdout", 1536 | "output_type": "stream", 1537 | "text": [ 1538 | "--------------------------------------------------\n", 1539 | "I AM AMAZED RIGHT NOW!! MIGHT JUST CRY!!!\n", 1540 | "--------------------------------------------------\n" 1541 | ] 1542 | } 1543 | ], 1544 | "source": [ 1545 | "magical_bar()" 1546 | ] 1547 | }, 1548 | { 1549 | "cell_type": "code", 1550 | "execution_count": null, 1551 | "id": "7377e94f", 1552 | "metadata": { 1553 | "id": "7377e94f" 1554 | }, 1555 | "outputs": [], 1556 | "source": [] 1557 | } 1558 | ], 1559 | "metadata": { 1560 | "kernelspec": { 1561 | "display_name": "Python 3 (ipykernel)", 1562 | "language": "python", 1563 | "name": "python3" 1564 | }, 1565 | "language_info": { 1566 | "codemirror_mode": { 1567 | "name": "ipython", 1568 | "version": 3 1569 | }, 1570 | "file_extension": ".py", 1571 | "mimetype": "text/x-python", 1572 | "name": "python", 1573 | "nbconvert_exporter": "python", 1574 | "pygments_lexer": "ipython3", 1575 | "version": "3.9.12" 1576 | }, 1577 | "colab": { 1578 | "provenance": [], 1579 | "include_colab_link": true 1580 | } 1581 | }, 1582 | "nbformat": 4, 1583 | "nbformat_minor": 5 1584 | } -------------------------------------------------------------------------------- /Functional_Programming_2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "4af46559", 6 | "metadata": {}, 7 | "source": [ 8 | "## Functional Programming - 2" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "e5a39585", 14 | "metadata": {}, 15 | "source": [ 16 | "## Recap" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": null, 22 | "id": "531eded5", 23 | "metadata": {}, 24 | "outputs": [], 25 | "source": [ 26 | "#CodeA\n", 27 | "@f\n", 28 | "def f1():\n", 29 | " print(“Hello”)\n", 30 | "\n", 31 | "#CodeB\n", 32 | "def f1():\n", 33 | " print(“Hello”)\n", 34 | "f1 = f(f1)\n" 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "id": "8573c8db", 40 | "metadata": {}, 41 | "source": [ 42 | "## Why functional programming?" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 3, 48 | "id": "01c7b1b7", 49 | "metadata": {}, 50 | "outputs": [ 51 | { 52 | "name": "stdout", 53 | "output_type": "stream", 54 | "text": [ 55 | "17\n" 56 | ] 57 | } 58 | ], 59 | "source": [ 60 | "x = 5 # initial data\n", 61 | "\n", 62 | "# hidden mutations => changing the state, but can't track them\n", 63 | "x = 3*x\n", 64 | "x += 2\n", 65 | "\n", 66 | "print(x)\n", 67 | "# print(previous_x?)\n", 68 | "# can you rollback (go back to a previous state) of the variable x?" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 6, 74 | "id": "43d6c872", 75 | "metadata": {}, 76 | "outputs": [ 77 | { 78 | "name": "stdout", 79 | "output_type": "stream", 80 | "text": [ 81 | "5 15 17\n" 82 | ] 83 | } 84 | ], 85 | "source": [ 86 | "x = 5\n", 87 | "\n", 88 | "# Mutation 1\n", 89 | "x1 = 3*x\n", 90 | "\n", 91 | "# Mutation 2\n", 92 | "x2 = x1 + 2\n", 93 | "\n", 94 | "# Can you get back to the previous states now?\n", 95 | "\n", 96 | "print(x, x1, x2)\n", 97 | "\n", 98 | "# PROBLEM : TOO MANY VARIABLES" 99 | ] 100 | }, 101 | { 102 | "cell_type": "markdown", 103 | "id": "abfd3222", 104 | "metadata": {}, 105 | "source": [ 106 | "Best of both worlds => tracking but with less no of variables" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 8, 112 | "id": "3e5b8b75", 113 | "metadata": {}, 114 | "outputs": [ 115 | { 116 | "name": "stdout", 117 | "output_type": "stream", 118 | "text": [ 119 | "5 16\n" 120 | ] 121 | } 122 | ], 123 | "source": [ 124 | "x = 5\n", 125 | "\n", 126 | "# what are the pros of making changes to data via calling functions\n", 127 | "# 1. reusability of the changes will increase\n", 128 | "# 2. tracking the data\n", 129 | "def mutation_1(x): # x is a parameter, different from the global x\n", 130 | " x = 3*x\n", 131 | " x += 1\n", 132 | " return x\n", 133 | "\n", 134 | "x1 = mutation_1(x)\n", 135 | "\n", 136 | "print(x, x1)" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "id": "f23f68dd", 142 | "metadata": {}, 143 | "source": [ 144 | "## Map function" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": 9, 150 | "id": "5ce05c87", 151 | "metadata": {}, 152 | "outputs": [], 153 | "source": [ 154 | "a = [1, 2, 3, 4, 5]" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 10, 160 | "id": "06df788a", 161 | "metadata": {}, 162 | "outputs": [], 163 | "source": [ 164 | "# give me the squares of elements in the list" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": 13, 170 | "id": "0c355dd9", 171 | "metadata": {}, 172 | "outputs": [], 173 | "source": [ 174 | "# code 1\n", 175 | "res = [ i**2 for i in a ]" 176 | ] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "execution_count": 12, 181 | "id": "f7a9dda1", 182 | "metadata": {}, 183 | "outputs": [ 184 | { 185 | "name": "stdout", 186 | "output_type": "stream", 187 | "text": [ 188 | "[1, 4, 9, 16, 25]\n" 189 | ] 190 | } 191 | ], 192 | "source": [ 193 | "print(res)" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": 16, 199 | "id": "f7930bfc", 200 | "metadata": {}, 201 | "outputs": [], 202 | "source": [ 203 | "# same code without list comprehension\n", 204 | "# code2\n", 205 | "res2 = []\n", 206 | "for i in a:\n", 207 | " res2.append(i*i)\n", 208 | "\n", 209 | "# they work using iteration protocol => iter and next" 210 | ] 211 | }, 212 | { 213 | "cell_type": "code", 214 | "execution_count": 15, 215 | "id": "fe9a1079", 216 | "metadata": {}, 217 | "outputs": [ 218 | { 219 | "name": "stdout", 220 | "output_type": "stream", 221 | "text": [ 222 | "[1, 4, 9, 16, 25]\n" 223 | ] 224 | } 225 | ], 226 | "source": [ 227 | "print(res2)" 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": 18, 233 | "id": "90ea3037", 234 | "metadata": {}, 235 | "outputs": [], 236 | "source": [ 237 | "# __iter__\n", 238 | "# __next__\n", 239 | "\n", 240 | "# they are actually behaviors in python => dunder method" 241 | ] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "execution_count": null, 246 | "id": "231ee4d1", 247 | "metadata": {}, 248 | "outputs": [], 249 | "source": [] 250 | }, 251 | { 252 | "cell_type": "markdown", 253 | "id": "7950b653", 254 | "metadata": {}, 255 | "source": [ 256 | "### Functional way" 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "execution_count": 19, 262 | "id": "da7390e3", 263 | "metadata": {}, 264 | "outputs": [], 265 | "source": [ 266 | "map?" 267 | ] 268 | }, 269 | { 270 | "cell_type": "code", 271 | "execution_count": 20, 272 | "id": "7e19a713", 273 | "metadata": {}, 274 | "outputs": [], 275 | "source": [ 276 | "a = [1, 2, 3, 4, 5]" 277 | ] 278 | }, 279 | { 280 | "cell_type": "code", 281 | "execution_count": 21, 282 | "id": "a8941767", 283 | "metadata": {}, 284 | "outputs": [], 285 | "source": [ 286 | "res = map(lambda x: x**2, a)" 287 | ] 288 | }, 289 | { 290 | "cell_type": "code", 291 | "execution_count": 22, 292 | "id": "44e08e3e", 293 | "metadata": {}, 294 | "outputs": [ 295 | { 296 | "name": "stdout", 297 | "output_type": "stream", 298 | "text": [ 299 | "\n" 300 | ] 301 | } 302 | ], 303 | "source": [ 304 | "print(res)" 305 | ] 306 | }, 307 | { 308 | "cell_type": "code", 309 | "execution_count": 23, 310 | "id": "53901a66", 311 | "metadata": {}, 312 | "outputs": [], 313 | "source": [ 314 | "res_list = list(res)" 315 | ] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "execution_count": 24, 320 | "id": "631c2371", 321 | "metadata": {}, 322 | "outputs": [ 323 | { 324 | "name": "stdout", 325 | "output_type": "stream", 326 | "text": [ 327 | "[1, 4, 9, 16, 25]\n" 328 | ] 329 | } 330 | ], 331 | "source": [ 332 | "print(res_list)" 333 | ] 334 | }, 335 | { 336 | "cell_type": "code", 337 | "execution_count": 27, 338 | "id": "fbcea95f", 339 | "metadata": {}, 340 | "outputs": [ 341 | { 342 | "name": "stdout", 343 | "output_type": "stream", 344 | "text": [ 345 | "[1, 4, 9, 16, 25]\n" 346 | ] 347 | } 348 | ], 349 | "source": [ 350 | "# doing in a single line => very elegant\n", 351 | "res_direct = list(map(lambda x: x**2, a))\n", 352 | "print(res_direct)" 353 | ] 354 | }, 355 | { 356 | "cell_type": "code", 357 | "execution_count": 28, 358 | "id": "eb8df31b", 359 | "metadata": {}, 360 | "outputs": [], 361 | "source": [ 362 | "sqr = lambda x: x**2" 363 | ] 364 | }, 365 | { 366 | "cell_type": "code", 367 | "execution_count": 29, 368 | "id": "31d59bdd", 369 | "metadata": {}, 370 | "outputs": [ 371 | { 372 | "data": { 373 | "text/plain": [ 374 | "16" 375 | ] 376 | }, 377 | "execution_count": 29, 378 | "metadata": {}, 379 | "output_type": "execute_result" 380 | } 381 | ], 382 | "source": [ 383 | "sqr(4)" 384 | ] 385 | }, 386 | { 387 | "cell_type": "code", 388 | "execution_count": 32, 389 | "id": "c0e4b5e2", 390 | "metadata": {}, 391 | "outputs": [ 392 | { 393 | "name": "stdout", 394 | "output_type": "stream", 395 | "text": [ 396 | "[1, 4, 9, 16, 25]\n" 397 | ] 398 | } 399 | ], 400 | "source": [ 401 | "# we can pass a named function as well\n", 402 | "res_2 = list(map(sqr(), a))\n", 403 | "print(res_2)" 404 | ] 405 | }, 406 | { 407 | "cell_type": "markdown", 408 | "id": "5dcc8a67", 409 | "metadata": {}, 410 | "source": [ 411 | "## Map Challenges" 412 | ] 413 | }, 414 | { 415 | "cell_type": "code", 416 | "execution_count": 45, 417 | "id": "22815757", 418 | "metadata": {}, 419 | "outputs": [], 420 | "source": [ 421 | "# Convert given height (assume integers) to t-shirt size\n", 422 | "# h < 150 -> M\n", 423 | "# h >= 150 -> L\n", 424 | "\n", 425 | "heights = [144, 167, 189, 170, 190, 150, 165, 178, 200, 130]" 426 | ] 427 | }, 428 | { 429 | "cell_type": "code", 430 | "execution_count": 34, 431 | "id": "d4bd5a8e", 432 | "metadata": {}, 433 | "outputs": [], 434 | "source": [ 435 | "def complex_logic(height):\n", 436 | " if height < 150:\n", 437 | " return \"M\"\n", 438 | " else:\n", 439 | " return \"L\"" 440 | ] 441 | }, 442 | { 443 | "cell_type": "code", 444 | "execution_count": 35, 445 | "id": "cb0667f1", 446 | "metadata": {}, 447 | "outputs": [], 448 | "source": [ 449 | "sizes = list(map(complex_logic, heights))" 450 | ] 451 | }, 452 | { 453 | "cell_type": "code", 454 | "execution_count": 36, 455 | "id": "664c4623", 456 | "metadata": {}, 457 | "outputs": [ 458 | { 459 | "name": "stdout", 460 | "output_type": "stream", 461 | "text": [ 462 | "['M', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'M']\n" 463 | ] 464 | } 465 | ], 466 | "source": [ 467 | "print(sizes)" 468 | ] 469 | }, 470 | { 471 | "cell_type": "code", 472 | "execution_count": 37, 473 | "id": "389bd3cb", 474 | "metadata": {}, 475 | "outputs": [], 476 | "source": [ 477 | "# can we do it in one line?" 478 | ] 479 | }, 480 | { 481 | "cell_type": "code", 482 | "execution_count": 41, 483 | "id": "55263959", 484 | "metadata": {}, 485 | "outputs": [], 486 | "source": [ 487 | "def complex_logic_one_liner(height):\n", 488 | " # shorthand if else, TERNARY IF-ELSE\n", 489 | " return \"M\" if height < 150 else \"L\"" 490 | ] 491 | }, 492 | { 493 | "cell_type": "code", 494 | "execution_count": 39, 495 | "id": "f3b053fb", 496 | "metadata": {}, 497 | "outputs": [ 498 | { 499 | "data": { 500 | "text/plain": [ 501 | "'L'" 502 | ] 503 | }, 504 | "execution_count": 39, 505 | "metadata": {}, 506 | "output_type": "execute_result" 507 | } 508 | ], 509 | "source": [ 510 | "complex_logic_one_liner(150)" 511 | ] 512 | }, 513 | { 514 | "cell_type": "code", 515 | "execution_count": 42, 516 | "id": "c1dd1696", 517 | "metadata": {}, 518 | "outputs": [ 519 | { 520 | "data": { 521 | "text/plain": [ 522 | "'M'" 523 | ] 524 | }, 525 | "execution_count": 42, 526 | "metadata": {}, 527 | "output_type": "execute_result" 528 | } 529 | ], 530 | "source": [ 531 | "complex_logic_one_liner(149)" 532 | ] 533 | }, 534 | { 535 | "cell_type": "code", 536 | "execution_count": 44, 537 | "id": "d1ec1f90", 538 | "metadata": {}, 539 | "outputs": [ 540 | { 541 | "name": "stdout", 542 | "output_type": "stream", 543 | "text": [ 544 | "['M', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'M']\n" 545 | ] 546 | } 547 | ], 548 | "source": [ 549 | "sizes = list(map(complex_logic_one_liner, heights))\n", 550 | "print(sizes)" 551 | ] 552 | }, 553 | { 554 | "cell_type": "markdown", 555 | "id": "33966f18", 556 | "metadata": {}, 557 | "source": [ 558 | "### With lambda function" 559 | ] 560 | }, 561 | { 562 | "cell_type": "code", 563 | "execution_count": null, 564 | "id": "8a6bc40b", 565 | "metadata": {}, 566 | "outputs": [], 567 | "source": [ 568 | "sizes = list(map(complex_logic_one_liner, heights))" 569 | ] 570 | }, 571 | { 572 | "cell_type": "code", 573 | "execution_count": 49, 574 | "id": "0bc0dbf6", 575 | "metadata": {}, 576 | "outputs": [], 577 | "source": [ 578 | "sizes = list(map(lambda x: \"M\" if x < 150 else \"L\", heights))" 579 | ] 580 | }, 581 | { 582 | "cell_type": "code", 583 | "execution_count": 50, 584 | "id": "4f613f14", 585 | "metadata": {}, 586 | "outputs": [ 587 | { 588 | "name": "stdout", 589 | "output_type": "stream", 590 | "text": [ 591 | "['M', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'M']\n" 592 | ] 593 | } 594 | ], 595 | "source": [ 596 | "print(sizes)" 597 | ] 598 | }, 599 | { 600 | "cell_type": "code", 601 | "execution_count": 54, 602 | "id": "4d44496e", 603 | "metadata": {}, 604 | "outputs": [], 605 | "source": [ 606 | "## Problem\n", 607 | "\n", 608 | "# Convert given height (assume integers) to t-shirt size\n", 609 | "# h < 150 -> S\n", 610 | "# h >= 150 and h < 180 -> M\n", 611 | "# h > 180 -> L\n", 612 | "heights = [144, 167, 189, 170, 190, 150, 165, 178, 200, 130]" 613 | ] 614 | }, 615 | { 616 | "cell_type": "markdown", 617 | "id": "892d5d3e", 618 | "metadata": {}, 619 | "source": [ 620 | "### SOLVE" 621 | ] 622 | }, 623 | { 624 | "cell_type": "code", 625 | "execution_count": 51, 626 | "id": "c82987da", 627 | "metadata": {}, 628 | "outputs": [], 629 | "source": [ 630 | "def complex_logic(height):\n", 631 | " if height < 150:\n", 632 | " return \"S\"\n", 633 | " elif height >= 150 and height < 180:\n", 634 | " return \"M\"\n", 635 | " else:\n", 636 | " return \"L\"" 637 | ] 638 | }, 639 | { 640 | "cell_type": "code", 641 | "execution_count": 52, 642 | "id": "95167887", 643 | "metadata": {}, 644 | "outputs": [], 645 | "source": [ 646 | "sizes = list(map(complex_logic, heights))" 647 | ] 648 | }, 649 | { 650 | "cell_type": "code", 651 | "execution_count": 53, 652 | "id": "2748897f", 653 | "metadata": {}, 654 | "outputs": [ 655 | { 656 | "name": "stdout", 657 | "output_type": "stream", 658 | "text": [ 659 | "['S', 'M', 'L', 'M', 'L', 'M', 'M', 'M', 'L', 'S']\n" 660 | ] 661 | } 662 | ], 663 | "source": [ 664 | "print(sizes)" 665 | ] 666 | }, 667 | { 668 | "cell_type": "code", 669 | "execution_count": 57, 670 | "id": "fc7d89a8", 671 | "metadata": {}, 672 | "outputs": [], 673 | "source": [ 674 | "sizes2 = list(map(lambda x: \"S\" if x < 150 else \"M\"\n", 675 | " if x >= 150 and x < 180 else \"L\", heights))" 676 | ] 677 | }, 678 | { 679 | "cell_type": "code", 680 | "execution_count": 58, 681 | "id": "30f5ba63", 682 | "metadata": {}, 683 | "outputs": [ 684 | { 685 | "name": "stdout", 686 | "output_type": "stream", 687 | "text": [ 688 | "['S', 'M', 'L', 'M', 'L', 'M', 'M', 'M', 'L', 'S']\n" 689 | ] 690 | } 691 | ], 692 | "source": [ 693 | "print(sizes2)" 694 | ] 695 | }, 696 | { 697 | "cell_type": "markdown", 698 | "id": "7ba114e4", 699 | "metadata": {}, 700 | "source": [ 701 | "### Problem coming up" 702 | ] 703 | }, 704 | { 705 | "cell_type": "code", 706 | "execution_count": 61, 707 | "id": "74e6d00a", 708 | "metadata": {}, 709 | "outputs": [], 710 | "source": [ 711 | "random1 = [1, 2, 3]\n", 712 | "random2 = [4, 5, 6]\n", 713 | "\n", 714 | "# Add the 2 lists using map function\n", 715 | "# res = [5, 7, 9]" 716 | ] 717 | }, 718 | { 719 | "cell_type": "code", 720 | "execution_count": 62, 721 | "id": "15d85bdc", 722 | "metadata": {}, 723 | "outputs": [], 724 | "source": [ 725 | "map?" 726 | ] 727 | }, 728 | { 729 | "cell_type": "code", 730 | "execution_count": 63, 731 | "id": "5d4da8b0", 732 | "metadata": {}, 733 | "outputs": [ 734 | { 735 | "name": "stdout", 736 | "output_type": "stream", 737 | "text": [ 738 | "[5, 7, 9]\n" 739 | ] 740 | } 741 | ], 742 | "source": [ 743 | "res = list(map(lambda x, y: x + y, random1, random2))\n", 744 | "print(res)" 745 | ] 746 | }, 747 | { 748 | "cell_type": "markdown", 749 | "id": "a779218d", 750 | "metadata": {}, 751 | "source": [ 752 | "### Quiz" 753 | ] 754 | }, 755 | { 756 | "cell_type": "code", 757 | "execution_count": 64, 758 | "id": "28696e91", 759 | "metadata": {}, 760 | "outputs": [], 761 | "source": [ 762 | "list1 = [1, 0, 1]\n", 763 | "list2 = [0, 0, 1]\n", 764 | "\n", 765 | "res = list(map(lambda x, y: x == y, list1, list2))" 766 | ] 767 | }, 768 | { 769 | "cell_type": "code", 770 | "execution_count": 65, 771 | "id": "034ebff2", 772 | "metadata": {}, 773 | "outputs": [ 774 | { 775 | "name": "stdout", 776 | "output_type": "stream", 777 | "text": [ 778 | "[False, True, True]\n" 779 | ] 780 | } 781 | ], 782 | "source": [ 783 | "print(res)" 784 | ] 785 | }, 786 | { 787 | "cell_type": "code", 788 | "execution_count": 66, 789 | "id": "697a1306", 790 | "metadata": {}, 791 | "outputs": [], 792 | "source": [ 793 | "list1 = [1, 0, 1, 0]\n", 794 | "list2 = [0, 0, 1]\n", 795 | "\n", 796 | "res = list(map(lambda x, y: x == y, list1, list2))" 797 | ] 798 | }, 799 | { 800 | "cell_type": "code", 801 | "execution_count": 67, 802 | "id": "68e5a5eb", 803 | "metadata": {}, 804 | "outputs": [ 805 | { 806 | "name": "stdout", 807 | "output_type": "stream", 808 | "text": [ 809 | "[False, True, True]\n" 810 | ] 811 | } 812 | ], 813 | "source": [ 814 | "print(res)" 815 | ] 816 | }, 817 | { 818 | "cell_type": "code", 819 | "execution_count": 68, 820 | "id": "f657e5b3", 821 | "metadata": {}, 822 | "outputs": [], 823 | "source": [ 824 | "list1 = [1, 0, 1, 0]\n", 825 | "list2 = [0, 0, 1, 1, 1]\n", 826 | "\n", 827 | "res = list(map(lambda x, y: x == y, list1, list2))" 828 | ] 829 | }, 830 | { 831 | "cell_type": "code", 832 | "execution_count": 69, 833 | "id": "56c4d639", 834 | "metadata": {}, 835 | "outputs": [ 836 | { 837 | "name": "stdout", 838 | "output_type": "stream", 839 | "text": [ 840 | "[False, True, True, False]\n" 841 | ] 842 | } 843 | ], 844 | "source": [ 845 | "print(res)" 846 | ] 847 | }, 848 | { 849 | "cell_type": "markdown", 850 | "id": "5f54e8b7", 851 | "metadata": {}, 852 | "source": [ 853 | "## Filter function" 854 | ] 855 | }, 856 | { 857 | "cell_type": "code", 858 | "execution_count": 70, 859 | "id": "7c336bf0", 860 | "metadata": {}, 861 | "outputs": [], 862 | "source": [ 863 | "a = [1, 2, 3, 4, 5]\n" 864 | ] 865 | }, 866 | { 867 | "cell_type": "code", 868 | "execution_count": 71, 869 | "id": "5f37420a", 870 | "metadata": {}, 871 | "outputs": [], 872 | "source": [ 873 | "filter?" 874 | ] 875 | }, 876 | { 877 | "cell_type": "code", 878 | "execution_count": 72, 879 | "id": "d2a4f4c8", 880 | "metadata": {}, 881 | "outputs": [], 882 | "source": [ 883 | "res = filter(lambda x: x % 2 == 0, a)" 884 | ] 885 | }, 886 | { 887 | "cell_type": "code", 888 | "execution_count": 73, 889 | "id": "4b3ff314", 890 | "metadata": {}, 891 | "outputs": [ 892 | { 893 | "name": "stdout", 894 | "output_type": "stream", 895 | "text": [ 896 | "\n" 897 | ] 898 | } 899 | ], 900 | "source": [ 901 | "print(res)" 902 | ] 903 | }, 904 | { 905 | "cell_type": "code", 906 | "execution_count": 74, 907 | "id": "3918ef80", 908 | "metadata": {}, 909 | "outputs": [], 910 | "source": [ 911 | "res_list = list(res)" 912 | ] 913 | }, 914 | { 915 | "cell_type": "code", 916 | "execution_count": 75, 917 | "id": "0ce2bc57", 918 | "metadata": {}, 919 | "outputs": [ 920 | { 921 | "name": "stdout", 922 | "output_type": "stream", 923 | "text": [ 924 | "[2, 4]\n" 925 | ] 926 | } 927 | ], 928 | "source": [ 929 | "print(res_list)" 930 | ] 931 | }, 932 | { 933 | "cell_type": "code", 934 | "execution_count": 77, 935 | "id": "04329db1", 936 | "metadata": {}, 937 | "outputs": [], 938 | "source": [ 939 | "# single line with typecasting\n", 940 | "f_direct = list(filter(lambda x: x % 2 == 0, a))" 941 | ] 942 | }, 943 | { 944 | "cell_type": "code", 945 | "execution_count": 78, 946 | "id": "a17f7d44", 947 | "metadata": {}, 948 | "outputs": [ 949 | { 950 | "name": "stdout", 951 | "output_type": "stream", 952 | "text": [ 953 | "[2, 4]\n" 954 | ] 955 | } 956 | ], 957 | "source": [ 958 | "print(f_direct)" 959 | ] 960 | }, 961 | { 962 | "cell_type": "markdown", 963 | "id": "2f798ffc", 964 | "metadata": {}, 965 | "source": [ 966 | "### Quizzes" 967 | ] 968 | }, 969 | { 970 | "cell_type": "code", 971 | "execution_count": 80, 972 | "id": "4db553e0", 973 | "metadata": {}, 974 | "outputs": [ 975 | { 976 | "name": "stdout", 977 | "output_type": "stream", 978 | "text": [ 979 | "[1, -3, 4]\n" 980 | ] 981 | } 982 | ], 983 | "source": [ 984 | "l = [1, 10, -3, 4, 15]\n", 985 | "\n", 986 | "def f1(x):\n", 987 | " return x<6\n", 988 | "\n", 989 | "# m1 = ????\n", 990 | "\n", 991 | "m1 = filter(f1, l)\n", 992 | "\n", 993 | "print(list(m1))\n", 994 | "\n", 995 | "# Replace ??? to print all numbers less than 6 in the list." 996 | ] 997 | }, 998 | { 999 | "cell_type": "code", 1000 | "execution_count": 82, 1001 | "id": "9037c730", 1002 | "metadata": {}, 1003 | "outputs": [ 1004 | { 1005 | "ename": "TypeError", 1006 | "evalue": "'function' object is not iterable", 1007 | "output_type": "error", 1008 | "traceback": [ 1009 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 1010 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 1011 | "Input \u001b[0;32mIn [82]\u001b[0m, in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m m1 \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mfilter\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43ml\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mf1\u001b[49m\u001b[43m)\u001b[49m\n", 1012 | "\u001b[0;31mTypeError\u001b[0m: 'function' object is not iterable" 1013 | ] 1014 | } 1015 | ], 1016 | "source": [ 1017 | "m1 = filter(l, f1)\n", 1018 | "# first argument should be the function\n", 1019 | "# second argument should be the iterable" 1020 | ] 1021 | }, 1022 | { 1023 | "cell_type": "code", 1024 | "execution_count": 86, 1025 | "id": "71c70c11", 1026 | "metadata": {}, 1027 | "outputs": [ 1028 | { 1029 | "name": "stdout", 1030 | "output_type": "stream", 1031 | "text": [ 1032 | "{1, 2, 5}\n", 1033 | "[1, 2, 5]\n" 1034 | ] 1035 | } 1036 | ], 1037 | "source": [ 1038 | "s = {1, 2, 2, 5}\n", 1039 | "print(s)\n", 1040 | "\n", 1041 | "def f1(x):\n", 1042 | " return x<6\n", 1043 | "\n", 1044 | "m1 = filter(f1, s)\n", 1045 | "print(list(m1))" 1046 | ] 1047 | }, 1048 | { 1049 | "cell_type": "markdown", 1050 | "id": "11e60403", 1051 | "metadata": {}, 1052 | "source": [ 1053 | "## Reduce" 1054 | ] 1055 | }, 1056 | { 1057 | "cell_type": "code", 1058 | "execution_count": 87, 1059 | "id": "251a9af7", 1060 | "metadata": {}, 1061 | "outputs": [ 1062 | { 1063 | "name": "stdout", 1064 | "output_type": "stream", 1065 | "text": [ 1066 | "Object `reduce` not found.\n" 1067 | ] 1068 | } 1069 | ], 1070 | "source": [ 1071 | "reduce?" 1072 | ] 1073 | }, 1074 | { 1075 | "cell_type": "code", 1076 | "execution_count": 88, 1077 | "id": "3c43762c", 1078 | "metadata": {}, 1079 | "outputs": [], 1080 | "source": [ 1081 | "from functools import reduce\n", 1082 | "\n", 1083 | "# this line will bring the reduce function into our current code" 1084 | ] 1085 | }, 1086 | { 1087 | "cell_type": "code", 1088 | "execution_count": 89, 1089 | "id": "f50f24db", 1090 | "metadata": {}, 1091 | "outputs": [], 1092 | "source": [ 1093 | "reduce?" 1094 | ] 1095 | }, 1096 | { 1097 | "cell_type": "code", 1098 | "execution_count": 90, 1099 | "id": "2e517f95", 1100 | "metadata": {}, 1101 | "outputs": [], 1102 | "source": [ 1103 | "a = [1, 2, 3, 4, 5]" 1104 | ] 1105 | }, 1106 | { 1107 | "cell_type": "code", 1108 | "execution_count": 91, 1109 | "id": "66fe4013", 1110 | "metadata": {}, 1111 | "outputs": [], 1112 | "source": [ 1113 | "# 1st arg: function with 2 arguments\n", 1114 | "# 2nd arg: the iterable\n", 1115 | "res = reduce(lambda x, y: x + y, a)" 1116 | ] 1117 | }, 1118 | { 1119 | "cell_type": "code", 1120 | "execution_count": 92, 1121 | "id": "e82a86f5", 1122 | "metadata": {}, 1123 | "outputs": [ 1124 | { 1125 | "name": "stdout", 1126 | "output_type": "stream", 1127 | "text": [ 1128 | "15\n" 1129 | ] 1130 | } 1131 | ], 1132 | "source": [ 1133 | "print(res)" 1134 | ] 1135 | }, 1136 | { 1137 | "cell_type": "markdown", 1138 | "id": "9e779080", 1139 | "metadata": {}, 1140 | "source": [ 1141 | "## Quizzes" 1142 | ] 1143 | }, 1144 | { 1145 | "cell_type": "code", 1146 | "execution_count": 93, 1147 | "id": "18927616", 1148 | "metadata": {}, 1149 | "outputs": [], 1150 | "source": [ 1151 | "a = list(range(1, 11))" 1152 | ] 1153 | }, 1154 | { 1155 | "cell_type": "code", 1156 | "execution_count": 94, 1157 | "id": "26b62d0b", 1158 | "metadata": {}, 1159 | "outputs": [ 1160 | { 1161 | "name": "stdout", 1162 | "output_type": "stream", 1163 | "text": [ 1164 | "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n" 1165 | ] 1166 | } 1167 | ], 1168 | "source": [ 1169 | "print(a)" 1170 | ] 1171 | }, 1172 | { 1173 | "cell_type": "code", 1174 | "execution_count": 95, 1175 | "id": "a7e052e5", 1176 | "metadata": {}, 1177 | "outputs": [ 1178 | { 1179 | "name": "stdout", 1180 | "output_type": "stream", 1181 | "text": [ 1182 | "55\n" 1183 | ] 1184 | } 1185 | ], 1186 | "source": [ 1187 | "res = reduce(lambda x, y: x + y, a)\n", 1188 | "print(res)" 1189 | ] 1190 | }, 1191 | { 1192 | "cell_type": "code", 1193 | "execution_count": 96, 1194 | "id": "aa736685", 1195 | "metadata": {}, 1196 | "outputs": [ 1197 | { 1198 | "name": "stdout", 1199 | "output_type": "stream", 1200 | "text": [ 1201 | "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]\n" 1202 | ] 1203 | } 1204 | ], 1205 | "source": [ 1206 | "b = a[::-1]\n", 1207 | "print(b)" 1208 | ] 1209 | }, 1210 | { 1211 | "cell_type": "code", 1212 | "execution_count": 98, 1213 | "id": "ae7d86f6", 1214 | "metadata": {}, 1215 | "outputs": [ 1216 | { 1217 | "name": "stdout", 1218 | "output_type": "stream", 1219 | "text": [ 1220 | "55\n" 1221 | ] 1222 | } 1223 | ], 1224 | "source": [ 1225 | "res = reduce(lambda x, y: x + y, b)\n", 1226 | "print(res)" 1227 | ] 1228 | }, 1229 | { 1230 | "cell_type": "markdown", 1231 | "id": "e2e53e73", 1232 | "metadata": {}, 1233 | "source": [ 1234 | "## Tricky Quizzes" 1235 | ] 1236 | }, 1237 | { 1238 | "cell_type": "code", 1239 | "execution_count": 99, 1240 | "id": "e5692ae5", 1241 | "metadata": {}, 1242 | "outputs": [ 1243 | { 1244 | "name": "stdout", 1245 | "output_type": "stream", 1246 | "text": [ 1247 | "{1, 2, 33}\n", 1248 | "36\n" 1249 | ] 1250 | } 1251 | ], 1252 | "source": [ 1253 | "from functools import reduce\n", 1254 | "\n", 1255 | "list = {1, 2, 33, 2}\n", 1256 | "print(list)\n", 1257 | "\n", 1258 | "print(reduce(lambda x, y: x+y, list))" 1259 | ] 1260 | }, 1261 | { 1262 | "cell_type": "code", 1263 | "execution_count": null, 1264 | "id": "857fabdc", 1265 | "metadata": {}, 1266 | "outputs": [], 1267 | "source": [ 1268 | "from functools import reduce\n", 1269 | "\n", 1270 | "list = {1, 2, 33, 2}\n", 1271 | "print(list)\n", 1272 | "\n", 1273 | "print(reduce(lambda x, y: x+y, list))" 1274 | ] 1275 | }, 1276 | { 1277 | "cell_type": "code", 1278 | "execution_count": 100, 1279 | "id": "919ecd3b", 1280 | "metadata": {}, 1281 | "outputs": [], 1282 | "source": [ 1283 | "reduce?" 1284 | ] 1285 | }, 1286 | { 1287 | "cell_type": "code", 1288 | "execution_count": 101, 1289 | "id": "ed2c447d", 1290 | "metadata": {}, 1291 | "outputs": [], 1292 | "source": [ 1293 | "# initial value => might be useful when carry over from some\n", 1294 | "# previous operations" 1295 | ] 1296 | }, 1297 | { 1298 | "cell_type": "code", 1299 | "execution_count": 102, 1300 | "id": "a6e94c20", 1301 | "metadata": {}, 1302 | "outputs": [ 1303 | { 1304 | "name": "stdout", 1305 | "output_type": "stream", 1306 | "text": [ 1307 | "41\n" 1308 | ] 1309 | } 1310 | ], 1311 | "source": [ 1312 | "print(reduce(lambda x, y: x+y, list, 5))" 1313 | ] 1314 | }, 1315 | { 1316 | "cell_type": "code", 1317 | "execution_count": 103, 1318 | "id": "f050b724", 1319 | "metadata": {}, 1320 | "outputs": [ 1321 | { 1322 | "name": "stdout", 1323 | "output_type": "stream", 1324 | "text": [ 1325 | "48\n" 1326 | ] 1327 | } 1328 | ], 1329 | "source": [ 1330 | "from functools import reduce\n", 1331 | "list = [1, 2, 33, 2]\n", 1332 | "print(reduce(lambda x, y: x+y, list, 10))" 1333 | ] 1334 | }, 1335 | { 1336 | "cell_type": "code", 1337 | "execution_count": 104, 1338 | "id": "a2c37c6c", 1339 | "metadata": {}, 1340 | "outputs": [ 1341 | { 1342 | "name": "stdout", 1343 | "output_type": "stream", 1344 | "text": [ 1345 | "{1, 2, 33}\n", 1346 | "37\n" 1347 | ] 1348 | } 1349 | ], 1350 | "source": [ 1351 | "from functools import reduce\n", 1352 | "\n", 1353 | "list = {1, 2, 33, 2}\n", 1354 | "print(list)\n", 1355 | "\n", 1356 | "print(reduce(lambda x, y: x+y, list, 1))" 1357 | ] 1358 | }, 1359 | { 1360 | "cell_type": "markdown", 1361 | "id": "ff08daf3", 1362 | "metadata": {}, 1363 | "source": [ 1364 | "## zip function" 1365 | ] 1366 | }, 1367 | { 1368 | "cell_type": "markdown", 1369 | "id": "e69c0ee0", 1370 | "metadata": {}, 1371 | "source": [ 1372 | "will be covered again in next class problem solving beginning" 1373 | ] 1374 | }, 1375 | { 1376 | "cell_type": "code", 1377 | "execution_count": 106, 1378 | "id": "0ab10fab", 1379 | "metadata": {}, 1380 | "outputs": [], 1381 | "source": [ 1382 | "a = [\"sachin\", \"shreyash\", \"needa\", \"harshita\", \"anwesh\"]" 1383 | ] 1384 | }, 1385 | { 1386 | "cell_type": "code", 1387 | "execution_count": 107, 1388 | "id": "66b55661", 1389 | "metadata": {}, 1390 | "outputs": [ 1391 | { 1392 | "name": "stdout", 1393 | "output_type": "stream", 1394 | "text": [ 1395 | "(0, 'sachin')\n", 1396 | "(1, 'shreyash')\n", 1397 | "(2, 'needa')\n", 1398 | "(3, 'harshita')\n", 1399 | "(4, 'anwesh')\n" 1400 | ] 1401 | } 1402 | ], 1403 | "source": [ 1404 | "for i in enumerate(a):\n", 1405 | " print(i)" 1406 | ] 1407 | }, 1408 | { 1409 | "cell_type": "code", 1410 | "execution_count": 1, 1411 | "id": "5ecc9d2b", 1412 | "metadata": {}, 1413 | "outputs": [], 1414 | "source": [ 1415 | "states = ['Haryana', 'Uttarakhand', 'Punjab', 'Uttar Pradesh']\n", 1416 | "capitals = ['Chandigarh', 'Dehradun', 'Chandigarh', 'Lucknow']\n", 1417 | "\n", 1418 | "res = zip(states, capitals)" 1419 | ] 1420 | }, 1421 | { 1422 | "cell_type": "code", 1423 | "execution_count": 2, 1424 | "id": "bb8d6096", 1425 | "metadata": {}, 1426 | "outputs": [ 1427 | { 1428 | "name": "stdout", 1429 | "output_type": "stream", 1430 | "text": [ 1431 | "\n" 1432 | ] 1433 | } 1434 | ], 1435 | "source": [ 1436 | "print(res)" 1437 | ] 1438 | }, 1439 | { 1440 | "cell_type": "code", 1441 | "execution_count": 3, 1442 | "id": "10aeaadd", 1443 | "metadata": {}, 1444 | "outputs": [], 1445 | "source": [ 1446 | "res_list = list(res)" 1447 | ] 1448 | }, 1449 | { 1450 | "cell_type": "code", 1451 | "execution_count": 4, 1452 | "id": "7d811a26", 1453 | "metadata": {}, 1454 | "outputs": [ 1455 | { 1456 | "name": "stdout", 1457 | "output_type": "stream", 1458 | "text": [ 1459 | "[('Haryana', 'Chandigarh'), ('Uttarakhand', 'Dehradun'), ('Punjab', 'Chandigarh'), ('Uttar Pradesh', 'Lucknow')]\n" 1460 | ] 1461 | } 1462 | ], 1463 | "source": [ 1464 | "print(res_list)" 1465 | ] 1466 | }, 1467 | { 1468 | "cell_type": "markdown", 1469 | "id": "7a004c01", 1470 | "metadata": {}, 1471 | "source": [ 1472 | "## Iterate on list of tuples" 1473 | ] 1474 | }, 1475 | { 1476 | "cell_type": "code", 1477 | "execution_count": 5, 1478 | "id": "a75e9491", 1479 | "metadata": {}, 1480 | "outputs": [ 1481 | { 1482 | "name": "stdout", 1483 | "output_type": "stream", 1484 | "text": [ 1485 | "('Haryana', 'Chandigarh')\n", 1486 | "('Uttarakhand', 'Dehradun')\n", 1487 | "('Punjab', 'Chandigarh')\n", 1488 | "('Uttar Pradesh', 'Lucknow')\n" 1489 | ] 1490 | } 1491 | ], 1492 | "source": [ 1493 | "res = list(zip(states, capitals))\n", 1494 | "\n", 1495 | "for state_capital_pair in res:\n", 1496 | " print(state_capital_pair)" 1497 | ] 1498 | }, 1499 | { 1500 | "cell_type": "code", 1501 | "execution_count": 6, 1502 | "id": "06447887", 1503 | "metadata": {}, 1504 | "outputs": [ 1505 | { 1506 | "name": "stdout", 1507 | "output_type": "stream", 1508 | "text": [ 1509 | "{'Haryana': 'Chandigarh', 'Uttarakhand': 'Dehradun', 'Punjab': 'Chandigarh', 'Uttar Pradesh': 'Lucknow'}\n" 1510 | ] 1511 | } 1512 | ], 1513 | "source": [ 1514 | "res = dict(zip(states, capitals))\n", 1515 | "\n", 1516 | "print(res)" 1517 | ] 1518 | }, 1519 | { 1520 | "cell_type": "code", 1521 | "execution_count": null, 1522 | "id": "f79c2495", 1523 | "metadata": {}, 1524 | "outputs": [], 1525 | "source": [] 1526 | } 1527 | ], 1528 | "metadata": { 1529 | "kernelspec": { 1530 | "display_name": "Python 3 (ipykernel)", 1531 | "language": "python", 1532 | "name": "python3" 1533 | }, 1534 | "language_info": { 1535 | "codemirror_mode": { 1536 | "name": "ipython", 1537 | "version": 3 1538 | }, 1539 | "file_extension": ".py", 1540 | "mimetype": "text/x-python", 1541 | "name": "python", 1542 | "nbconvert_exporter": "python", 1543 | "pygments_lexer": "ipython3", 1544 | "version": "3.9.12" 1545 | } 1546 | }, 1547 | "nbformat": 4, 1548 | "nbformat_minor": 5 1549 | } 1550 | -------------------------------------------------------------------------------- /Modules.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "212c39f4", 6 | "metadata": {}, 7 | "source": [ 8 | "# Modules" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 1, 14 | "id": "52d91124", 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "import math" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 2, 24 | "id": "0bf1a71f", 25 | "metadata": {}, 26 | "outputs": [], 27 | "source": [ 28 | "from functools import reduce" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 3, 34 | "id": "3617341e", 35 | "metadata": {}, 36 | "outputs": [ 37 | { 38 | "data": { 39 | "text/plain": [ 40 | "9" 41 | ] 42 | }, 43 | "execution_count": 3, 44 | "metadata": {}, 45 | "output_type": "execute_result" 46 | } 47 | ], 48 | "source": [ 49 | "math.floor(9.6)" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 15, 55 | "id": "75a7ebac", 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "data": { 60 | "text/plain": [ 61 | "True" 62 | ] 63 | }, 64 | "execution_count": 15, 65 | "metadata": {}, 66 | "output_type": "execute_result" 67 | } 68 | ], 69 | "source": [ 70 | "isinstance(math, object) # math is a module technically\n", 71 | "# in Python everything is an object" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 6, 77 | "id": "8ea7c0de", 78 | "metadata": {}, 79 | "outputs": [ 80 | { 81 | "data": { 82 | "text/plain": [ 83 | "3.1622776601683795" 84 | ] 85 | }, 86 | "execution_count": 6, 87 | "metadata": {}, 88 | "output_type": "execute_result" 89 | } 90 | ], 91 | "source": [ 92 | "math.sqrt(10)" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 8, 98 | "id": "3ffd5101", 99 | "metadata": {}, 100 | "outputs": [], 101 | "source": [ 102 | "# methods" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 9, 108 | "id": "707c6ed4", 109 | "metadata": {}, 110 | "outputs": [ 111 | { 112 | "data": { 113 | "text/plain": [ 114 | "['__doc__',\n", 115 | " '__file__',\n", 116 | " '__loader__',\n", 117 | " '__name__',\n", 118 | " '__package__',\n", 119 | " '__spec__',\n", 120 | " 'acos',\n", 121 | " 'acosh',\n", 122 | " 'asin',\n", 123 | " 'asinh',\n", 124 | " 'atan',\n", 125 | " 'atan2',\n", 126 | " 'atanh',\n", 127 | " 'ceil',\n", 128 | " 'comb',\n", 129 | " 'copysign',\n", 130 | " 'cos',\n", 131 | " 'cosh',\n", 132 | " 'degrees',\n", 133 | " 'dist',\n", 134 | " 'e',\n", 135 | " 'erf',\n", 136 | " 'erfc',\n", 137 | " 'exp',\n", 138 | " 'expm1',\n", 139 | " 'fabs',\n", 140 | " 'factorial',\n", 141 | " 'floor',\n", 142 | " 'fmod',\n", 143 | " 'frexp',\n", 144 | " 'fsum',\n", 145 | " 'gamma',\n", 146 | " 'gcd',\n", 147 | " 'hypot',\n", 148 | " 'inf',\n", 149 | " 'isclose',\n", 150 | " 'isfinite',\n", 151 | " 'isinf',\n", 152 | " 'isnan',\n", 153 | " 'isqrt',\n", 154 | " 'lcm',\n", 155 | " 'ldexp',\n", 156 | " 'lgamma',\n", 157 | " 'log',\n", 158 | " 'log10',\n", 159 | " 'log1p',\n", 160 | " 'log2',\n", 161 | " 'modf',\n", 162 | " 'nan',\n", 163 | " 'nextafter',\n", 164 | " 'perm',\n", 165 | " 'pi',\n", 166 | " 'pow',\n", 167 | " 'prod',\n", 168 | " 'radians',\n", 169 | " 'remainder',\n", 170 | " 'sin',\n", 171 | " 'sinh',\n", 172 | " 'sqrt',\n", 173 | " 'tan',\n", 174 | " 'tanh',\n", 175 | " 'tau',\n", 176 | " 'trunc',\n", 177 | " 'ulp']" 178 | ] 179 | }, 180 | "execution_count": 9, 181 | "metadata": {}, 182 | "output_type": "execute_result" 183 | } 184 | ], 185 | "source": [ 186 | "dir(math)" 187 | ] 188 | }, 189 | { 190 | "cell_type": "code", 191 | "execution_count": 10, 192 | "id": "6aff9397", 193 | "metadata": {}, 194 | "outputs": [], 195 | "source": [ 196 | "import random" 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": 14, 202 | "id": "47ba72b2", 203 | "metadata": {}, 204 | "outputs": [ 205 | { 206 | "data": { 207 | "text/plain": [ 208 | "53" 209 | ] 210 | }, 211 | "execution_count": 14, 212 | "metadata": {}, 213 | "output_type": "execute_result" 214 | } 215 | ], 216 | "source": [ 217 | "random.randint(0, 100)" 218 | ] 219 | }, 220 | { 221 | "cell_type": "markdown", 222 | "id": "a7450a8b", 223 | "metadata": {}, 224 | "source": [ 225 | "## Ways to import" 226 | ] 227 | }, 228 | { 229 | "cell_type": "markdown", 230 | "id": "efbde9f3", 231 | "metadata": {}, 232 | "source": [ 233 | "### With the alias" 234 | ] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "execution_count": 17, 239 | "id": "5f28c74c", 240 | "metadata": {}, 241 | "outputs": [], 242 | "source": [ 243 | "import math as m # an alias" 244 | ] 245 | }, 246 | { 247 | "cell_type": "code", 248 | "execution_count": 18, 249 | "id": "d765d2dc", 250 | "metadata": {}, 251 | "outputs": [ 252 | { 253 | "data": { 254 | "text/plain": [ 255 | "3.1622776601683795" 256 | ] 257 | }, 258 | "execution_count": 18, 259 | "metadata": {}, 260 | "output_type": "execute_result" 261 | } 262 | ], 263 | "source": [ 264 | "m.sqrt(10)" 265 | ] 266 | }, 267 | { 268 | "cell_type": "code", 269 | "execution_count": 25, 270 | "id": "9d8d91e5", 271 | "metadata": {}, 272 | "outputs": [ 273 | { 274 | "data": { 275 | "text/plain": [ 276 | "104" 277 | ] 278 | }, 279 | "execution_count": 25, 280 | "metadata": {}, 281 | "output_type": "execute_result" 282 | } 283 | ], 284 | "source": [ 285 | "m.floor(104.4) # it is a method" 286 | ] 287 | }, 288 | { 289 | "cell_type": "code", 290 | "execution_count": 21, 291 | "id": "30a65e84", 292 | "metadata": {}, 293 | "outputs": [], 294 | "source": [ 295 | "import numpy as np\n", 296 | "import pandas as np" 297 | ] 298 | }, 299 | { 300 | "cell_type": "markdown", 301 | "id": "70ce8b0f", 302 | "metadata": {}, 303 | "source": [ 304 | "### A specific method" 305 | ] 306 | }, 307 | { 308 | "cell_type": "code", 309 | "execution_count": 22, 310 | "id": "4695d60a", 311 | "metadata": {}, 312 | "outputs": [], 313 | "source": [ 314 | "from math import sqrt" 315 | ] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "execution_count": 27, 320 | "id": "5811ff54", 321 | "metadata": {}, 322 | "outputs": [ 323 | { 324 | "data": { 325 | "text/plain": [ 326 | "3.1622776601683795" 327 | ] 328 | }, 329 | "execution_count": 27, 330 | "metadata": {}, 331 | "output_type": "execute_result" 332 | } 333 | ], 334 | "source": [ 335 | "sqrt(10) # using the function" 336 | ] 337 | }, 338 | { 339 | "cell_type": "code", 340 | "execution_count": 26, 341 | "id": "b801c911", 342 | "metadata": {}, 343 | "outputs": [], 344 | "source": [ 345 | "from math import sqrt, floor" 346 | ] 347 | }, 348 | { 349 | "cell_type": "code", 350 | "execution_count": 28, 351 | "id": "caee7433", 352 | "metadata": {}, 353 | "outputs": [ 354 | { 355 | "data": { 356 | "text/plain": [ 357 | "9" 358 | ] 359 | }, 360 | "execution_count": 28, 361 | "metadata": {}, 362 | "output_type": "execute_result" 363 | } 364 | ], 365 | "source": [ 366 | "floor(9.3)" 367 | ] 368 | }, 369 | { 370 | "cell_type": "code", 371 | "execution_count": 29, 372 | "id": "1ea497ac", 373 | "metadata": {}, 374 | "outputs": [ 375 | { 376 | "ename": "NameError", 377 | "evalue": "name 'ceil' is not defined", 378 | "output_type": "error", 379 | "traceback": [ 380 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 381 | "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", 382 | "Input \u001b[0;32mIn [29]\u001b[0m, in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mceil\u001b[49m(\u001b[38;5;241m2\u001b[39m)\n", 383 | "\u001b[0;31mNameError\u001b[0m: name 'ceil' is not defined" 384 | ] 385 | } 386 | ], 387 | "source": [ 388 | "ceil(2)" 389 | ] 390 | }, 391 | { 392 | "cell_type": "code", 393 | "execution_count": 30, 394 | "id": "b12321f5", 395 | "metadata": {}, 396 | "outputs": [], 397 | "source": [ 398 | "from math import ceil" 399 | ] 400 | }, 401 | { 402 | "cell_type": "code", 403 | "execution_count": 31, 404 | "id": "d94394bf", 405 | "metadata": {}, 406 | "outputs": [ 407 | { 408 | "data": { 409 | "text/plain": [ 410 | "3" 411 | ] 412 | }, 413 | "execution_count": 31, 414 | "metadata": {}, 415 | "output_type": "execute_result" 416 | } 417 | ], 418 | "source": [ 419 | "ceil(2.5)" 420 | ] 421 | }, 422 | { 423 | "cell_type": "markdown", 424 | "id": "33b0ae0d", 425 | "metadata": {}, 426 | "source": [ 427 | "### Quiz" 428 | ] 429 | }, 430 | { 431 | "cell_type": "code", 432 | "execution_count": 33, 433 | "id": "070553bc", 434 | "metadata": {}, 435 | "outputs": [], 436 | "source": [ 437 | "import random" 438 | ] 439 | }, 440 | { 441 | "cell_type": "code", 442 | "execution_count": 34, 443 | "id": "e3afb81f", 444 | "metadata": {}, 445 | "outputs": [ 446 | { 447 | "ename": "NameError", 448 | "evalue": "name 'randint' is not defined", 449 | "output_type": "error", 450 | "traceback": [ 451 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 452 | "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", 453 | "Input \u001b[0;32mIn [34]\u001b[0m, in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mrandint\u001b[49m(\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m)\n", 454 | "\u001b[0;31mNameError\u001b[0m: name 'randint' is not defined" 455 | ] 456 | } 457 | ], 458 | "source": [ 459 | "randint(1, 2)" 460 | ] 461 | }, 462 | { 463 | "cell_type": "code", 464 | "execution_count": 36, 465 | "id": "55792d2e", 466 | "metadata": {}, 467 | "outputs": [ 468 | { 469 | "data": { 470 | "text/plain": [ 471 | "25" 472 | ] 473 | }, 474 | "execution_count": 36, 475 | "metadata": {}, 476 | "output_type": "execute_result" 477 | } 478 | ], 479 | "source": [ 480 | "random.randint(5, 30)" 481 | ] 482 | }, 483 | { 484 | "cell_type": "code", 485 | "execution_count": 37, 486 | "id": "b93231aa", 487 | "metadata": {}, 488 | "outputs": [], 489 | "source": [ 490 | "def randint(a, b):\n", 491 | " return (a + b)//2" 492 | ] 493 | }, 494 | { 495 | "cell_type": "code", 496 | "execution_count": 38, 497 | "id": "b8d4ee87", 498 | "metadata": {}, 499 | "outputs": [ 500 | { 501 | "data": { 502 | "text/plain": [ 503 | "17" 504 | ] 505 | }, 506 | "execution_count": 38, 507 | "metadata": {}, 508 | "output_type": "execute_result" 509 | } 510 | ], 511 | "source": [ 512 | "randint(5, 30)" 513 | ] 514 | }, 515 | { 516 | "cell_type": "code", 517 | "execution_count": null, 518 | "id": "43349f9f", 519 | "metadata": {}, 520 | "outputs": [], 521 | "source": [] 522 | }, 523 | { 524 | "cell_type": "markdown", 525 | "id": "54eaa8c3", 526 | "metadata": {}, 527 | "source": [ 528 | "### Import all" 529 | ] 530 | }, 531 | { 532 | "cell_type": "code", 533 | "execution_count": 42, 534 | "id": "e4e747eb", 535 | "metadata": {}, 536 | "outputs": [], 537 | "source": [ 538 | "from math import * # to use them you have to say\n", 539 | "# fun(..)" 540 | ] 541 | }, 542 | { 543 | "cell_type": "code", 544 | "execution_count": 43, 545 | "id": "d856f5ae", 546 | "metadata": {}, 547 | "outputs": [], 548 | "source": [ 549 | "import math # to use them to you have to say\n", 550 | "# math.fun(..)" 551 | ] 552 | }, 553 | { 554 | "cell_type": "code", 555 | "execution_count": null, 556 | "id": "857e719a", 557 | "metadata": {}, 558 | "outputs": [], 559 | "source": [] 560 | }, 561 | { 562 | "cell_type": "markdown", 563 | "id": "d7475a34", 564 | "metadata": {}, 565 | "source": [ 566 | "## Homeworks" 567 | ] 568 | }, 569 | { 570 | "cell_type": "markdown", 571 | "id": "71755386", 572 | "metadata": {}, 573 | "source": [ 574 | "Write a program that calculates an adult's fat-burning heart rate, which is 70% of the difference between 220 and the person's age respectively. Complete fat_burning_heart_rate() to calculate the fat burning heart rate.\n", 575 | "\n", 576 | "The adult's age must be between the ages of 18 and 75 inclusive. If the age entered is not in this range, raise a ValueError exception in get_age() with the message \"Invalid age.\" Handle the exception in __main__ and print the ValueError message along with \"Could not calculate heart rate info.\"\n", 577 | "\n" 578 | ] 579 | }, 580 | { 581 | "cell_type": "markdown", 582 | "id": "100f0e39", 583 | "metadata": {}, 584 | "source": [ 585 | "Ex: If the input is:\n", 586 | "\n", 587 | "35\n", 588 | "the output is:\n", 589 | "\n", 590 | "Fat burning heart rate for a 35 year-old: 129.5 bpm" 591 | ] 592 | }, 593 | { 594 | "cell_type": "markdown", 595 | "id": "82a18ca4", 596 | "metadata": {}, 597 | "source": [ 598 | "If the input is:\n", 599 | "\n", 600 | "17\n", 601 | "the output is:\n", 602 | "\n", 603 | "Invalid age.\n", 604 | "Could not calculate heart rate info." 605 | ] 606 | }, 607 | { 608 | "cell_type": "code", 609 | "execution_count": null, 610 | "id": "74a44edc", 611 | "metadata": {}, 612 | "outputs": [], 613 | "source": [] 614 | }, 615 | { 616 | "cell_type": "code", 617 | "execution_count": null, 618 | "id": "dfb9e4ba", 619 | "metadata": {}, 620 | "outputs": [], 621 | "source": [] 622 | }, 623 | { 624 | "cell_type": "code", 625 | "execution_count": null, 626 | "id": "cc3bd80a", 627 | "metadata": {}, 628 | "outputs": [], 629 | "source": [] 630 | }, 631 | { 632 | "cell_type": "code", 633 | "execution_count": null, 634 | "id": "64b72350", 635 | "metadata": {}, 636 | "outputs": [], 637 | "source": [] 638 | }, 639 | { 640 | "cell_type": "code", 641 | "execution_count": null, 642 | "id": "100b25c3", 643 | "metadata": {}, 644 | "outputs": [], 645 | "source": [] 646 | }, 647 | { 648 | "cell_type": "code", 649 | "execution_count": null, 650 | "id": "2bf9e00f", 651 | "metadata": {}, 652 | "outputs": [], 653 | "source": [] 654 | }, 655 | { 656 | "cell_type": "markdown", 657 | "id": "d3fdef60", 658 | "metadata": {}, 659 | "source": [ 660 | "## Module HW" 661 | ] 662 | }, 663 | { 664 | "cell_type": "markdown", 665 | "id": "20aa967c", 666 | "metadata": {}, 667 | "source": [ 668 | "Define the Artist class in Artist.py with a constructor to initialize an artist's information. The constructor should by default initialize the artist's name to \"unknown\" and the years of birth and death to -1.\n", 669 | "\n", 670 | "Define the Artwork class in Artwork.py with a constructor to initialize an artwork's information. The constructor should by default initialize the title to \"unknown\", the year created to -1, and the artist to use the Artist default constructor parameter values. Add an import statement to import the Artist class.\n", 671 | "\n", 672 | "Add import statements to main.py to import the Artist and Artwork classes." 673 | ] 674 | }, 675 | { 676 | "cell_type": "markdown", 677 | "id": "309588d9", 678 | "metadata": {}, 679 | "source": [ 680 | "Ex: If the input is:\n", 681 | "\n", 682 | "Pablo Picasso\n", 683 | "1881\n", 684 | "1973\n", 685 | "Three Musicians\n", 686 | "1921" 687 | ] 688 | }, 689 | { 690 | "cell_type": "markdown", 691 | "id": "210eef4c", 692 | "metadata": {}, 693 | "source": [ 694 | "the output is:\n", 695 | "\n", 696 | "Artist: Pablo Picasso (1881 to 1973)\n", 697 | "Title: Three Musicians, 1921" 698 | ] 699 | }, 700 | { 701 | "cell_type": "markdown", 702 | "id": "6c49bd80", 703 | "metadata": {}, 704 | "source": [ 705 | "Ex: If the input is:\n", 706 | "\n", 707 | "Brice Marden\n", 708 | "1938\n", 709 | "-1\n", 710 | "Distant Muses\n", 711 | "2000" 712 | ] 713 | }, 714 | { 715 | "cell_type": "markdown", 716 | "id": "c1dc3323", 717 | "metadata": {}, 718 | "source": [ 719 | "the output is:\n", 720 | "\n", 721 | "Artist: Brice Marden (1938 to present)\n", 722 | "Title: Distant Muses, 2000" 723 | ] 724 | }, 725 | { 726 | "cell_type": "markdown", 727 | "id": "9d439027", 728 | "metadata": {}, 729 | "source": [ 730 | "Ex: If the input is:\n", 731 | "\n", 732 | "Banksy\n", 733 | "-1\n", 734 | "-1\n", 735 | "Balloon Girl\n", 736 | "2002" 737 | ] 738 | }, 739 | { 740 | "cell_type": "markdown", 741 | "id": "ef70edd7", 742 | "metadata": {}, 743 | "source": [ 744 | "the output is:\n", 745 | "\n", 746 | "Artist: Banksy (unknown)\n", 747 | "Title: Balloon Girl, 2002" 748 | ] 749 | }, 750 | { 751 | "cell_type": "code", 752 | "execution_count": null, 753 | "id": "a54fa0ee", 754 | "metadata": {}, 755 | "outputs": [], 756 | "source": [ 757 | "# main.py\n", 758 | "\n", 759 | "# TODO: Import Artist from Artist.py and Artwork from Artwork.py\n", 760 | "\n", 761 | "if __name__ == \"__main__\":\n", 762 | " user_artist_name = input()\n", 763 | " user_birth_year = int(input())\n", 764 | " user_death_year = int(input())\n", 765 | " user_title = input()\n", 766 | " user_year_created = int(input())\n", 767 | "\n", 768 | " user_artist = Artist(user_artist_name, user_birth_year, user_death_year)\n", 769 | "\n", 770 | " new_artwork = Artwork(user_title, user_year_created, user_artist)\n", 771 | " \n", 772 | " new_artwork.print_info()" 773 | ] 774 | }, 775 | { 776 | "cell_type": "code", 777 | "execution_count": 45, 778 | "id": "2ce7fd0c", 779 | "metadata": {}, 780 | "outputs": [], 781 | "source": [ 782 | "# Artist.py\n", 783 | "\n", 784 | "class Artist:\n", 785 | " # TODO: Define constructor with parameters to initialize instance attributes\n", 786 | " # (name, birth_year, death_year)\n", 787 | "\n", 788 | " def print_info(self):\n", 789 | " if self.birth_year >= 0 and self.death_year >= 0:\n", 790 | " print(f'Artist: {self.name} ({self.birth_year} to {self.death_year})')\n", 791 | " elif self.birth_year >= 0: \n", 792 | " print(f'Artist: {self.name}, ({self.birth_year} - present)')\n", 793 | " else:\n", 794 | " print(f'Artist: {self.name} (unkown)')\n", 795 | " " 796 | ] 797 | }, 798 | { 799 | "cell_type": "code", 800 | "execution_count": 47, 801 | "id": "f8703450", 802 | "metadata": {}, 803 | "outputs": [], 804 | "source": [ 805 | "# Artwork.py\n", 806 | "\n", 807 | "# TODO: Import Artist from Artist.py\n", 808 | "\n", 809 | "class Artwork:\n", 810 | " # TODO: Define constructor with parameters to initialize instance attributes\n", 811 | " # (title, year_created, artist)\n", 812 | "\n", 813 | " def print_info(self):\n", 814 | " self.artist.print_info()\n", 815 | " print(f'Title: {self.title}, {self.year_created}')" 816 | ] 817 | }, 818 | { 819 | "cell_type": "code", 820 | "execution_count": null, 821 | "id": "84b613cc", 822 | "metadata": {}, 823 | "outputs": [], 824 | "source": [] 825 | } 826 | ], 827 | "metadata": { 828 | "kernelspec": { 829 | "display_name": "Python 3 (ipykernel)", 830 | "language": "python", 831 | "name": "python3" 832 | }, 833 | "language_info": { 834 | "codemirror_mode": { 835 | "name": "ipython", 836 | "version": 3 837 | }, 838 | "file_extension": ".py", 839 | "mimetype": "text/x-python", 840 | "name": "python", 841 | "nbconvert_exporter": "python", 842 | "pygments_lexer": "ipython3", 843 | "version": "3.9.12" 844 | } 845 | }, 846 | "nbformat": 4, 847 | "nbformat_minor": 5 848 | } 849 | -------------------------------------------------------------------------------- /Modules/A.py: -------------------------------------------------------------------------------- 1 | def add(a, b): 2 | return a + b 3 | 4 | # is this module something you are running? 5 | # NO, you are importing it 6 | 7 | # __name__ == 'A' 8 | 9 | if __name__ == '__main__': 10 | print(f'__name__ in A.py {__name__}') 11 | -------------------------------------------------------------------------------- /Modules/B.py: -------------------------------------------------------------------------------- 1 | import A 2 | 3 | # is this file something you are running? 4 | # YES 5 | 6 | # __name__ == '__main__' 7 | 8 | if __name__ == '__main__': 9 | print(A.add(5, 6)) 10 | print(f'__name__ in B.py {__name__}') 11 | -------------------------------------------------------------------------------- /Modules/arithmetic.py: -------------------------------------------------------------------------------- 1 | def calculate(number): 2 | print('Function called from arithmetic.py') 3 | return number + 2 -------------------------------------------------------------------------------- /Modules/importer.py: -------------------------------------------------------------------------------- 1 | # how to use the package? 2 | 3 | import my_package 4 | 5 | print(dir(my_package)) 6 | 7 | print(my_package.file1.add(5, 6)) 8 | print(my_package.file2.multiply(6, 7)) -------------------------------------------------------------------------------- /Modules/main.py: -------------------------------------------------------------------------------- 1 | import arithmetic 2 | 3 | def calculate(number): 4 | print('Function called from main.py') 5 | return number - 2 6 | 7 | print(calculate(1)) 8 | print(arithmetic.calculate(1)) -------------------------------------------------------------------------------- /Modules/my_package/__init__.py: -------------------------------------------------------------------------------- 1 | print('The package is initialised!') 2 | 3 | # package is a collection of modules 4 | import my_package.file1 5 | import my_package.file2 -------------------------------------------------------------------------------- /Modules/my_package/file1.py: -------------------------------------------------------------------------------- 1 | 2 | def add(x, y): 3 | return x + y 4 | -------------------------------------------------------------------------------- /Modules/my_package/file2.py: -------------------------------------------------------------------------------- 1 | 2 | def multiply(x, y): 3 | return x * y -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DSML-Intermediate-Module-Aug-22 (DSML June22 Beginner Batch) 2 | 3 | This repository contains all the lecture material explained during the sessions. 4 | 5 | ## PDF Notes 6 | [Drive Link](https://drive.google.com/drive/folders/1gcUTPb8BNqtdQglnfCDFU5W2q57vv8NG?usp=sharing) 7 | 8 | ## Topic Wise Codes 9 | | Session | Title | Jupyter Notebook | 10 | |---------|-------|-------------| 11 | | 1. | Python Refresher - 1 | [Refresher_1](Python_Refresher_1.ipynb) | 12 | | 2. | Python Refresher - 2 | [Refresher_2](Python_Refresher_2.ipynb) | 13 | | 3. | Python Refresher - 3 | [Refresher_3](Python_Refresher_3.ipynb) | 14 | | 4. | Python Refresher - 4 | [Refresher_4](Python_Refresher_4.ipynb) | 15 | | 5. | Searching | [Searching](Searching.ipynb) | 16 | | 6. | Sorting & Time Complexity - 1 | [Sorting_Time_Complexity_1](Sorting_1.ipynb) | 17 | | 7. | Sorting & Time Complexity - 3 | [Sorting_Time_Complexity_3](Sorting_3.ipynb) | 18 | | 8. | Object Oriented Programming - 1 | [OOP_101](OOP_101.ipynb) | 19 | | 9. | Object Oriented Programming - 2 | [OOP 2](OOPS_2.ipynb) | 20 | | 10. | Functional Programming - 1 | [FP 1](Functional_Programming.ipynb) | 21 | | 11. | Functional Programming - 2 | [FP 2](Functional_Programming_2.ipynb) | 22 | | 12. | Problem Solving - 3 | [Problem Solving](Problem_Solving_3.ipynb) | 23 | | 13. | File Handling | [File Handling](File_Handling/file_handling.ipynb) | 24 | | 14. | Exception Handling | [Exception Handling](Exceptions.ipynb) | 25 | | 14. | Modules | [Modules](Modules.ipynb) | 26 | | 14. | Packages | [Packages](Modules/importer.py) | 27 | | 15. | Calculus Basics | [Calculus](Calculus_Basics.ipynb) | 28 | 29 | Rest 7-8 session codes were shared directly in recordings via IDE links pinned in the chat. 30 | 31 | ## Extra Reference 32 | 33 | - [Python Refresher References](https://docs.google.com/document/d/1NnDoHl0IYMnPuNMuzftMnXlGd3xVtdOwHknhFS5Cuz4/edit?usp=sharing) 34 | 35 | - [Sorting and Time Complexity References](https://docs.google.com/document/d/1f4kYFl_-9EBWSTVVX3awS_d5saAdhfdVi6koPnyWJOI/edit?usp=sharing) 36 | 37 | Rest reference materials were directly added in Extra Reference section in the dashboard. -------------------------------------------------------------------------------- /Searching.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "id": "13c7e4e2", 7 | "metadata": {}, 8 | "outputs": [ 9 | { 10 | "name": "stdout", 11 | "output_type": "stream", 12 | "text": [ 13 | "7 7 7 7 7 7 7 \n", 14 | "7 6 6 6 6 6 7 \n", 15 | "7 6 5 5 5 6 7 \n", 16 | "7 6 5 4 5 6 7 \n", 17 | "7 6 5 5 5 6 7 \n", 18 | "7 6 6 6 6 6 7 \n", 19 | "7 7 7 7 7 7 7 \n" 20 | ] 21 | } 22 | ], 23 | "source": [ 24 | "n = 7\n", 25 | "\n", 26 | "# iterating over all rows\n", 27 | "for i in range(n):\n", 28 | " # iterating over all columns\n", 29 | " for j in range(n):\n", 30 | " print(max(i,j,(n-i-1),(n-j-1))+1,end=' ')\n", 31 | " \n", 32 | " print(end='\\n')" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 9, 38 | "id": "301f4d33", 39 | "metadata": {}, 40 | "outputs": [ 41 | { 42 | "name": "stdout", 43 | "output_type": "stream", 44 | "text": [ 45 | "210\n" 46 | ] 47 | } 48 | ], 49 | "source": [ 50 | "n = 3\n", 51 | "a = \"\"\n", 52 | "while (n>0):\n", 53 | " n -= 1\n", 54 | " a += str(n)\n", 55 | " \n", 56 | "print(a)\n", 57 | "# print(a[::2])" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 10, 63 | "id": "836e86ae", 64 | "metadata": {}, 65 | "outputs": [ 66 | { 67 | "name": "stdout", 68 | "output_type": "stream", 69 | "text": [ 70 | "[1, 5, 9]\n" 71 | ] 72 | } 73 | ], 74 | "source": [ 75 | "a = [(2*x - 1) for x in range(6) if x % 2 == 1]\n", 76 | "print(a)" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 11, 82 | "id": "0720f5ab", 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [ 86 | "# 1, 5, 9" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "id": "49a6b6b2", 92 | "metadata": {}, 93 | "source": [ 94 | "### Linear Search" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 20, 100 | "id": "0abe83c2", 101 | "metadata": {}, 102 | "outputs": [], 103 | "source": [ 104 | "def linear_search(arr, target):\n", 105 | " n = len(arr)\n", 106 | " \n", 107 | " for i in range(n):\n", 108 | " if arr[i] == target:\n", 109 | " return \"Found at idx:\" + str(i)\n", 110 | "\n", 111 | "\n", 112 | " # after the loop is over.\n", 113 | " # all elements said NO\n", 114 | " return \"Not found\" " 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 21, 120 | "id": "76a3ca80", 121 | "metadata": {}, 122 | "outputs": [], 123 | "source": [ 124 | "arr = [4, 8, 2, 1, 7, 9, 3]\n", 125 | "target = 1" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 22, 131 | "id": "27999795", 132 | "metadata": {}, 133 | "outputs": [ 134 | { 135 | "data": { 136 | "text/plain": [ 137 | "'Found at idx:3'" 138 | ] 139 | }, 140 | "execution_count": 22, 141 | "metadata": {}, 142 | "output_type": "execute_result" 143 | } 144 | ], 145 | "source": [ 146 | "linear_search(arr, target)" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": 23, 152 | "id": "7f0b2934", 153 | "metadata": {}, 154 | "outputs": [ 155 | { 156 | "data": { 157 | "text/plain": [ 158 | "'Found at idx:4'" 159 | ] 160 | }, 161 | "execution_count": 23, 162 | "metadata": {}, 163 | "output_type": "execute_result" 164 | } 165 | ], 166 | "source": [ 167 | "target = 7\n", 168 | "linear_search(arr, target)" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": 24, 174 | "id": "5b47af2c", 175 | "metadata": {}, 176 | "outputs": [ 177 | { 178 | "data": { 179 | "text/plain": [ 180 | "'Not found'" 181 | ] 182 | }, 183 | "execution_count": 24, 184 | "metadata": {}, 185 | "output_type": "execute_result" 186 | } 187 | ], 188 | "source": [ 189 | "target = 5\n", 190 | "linear_search(arr, target)" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": 25, 196 | "id": "4914f3a7", 197 | "metadata": {}, 198 | "outputs": [ 199 | { 200 | "data": { 201 | "text/plain": [ 202 | "False" 203 | ] 204 | }, 205 | "execution_count": 25, 206 | "metadata": {}, 207 | "output_type": "execute_result" 208 | } 209 | ], 210 | "source": [ 211 | "# linear search\n", 212 | "5 in arr" 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": 26, 218 | "id": "2a8ccff3", 219 | "metadata": {}, 220 | "outputs": [ 221 | { 222 | "data": { 223 | "text/plain": [ 224 | "True" 225 | ] 226 | }, 227 | "execution_count": 26, 228 | "metadata": {}, 229 | "output_type": "execute_result" 230 | } 231 | ], 232 | "source": [ 233 | "7 in arr" 234 | ] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "execution_count": 27, 239 | "id": "062fe7dd", 240 | "metadata": {}, 241 | "outputs": [ 242 | { 243 | "data": { 244 | "text/plain": [ 245 | "True" 246 | ] 247 | }, 248 | "execution_count": 27, 249 | "metadata": {}, 250 | "output_type": "execute_result" 251 | } 252 | ], 253 | "source": [ 254 | "1 in arr" 255 | ] 256 | }, 257 | { 258 | "cell_type": "code", 259 | "execution_count": 42, 260 | "id": "3d971c9f", 261 | "metadata": {}, 262 | "outputs": [], 263 | "source": [ 264 | "def binary_search(arr, target):\n", 265 | " \n", 266 | " n = len(arr)\n", 267 | " \n", 268 | " start = 0\n", 269 | " end = n - 1\n", 270 | " \n", 271 | " while(start <= end):\n", 272 | " mid = (start+ end)//2\n", 273 | " \n", 274 | " print(\"Array to be searched :\", arr[start: end+1])\n", 275 | " print(start, mid, end )\n", 276 | " print(\"mid :\",arr[mid])\n", 277 | " print(\"-\"*50)\n", 278 | " \n", 279 | " if arr[mid] == target:\n", 280 | " return \"Found at \" + str(mid)\n", 281 | " elif target > arr[mid]:\n", 282 | " start = mid+1\n", 283 | " else:\n", 284 | " end = mid - 1\n", 285 | " \n", 286 | " return \"Not found\"" 287 | ] 288 | }, 289 | { 290 | "cell_type": "code", 291 | "execution_count": 43, 292 | "id": "5e07a226", 293 | "metadata": {}, 294 | "outputs": [], 295 | "source": [ 296 | "arr = [2, 4, 6, 7, 9, 10, 15]\n", 297 | "target = 4" 298 | ] 299 | }, 300 | { 301 | "cell_type": "code", 302 | "execution_count": 44, 303 | "id": "afde8b21", 304 | "metadata": {}, 305 | "outputs": [ 306 | { 307 | "name": "stdout", 308 | "output_type": "stream", 309 | "text": [ 310 | "Array to be searched : [2, 4, 6, 7, 9, 10, 15]\n", 311 | "0 3 6\n", 312 | "mid : 7\n", 313 | "--------------------------------------------------\n", 314 | "Array to be searched : [2, 4, 6]\n", 315 | "0 1 2\n", 316 | "mid : 4\n", 317 | "--------------------------------------------------\n" 318 | ] 319 | }, 320 | { 321 | "data": { 322 | "text/plain": [ 323 | "'Found at 1'" 324 | ] 325 | }, 326 | "execution_count": 44, 327 | "metadata": {}, 328 | "output_type": "execute_result" 329 | } 330 | ], 331 | "source": [ 332 | "target = 4\n", 333 | "binary_search(arr, target)" 334 | ] 335 | }, 336 | { 337 | "cell_type": "code", 338 | "execution_count": 45, 339 | "id": "b9cb6113", 340 | "metadata": {}, 341 | "outputs": [ 342 | { 343 | "name": "stdout", 344 | "output_type": "stream", 345 | "text": [ 346 | "Array to be searched : [2, 4, 6, 7, 9, 10, 15]\n", 347 | "0 3 6\n", 348 | "mid : 7\n", 349 | "--------------------------------------------------\n", 350 | "Array to be searched : [9, 10, 15]\n", 351 | "4 5 6\n", 352 | "mid : 10\n", 353 | "--------------------------------------------------\n" 354 | ] 355 | }, 356 | { 357 | "data": { 358 | "text/plain": [ 359 | "'Found at 5'" 360 | ] 361 | }, 362 | "execution_count": 45, 363 | "metadata": {}, 364 | "output_type": "execute_result" 365 | } 366 | ], 367 | "source": [ 368 | "target = 10\n", 369 | "binary_search(arr, target)" 370 | ] 371 | }, 372 | { 373 | "cell_type": "code", 374 | "execution_count": 46, 375 | "id": "517e0a46", 376 | "metadata": {}, 377 | "outputs": [ 378 | { 379 | "name": "stdout", 380 | "output_type": "stream", 381 | "text": [ 382 | "Array to be searched : [2, 4, 6, 7, 9, 10, 15]\n", 383 | "0 3 6\n", 384 | "mid : 7\n", 385 | "--------------------------------------------------\n", 386 | "Array to be searched : [9, 10, 15]\n", 387 | "4 5 6\n", 388 | "mid : 10\n", 389 | "--------------------------------------------------\n", 390 | "Array to be searched : [15]\n", 391 | "6 6 6\n", 392 | "mid : 15\n", 393 | "--------------------------------------------------\n" 394 | ] 395 | }, 396 | { 397 | "data": { 398 | "text/plain": [ 399 | "'Found at 6'" 400 | ] 401 | }, 402 | "execution_count": 46, 403 | "metadata": {}, 404 | "output_type": "execute_result" 405 | } 406 | ], 407 | "source": [ 408 | "target = 15\n", 409 | "binary_search(arr, target)" 410 | ] 411 | }, 412 | { 413 | "cell_type": "code", 414 | "execution_count": 47, 415 | "id": "5cda3ccb", 416 | "metadata": {}, 417 | "outputs": [ 418 | { 419 | "name": "stdout", 420 | "output_type": "stream", 421 | "text": [ 422 | "Array to be searched : [2, 4, 6, 7, 9, 10, 15]\n", 423 | "0 3 6\n", 424 | "mid : 7\n", 425 | "--------------------------------------------------\n", 426 | "Array to be searched : [9, 10, 15]\n", 427 | "4 5 6\n", 428 | "mid : 10\n", 429 | "--------------------------------------------------\n", 430 | "Array to be searched : [9]\n", 431 | "4 4 4\n", 432 | "mid : 9\n", 433 | "--------------------------------------------------\n" 434 | ] 435 | }, 436 | { 437 | "data": { 438 | "text/plain": [ 439 | "'Not found'" 440 | ] 441 | }, 442 | "execution_count": 47, 443 | "metadata": {}, 444 | "output_type": "execute_result" 445 | } 446 | ], 447 | "source": [ 448 | "target = 8\n", 449 | "binary_search(arr, target)" 450 | ] 451 | }, 452 | { 453 | "cell_type": "code", 454 | "execution_count": null, 455 | "id": "170111ac", 456 | "metadata": {}, 457 | "outputs": [], 458 | "source": [ 459 | "end = mid - 1" 460 | ] 461 | }, 462 | { 463 | "cell_type": "code", 464 | "execution_count": null, 465 | "id": "ea64c7b4", 466 | "metadata": {}, 467 | "outputs": [], 468 | "source": [] 469 | }, 470 | { 471 | "cell_type": "markdown", 472 | "id": "c12e6d1a", 473 | "metadata": {}, 474 | "source": [ 475 | "# Doubts\n" 476 | ] 477 | }, 478 | { 479 | "cell_type": "code", 480 | "execution_count": 48, 481 | "id": "0de41d4f", 482 | "metadata": {}, 483 | "outputs": [], 484 | "source": [ 485 | "a = [1,3,6,10]\n", 486 | "b = a" 487 | ] 488 | }, 489 | { 490 | "cell_type": "code", 491 | "execution_count": 49, 492 | "id": "cbc87c19", 493 | "metadata": {}, 494 | "outputs": [ 495 | { 496 | "name": "stdout", 497 | "output_type": "stream", 498 | "text": [ 499 | "[1, 3, 6, 10]\n" 500 | ] 501 | } 502 | ], 503 | "source": [ 504 | "print(a)" 505 | ] 506 | }, 507 | { 508 | "cell_type": "code", 509 | "execution_count": 50, 510 | "id": "1c914736", 511 | "metadata": {}, 512 | "outputs": [ 513 | { 514 | "name": "stdout", 515 | "output_type": "stream", 516 | "text": [ 517 | "[1, 3, 6, 10]\n" 518 | ] 519 | } 520 | ], 521 | "source": [ 522 | "print(b)" 523 | ] 524 | }, 525 | { 526 | "cell_type": "code", 527 | "execution_count": 51, 528 | "id": "7c1e6d40", 529 | "metadata": {}, 530 | "outputs": [], 531 | "source": [ 532 | "b[0] = 100" 533 | ] 534 | }, 535 | { 536 | "cell_type": "code", 537 | "execution_count": 52, 538 | "id": "be65ec74", 539 | "metadata": {}, 540 | "outputs": [ 541 | { 542 | "name": "stdout", 543 | "output_type": "stream", 544 | "text": [ 545 | "[100, 3, 6, 10]\n" 546 | ] 547 | } 548 | ], 549 | "source": [ 550 | "print(b)" 551 | ] 552 | }, 553 | { 554 | "cell_type": "code", 555 | "execution_count": 53, 556 | "id": "b9a1e0ec", 557 | "metadata": {}, 558 | "outputs": [ 559 | { 560 | "name": "stdout", 561 | "output_type": "stream", 562 | "text": [ 563 | "[100, 3, 6, 10]\n" 564 | ] 565 | } 566 | ], 567 | "source": [ 568 | "print(a)" 569 | ] 570 | }, 571 | { 572 | "cell_type": "code", 573 | "execution_count": 54, 574 | "id": "5cd74898", 575 | "metadata": {}, 576 | "outputs": [], 577 | "source": [ 578 | "# created a deep copy.\n", 579 | "c = a.copy()" 580 | ] 581 | }, 582 | { 583 | "cell_type": "code", 584 | "execution_count": 55, 585 | "id": "ee6a9f06", 586 | "metadata": {}, 587 | "outputs": [ 588 | { 589 | "name": "stdout", 590 | "output_type": "stream", 591 | "text": [ 592 | "[100, 3, 6, 10]\n" 593 | ] 594 | } 595 | ], 596 | "source": [ 597 | "print(c)" 598 | ] 599 | }, 600 | { 601 | "cell_type": "code", 602 | "execution_count": 56, 603 | "id": "a3dbfbb5", 604 | "metadata": {}, 605 | "outputs": [ 606 | { 607 | "name": "stdout", 608 | "output_type": "stream", 609 | "text": [ 610 | "[100, 3, 6, 10]\n" 611 | ] 612 | } 613 | ], 614 | "source": [ 615 | "print(a)" 616 | ] 617 | }, 618 | { 619 | "cell_type": "code", 620 | "execution_count": 57, 621 | "id": "551d17c2", 622 | "metadata": {}, 623 | "outputs": [], 624 | "source": [ 625 | "c[0] = -1" 626 | ] 627 | }, 628 | { 629 | "cell_type": "code", 630 | "execution_count": 58, 631 | "id": "249dc26e", 632 | "metadata": {}, 633 | "outputs": [ 634 | { 635 | "name": "stdout", 636 | "output_type": "stream", 637 | "text": [ 638 | "[-1, 3, 6, 10]\n" 639 | ] 640 | } 641 | ], 642 | "source": [ 643 | "print(c)" 644 | ] 645 | }, 646 | { 647 | "cell_type": "code", 648 | "execution_count": 59, 649 | "id": "ed8c03f4", 650 | "metadata": {}, 651 | "outputs": [ 652 | { 653 | "name": "stdout", 654 | "output_type": "stream", 655 | "text": [ 656 | "[100, 3, 6, 10]\n" 657 | ] 658 | } 659 | ], 660 | "source": [ 661 | "print(a)" 662 | ] 663 | }, 664 | { 665 | "cell_type": "code", 666 | "execution_count": 60, 667 | "id": "0b35af7c", 668 | "metadata": {}, 669 | "outputs": [ 670 | { 671 | "data": { 672 | "text/plain": [ 673 | "False" 674 | ] 675 | }, 676 | "execution_count": 60, 677 | "metadata": {}, 678 | "output_type": "execute_result" 679 | } 680 | ], 681 | "source": [ 682 | "\"Akhil\"> \"Mohit\"" 683 | ] 684 | }, 685 | { 686 | "cell_type": "code", 687 | "execution_count": 61, 688 | "id": "5f9ab814", 689 | "metadata": {}, 690 | "outputs": [ 691 | { 692 | "data": { 693 | "text/plain": [ 694 | "65" 695 | ] 696 | }, 697 | "execution_count": 61, 698 | "metadata": {}, 699 | "output_type": "execute_result" 700 | } 701 | ], 702 | "source": [ 703 | "ord('A')" 704 | ] 705 | }, 706 | { 707 | "cell_type": "code", 708 | "execution_count": 62, 709 | "id": "2ba4ca2a", 710 | "metadata": {}, 711 | "outputs": [ 712 | { 713 | "data": { 714 | "text/plain": [ 715 | "77" 716 | ] 717 | }, 718 | "execution_count": 62, 719 | "metadata": {}, 720 | "output_type": "execute_result" 721 | } 722 | ], 723 | "source": [ 724 | "ord('M')" 725 | ] 726 | }, 727 | { 728 | "cell_type": "code", 729 | "execution_count": 63, 730 | "id": "04e90e77", 731 | "metadata": {}, 732 | "outputs": [ 733 | { 734 | "ename": "TypeError", 735 | "evalue": "'>' not supported between instances of 'float' and 'str'", 736 | "output_type": "error", 737 | "traceback": [ 738 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 739 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 740 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;36m8.2\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;34m\"Akhil\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 741 | "\u001b[0;31mTypeError\u001b[0m: '>' not supported between instances of 'float' and 'str'" 742 | ] 743 | } 744 | ], 745 | "source": [ 746 | "8.2 > \"Akhil\"" 747 | ] 748 | }, 749 | { 750 | "cell_type": "code", 751 | "execution_count": 64, 752 | "id": "e242128a", 753 | "metadata": {}, 754 | "outputs": [], 755 | "source": [ 756 | "a = [1,2,3,4]" 757 | ] 758 | }, 759 | { 760 | "cell_type": "code", 761 | "execution_count": 66, 762 | "id": "b3815e16", 763 | "metadata": {}, 764 | "outputs": [], 765 | "source": [ 766 | "# a.replace" 767 | ] 768 | }, 769 | { 770 | "cell_type": "code", 771 | "execution_count": 67, 772 | "id": "af77afa3", 773 | "metadata": {}, 774 | "outputs": [], 775 | "source": [ 776 | "s = \"Hello World\"" 777 | ] 778 | }, 779 | { 780 | "cell_type": "code", 781 | "execution_count": 68, 782 | "id": "91511098", 783 | "metadata": {}, 784 | "outputs": [ 785 | { 786 | "data": { 787 | "text/plain": [ 788 | "'Yello World'" 789 | ] 790 | }, 791 | "execution_count": 68, 792 | "metadata": {}, 793 | "output_type": "execute_result" 794 | } 795 | ], 796 | "source": [ 797 | "s.replace('H', 'Y')" 798 | ] 799 | }, 800 | { 801 | "cell_type": "code", 802 | "execution_count": 69, 803 | "id": "a8b2507a", 804 | "metadata": {}, 805 | "outputs": [ 806 | { 807 | "ename": "TypeError", 808 | "evalue": "'str' object does not support item assignment", 809 | "output_type": "error", 810 | "traceback": [ 811 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 812 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 813 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0ms\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"hello\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0ms\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'y'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 814 | "\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment" 815 | ] 816 | } 817 | ], 818 | "source": [ 819 | "s = \"hello\"\n", 820 | "s[0] = 'y'\n", 821 | "print(s)" 822 | ] 823 | }, 824 | { 825 | "cell_type": "code", 826 | "execution_count": 71, 827 | "id": "5c7d0953", 828 | "metadata": {}, 829 | "outputs": [ 830 | { 831 | "name": "stdout", 832 | "output_type": "stream", 833 | "text": [ 834 | "140705290144368\n", 835 | "140705290545584\n", 836 | "yello\n" 837 | ] 838 | } 839 | ], 840 | "source": [ 841 | "s = \"hello\"\n", 842 | "print(id(s))\n", 843 | "s = s.replace('h', 'y')\n", 844 | "print(id(s))\n", 845 | "print(s)" 846 | ] 847 | }, 848 | { 849 | "cell_type": "code", 850 | "execution_count": 72, 851 | "id": "11a2f6e8", 852 | "metadata": {}, 853 | "outputs": [], 854 | "source": [ 855 | "arr = [5, 7, 4, 2, 1, 0, 8, 2]" 856 | ] 857 | }, 858 | { 859 | "cell_type": "code", 860 | "execution_count": 75, 861 | "id": "39366e1b", 862 | "metadata": {}, 863 | "outputs": [], 864 | "source": [ 865 | "def isprime(n):\n", 866 | " # complete the function\n", 867 | " return None" 868 | ] 869 | }, 870 | { 871 | "cell_type": "code", 872 | "execution_count": null, 873 | "id": "ea3c6d3f", 874 | "metadata": {}, 875 | "outputs": [], 876 | "source": [] 877 | }, 878 | { 879 | "cell_type": "code", 880 | "execution_count": 74, 881 | "id": "9f523b33", 882 | "metadata": {}, 883 | "outputs": [ 884 | { 885 | "data": { 886 | "text/plain": [ 887 | "[4, 2, 0, 8, 2]" 888 | ] 889 | }, 890 | "execution_count": 74, 891 | "metadata": {}, 892 | "output_type": "execute_result" 893 | } 894 | ], 895 | "source": [ 896 | "[ele for ele in arr if isprime(ele)]" 897 | ] 898 | }, 899 | { 900 | "cell_type": "code", 901 | "execution_count": null, 902 | "id": "fa203535", 903 | "metadata": {}, 904 | "outputs": [], 905 | "source": [] 906 | } 907 | ], 908 | "metadata": { 909 | "kernelspec": { 910 | "display_name": "Python 3", 911 | "language": "python", 912 | "name": "python3" 913 | }, 914 | "language_info": { 915 | "codemirror_mode": { 916 | "name": "ipython", 917 | "version": 3 918 | }, 919 | "file_extension": ".py", 920 | "mimetype": "text/x-python", 921 | "name": "python", 922 | "nbconvert_exporter": "python", 923 | "pygments_lexer": "ipython3", 924 | "version": "3.8.8" 925 | } 926 | }, 927 | "nbformat": 4, 928 | "nbformat_minor": 5 929 | } 930 | -------------------------------------------------------------------------------- /Sorting_1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "8e27127d", 6 | "metadata": {}, 7 | "source": [ 8 | "# DSML Intermediate : Time Complexity and Sorting I" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "075bf9c3", 14 | "metadata": {}, 15 | "source": [ 16 | "## Recap" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 1, 22 | "id": "666a1b02", 23 | "metadata": {}, 24 | "outputs": [ 25 | { 26 | "data": { 27 | "text/plain": [ 28 | "['weirdamI']" 29 | ] 30 | }, 31 | "execution_count": 1, 32 | "metadata": {}, 33 | "output_type": "execute_result" 34 | } 35 | ], 36 | "source": [ 37 | "[ i+j+k for k in [\"I\"] for j in [\"am\"] for i in [\"weird\"] ]" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 2, 43 | "id": "66a98c86", 44 | "metadata": {}, 45 | "outputs": [ 46 | { 47 | "name": "stdout", 48 | "output_type": "stream", 49 | "text": [ 50 | "weirdamI\n" 51 | ] 52 | } 53 | ], 54 | "source": [ 55 | "for i in [\"weird\"]:\n", 56 | " for j in [\"am\"]:\n", 57 | " for k in [\"I\"]:\n", 58 | " print(i+j+k)" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 12, 64 | "id": "c18c1c8c", 65 | "metadata": {}, 66 | "outputs": [ 67 | { 68 | "name": "stdout", 69 | "output_type": "stream", 70 | "text": [ 71 | "JavaScript and TypeScript\n" 72 | ] 73 | } 74 | ], 75 | "source": [ 76 | "random = {}\n", 77 | "random[1.1] = \"JavaScript\"\n", 78 | "random[1.0] = \"Python\"\n", 79 | "random[1] = \"C++\"\n", 80 | "random[True] = \"JAVA\"\n", 81 | "random[1+0j] = \"TypeScript\"\n", 82 | "\n", 83 | "print(f\"{random[1.1]} and {random[True]}\")" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 3, 89 | "id": "f61f34fa", 90 | "metadata": {}, 91 | "outputs": [ 92 | { 93 | "name": "stdout", 94 | "output_type": "stream", 95 | "text": [ 96 | "{1.1: 'JavaScript'}\n" 97 | ] 98 | } 99 | ], 100 | "source": [ 101 | "random = {}\n", 102 | "random[1.1] = \"JavaScript\"\n", 103 | "print(random)" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 4, 109 | "id": "630642dd", 110 | "metadata": {}, 111 | "outputs": [ 112 | { 113 | "name": "stdout", 114 | "output_type": "stream", 115 | "text": [ 116 | "{1.1: 'JavaScript', 1.0: 'Python'}\n" 117 | ] 118 | } 119 | ], 120 | "source": [ 121 | "random[1.0] = \"Python\"\n", 122 | "print(random)" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 5, 128 | "id": "6909a18b", 129 | "metadata": {}, 130 | "outputs": [ 131 | { 132 | "name": "stdout", 133 | "output_type": "stream", 134 | "text": [ 135 | "{1.1: 'JavaScript', 1.0: 'C++'}\n" 136 | ] 137 | } 138 | ], 139 | "source": [ 140 | "random[1] = \"C++\"\n", 141 | "print(random)" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 7, 147 | "id": "ffb881d7", 148 | "metadata": {}, 149 | "outputs": [ 150 | { 151 | "name": "stdout", 152 | "output_type": "stream", 153 | "text": [ 154 | "{1.1: 'JavaScript', 1.0: 'JAVA'}\n" 155 | ] 156 | } 157 | ], 158 | "source": [ 159 | "random[True] = \"JAVA\"\n", 160 | "print(random)" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": 11, 166 | "id": "aa7aca8d", 167 | "metadata": {}, 168 | "outputs": [ 169 | { 170 | "name": "stdout", 171 | "output_type": "stream", 172 | "text": [ 173 | "{1.1: 'JavaScript', 1.0: 'TypeScript'}\n" 174 | ] 175 | } 176 | ], 177 | "source": [ 178 | "random[1+0j] = \"TypeScript\"\n", 179 | "print(random)" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": 9, 185 | "id": "9ae2d333", 186 | "metadata": {}, 187 | "outputs": [ 188 | { 189 | "data": { 190 | "text/plain": [ 191 | "(1+0j)" 192 | ] 193 | }, 194 | "execution_count": 9, 195 | "metadata": {}, 196 | "output_type": "execute_result" 197 | } 198 | ], 199 | "source": [ 200 | "1+0j" 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": 10, 206 | "id": "2043b9e3", 207 | "metadata": {}, 208 | "outputs": [ 209 | { 210 | "data": { 211 | "text/plain": [ 212 | "complex" 213 | ] 214 | }, 215 | "execution_count": 10, 216 | "metadata": {}, 217 | "output_type": "execute_result" 218 | } 219 | ], 220 | "source": [ 221 | "type(1+0j)" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": null, 227 | "id": "d37a3f50", 228 | "metadata": {}, 229 | "outputs": [], 230 | "source": [] 231 | }, 232 | { 233 | "cell_type": "code", 234 | "execution_count": null, 235 | "id": "1b1defed", 236 | "metadata": {}, 237 | "outputs": [], 238 | "source": [] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": null, 243 | "id": "4ca4b3ac", 244 | "metadata": {}, 245 | "outputs": [], 246 | "source": [] 247 | }, 248 | { 249 | "cell_type": "code", 250 | "execution_count": 6, 251 | "id": "5a86e2e7", 252 | "metadata": {}, 253 | "outputs": [ 254 | { 255 | "name": "stdout", 256 | "output_type": "stream", 257 | "text": [ 258 | "{1: 'Surya'}\n" 259 | ] 260 | } 261 | ], 262 | "source": [ 263 | "x = {1: \"harshita\", 1.0: \"Surya\"}\n", 264 | "print(x)" 265 | ] 266 | }, 267 | { 268 | "cell_type": "code", 269 | "execution_count": null, 270 | "id": "efba2741", 271 | "metadata": {}, 272 | "outputs": [], 273 | "source": [] 274 | }, 275 | { 276 | "cell_type": "code", 277 | "execution_count": null, 278 | "id": "047ac02b", 279 | "metadata": {}, 280 | "outputs": [], 281 | "source": [] 282 | }, 283 | { 284 | "cell_type": "code", 285 | "execution_count": null, 286 | "id": "41835b95", 287 | "metadata": {}, 288 | "outputs": [], 289 | "source": [] 290 | }, 291 | { 292 | "cell_type": "code", 293 | "execution_count": null, 294 | "id": "3f95b32c", 295 | "metadata": {}, 296 | "outputs": [], 297 | "source": [] 298 | }, 299 | { 300 | "cell_type": "code", 301 | "execution_count": null, 302 | "id": "0dd2f93b", 303 | "metadata": {}, 304 | "outputs": [], 305 | "source": [] 306 | }, 307 | { 308 | "cell_type": "code", 309 | "execution_count": null, 310 | "id": "26cbd911", 311 | "metadata": {}, 312 | "outputs": [], 313 | "source": [] 314 | }, 315 | { 316 | "cell_type": "markdown", 317 | "id": "7654a25e", 318 | "metadata": {}, 319 | "source": [ 320 | "## Time Complexity" 321 | ] 322 | }, 323 | { 324 | "cell_type": "code", 325 | "execution_count": null, 326 | "id": "eda6be49", 327 | "metadata": {}, 328 | "outputs": [], 329 | "source": [] 330 | }, 331 | { 332 | "cell_type": "code", 333 | "execution_count": null, 334 | "id": "501ff4d3", 335 | "metadata": {}, 336 | "outputs": [], 337 | "source": [] 338 | }, 339 | { 340 | "cell_type": "code", 341 | "execution_count": null, 342 | "id": "0532fe37", 343 | "metadata": {}, 344 | "outputs": [], 345 | "source": [] 346 | }, 347 | { 348 | "cell_type": "code", 349 | "execution_count": null, 350 | "id": "bd303168", 351 | "metadata": {}, 352 | "outputs": [], 353 | "source": [] 354 | }, 355 | { 356 | "cell_type": "code", 357 | "execution_count": null, 358 | "id": "cdd1e430", 359 | "metadata": {}, 360 | "outputs": [], 361 | "source": [] 362 | }, 363 | { 364 | "cell_type": "code", 365 | "execution_count": null, 366 | "id": "48e2d33e", 367 | "metadata": {}, 368 | "outputs": [], 369 | "source": [] 370 | }, 371 | { 372 | "cell_type": "code", 373 | "execution_count": null, 374 | "id": "70a048b8", 375 | "metadata": {}, 376 | "outputs": [], 377 | "source": [] 378 | }, 379 | { 380 | "cell_type": "code", 381 | "execution_count": null, 382 | "id": "b14c87a9", 383 | "metadata": {}, 384 | "outputs": [], 385 | "source": [] 386 | }, 387 | { 388 | "cell_type": "code", 389 | "execution_count": null, 390 | "id": "68b9449f", 391 | "metadata": {}, 392 | "outputs": [], 393 | "source": [] 394 | }, 395 | { 396 | "cell_type": "code", 397 | "execution_count": null, 398 | "id": "9a25ada9", 399 | "metadata": {}, 400 | "outputs": [], 401 | "source": [] 402 | }, 403 | { 404 | "cell_type": "code", 405 | "execution_count": null, 406 | "id": "81bc15a3", 407 | "metadata": {}, 408 | "outputs": [], 409 | "source": [] 410 | }, 411 | { 412 | "cell_type": "code", 413 | "execution_count": null, 414 | "id": "3fdf8945", 415 | "metadata": {}, 416 | "outputs": [], 417 | "source": [] 418 | }, 419 | { 420 | "cell_type": "code", 421 | "execution_count": null, 422 | "id": "9a0d1932", 423 | "metadata": {}, 424 | "outputs": [], 425 | "source": [] 426 | }, 427 | { 428 | "cell_type": "code", 429 | "execution_count": null, 430 | "id": "5b66e3f5", 431 | "metadata": {}, 432 | "outputs": [], 433 | "source": [] 434 | }, 435 | { 436 | "cell_type": "code", 437 | "execution_count": null, 438 | "id": "e39cc4bf", 439 | "metadata": {}, 440 | "outputs": [], 441 | "source": [] 442 | }, 443 | { 444 | "cell_type": "code", 445 | "execution_count": null, 446 | "id": "6487025f", 447 | "metadata": {}, 448 | "outputs": [], 449 | "source": [] 450 | }, 451 | { 452 | "cell_type": "code", 453 | "execution_count": null, 454 | "id": "5bff07af", 455 | "metadata": {}, 456 | "outputs": [], 457 | "source": [] 458 | }, 459 | { 460 | "cell_type": "code", 461 | "execution_count": null, 462 | "id": "5a8b7a69", 463 | "metadata": {}, 464 | "outputs": [], 465 | "source": [] 466 | }, 467 | { 468 | "cell_type": "code", 469 | "execution_count": null, 470 | "id": "9d58cb21", 471 | "metadata": {}, 472 | "outputs": [], 473 | "source": [] 474 | }, 475 | { 476 | "cell_type": "code", 477 | "execution_count": null, 478 | "id": "62f1f855", 479 | "metadata": {}, 480 | "outputs": [], 481 | "source": [] 482 | }, 483 | { 484 | "cell_type": "code", 485 | "execution_count": null, 486 | "id": "12c1a86f", 487 | "metadata": {}, 488 | "outputs": [], 489 | "source": [] 490 | }, 491 | { 492 | "cell_type": "code", 493 | "execution_count": null, 494 | "id": "d48df211", 495 | "metadata": {}, 496 | "outputs": [], 497 | "source": [] 498 | }, 499 | { 500 | "cell_type": "code", 501 | "execution_count": null, 502 | "id": "59ab34a2", 503 | "metadata": {}, 504 | "outputs": [], 505 | "source": [] 506 | }, 507 | { 508 | "cell_type": "code", 509 | "execution_count": null, 510 | "id": "11729fa5", 511 | "metadata": {}, 512 | "outputs": [], 513 | "source": [] 514 | }, 515 | { 516 | "cell_type": "markdown", 517 | "id": "191ee542", 518 | "metadata": {}, 519 | "source": [ 520 | "## Sorting" 521 | ] 522 | }, 523 | { 524 | "cell_type": "code", 525 | "execution_count": null, 526 | "id": "47e8ba38", 527 | "metadata": {}, 528 | "outputs": [], 529 | "source": [] 530 | }, 531 | { 532 | "cell_type": "code", 533 | "execution_count": null, 534 | "id": "b9705b9a", 535 | "metadata": {}, 536 | "outputs": [], 537 | "source": [] 538 | }, 539 | { 540 | "cell_type": "code", 541 | "execution_count": null, 542 | "id": "817e833c", 543 | "metadata": {}, 544 | "outputs": [], 545 | "source": [] 546 | }, 547 | { 548 | "cell_type": "code", 549 | "execution_count": null, 550 | "id": "30764662", 551 | "metadata": {}, 552 | "outputs": [], 553 | "source": [] 554 | }, 555 | { 556 | "cell_type": "code", 557 | "execution_count": null, 558 | "id": "093b0d83", 559 | "metadata": {}, 560 | "outputs": [], 561 | "source": [] 562 | }, 563 | { 564 | "cell_type": "code", 565 | "execution_count": null, 566 | "id": "8860714b", 567 | "metadata": {}, 568 | "outputs": [], 569 | "source": [] 570 | }, 571 | { 572 | "cell_type": "code", 573 | "execution_count": null, 574 | "id": "e4b02e1f", 575 | "metadata": {}, 576 | "outputs": [], 577 | "source": [] 578 | }, 579 | { 580 | "cell_type": "code", 581 | "execution_count": null, 582 | "id": "92e3f82d", 583 | "metadata": {}, 584 | "outputs": [], 585 | "source": [] 586 | }, 587 | { 588 | "cell_type": "code", 589 | "execution_count": null, 590 | "id": "73e8fd1b", 591 | "metadata": {}, 592 | "outputs": [], 593 | "source": [] 594 | }, 595 | { 596 | "cell_type": "code", 597 | "execution_count": null, 598 | "id": "362c17b9", 599 | "metadata": {}, 600 | "outputs": [], 601 | "source": [] 602 | }, 603 | { 604 | "cell_type": "code", 605 | "execution_count": null, 606 | "id": "e22db304", 607 | "metadata": {}, 608 | "outputs": [], 609 | "source": [] 610 | }, 611 | { 612 | "cell_type": "code", 613 | "execution_count": null, 614 | "id": "ea98260a", 615 | "metadata": {}, 616 | "outputs": [], 617 | "source": [] 618 | }, 619 | { 620 | "cell_type": "code", 621 | "execution_count": null, 622 | "id": "0de0ac37", 623 | "metadata": {}, 624 | "outputs": [], 625 | "source": [] 626 | }, 627 | { 628 | "cell_type": "code", 629 | "execution_count": null, 630 | "id": "831a505d", 631 | "metadata": {}, 632 | "outputs": [], 633 | "source": [] 634 | }, 635 | { 636 | "cell_type": "code", 637 | "execution_count": null, 638 | "id": "83ddaca5", 639 | "metadata": {}, 640 | "outputs": [], 641 | "source": [] 642 | }, 643 | { 644 | "cell_type": "code", 645 | "execution_count": null, 646 | "id": "11a29a0e", 647 | "metadata": {}, 648 | "outputs": [], 649 | "source": [] 650 | }, 651 | { 652 | "cell_type": "code", 653 | "execution_count": null, 654 | "id": "397d0eae", 655 | "metadata": {}, 656 | "outputs": [], 657 | "source": [] 658 | }, 659 | { 660 | "cell_type": "code", 661 | "execution_count": null, 662 | "id": "211792c6", 663 | "metadata": {}, 664 | "outputs": [], 665 | "source": [] 666 | }, 667 | { 668 | "cell_type": "code", 669 | "execution_count": null, 670 | "id": "4c175a4b", 671 | "metadata": {}, 672 | "outputs": [], 673 | "source": [] 674 | }, 675 | { 676 | "cell_type": "code", 677 | "execution_count": null, 678 | "id": "2cff8d2b", 679 | "metadata": {}, 680 | "outputs": [], 681 | "source": [] 682 | }, 683 | { 684 | "cell_type": "code", 685 | "execution_count": null, 686 | "id": "aa7d53f8", 687 | "metadata": {}, 688 | "outputs": [], 689 | "source": [] 690 | }, 691 | { 692 | "cell_type": "code", 693 | "execution_count": null, 694 | "id": "89663ef4", 695 | "metadata": {}, 696 | "outputs": [], 697 | "source": [] 698 | }, 699 | { 700 | "cell_type": "code", 701 | "execution_count": null, 702 | "id": "bf061415", 703 | "metadata": {}, 704 | "outputs": [], 705 | "source": [] 706 | }, 707 | { 708 | "cell_type": "code", 709 | "execution_count": null, 710 | "id": "d735a9fd", 711 | "metadata": {}, 712 | "outputs": [], 713 | "source": [] 714 | }, 715 | { 716 | "cell_type": "code", 717 | "execution_count": null, 718 | "id": "355e26c4", 719 | "metadata": {}, 720 | "outputs": [], 721 | "source": [] 722 | }, 723 | { 724 | "cell_type": "code", 725 | "execution_count": null, 726 | "id": "a4bcdf7f", 727 | "metadata": {}, 728 | "outputs": [], 729 | "source": [] 730 | }, 731 | { 732 | "cell_type": "code", 733 | "execution_count": null, 734 | "id": "e15278c8", 735 | "metadata": {}, 736 | "outputs": [], 737 | "source": [] 738 | }, 739 | { 740 | "cell_type": "code", 741 | "execution_count": null, 742 | "id": "0325c61d", 743 | "metadata": {}, 744 | "outputs": [], 745 | "source": [] 746 | }, 747 | { 748 | "cell_type": "code", 749 | "execution_count": null, 750 | "id": "59973cd9", 751 | "metadata": {}, 752 | "outputs": [], 753 | "source": [] 754 | }, 755 | { 756 | "cell_type": "code", 757 | "execution_count": null, 758 | "id": "841c58ae", 759 | "metadata": {}, 760 | "outputs": [], 761 | "source": [] 762 | }, 763 | { 764 | "cell_type": "code", 765 | "execution_count": null, 766 | "id": "07d09fb4", 767 | "metadata": {}, 768 | "outputs": [], 769 | "source": [] 770 | }, 771 | { 772 | "cell_type": "code", 773 | "execution_count": null, 774 | "id": "bc96945b", 775 | "metadata": {}, 776 | "outputs": [], 777 | "source": [] 778 | }, 779 | { 780 | "cell_type": "code", 781 | "execution_count": null, 782 | "id": "3bea5afe", 783 | "metadata": {}, 784 | "outputs": [], 785 | "source": [] 786 | }, 787 | { 788 | "cell_type": "markdown", 789 | "id": "0734190f", 790 | "metadata": {}, 791 | "source": [ 792 | "## Challenge Problems" 793 | ] 794 | }, 795 | { 796 | "cell_type": "code", 797 | "execution_count": null, 798 | "id": "70336078", 799 | "metadata": {}, 800 | "outputs": [], 801 | "source": [] 802 | }, 803 | { 804 | "cell_type": "code", 805 | "execution_count": null, 806 | "id": "8d128605", 807 | "metadata": {}, 808 | "outputs": [], 809 | "source": [] 810 | }, 811 | { 812 | "cell_type": "code", 813 | "execution_count": null, 814 | "id": "b7a0ab93", 815 | "metadata": {}, 816 | "outputs": [], 817 | "source": [] 818 | }, 819 | { 820 | "cell_type": "code", 821 | "execution_count": null, 822 | "id": "6cfc90f8", 823 | "metadata": {}, 824 | "outputs": [], 825 | "source": [] 826 | }, 827 | { 828 | "cell_type": "code", 829 | "execution_count": null, 830 | "id": "917efb27", 831 | "metadata": {}, 832 | "outputs": [], 833 | "source": [] 834 | }, 835 | { 836 | "cell_type": "code", 837 | "execution_count": null, 838 | "id": "d9f9bc50", 839 | "metadata": {}, 840 | "outputs": [], 841 | "source": [] 842 | }, 843 | { 844 | "cell_type": "code", 845 | "execution_count": null, 846 | "id": "be011254", 847 | "metadata": {}, 848 | "outputs": [], 849 | "source": [] 850 | }, 851 | { 852 | "cell_type": "code", 853 | "execution_count": null, 854 | "id": "6cd4a3aa", 855 | "metadata": {}, 856 | "outputs": [], 857 | "source": [] 858 | }, 859 | { 860 | "cell_type": "code", 861 | "execution_count": null, 862 | "id": "b3bf4faa", 863 | "metadata": {}, 864 | "outputs": [], 865 | "source": [] 866 | }, 867 | { 868 | "cell_type": "code", 869 | "execution_count": null, 870 | "id": "757074c8", 871 | "metadata": {}, 872 | "outputs": [], 873 | "source": [] 874 | }, 875 | { 876 | "cell_type": "code", 877 | "execution_count": null, 878 | "id": "e474b578", 879 | "metadata": {}, 880 | "outputs": [], 881 | "source": [] 882 | }, 883 | { 884 | "cell_type": "code", 885 | "execution_count": null, 886 | "id": "71dac0e9", 887 | "metadata": {}, 888 | "outputs": [], 889 | "source": [] 890 | }, 891 | { 892 | "cell_type": "code", 893 | "execution_count": null, 894 | "id": "89b53729", 895 | "metadata": {}, 896 | "outputs": [], 897 | "source": [] 898 | }, 899 | { 900 | "cell_type": "code", 901 | "execution_count": null, 902 | "id": "cb810b06", 903 | "metadata": {}, 904 | "outputs": [], 905 | "source": [] 906 | }, 907 | { 908 | "cell_type": "code", 909 | "execution_count": null, 910 | "id": "e207c4ec", 911 | "metadata": {}, 912 | "outputs": [], 913 | "source": [] 914 | }, 915 | { 916 | "cell_type": "code", 917 | "execution_count": null, 918 | "id": "e4eafa7f", 919 | "metadata": {}, 920 | "outputs": [], 921 | "source": [] 922 | }, 923 | { 924 | "cell_type": "markdown", 925 | "id": "7b86c4bd", 926 | "metadata": {}, 927 | "source": [ 928 | "## Doubts" 929 | ] 930 | }, 931 | { 932 | "cell_type": "code", 933 | "execution_count": null, 934 | "id": "afeb2169", 935 | "metadata": {}, 936 | "outputs": [], 937 | "source": [] 938 | } 939 | ], 940 | "metadata": { 941 | "kernelspec": { 942 | "display_name": "Python 3 (ipykernel)", 943 | "language": "python", 944 | "name": "python3" 945 | }, 946 | "language_info": { 947 | "codemirror_mode": { 948 | "name": "ipython", 949 | "version": 3 950 | }, 951 | "file_extension": ".py", 952 | "mimetype": "text/x-python", 953 | "name": "python", 954 | "nbconvert_exporter": "python", 955 | "pygments_lexer": "ipython3", 956 | "version": "3.9.12" 957 | } 958 | }, 959 | "nbformat": 4, 960 | "nbformat_minor": 5 961 | } 962 | -------------------------------------------------------------------------------- /Sorting_3.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "8e27127d", 6 | "metadata": {}, 7 | "source": [ 8 | "# DSML Intermediate : Time Complexity and Sorting III" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "9189e77a", 14 | "metadata": {}, 15 | "source": [ 16 | "## Recap" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "id": "baf88995", 22 | "metadata": {}, 23 | "source": [ 24 | "### Quizzes" 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "id": "5a18f9c5", 30 | "metadata": {}, 31 | "source": [ 32 | "Big-O:\n", 33 | "\n", 34 | "0. Calculate no of iterations / operations in the program\n", 35 | "1. Drop off constants\n", 36 | "2. Take only the highest power in terms of the input (N)" 37 | ] 38 | }, 39 | { 40 | "cell_type": "markdown", 41 | "id": "c2427a5e", 42 | "metadata": {}, 43 | "source": [ 44 | "### Quiz - 2" 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "id": "8edd0e11", 50 | "metadata": {}, 51 | "source": [ 52 | "for (i = 0; i < N; i++) {\n", 53 | " for (j = 0; j < N; j++) {\n", 54 | " if (numbers[i] < numbers[j]) {\n", 55 | " ++eqPerms\n", 56 | " }\n", 57 | " else {\n", 58 | " ++neqPerms\n", 59 | " }\n", 60 | " }\n", 61 | "}" 62 | ] 63 | }, 64 | { 65 | "cell_type": "markdown", 66 | "id": "dfadc62b", 67 | "metadata": {}, 68 | "source": [ 69 | "for i in range(N):\n", 70 | " for j in range(N):\n", 71 | " if numbers[i] < numbers[j]:\n", 72 | " eqPerms += 1\n", 73 | " else:\n", 74 | " neqPerms += 1" 75 | ] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "id": "8bfcb5fa", 80 | "metadata": {}, 81 | "source": [ 82 | "### Quiz - 4" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "id": "279627b5", 88 | "metadata": {}, 89 | "source": [ 90 | "for (i = 0; i < N; ++i) {\n", 91 | " if ((i % 2) == 0) {\n", 92 | " outVal[i] = inVals[i] * i\n", 93 | " }\n", 94 | "}" 95 | ] 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "id": "fbac1d41", 100 | "metadata": {}, 101 | "source": [ 102 | "for i in range(N):\n", 103 | " if i % 2 == 0:\n", 104 | " outVal[i] = inVal[i] * i" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": null, 110 | "id": "5bff07af", 111 | "metadata": {}, 112 | "outputs": [], 113 | "source": [ 114 | "i = 0\n", 115 | "while i < N:\n", 116 | " if i % 2 == 0:\n", 117 | " outVal[i] = inVal[i] * i\n", 118 | " i += 1" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": null, 124 | "id": "5a8b7a69", 125 | "metadata": {}, 126 | "outputs": [], 127 | "source": [] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": null, 132 | "id": "9d58cb21", 133 | "metadata": {}, 134 | "outputs": [], 135 | "source": [] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": null, 140 | "id": "62f1f855", 141 | "metadata": {}, 142 | "outputs": [], 143 | "source": [] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": null, 148 | "id": "12c1a86f", 149 | "metadata": {}, 150 | "outputs": [], 151 | "source": [] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": null, 156 | "id": "d48df211", 157 | "metadata": {}, 158 | "outputs": [], 159 | "source": [] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": null, 164 | "id": "59ab34a2", 165 | "metadata": {}, 166 | "outputs": [], 167 | "source": [] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": null, 172 | "id": "11729fa5", 173 | "metadata": {}, 174 | "outputs": [], 175 | "source": [] 176 | }, 177 | { 178 | "cell_type": "markdown", 179 | "id": "191ee542", 180 | "metadata": {}, 181 | "source": [ 182 | "## Sorting" 183 | ] 184 | }, 185 | { 186 | "cell_type": "markdown", 187 | "id": "242536e9", 188 | "metadata": {}, 189 | "source": [ 190 | "### Bubble Sort" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": 14, 196 | "id": "47e8ba38", 197 | "metadata": {}, 198 | "outputs": [], 199 | "source": [ 200 | "def bubble_sort(a):\n", 201 | " N = len(a) # get the length\n", 202 | " for i in range(N - 1):\n", 203 | " j = 0\n", 204 | " while j < N - 1:\n", 205 | " if a[j] > a[j + 1]:\n", 206 | " # swap larger with smaller\n", 207 | " a[j], a[j + 1] = a[j + 1], a[j]\n", 208 | " j += 1\n", 209 | " print(a)\n", 210 | " print('-'*30)\n", 211 | " return a" 212 | ] 213 | }, 214 | { 215 | "cell_type": "code", 216 | "execution_count": 15, 217 | "id": "af47d2a3", 218 | "metadata": {}, 219 | "outputs": [ 220 | { 221 | "name": "stdout", 222 | "output_type": "stream", 223 | "text": [ 224 | "[8, 7, 6, 5, 9]\n", 225 | "------------------------------\n", 226 | "[7, 6, 5, 8, 9]\n", 227 | "------------------------------\n", 228 | "[6, 5, 7, 8, 9]\n", 229 | "------------------------------\n", 230 | "[5, 6, 7, 8, 9]\n", 231 | "------------------------------\n" 232 | ] 233 | }, 234 | { 235 | "data": { 236 | "text/plain": [ 237 | "[5, 6, 7, 8, 9]" 238 | ] 239 | }, 240 | "execution_count": 15, 241 | "metadata": {}, 242 | "output_type": "execute_result" 243 | } 244 | ], 245 | "source": [ 246 | "bubble_sort([9, 8, 7, 6, 5])" 247 | ] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "execution_count": 40, 252 | "id": "bdf3ee05", 253 | "metadata": {}, 254 | "outputs": [], 255 | "source": [ 256 | "def bubble_sort(a):\n", 257 | " N = len(a) # get the length\n", 258 | " for i in range(N - 1):\n", 259 | " for j in range(N - 1):\n", 260 | " if a[j] > a[j + 1]:\n", 261 | " # swap larger with smaller\n", 262 | " a[j], a[j + 1] = a[j + 1], a[j]\n", 263 | " print(a)\n", 264 | " print('-'*30)\n", 265 | " return a" 266 | ] 267 | }, 268 | { 269 | "cell_type": "code", 270 | "execution_count": 41, 271 | "id": "d45635f4", 272 | "metadata": {}, 273 | "outputs": [ 274 | { 275 | "name": "stdout", 276 | "output_type": "stream", 277 | "text": [ 278 | "[10, 9, 8, 7, 6, 5, 11]\n", 279 | "------------------------------\n", 280 | "[9, 8, 7, 6, 5, 10, 11]\n", 281 | "------------------------------\n", 282 | "[8, 7, 6, 5, 9, 10, 11]\n", 283 | "------------------------------\n", 284 | "[7, 6, 5, 8, 9, 10, 11]\n", 285 | "------------------------------\n", 286 | "[6, 5, 7, 8, 9, 10, 11]\n", 287 | "------------------------------\n", 288 | "[5, 6, 7, 8, 9, 10, 11]\n", 289 | "------------------------------\n" 290 | ] 291 | }, 292 | { 293 | "data": { 294 | "text/plain": [ 295 | "[5, 6, 7, 8, 9, 10, 11]" 296 | ] 297 | }, 298 | "execution_count": 41, 299 | "metadata": {}, 300 | "output_type": "execute_result" 301 | } 302 | ], 303 | "source": [ 304 | "bubble_sort([11, 10, 9, 8, 7, 6, 5])" 305 | ] 306 | }, 307 | { 308 | "cell_type": "code", 309 | "execution_count": 42, 310 | "id": "530fcb0c", 311 | "metadata": {}, 312 | "outputs": [], 313 | "source": [ 314 | "def bubble_sort(a):\n", 315 | " N = len(a) # get the length\n", 316 | " for i in range(N - 1):\n", 317 | " for j in range(N - i - 1):\n", 318 | " if a[j] > a[j + 1]:\n", 319 | " # swap larger with smaller\n", 320 | " a[j], a[j + 1] = a[j + 1], a[j]\n", 321 | " print(a)\n", 322 | " print('-'*30)\n", 323 | " return a" 324 | ] 325 | }, 326 | { 327 | "cell_type": "code", 328 | "execution_count": 43, 329 | "id": "b9705b9a", 330 | "metadata": {}, 331 | "outputs": [ 332 | { 333 | "name": "stdout", 334 | "output_type": "stream", 335 | "text": [ 336 | "[10, 9, 8, 7, 6, 5, 11]\n", 337 | "------------------------------\n", 338 | "[9, 8, 7, 6, 5, 10, 11]\n", 339 | "------------------------------\n", 340 | "[8, 7, 6, 5, 9, 10, 11]\n", 341 | "------------------------------\n", 342 | "[7, 6, 5, 8, 9, 10, 11]\n", 343 | "------------------------------\n", 344 | "[6, 5, 7, 8, 9, 10, 11]\n", 345 | "------------------------------\n", 346 | "[5, 6, 7, 8, 9, 10, 11]\n", 347 | "------------------------------\n" 348 | ] 349 | }, 350 | { 351 | "data": { 352 | "text/plain": [ 353 | "[5, 6, 7, 8, 9, 10, 11]" 354 | ] 355 | }, 356 | "execution_count": 43, 357 | "metadata": {}, 358 | "output_type": "execute_result" 359 | } 360 | ], 361 | "source": [ 362 | "bubble_sort([11, 10, 9, 8, 7, 6, 5])" 363 | ] 364 | }, 365 | { 366 | "cell_type": "code", 367 | "execution_count": 44, 368 | "id": "8cf80577", 369 | "metadata": {}, 370 | "outputs": [ 371 | { 372 | "name": "stdout", 373 | "output_type": "stream", 374 | "text": [ 375 | "[4, 1, 2, 5, 7, 0, 8, 10]\n", 376 | "------------------------------\n", 377 | "[1, 2, 4, 5, 0, 7, 8, 10]\n", 378 | "------------------------------\n", 379 | "[1, 2, 4, 0, 5, 7, 8, 10]\n", 380 | "------------------------------\n", 381 | "[1, 2, 0, 4, 5, 7, 8, 10]\n", 382 | "------------------------------\n", 383 | "[1, 0, 2, 4, 5, 7, 8, 10]\n", 384 | "------------------------------\n", 385 | "[0, 1, 2, 4, 5, 7, 8, 10]\n", 386 | "------------------------------\n", 387 | "[0, 1, 2, 4, 5, 7, 8, 10]\n", 388 | "------------------------------\n" 389 | ] 390 | }, 391 | { 392 | "data": { 393 | "text/plain": [ 394 | "[0, 1, 2, 4, 5, 7, 8, 10]" 395 | ] 396 | }, 397 | "execution_count": 44, 398 | "metadata": {}, 399 | "output_type": "execute_result" 400 | } 401 | ], 402 | "source": [ 403 | "bubble_sort([5, 4, 1, 2, 7, 8, 0, 10])" 404 | ] 405 | }, 406 | { 407 | "cell_type": "code", 408 | "execution_count": 45, 409 | "id": "9d7e22b7", 410 | "metadata": {}, 411 | "outputs": [ 412 | { 413 | "name": "stdout", 414 | "output_type": "stream", 415 | "text": [ 416 | "[1, 2, 3, 4, 5, 6, 7]\n", 417 | "------------------------------\n", 418 | "[1, 2, 3, 4, 5, 6, 7]\n", 419 | "------------------------------\n", 420 | "[1, 2, 3, 4, 5, 6, 7]\n", 421 | "------------------------------\n", 422 | "[1, 2, 3, 4, 5, 6, 7]\n", 423 | "------------------------------\n", 424 | "[1, 2, 3, 4, 5, 6, 7]\n", 425 | "------------------------------\n", 426 | "[1, 2, 3, 4, 5, 6, 7]\n", 427 | "------------------------------\n" 428 | ] 429 | }, 430 | { 431 | "data": { 432 | "text/plain": [ 433 | "[1, 2, 3, 4, 5, 6, 7]" 434 | ] 435 | }, 436 | "execution_count": 45, 437 | "metadata": {}, 438 | "output_type": "execute_result" 439 | } 440 | ], 441 | "source": [ 442 | "bubble_sort([1, 2, 3, 5, 4, 7, 6])" 443 | ] 444 | }, 445 | { 446 | "cell_type": "code", 447 | "execution_count": null, 448 | "id": "aab39562", 449 | "metadata": {}, 450 | "outputs": [], 451 | "source": [] 452 | }, 453 | { 454 | "cell_type": "code", 455 | "execution_count": null, 456 | "id": "319236b0", 457 | "metadata": {}, 458 | "outputs": [], 459 | "source": [] 460 | }, 461 | { 462 | "cell_type": "markdown", 463 | "id": "4cd9d797", 464 | "metadata": {}, 465 | "source": [ 466 | "### Optimized Bubble Sort" 467 | ] 468 | }, 469 | { 470 | "cell_type": "code", 471 | "execution_count": 46, 472 | "id": "30764662", 473 | "metadata": {}, 474 | "outputs": [], 475 | "source": [ 476 | "def optimized_bubble_sort(a):\n", 477 | " N = len(a) # get the length\n", 478 | " for i in range(N - 1):\n", 479 | " num_swaps = 0\n", 480 | " for j in range(N - i - 1):\n", 481 | " if a[j] > a[j + 1]:\n", 482 | " # swap larger with smaller\n", 483 | " a[j], a[j + 1] = a[j + 1], a[j]\n", 484 | " num_swaps += 1\n", 485 | " if num_swaps == 0:\n", 486 | " break\n", 487 | " print(a)\n", 488 | " print('-'*30)\n", 489 | " return a" 490 | ] 491 | }, 492 | { 493 | "cell_type": "code", 494 | "execution_count": 47, 495 | "id": "093b0d83", 496 | "metadata": {}, 497 | "outputs": [ 498 | { 499 | "name": "stdout", 500 | "output_type": "stream", 501 | "text": [ 502 | "[4, 4, 2, 5, 5, 6]\n", 503 | "------------------------------\n", 504 | "[4, 2, 4, 5, 5, 6]\n", 505 | "------------------------------\n", 506 | "[2, 4, 4, 5, 5, 6]\n", 507 | "------------------------------\n" 508 | ] 509 | }, 510 | { 511 | "data": { 512 | "text/plain": [ 513 | "[2, 4, 4, 5, 5, 6]" 514 | ] 515 | }, 516 | "execution_count": 47, 517 | "metadata": {}, 518 | "output_type": "execute_result" 519 | } 520 | ], 521 | "source": [ 522 | "optimized_bubble_sort([4, 5, 4, 2, 5, 6])" 523 | ] 524 | }, 525 | { 526 | "cell_type": "code", 527 | "execution_count": 49, 528 | "id": "914b345b", 529 | "metadata": {}, 530 | "outputs": [ 531 | { 532 | "name": "stdout", 533 | "output_type": "stream", 534 | "text": [ 535 | "[6, 5, 4, 3, 2, 1, 7]\n", 536 | "------------------------------\n", 537 | "[5, 4, 3, 2, 1, 6, 7]\n", 538 | "------------------------------\n", 539 | "[4, 3, 2, 1, 5, 6, 7]\n", 540 | "------------------------------\n", 541 | "[3, 2, 1, 4, 5, 6, 7]\n", 542 | "------------------------------\n", 543 | "[2, 1, 3, 4, 5, 6, 7]\n", 544 | "------------------------------\n", 545 | "[1, 2, 3, 4, 5, 6, 7]\n", 546 | "------------------------------\n" 547 | ] 548 | }, 549 | { 550 | "data": { 551 | "text/plain": [ 552 | "[1, 2, 3, 4, 5, 6, 7]" 553 | ] 554 | }, 555 | "execution_count": 49, 556 | "metadata": {}, 557 | "output_type": "execute_result" 558 | } 559 | ], 560 | "source": [ 561 | "optimized_bubble_sort([7, 6, 5, 4, 3, 2, 1])" 562 | ] 563 | }, 564 | { 565 | "cell_type": "markdown", 566 | "id": "83d20774", 567 | "metadata": {}, 568 | "source": [ 569 | "Worst case time complexity: O(N^2)" 570 | ] 571 | }, 572 | { 573 | "cell_type": "markdown", 574 | "id": "0005da5b", 575 | "metadata": {}, 576 | "source": [ 577 | "Best case time complexity: O(N)" 578 | ] 579 | }, 580 | { 581 | "cell_type": "code", 582 | "execution_count": 50, 583 | "id": "92e3f82d", 584 | "metadata": {}, 585 | "outputs": [ 586 | { 587 | "data": { 588 | "text/plain": [ 589 | "[1, 2, 3, 4, 5]" 590 | ] 591 | }, 592 | "execution_count": 50, 593 | "metadata": {}, 594 | "output_type": "execute_result" 595 | } 596 | ], 597 | "source": [ 598 | "optimized_bubble_sort([1, 2, 3, 4, 5])" 599 | ] 600 | }, 601 | { 602 | "cell_type": "code", 603 | "execution_count": 51, 604 | "id": "7b28e4b8", 605 | "metadata": {}, 606 | "outputs": [ 607 | { 608 | "name": "stdout", 609 | "output_type": "stream", 610 | "text": [ 611 | "[1, 2, 3, 4, 5, 7, 10, 10, 11, 111]\n", 612 | "------------------------------\n" 613 | ] 614 | }, 615 | { 616 | "data": { 617 | "text/plain": [ 618 | "[1, 2, 3, 4, 5, 7, 10, 10, 11, 111]" 619 | ] 620 | }, 621 | "execution_count": 51, 622 | "metadata": {}, 623 | "output_type": "execute_result" 624 | } 625 | ], 626 | "source": [ 627 | "optimized_bubble_sort([1, 2, 3, 4, 5, 7, 10, 10, 111, 11])" 628 | ] 629 | }, 630 | { 631 | "cell_type": "code", 632 | "execution_count": 53, 633 | "id": "605f99ed", 634 | "metadata": {}, 635 | "outputs": [], 636 | "source": [ 637 | "# optimized_bubble_sort(list(range(1, 10000)))" 638 | ] 639 | }, 640 | { 641 | "cell_type": "markdown", 642 | "id": "90f0211e", 643 | "metadata": {}, 644 | "source": [ 645 | "In best case, we do not take any specific N value, we take a specific\n", 646 | "type of input which will run faster (have lesser iterations)." 647 | ] 648 | }, 649 | { 650 | "cell_type": "markdown", 651 | "id": "ce3f689a", 652 | "metadata": {}, 653 | "source": [ 654 | "### Selection Sort" 655 | ] 656 | }, 657 | { 658 | "cell_type": "markdown", 659 | "id": "0af30b24", 660 | "metadata": {}, 661 | "source": [ 662 | "Examples" 663 | ] 664 | }, 665 | { 666 | "cell_type": "code", 667 | "execution_count": 31, 668 | "id": "e22db304", 669 | "metadata": {}, 670 | "outputs": [], 671 | "source": [ 672 | "def selection_sort(a):\n", 673 | " # use for loop\n", 674 | " pass" 675 | ] 676 | }, 677 | { 678 | "cell_type": "code", 679 | "execution_count": 32, 680 | "id": "ea98260a", 681 | "metadata": {}, 682 | "outputs": [], 683 | "source": [ 684 | "selection_sort([5, 4, 1, 2, 7, 8, 0, 10])" 685 | ] 686 | }, 687 | { 688 | "cell_type": "code", 689 | "execution_count": null, 690 | "id": "0de0ac37", 691 | "metadata": {}, 692 | "outputs": [], 693 | "source": [] 694 | }, 695 | { 696 | "cell_type": "code", 697 | "execution_count": null, 698 | "id": "831a505d", 699 | "metadata": {}, 700 | "outputs": [], 701 | "source": [] 702 | }, 703 | { 704 | "cell_type": "code", 705 | "execution_count": null, 706 | "id": "83ddaca5", 707 | "metadata": {}, 708 | "outputs": [], 709 | "source": [] 710 | }, 711 | { 712 | "cell_type": "code", 713 | "execution_count": null, 714 | "id": "11a29a0e", 715 | "metadata": {}, 716 | "outputs": [], 717 | "source": [] 718 | }, 719 | { 720 | "cell_type": "code", 721 | "execution_count": null, 722 | "id": "397d0eae", 723 | "metadata": {}, 724 | "outputs": [], 725 | "source": [] 726 | }, 727 | { 728 | "cell_type": "code", 729 | "execution_count": null, 730 | "id": "211792c6", 731 | "metadata": {}, 732 | "outputs": [], 733 | "source": [] 734 | }, 735 | { 736 | "cell_type": "code", 737 | "execution_count": null, 738 | "id": "4c175a4b", 739 | "metadata": {}, 740 | "outputs": [], 741 | "source": [] 742 | }, 743 | { 744 | "cell_type": "code", 745 | "execution_count": null, 746 | "id": "2cff8d2b", 747 | "metadata": {}, 748 | "outputs": [], 749 | "source": [] 750 | }, 751 | { 752 | "cell_type": "code", 753 | "execution_count": null, 754 | "id": "aa7d53f8", 755 | "metadata": {}, 756 | "outputs": [], 757 | "source": [] 758 | }, 759 | { 760 | "cell_type": "code", 761 | "execution_count": null, 762 | "id": "89663ef4", 763 | "metadata": {}, 764 | "outputs": [], 765 | "source": [] 766 | }, 767 | { 768 | "cell_type": "code", 769 | "execution_count": null, 770 | "id": "bf061415", 771 | "metadata": {}, 772 | "outputs": [], 773 | "source": [] 774 | }, 775 | { 776 | "cell_type": "code", 777 | "execution_count": null, 778 | "id": "d735a9fd", 779 | "metadata": {}, 780 | "outputs": [], 781 | "source": [] 782 | }, 783 | { 784 | "cell_type": "code", 785 | "execution_count": null, 786 | "id": "355e26c4", 787 | "metadata": {}, 788 | "outputs": [], 789 | "source": [] 790 | }, 791 | { 792 | "cell_type": "code", 793 | "execution_count": null, 794 | "id": "a4bcdf7f", 795 | "metadata": {}, 796 | "outputs": [], 797 | "source": [] 798 | }, 799 | { 800 | "cell_type": "code", 801 | "execution_count": null, 802 | "id": "e15278c8", 803 | "metadata": {}, 804 | "outputs": [], 805 | "source": [] 806 | }, 807 | { 808 | "cell_type": "code", 809 | "execution_count": null, 810 | "id": "0325c61d", 811 | "metadata": {}, 812 | "outputs": [], 813 | "source": [] 814 | }, 815 | { 816 | "cell_type": "code", 817 | "execution_count": null, 818 | "id": "59973cd9", 819 | "metadata": {}, 820 | "outputs": [], 821 | "source": [] 822 | }, 823 | { 824 | "cell_type": "code", 825 | "execution_count": null, 826 | "id": "841c58ae", 827 | "metadata": {}, 828 | "outputs": [], 829 | "source": [] 830 | }, 831 | { 832 | "cell_type": "code", 833 | "execution_count": null, 834 | "id": "07d09fb4", 835 | "metadata": {}, 836 | "outputs": [], 837 | "source": [] 838 | }, 839 | { 840 | "cell_type": "code", 841 | "execution_count": null, 842 | "id": "bc96945b", 843 | "metadata": {}, 844 | "outputs": [], 845 | "source": [] 846 | }, 847 | { 848 | "cell_type": "code", 849 | "execution_count": null, 850 | "id": "3bea5afe", 851 | "metadata": {}, 852 | "outputs": [], 853 | "source": [] 854 | }, 855 | { 856 | "cell_type": "code", 857 | "execution_count": null, 858 | "id": "70336078", 859 | "metadata": {}, 860 | "outputs": [], 861 | "source": [] 862 | }, 863 | { 864 | "cell_type": "code", 865 | "execution_count": null, 866 | "id": "8d128605", 867 | "metadata": {}, 868 | "outputs": [], 869 | "source": [] 870 | }, 871 | { 872 | "cell_type": "code", 873 | "execution_count": null, 874 | "id": "b7a0ab93", 875 | "metadata": {}, 876 | "outputs": [], 877 | "source": [] 878 | }, 879 | { 880 | "cell_type": "code", 881 | "execution_count": null, 882 | "id": "6cfc90f8", 883 | "metadata": {}, 884 | "outputs": [], 885 | "source": [] 886 | }, 887 | { 888 | "cell_type": "code", 889 | "execution_count": null, 890 | "id": "917efb27", 891 | "metadata": {}, 892 | "outputs": [], 893 | "source": [] 894 | }, 895 | { 896 | "cell_type": "code", 897 | "execution_count": null, 898 | "id": "d9f9bc50", 899 | "metadata": {}, 900 | "outputs": [], 901 | "source": [] 902 | }, 903 | { 904 | "cell_type": "code", 905 | "execution_count": null, 906 | "id": "be011254", 907 | "metadata": {}, 908 | "outputs": [], 909 | "source": [] 910 | }, 911 | { 912 | "cell_type": "code", 913 | "execution_count": null, 914 | "id": "6cd4a3aa", 915 | "metadata": {}, 916 | "outputs": [], 917 | "source": [] 918 | }, 919 | { 920 | "cell_type": "code", 921 | "execution_count": null, 922 | "id": "b3bf4faa", 923 | "metadata": {}, 924 | "outputs": [], 925 | "source": [] 926 | }, 927 | { 928 | "cell_type": "code", 929 | "execution_count": null, 930 | "id": "757074c8", 931 | "metadata": {}, 932 | "outputs": [], 933 | "source": [] 934 | }, 935 | { 936 | "cell_type": "code", 937 | "execution_count": null, 938 | "id": "e474b578", 939 | "metadata": {}, 940 | "outputs": [], 941 | "source": [] 942 | }, 943 | { 944 | "cell_type": "code", 945 | "execution_count": null, 946 | "id": "71dac0e9", 947 | "metadata": {}, 948 | "outputs": [], 949 | "source": [] 950 | }, 951 | { 952 | "cell_type": "code", 953 | "execution_count": null, 954 | "id": "89b53729", 955 | "metadata": {}, 956 | "outputs": [], 957 | "source": [] 958 | }, 959 | { 960 | "cell_type": "code", 961 | "execution_count": null, 962 | "id": "cb810b06", 963 | "metadata": {}, 964 | "outputs": [], 965 | "source": [] 966 | }, 967 | { 968 | "cell_type": "code", 969 | "execution_count": null, 970 | "id": "e207c4ec", 971 | "metadata": {}, 972 | "outputs": [], 973 | "source": [] 974 | }, 975 | { 976 | "cell_type": "code", 977 | "execution_count": null, 978 | "id": "e4eafa7f", 979 | "metadata": {}, 980 | "outputs": [], 981 | "source": [] 982 | }, 983 | { 984 | "cell_type": "code", 985 | "execution_count": null, 986 | "id": "afeb2169", 987 | "metadata": {}, 988 | "outputs": [], 989 | "source": [] 990 | } 991 | ], 992 | "metadata": { 993 | "kernelspec": { 994 | "display_name": "Python 3 (ipykernel)", 995 | "language": "python", 996 | "name": "python3" 997 | }, 998 | "language_info": { 999 | "codemirror_mode": { 1000 | "name": "ipython", 1001 | "version": 3 1002 | }, 1003 | "file_extension": ".py", 1004 | "mimetype": "text/x-python", 1005 | "name": "python", 1006 | "nbconvert_exporter": "python", 1007 | "pygments_lexer": "ipython3", 1008 | "version": "3.9.12" 1009 | } 1010 | }, 1011 | "nbformat": 4, 1012 | "nbformat_minor": 5 1013 | } 1014 | --------------------------------------------------------------------------------