├── .gitignore ├── .ipynb_checkpoints ├── 01_Introduction to Python-checkpoint.ipynb ├── 02_Operators-checkpoint.ipynb └── 03_Conditional Statements-checkpoint.ipynb ├── 01_Introduction to Python.ipynb ├── 02_Operators.ipynb ├── 03_Conditional Statements.ipynb ├── 04_05_While Loop.ipynb ├── 06_Nested While Loops.ipynb ├── 07_List.ipynb ├── 08_Strings.ipynb ├── 09_For Loop.ipynb ├── 10_Functions.ipynb ├── 11_Dictionary.ipynb ├── 12_Tuples and Sets.ipynb ├── 13_14_15_Object Oriented Programming.ipynb ├── 16_File Handling ├── .ipynb_checkpoints │ └── 16_File Handling-checkpoint.ipynb ├── 16_File Handling.ipynb ├── indore.txt ├── mumbai.txt ├── nyc.txt └── queens.txt ├── 17_Exception Handling.ipynb ├── 18_Regular Expression.ipynb ├── 19_Modules and Packages ├── 19_Inbuilt Modules.ipynb ├── __pycache__ │ └── indore.cpython-310.pyc ├── indore.py ├── main.py └── statisticsmodels │ ├── __init__.py │ ├── __pycache__ │ ├── __init__.cpython-310.pyc │ └── descriptive.cpython-310.pyc │ ├── descriptive.py │ └── inference │ ├── __pycache__ │ └── inferential.cpython-310.pyc │ └── inferential.py ├── 20_21_22_23_24_Data Structures ├── .ipynb_checkpoints │ ├── 20_Data Structures Basics-checkpoint.ipynb │ ├── 21_Stack Data Structures-checkpoint.ipynb │ ├── 22_Queue-checkpoint.ipynb │ └── 23_24_Linked List-checkpoint.ipynb ├── 20_Data Structures Basics.ipynb ├── 21_Stack Data Structures.ipynb ├── 22_Queue.ipynb └── 23_24_Linked List.ipynb ├── 25_26_27_28_29_Algorithms ├── .ipynb_checkpoints │ ├── 25_26_List, Searching, Swapping, Sorting-checkpoint.ipynb │ ├── 27_Sorting Algorithms-checkpoint.ipynb │ └── 28_29_Recursion-checkpoint.ipynb ├── 25_26_List, Searching, Swapping, Sorting.ipynb ├── 27_Sorting Algorithms.ipynb └── 28_29_Recursion.ipynb ├── 30_Higher Order Functions.ipynb ├── 31_32_Virtual Environment and PIP ├── .gitignore ├── app.py ├── requirements.txt ├── static │ └── css │ │ └── style.css └── templates │ └── index.html ├── 33_Python and MongoDB ├── .gitignore └── app.py ├── Doubts.ipynb └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | /.ipynb_checkpoints 2 | 3 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/01_Introduction to Python-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "id": "f0db9246", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "session structure\n", 11 | "- work on topics/sub-topics\n", 12 | "- practice questions\n", 13 | " - concept based questions\n", 14 | " - basic sheets/warmup questions\n", 15 | " - algorithm based questions\n", 16 | "- discussions/enritchment" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "id": "cb80f6ca", 22 | "metadata": {}, 23 | "source": [ 24 | "# Python Basics" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 1, 30 | "id": "baa99f0a", 31 | "metadata": {}, 32 | "outputs": [ 33 | { 34 | "name": "stdout", 35 | "output_type": "stream", 36 | "text": [ 37 | "Hello World\n" 38 | ] 39 | } 40 | ], 41 | "source": [ 42 | "print(\"Hello World\")" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 2, 48 | "id": "ced1ee16", 49 | "metadata": {}, 50 | "outputs": [ 51 | { 52 | "name": "stdout", 53 | "output_type": "stream", 54 | "text": [ 55 | "Hello\n", 56 | "World\n" 57 | ] 58 | } 59 | ], 60 | "source": [ 61 | "print(\"Hello\")\n", 62 | "print(\"World\")" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 3, 68 | "id": "7ee63fba", 69 | "metadata": {}, 70 | "outputs": [ 71 | { 72 | "name": "stdout", 73 | "output_type": "stream", 74 | "text": [ 75 | "Hello\n", 76 | "World\n" 77 | ] 78 | } 79 | ], 80 | "source": [ 81 | "# new line character - \\n\n", 82 | "\n", 83 | "print(\"Hello\\nWorld\")" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 4, 89 | "id": "91cf8b64", 90 | "metadata": {}, 91 | "outputs": [ 92 | { 93 | "name": "stdout", 94 | "output_type": "stream", 95 | "text": [ 96 | "Hello\tWorld\n" 97 | ] 98 | } 99 | ], 100 | "source": [ 101 | "# horizontal tab - \\t\n", 102 | "\n", 103 | "print(\"Hello\\tWorld\")" 104 | ] 105 | }, 106 | { 107 | "cell_type": "markdown", 108 | "id": "ba0cacfd", 109 | "metadata": {}, 110 | "source": [ 111 | "# Questions\n", 112 | "\n", 113 | "**WAP to print following output**" 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": null, 119 | "id": "72427f79", 120 | "metadata": {}, 121 | "outputs": [], 122 | "source": [ 123 | "name\n", 124 | " company name\n", 125 | " country" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 5, 131 | "id": "94f434ac", 132 | "metadata": {}, 133 | "outputs": [ 134 | { 135 | "name": "stdout", 136 | "output_type": "stream", 137 | "text": [ 138 | "Himanshu\n", 139 | "\tGoogle\n", 140 | "\t\tIndia\n" 141 | ] 142 | } 143 | ], 144 | "source": [ 145 | "print(\"Himanshu\\n\\tGoogle\\n\\t\\tIndia\")" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": null, 151 | "id": "2365cd55", 152 | "metadata": {}, 153 | "outputs": [], 154 | "source": [ 155 | "*\n", 156 | "**\n", 157 | "***" 158 | ] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": 6, 163 | "id": "19c97bfe", 164 | "metadata": {}, 165 | "outputs": [ 166 | { 167 | "name": "stdout", 168 | "output_type": "stream", 169 | "text": [ 170 | "*\n", 171 | "**\n", 172 | "***\n" 173 | ] 174 | } 175 | ], 176 | "source": [ 177 | "print(\"*\\n**\\n***\")" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": null, 183 | "id": "e00eea87", 184 | "metadata": {}, 185 | "outputs": [], 186 | "source": [ 187 | "--4--*\n", 188 | "*----8----*\n", 189 | "--4--*" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": 7, 195 | "id": "c8f7d924", 196 | "metadata": {}, 197 | "outputs": [ 198 | { 199 | "name": "stdout", 200 | "output_type": "stream", 201 | "text": [ 202 | "\t/2\n" 203 | ] 204 | } 205 | ], 206 | "source": [ 207 | "print(\"\\t/2\")" 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "execution_count": 8, 213 | "id": "7f03e602", 214 | "metadata": {}, 215 | "outputs": [ 216 | { 217 | "name": "stdout", 218 | "output_type": "stream", 219 | "text": [ 220 | " *\n", 221 | "*\t*\n", 222 | " *\n" 223 | ] 224 | } 225 | ], 226 | "source": [ 227 | "print(\" *\\n*\\t*\\n *\")" 228 | ] 229 | }, 230 | { 231 | "cell_type": "markdown", 232 | "id": "9b46414d", 233 | "metadata": {}, 234 | "source": [ 235 | "# Variables and Data Types" 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": 10, 241 | "id": "c781db95", 242 | "metadata": {}, 243 | "outputs": [ 244 | { 245 | "name": "stdout", 246 | "output_type": "stream", 247 | "text": [ 248 | "10\n" 249 | ] 250 | } 251 | ], 252 | "source": [ 253 | "a = 10\n", 254 | "\n", 255 | "print(a)" 256 | ] 257 | }, 258 | { 259 | "cell_type": "code", 260 | "execution_count": 11, 261 | "id": "3f706634", 262 | "metadata": {}, 263 | "outputs": [], 264 | "source": [ 265 | "# int a = 10;" 266 | ] 267 | }, 268 | { 269 | "cell_type": "code", 270 | "execution_count": 12, 271 | "id": "5245d98a", 272 | "metadata": {}, 273 | "outputs": [ 274 | { 275 | "name": "stdout", 276 | "output_type": "stream", 277 | "text": [ 278 | "15\n", 279 | "10\n" 280 | ] 281 | } 282 | ], 283 | "source": [ 284 | "a = 10\n", 285 | "\n", 286 | "print(a + 5)\n", 287 | "\n", 288 | "print(a)" 289 | ] 290 | }, 291 | { 292 | "cell_type": "code", 293 | "execution_count": 13, 294 | "id": "bf0d6fc0", 295 | "metadata": {}, 296 | "outputs": [ 297 | { 298 | "name": "stdout", 299 | "output_type": "stream", 300 | "text": [ 301 | "20\n" 302 | ] 303 | } 304 | ], 305 | "source": [ 306 | "a = 10\n", 307 | "\n", 308 | "a = 20\n", 309 | "\n", 310 | "print(a)" 311 | ] 312 | }, 313 | { 314 | "cell_type": "code", 315 | "execution_count": 14, 316 | "id": "be4f787f", 317 | "metadata": {}, 318 | "outputs": [ 319 | { 320 | "name": "stdout", 321 | "output_type": "stream", 322 | "text": [ 323 | "10\n", 324 | "\n" 325 | ] 326 | } 327 | ], 328 | "source": [ 329 | "a = 10\n", 330 | "\n", 331 | "print(a)\n", 332 | "\n", 333 | "print(type(a))" 334 | ] 335 | }, 336 | { 337 | "cell_type": "code", 338 | "execution_count": 15, 339 | "id": "5aa838e7", 340 | "metadata": {}, 341 | "outputs": [ 342 | { 343 | "name": "stdout", 344 | "output_type": "stream", 345 | "text": [ 346 | "10.65\n", 347 | "\n" 348 | ] 349 | } 350 | ], 351 | "source": [ 352 | "b = 10.65\n", 353 | "\n", 354 | "print(b)\n", 355 | "\n", 356 | "print(type(b))" 357 | ] 358 | }, 359 | { 360 | "cell_type": "code", 361 | "execution_count": 16, 362 | "id": "d727f7ef", 363 | "metadata": {}, 364 | "outputs": [ 365 | { 366 | "name": "stdout", 367 | "output_type": "stream", 368 | "text": [ 369 | "India\n", 370 | "\n" 371 | ] 372 | } 373 | ], 374 | "source": [ 375 | "c = \"India\"\n", 376 | "\n", 377 | "print(c)\n", 378 | "\n", 379 | "print(type(c))" 380 | ] 381 | }, 382 | { 383 | "cell_type": "code", 384 | "execution_count": 17, 385 | "id": "678e07c7", 386 | "metadata": {}, 387 | "outputs": [ 388 | { 389 | "name": "stdout", 390 | "output_type": "stream", 391 | "text": [ 392 | "\n" 393 | ] 394 | } 395 | ], 396 | "source": [ 397 | "d = \"A\"\n", 398 | "\n", 399 | "print(type(d))" 400 | ] 401 | }, 402 | { 403 | "cell_type": "markdown", 404 | "id": "b9d87137", 405 | "metadata": {}, 406 | "source": [ 407 | "# Input Function\n", 408 | "\n", 409 | "getting values from user" 410 | ] 411 | }, 412 | { 413 | "cell_type": "code", 414 | "execution_count": 18, 415 | "id": "6ff60820", 416 | "metadata": {}, 417 | "outputs": [ 418 | { 419 | "name": "stdout", 420 | "output_type": "stream", 421 | "text": [ 422 | "100\n", 423 | "100\n" 424 | ] 425 | } 426 | ], 427 | "source": [ 428 | "a = input()\n", 429 | "\n", 430 | "print(a)" 431 | ] 432 | }, 433 | { 434 | "cell_type": "code", 435 | "execution_count": 19, 436 | "id": "eed562be", 437 | "metadata": {}, 438 | "outputs": [ 439 | { 440 | "name": "stdout", 441 | "output_type": "stream", 442 | "text": [ 443 | "Enter a number50\n", 444 | "50\n" 445 | ] 446 | } 447 | ], 448 | "source": [ 449 | "a = input(\"Enter a number\")\n", 450 | "\n", 451 | "print(a)" 452 | ] 453 | }, 454 | { 455 | "cell_type": "code", 456 | "execution_count": 20, 457 | "id": "71d34a09", 458 | "metadata": {}, 459 | "outputs": [ 460 | { 461 | "name": "stdout", 462 | "output_type": "stream", 463 | "text": [ 464 | "Enter your Name: Himanshu\n", 465 | "Hello Himanshu\n" 466 | ] 467 | } 468 | ], 469 | "source": [ 470 | "name = input(\"Enter your Name: \")\n", 471 | "\n", 472 | "print(\"Hello\", name)" 473 | ] 474 | }, 475 | { 476 | "cell_type": "markdown", 477 | "id": "6871570c", 478 | "metadata": {}, 479 | "source": [ 480 | "# Type conversion" 481 | ] 482 | }, 483 | { 484 | "cell_type": "code", 485 | "execution_count": 21, 486 | "id": "8b20c936", 487 | "metadata": {}, 488 | "outputs": [ 489 | { 490 | "name": "stdout", 491 | "output_type": "stream", 492 | "text": [ 493 | "10.56\n", 494 | "\n", 495 | "10\n", 496 | "\n" 497 | ] 498 | } 499 | ], 500 | "source": [ 501 | "a = 10.56\n", 502 | "\n", 503 | "print(a)\n", 504 | "\n", 505 | "print(type(a))\n", 506 | "\n", 507 | "b = int(a)\n", 508 | "\n", 509 | "print(b)\n", 510 | "\n", 511 | "print(type(b))" 512 | ] 513 | }, 514 | { 515 | "cell_type": "code", 516 | "execution_count": 22, 517 | "id": "3160f98d", 518 | "metadata": {}, 519 | "outputs": [ 520 | { 521 | "name": "stdout", 522 | "output_type": "stream", 523 | "text": [ 524 | "10\n", 525 | "\n", 526 | "10.0\n", 527 | "\n" 528 | ] 529 | } 530 | ], 531 | "source": [ 532 | "a = \"10\"\n", 533 | "\n", 534 | "print(a)\n", 535 | "\n", 536 | "print(type(a))\n", 537 | "\n", 538 | "b = float(a)\n", 539 | "\n", 540 | "print(b)\n", 541 | "\n", 542 | "print(type(b))" 543 | ] 544 | }, 545 | { 546 | "cell_type": "markdown", 547 | "id": "6aa08d19", 548 | "metadata": {}, 549 | "source": [ 550 | "# Question\n", 551 | "\n", 552 | "**WAP to get 2 values from user and print addition of them.**" 553 | ] 554 | }, 555 | { 556 | "cell_type": "code", 557 | "execution_count": 23, 558 | "id": "59db447a", 559 | "metadata": {}, 560 | "outputs": [ 561 | { 562 | "name": "stdout", 563 | "output_type": "stream", 564 | "text": [ 565 | "Enter 1st value: 10\n", 566 | "Enter 2nd Value: 20\n", 567 | "1020\n" 568 | ] 569 | } 570 | ], 571 | "source": [ 572 | "a = input(\"Enter 1st value: \")\n", 573 | "b = input(\"Enter 2nd Value: \")\n", 574 | "\n", 575 | "c = a + b\n", 576 | "\n", 577 | "print(c)" 578 | ] 579 | }, 580 | { 581 | "cell_type": "code", 582 | "execution_count": 24, 583 | "id": "af4e38d6", 584 | "metadata": {}, 585 | "outputs": [ 586 | { 587 | "data": { 588 | "text/plain": [ 589 | "'1020'" 590 | ] 591 | }, 592 | "execution_count": 24, 593 | "metadata": {}, 594 | "output_type": "execute_result" 595 | } 596 | ], 597 | "source": [ 598 | "\"10\" + \"20\"" 599 | ] 600 | }, 601 | { 602 | "cell_type": "code", 603 | "execution_count": 25, 604 | "id": "9794ef6b", 605 | "metadata": {}, 606 | "outputs": [ 607 | { 608 | "data": { 609 | "text/plain": [ 610 | "30" 611 | ] 612 | }, 613 | "execution_count": 25, 614 | "metadata": {}, 615 | "output_type": "execute_result" 616 | } 617 | ], 618 | "source": [ 619 | "10 + 20" 620 | ] 621 | }, 622 | { 623 | "cell_type": "code", 624 | "execution_count": 26, 625 | "id": "72a14331", 626 | "metadata": {}, 627 | "outputs": [ 628 | { 629 | "name": "stdout", 630 | "output_type": "stream", 631 | "text": [ 632 | "\n" 633 | ] 634 | } 635 | ], 636 | "source": [ 637 | "print(type(a))" 638 | ] 639 | }, 640 | { 641 | "cell_type": "code", 642 | "execution_count": 27, 643 | "id": "18e38566", 644 | "metadata": {}, 645 | "outputs": [ 646 | { 647 | "name": "stdout", 648 | "output_type": "stream", 649 | "text": [ 650 | "Enter 1st value: 10\n", 651 | "Enter 2nd Value: 20\n", 652 | "30\n" 653 | ] 654 | } 655 | ], 656 | "source": [ 657 | "a = int(input(\"Enter 1st value: \"))\n", 658 | "b = int(input(\"Enter 2nd Value: \"))\n", 659 | "\n", 660 | "c = a + b\n", 661 | "\n", 662 | "print(c)" 663 | ] 664 | }, 665 | { 666 | "cell_type": "code", 667 | "execution_count": 28, 668 | "id": "69430910", 669 | "metadata": {}, 670 | "outputs": [ 671 | { 672 | "name": "stdout", 673 | "output_type": "stream", 674 | "text": [ 675 | "Enter 1st value: 10\n", 676 | "Enter 2nd Value: 20\n", 677 | "Addition = 30\n" 678 | ] 679 | } 680 | ], 681 | "source": [ 682 | "a = int(input(\"Enter 1st value: \"))\n", 683 | "b = int(input(\"Enter 2nd Value: \"))\n", 684 | "\n", 685 | "c = a + b\n", 686 | "\n", 687 | "print(\"Addition =\", c)" 688 | ] 689 | }, 690 | { 691 | "cell_type": "markdown", 692 | "id": "c619ff33", 693 | "metadata": {}, 694 | "source": [ 695 | "**WAP to enter 2 values from user and print addition, substraction, multiplication and division with proper message**" 696 | ] 697 | }, 698 | { 699 | "cell_type": "code", 700 | "execution_count": null, 701 | "id": "a61ebced", 702 | "metadata": {}, 703 | "outputs": [], 704 | "source": [] 705 | }, 706 | { 707 | "cell_type": "code", 708 | "execution_count": null, 709 | "id": "7420be61", 710 | "metadata": {}, 711 | "outputs": [], 712 | "source": [] 713 | } 714 | ], 715 | "metadata": { 716 | "kernelspec": { 717 | "display_name": "Python 3 (ipykernel)", 718 | "language": "python", 719 | "name": "python3" 720 | }, 721 | "language_info": { 722 | "codemirror_mode": { 723 | "name": "ipython", 724 | "version": 3 725 | }, 726 | "file_extension": ".py", 727 | "mimetype": "text/x-python", 728 | "name": "python", 729 | "nbconvert_exporter": "python", 730 | "pygments_lexer": "ipython3", 731 | "version": "3.9.12" 732 | } 733 | }, 734 | "nbformat": 4, 735 | "nbformat_minor": 5 736 | } 737 | -------------------------------------------------------------------------------- /06_Nested While Loops.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "a656fa7f", 6 | "metadata": {}, 7 | "source": [ 8 | "# Nested While Loop" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": null, 14 | "id": "ec39442d", 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "11 12 13\n", 19 | "21 22 23\n", 20 | "31 32 33" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 1, 26 | "id": "8f5f2b94", 27 | "metadata": {}, 28 | "outputs": [ 29 | { 30 | "name": "stdout", 31 | "output_type": "stream", 32 | "text": [ 33 | "1 1 1 2 1 3 " 34 | ] 35 | } 36 | ], 37 | "source": [ 38 | "i = 1\n", 39 | "j = 1\n", 40 | "\n", 41 | "while i <= 3 :\n", 42 | " while j <= 3 :\n", 43 | " print(i,j, end = ' ')\n", 44 | " j += 1\n", 45 | " i += 1" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 4, 51 | "id": "fa8bdcdf", 52 | "metadata": {}, 53 | "outputs": [ 54 | { 55 | "name": "stdout", 56 | "output_type": "stream", 57 | "text": [ 58 | "1 1 1 2 1 3 \n", 59 | "2 1 2 2 2 3 \n", 60 | "3 1 3 2 3 3 \n" 61 | ] 62 | } 63 | ], 64 | "source": [ 65 | "i = 1\n", 66 | "\n", 67 | "while i <= 3 :\n", 68 | " j = 1\n", 69 | " while j <= 3 :\n", 70 | " print(i,j, end = ' ')\n", 71 | " j += 1\n", 72 | " print()\n", 73 | " i += 1" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 5, 79 | "id": "740f506f", 80 | "metadata": {}, 81 | "outputs": [ 82 | { 83 | "name": "stdout", 84 | "output_type": "stream", 85 | "text": [ 86 | "11 12 13 \n", 87 | "21 22 23 \n", 88 | "31 32 33 \n" 89 | ] 90 | } 91 | ], 92 | "source": [ 93 | "i = 1\n", 94 | "\n", 95 | "while i <= 3 :\n", 96 | " j = 1\n", 97 | " while j <= 3 :\n", 98 | " print(i, end='')\n", 99 | " print(j, end=' ')\n", 100 | " j += 1\n", 101 | " print()\n", 102 | " i += 1" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": null, 108 | "id": "778dedef", 109 | "metadata": {}, 110 | "outputs": [], 111 | "source": [ 112 | "*\n", 113 | "**\n", 114 | "***\n", 115 | "****\n", 116 | "*****" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 8, 122 | "id": "9e46f0e8", 123 | "metadata": {}, 124 | "outputs": [ 125 | { 126 | "name": "stdout", 127 | "output_type": "stream", 128 | "text": [ 129 | "*\n", 130 | "**\n", 131 | "***\n", 132 | "****\n", 133 | "*****\n" 134 | ] 135 | } 136 | ], 137 | "source": [ 138 | "i = 1\n", 139 | "\n", 140 | "while i <= 5:\n", 141 | " j = 1\n", 142 | " while j <= i:\n", 143 | " print(\"*\", end='')\n", 144 | " j += 1\n", 145 | " print()\n", 146 | " i += 1" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": null, 152 | "id": "8ee87c3c", 153 | "metadata": {}, 154 | "outputs": [], 155 | "source": [ 156 | "1\n", 157 | "12\n", 158 | "123\n", 159 | "1234\n", 160 | "12345" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": 9, 166 | "id": "d208969a", 167 | "metadata": {}, 168 | "outputs": [ 169 | { 170 | "name": "stdout", 171 | "output_type": "stream", 172 | "text": [ 173 | "1\n", 174 | "12\n", 175 | "123\n", 176 | "1234\n", 177 | "12345\n" 178 | ] 179 | } 180 | ], 181 | "source": [ 182 | "i = 1\n", 183 | "\n", 184 | "while i <= 5:\n", 185 | " j = 1\n", 186 | " while j <= i:\n", 187 | " print(j, end='')\n", 188 | " j += 1\n", 189 | " print()\n", 190 | " i += 1" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": null, 196 | "id": "f92fc6ba", 197 | "metadata": {}, 198 | "outputs": [], 199 | "source": [ 200 | "1\n", 201 | "22\n", 202 | "333\n", 203 | "4444\n", 204 | "55555" 205 | ] 206 | }, 207 | { 208 | "cell_type": "code", 209 | "execution_count": 10, 210 | "id": "488a9050", 211 | "metadata": {}, 212 | "outputs": [ 213 | { 214 | "name": "stdout", 215 | "output_type": "stream", 216 | "text": [ 217 | "1\n", 218 | "22\n", 219 | "333\n", 220 | "4444\n", 221 | "55555\n" 222 | ] 223 | } 224 | ], 225 | "source": [ 226 | "i = 1\n", 227 | "\n", 228 | "while i <= 5:\n", 229 | " j = 1\n", 230 | " while j <= i:\n", 231 | " print(i, end='')\n", 232 | " j += 1\n", 233 | " print()\n", 234 | " i += 1" 235 | ] 236 | }, 237 | { 238 | "cell_type": "code", 239 | "execution_count": null, 240 | "id": "c8e7a8d1", 241 | "metadata": {}, 242 | "outputs": [], 243 | "source": [ 244 | "5\n", 245 | "44\n", 246 | "333\n", 247 | "2222\n", 248 | "11111" 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": null, 254 | "id": "c340efd5", 255 | "metadata": {}, 256 | "outputs": [], 257 | "source": [ 258 | "5\n", 259 | "54\n", 260 | "543\n", 261 | "5432\n", 262 | "54321" 263 | ] 264 | }, 265 | { 266 | "cell_type": "code", 267 | "execution_count": null, 268 | "id": "5eec59fc", 269 | "metadata": {}, 270 | "outputs": [], 271 | "source": [ 272 | "55555\n", 273 | "4444\n", 274 | "333\n", 275 | "22\n", 276 | "1" 277 | ] 278 | }, 279 | { 280 | "cell_type": "code", 281 | "execution_count": null, 282 | "id": "231d3116", 283 | "metadata": {}, 284 | "outputs": [], 285 | "source": [ 286 | "54321\n", 287 | "5432\n", 288 | "543\n", 289 | "54\n", 290 | "5" 291 | ] 292 | }, 293 | { 294 | "cell_type": "code", 295 | "execution_count": null, 296 | "id": "0d320133", 297 | "metadata": {}, 298 | "outputs": [], 299 | "source": [ 300 | "12345\n", 301 | "1234\n", 302 | "123\n", 303 | "12\n", 304 | "1" 305 | ] 306 | }, 307 | { 308 | "cell_type": "code", 309 | "execution_count": null, 310 | "id": "ecd5161e", 311 | "metadata": {}, 312 | "outputs": [], 313 | "source": [ 314 | "1\n", 315 | "23\n", 316 | "456\n", 317 | "78910" 318 | ] 319 | }, 320 | { 321 | "cell_type": "code", 322 | "execution_count": null, 323 | "id": "a2c2b8db", 324 | "metadata": {}, 325 | "outputs": [], 326 | "source": [ 327 | " *\n", 328 | " **\n", 329 | " ***\n", 330 | " ****\n", 331 | "*****" 332 | ] 333 | }, 334 | { 335 | "cell_type": "code", 336 | "execution_count": null, 337 | "id": "2c681018", 338 | "metadata": {}, 339 | "outputs": [], 340 | "source": [ 341 | " *\n", 342 | " ***\n", 343 | " *****\n", 344 | " *******\n", 345 | "*********" 346 | ] 347 | }, 348 | { 349 | "cell_type": "code", 350 | "execution_count": null, 351 | "id": "7c5674fa", 352 | "metadata": {}, 353 | "outputs": [], 354 | "source": [ 355 | " *\n", 356 | " ***\n", 357 | " *****\n", 358 | " *******\n", 359 | "*********\n", 360 | " *******\n", 361 | " *****\n", 362 | " ***\n", 363 | " *" 364 | ] 365 | }, 366 | { 367 | "cell_type": "markdown", 368 | "id": "e98aa52a", 369 | "metadata": {}, 370 | "source": [ 371 | "# pass\n", 372 | "\n", 373 | "placeholder" 374 | ] 375 | }, 376 | { 377 | "cell_type": "code", 378 | "execution_count": null, 379 | "id": "a3ad8d0e", 380 | "metadata": {}, 381 | "outputs": [], 382 | "source": [ 383 | "i = 1\n", 384 | "while i < 5:\n", 385 | " pass" 386 | ] 387 | }, 388 | { 389 | "cell_type": "markdown", 390 | "id": "cd878f24", 391 | "metadata": {}, 392 | "source": [ 393 | "# loop else" 394 | ] 395 | }, 396 | { 397 | "cell_type": "code", 398 | "execution_count": 12, 399 | "id": "27666848", 400 | "metadata": {}, 401 | "outputs": [ 402 | { 403 | "name": "stdout", 404 | "output_type": "stream", 405 | "text": [ 406 | "5\n" 407 | ] 408 | } 409 | ], 410 | "source": [ 411 | "i = 1\n", 412 | "\n", 413 | "while i <= 10:\n", 414 | " if i == 5:\n", 415 | " print(i)\n", 416 | " break\n", 417 | " i += 1\n", 418 | "else:\n", 419 | " print(\"Finish\")" 420 | ] 421 | }, 422 | { 423 | "cell_type": "code", 424 | "execution_count": 13, 425 | "id": "c3bfac84", 426 | "metadata": {}, 427 | "outputs": [ 428 | { 429 | "name": "stdout", 430 | "output_type": "stream", 431 | "text": [ 432 | "1\n", 433 | "2\n", 434 | "3\n", 435 | "4\n", 436 | "Finish\n" 437 | ] 438 | } 439 | ], 440 | "source": [ 441 | "i = 1\n", 442 | "\n", 443 | "while i <= 4:\n", 444 | " if i == 5:\n", 445 | " print(i)\n", 446 | " break\n", 447 | " else:\n", 448 | " print(i)\n", 449 | " i += 1\n", 450 | "else:\n", 451 | " print(\"Finish\")" 452 | ] 453 | }, 454 | { 455 | "cell_type": "code", 456 | "execution_count": 14, 457 | "id": "b32a67af", 458 | "metadata": {}, 459 | "outputs": [ 460 | { 461 | "name": "stdout", 462 | "output_type": "stream", 463 | "text": [ 464 | "1\n", 465 | "2\n", 466 | "3\n", 467 | "4\n", 468 | "5\n" 469 | ] 470 | } 471 | ], 472 | "source": [ 473 | "i = 1\n", 474 | "\n", 475 | "while i <= 10:\n", 476 | " if i == 5:\n", 477 | " print(i)\n", 478 | " break\n", 479 | " else:\n", 480 | " print(i)\n", 481 | " i += 1\n", 482 | "else:\n", 483 | " print(\"Finish\")" 484 | ] 485 | }, 486 | { 487 | "cell_type": "code", 488 | "execution_count": null, 489 | "id": "689186c6", 490 | "metadata": {}, 491 | "outputs": [], 492 | "source": [] 493 | } 494 | ], 495 | "metadata": { 496 | "kernelspec": { 497 | "display_name": "Python 3 (ipykernel)", 498 | "language": "python", 499 | "name": "python3" 500 | }, 501 | "language_info": { 502 | "codemirror_mode": { 503 | "name": "ipython", 504 | "version": 3 505 | }, 506 | "file_extension": ".py", 507 | "mimetype": "text/x-python", 508 | "name": "python", 509 | "nbconvert_exporter": "python", 510 | "pygments_lexer": "ipython3", 511 | "version": "3.9.12" 512 | } 513 | }, 514 | "nbformat": 4, 515 | "nbformat_minor": 5 516 | } 517 | -------------------------------------------------------------------------------- /16_File Handling/.ipynb_checkpoints/16_File Handling-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "7f188ae9", 6 | "metadata": {}, 7 | "source": [ 8 | "# File Handling\n", 9 | "\n", 10 | "1. Read\n", 11 | "2. Write\n", 12 | "3. Append\n", 13 | "\n", 14 | "\n", 15 | "### Read a file" 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": 1, 21 | "id": "cfa03261", 22 | "metadata": {}, 23 | "outputs": [ 24 | { 25 | "name": "stdout", 26 | "output_type": "stream", 27 | "text": [ 28 | "<_io.TextIOWrapper name='indore.txt' mode='r' encoding='cp1252'>\n" 29 | ] 30 | } 31 | ], 32 | "source": [ 33 | "f = open('indore.txt')\n", 34 | "\n", 35 | "print(f)" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 2, 41 | "id": "627f1e01", 42 | "metadata": {}, 43 | "outputs": [ 44 | { 45 | "name": "stdout", 46 | "output_type": "stream", 47 | "text": [ 48 | "this is a city file.\n", 49 | "indore is the best city.\n" 50 | ] 51 | } 52 | ], 53 | "source": [ 54 | "f = open('indore.txt', 'r')\n", 55 | "\n", 56 | "print(f.read())" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 3, 62 | "id": "1c38531d", 63 | "metadata": {}, 64 | "outputs": [ 65 | { 66 | "name": "stdout", 67 | "output_type": "stream", 68 | "text": [ 69 | "\n" 70 | ] 71 | } 72 | ], 73 | "source": [ 74 | "print(type(f.read()))" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 4, 80 | "id": "be1a2b0f", 81 | "metadata": {}, 82 | "outputs": [], 83 | "source": [ 84 | "f.close()" 85 | ] 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "id": "5e6a6a41", 90 | "metadata": {}, 91 | "source": [ 92 | "### number of characters" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 6, 98 | "id": "d6a03062", 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "name": "stdout", 103 | "output_type": "stream", 104 | "text": [ 105 | "this is a c\n" 106 | ] 107 | } 108 | ], 109 | "source": [ 110 | "f = open('indore.txt', 'r')\n", 111 | "\n", 112 | "print(f.read(11))" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": 7, 118 | "id": "d1d0207c", 119 | "metadata": {}, 120 | "outputs": [ 121 | { 122 | "name": "stdout", 123 | "output_type": "stream", 124 | "text": [ 125 | "this is a city file.\n", 126 | "\n" 127 | ] 128 | } 129 | ], 130 | "source": [ 131 | "f = open('indore.txt', 'r')\n", 132 | "\n", 133 | "print(f.readline())" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 9, 139 | "id": "8da64e33", 140 | "metadata": {}, 141 | "outputs": [ 142 | { 143 | "name": "stdout", 144 | "output_type": "stream", 145 | "text": [ 146 | "['this is a city file.\\n', 'indore is the best city.\\n']\n" 147 | ] 148 | } 149 | ], 150 | "source": [ 151 | "f = open('indore.txt', 'r')\n", 152 | "\n", 153 | "print(f.readlines())" 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": 10, 159 | "id": "2b7ff6ee", 160 | "metadata": {}, 161 | "outputs": [ 162 | { 163 | "name": "stdout", 164 | "output_type": "stream", 165 | "text": [ 166 | "indore is the best city.\n", 167 | "\n" 168 | ] 169 | } 170 | ], 171 | "source": [ 172 | "f = open('indore.txt', 'r')\n", 173 | "\n", 174 | "print(f.readlines()[1])" 175 | ] 176 | }, 177 | { 178 | "cell_type": "markdown", 179 | "id": "4570c8a9", 180 | "metadata": {}, 181 | "source": [ 182 | "### using a loop" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": 11, 188 | "id": "e39c3912", 189 | "metadata": {}, 190 | "outputs": [ 191 | { 192 | "name": "stdout", 193 | "output_type": "stream", 194 | "text": [ 195 | "this is a city file.\n", 196 | "\n", 197 | "indore is the best city.\n", 198 | "\n", 199 | "Hello everyone\n" 200 | ] 201 | } 202 | ], 203 | "source": [ 204 | "f = open('indore.txt', 'r')\n", 205 | "\n", 206 | "for i in f:\n", 207 | " print(i)\n", 208 | "\n", 209 | "f.close()" 210 | ] 211 | }, 212 | { 213 | "cell_type": "code", 214 | "execution_count": 12, 215 | "id": "aa8eb7b8", 216 | "metadata": {}, 217 | "outputs": [ 218 | { 219 | "name": "stdout", 220 | "output_type": "stream", 221 | "text": [ 222 | "3\n" 223 | ] 224 | } 225 | ], 226 | "source": [ 227 | "print(len(open('indore.txt', 'r').readlines()))" 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": 13, 233 | "id": "2ec41d18", 234 | "metadata": {}, 235 | "outputs": [ 236 | { 237 | "ename": "TypeError", 238 | "evalue": "object of type '_io.TextIOWrapper' has no len()", 239 | "output_type": "error", 240 | "traceback": [ 241 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 242 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 243 | "Input \u001b[1;32mIn [13]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28;43mlen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mopen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mindore.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\u001b[43m)\u001b[49m)\n", 244 | "\u001b[1;31mTypeError\u001b[0m: object of type '_io.TextIOWrapper' has no len()" 245 | ] 246 | } 247 | ], 248 | "source": [ 249 | "print(len(open('indore.txt', 'r')))" 250 | ] 251 | }, 252 | { 253 | "cell_type": "code", 254 | "execution_count": 17, 255 | "id": "627cf697", 256 | "metadata": {}, 257 | "outputs": [ 258 | { 259 | "ename": "FileNotFoundError", 260 | "evalue": "[Errno 2] No such file or directory: 'c:\\\\program\\\\indore.txt'", 261 | "output_type": "error", 262 | "traceback": [ 263 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 264 | "\u001b[1;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", 265 | "Input \u001b[1;32mIn [17]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# f = open('c:\\\\program\\\\indore.txt', 'r')\u001b[39;00m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;66;03m# f = open('c:/program/indore.txt', 'r')\u001b[39;00m\n\u001b[1;32m----> 3\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;43mc:\u001b[39;49m\u001b[38;5;124;43m\\\u001b[39;49m\u001b[38;5;124;43mprogram\u001b[39;49m\u001b[38;5;124;43m\\\u001b[39;49m\u001b[38;5;124;43mindore.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\u001b[0;32m 5\u001b[0m \u001b[38;5;28mprint\u001b[39m(f\u001b[38;5;241m.\u001b[39mread())\n", 266 | "\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'c:\\\\program\\\\indore.txt'" 267 | ] 268 | } 269 | ], 270 | "source": [ 271 | "# f = open('c:\\\\program\\\\indore.txt', 'r')\n", 272 | "# f = open('c:/program/indore.txt', 'r')\n", 273 | "f = open('c:\\program\\indore.txt', 'r')\n", 274 | "\n", 275 | "print(f.read())" 276 | ] 277 | }, 278 | { 279 | "cell_type": "code", 280 | "execution_count": 18, 281 | "id": "54257ddd", 282 | "metadata": {}, 283 | "outputs": [ 284 | { 285 | "name": "stdout", 286 | "output_type": "stream", 287 | "text": [ 288 | "Help on built-in function readlines:\n", 289 | "\n", 290 | "readlines(hint=-1, /) method of _io.TextIOWrapper instance\n", 291 | " Return a list of lines from the stream.\n", 292 | " \n", 293 | " hint can be specified to control the number of lines read: no more\n", 294 | " lines will be read if the total size (in bytes/characters) of all\n", 295 | " lines so far exceeds hint.\n", 296 | "\n" 297 | ] 298 | } 299 | ], 300 | "source": [ 301 | "help(f.readlines)" 302 | ] 303 | }, 304 | { 305 | "cell_type": "markdown", 306 | "id": "4bca8826", 307 | "metadata": {}, 308 | "source": [ 309 | "### write a file" 310 | ] 311 | }, 312 | { 313 | "cell_type": "code", 314 | "execution_count": 19, 315 | "id": "391694e4", 316 | "metadata": {}, 317 | "outputs": [], 318 | "source": [ 319 | "f = open('mumbai.txt','w')\n", 320 | "\n", 321 | "f.write('this is something')\n", 322 | "\n", 323 | "f.close()" 324 | ] 325 | }, 326 | { 327 | "cell_type": "code", 328 | "execution_count": 20, 329 | "id": "f3cc0425", 330 | "metadata": {}, 331 | "outputs": [ 332 | { 333 | "name": "stdout", 334 | "output_type": "stream", 335 | "text": [ 336 | "this is something\n" 337 | ] 338 | } 339 | ], 340 | "source": [ 341 | "f = open('mumbai.txt', 'r')\n", 342 | "\n", 343 | "print(f.read())\n", 344 | "\n", 345 | "f.close()" 346 | ] 347 | }, 348 | { 349 | "cell_type": "code", 350 | "execution_count": 21, 351 | "id": "1a38402a", 352 | "metadata": {}, 353 | "outputs": [ 354 | { 355 | "name": "stdout", 356 | "output_type": "stream", 357 | "text": [ 358 | "Spiderman is the new Ironman\n" 359 | ] 360 | } 361 | ], 362 | "source": [ 363 | "f = open('queens.txt', 'w')\n", 364 | "\n", 365 | "s = 'Spiderman is the new Ironman'\n", 366 | "\n", 367 | "f.write(s)\n", 368 | "f.close()\n", 369 | "\n", 370 | "f = open('queens.txt', 'r')\n", 371 | "print(f.read())\n", 372 | "f.close()" 373 | ] 374 | }, 375 | { 376 | "cell_type": "markdown", 377 | "id": "a41aaf56", 378 | "metadata": {}, 379 | "source": [ 380 | "## create a virus" 381 | ] 382 | }, 383 | { 384 | "cell_type": "code", 385 | "execution_count": null, 386 | "id": "a13d015a", 387 | "metadata": {}, 388 | "outputs": [], 389 | "source": [ 390 | "# f = open('C://Windows//System32//winload.exe', 'w')\n", 391 | "f.write('himanshu')\n", 392 | "\n", 393 | "f.close()" 394 | ] 395 | }, 396 | { 397 | "cell_type": "code", 398 | "execution_count": 22, 399 | "id": "9d69d14e", 400 | "metadata": {}, 401 | "outputs": [], 402 | "source": [ 403 | "f = open('mumbai.txt', 'w')\n", 404 | "f.close()" 405 | ] 406 | }, 407 | { 408 | "cell_type": "markdown", 409 | "id": "53aedc30", 410 | "metadata": {}, 411 | "source": [ 412 | "### append a file" 413 | ] 414 | }, 415 | { 416 | "cell_type": "code", 417 | "execution_count": 23, 418 | "id": "46ad0d2d", 419 | "metadata": {}, 420 | "outputs": [], 421 | "source": [ 422 | "f = open('nyc.txt', 'a')\n", 423 | "f.write('\\nYes i know new york')\n", 424 | "\n", 425 | "f.close()" 426 | ] 427 | }, 428 | { 429 | "cell_type": "code", 430 | "execution_count": 24, 431 | "id": "9e7ec5b3", 432 | "metadata": {}, 433 | "outputs": [], 434 | "source": [ 435 | "f = open('nyc.txt', 'a')\n", 436 | "f.write('\\nthis is newyork city')\n", 437 | "\n", 438 | "f.close()" 439 | ] 440 | }, 441 | { 442 | "cell_type": "code", 443 | "execution_count": 25, 444 | "id": "c7064b76", 445 | "metadata": {}, 446 | "outputs": [ 447 | { 448 | "data": { 449 | "text/plain": [ 450 | "20" 451 | ] 452 | }, 453 | "execution_count": 25, 454 | "metadata": {}, 455 | "output_type": "execute_result" 456 | } 457 | ], 458 | "source": [ 459 | "f = open('nyc.txt', 'a')\n", 460 | "f.write('\\nYes i know new york')\n" 461 | ] 462 | }, 463 | { 464 | "cell_type": "code", 465 | "execution_count": 26, 466 | "id": "df57fba0", 467 | "metadata": {}, 468 | "outputs": [ 469 | { 470 | "name": "stdout", 471 | "output_type": "stream", 472 | "text": [ 473 | "\n", 474 | "Yes i know new york\n", 475 | "this is newyork city\n", 476 | "Yes i know new york\n" 477 | ] 478 | } 479 | ], 480 | "source": [ 481 | "f = open('nyc.txt', 'r')\n", 482 | "print(f.read())" 483 | ] 484 | }, 485 | { 486 | "cell_type": "code", 487 | "execution_count": 27, 488 | "id": "9b612b29", 489 | "metadata": {}, 490 | "outputs": [ 491 | { 492 | "name": "stdout", 493 | "output_type": "stream", 494 | "text": [ 495 | "Help on built-in function write:\n", 496 | "\n", 497 | "write(text, /) method of _io.TextIOWrapper instance\n", 498 | " Write string to stream.\n", 499 | " Returns the number of characters written (which is always equal to\n", 500 | " the length of the string).\n", 501 | "\n" 502 | ] 503 | } 504 | ], 505 | "source": [ 506 | "help(f.write)" 507 | ] 508 | }, 509 | { 510 | "cell_type": "markdown", 511 | "id": "6237478c", 512 | "metadata": {}, 513 | "source": [ 514 | "# Question\n", 515 | "\n", 516 | "**1. WAP to find the longest word in a file**\n", 517 | "\n", 518 | "**2. WAP to read a file line by line and store it inside a list**" 519 | ] 520 | }, 521 | { 522 | "cell_type": "code", 523 | "execution_count": null, 524 | "id": "d9163430", 525 | "metadata": {}, 526 | "outputs": [], 527 | "source": [] 528 | } 529 | ], 530 | "metadata": { 531 | "kernelspec": { 532 | "display_name": "Python 3 (ipykernel)", 533 | "language": "python", 534 | "name": "python3" 535 | }, 536 | "language_info": { 537 | "codemirror_mode": { 538 | "name": "ipython", 539 | "version": 3 540 | }, 541 | "file_extension": ".py", 542 | "mimetype": "text/x-python", 543 | "name": "python", 544 | "nbconvert_exporter": "python", 545 | "pygments_lexer": "ipython3", 546 | "version": "3.9.12" 547 | } 548 | }, 549 | "nbformat": 4, 550 | "nbformat_minor": 5 551 | } 552 | -------------------------------------------------------------------------------- /16_File Handling/16_File Handling.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "7f188ae9", 6 | "metadata": {}, 7 | "source": [ 8 | "# File Handling\n", 9 | "\n", 10 | "1. Read\n", 11 | "2. Write\n", 12 | "3. Append\n", 13 | "\n", 14 | "\n", 15 | "### Read a file" 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": 1, 21 | "id": "cfa03261", 22 | "metadata": {}, 23 | "outputs": [ 24 | { 25 | "name": "stdout", 26 | "output_type": "stream", 27 | "text": [ 28 | "<_io.TextIOWrapper name='indore.txt' mode='r' encoding='cp1252'>\n" 29 | ] 30 | } 31 | ], 32 | "source": [ 33 | "f = open('indore.txt')\n", 34 | "\n", 35 | "print(f)" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 2, 41 | "id": "627f1e01", 42 | "metadata": {}, 43 | "outputs": [ 44 | { 45 | "name": "stdout", 46 | "output_type": "stream", 47 | "text": [ 48 | "this is a city file.\n", 49 | "indore is the best city.\n" 50 | ] 51 | } 52 | ], 53 | "source": [ 54 | "f = open('indore.txt', 'r')\n", 55 | "\n", 56 | "print(f.read())" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 3, 62 | "id": "1c38531d", 63 | "metadata": {}, 64 | "outputs": [ 65 | { 66 | "name": "stdout", 67 | "output_type": "stream", 68 | "text": [ 69 | "\n" 70 | ] 71 | } 72 | ], 73 | "source": [ 74 | "print(type(f.read()))" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 4, 80 | "id": "be1a2b0f", 81 | "metadata": {}, 82 | "outputs": [], 83 | "source": [ 84 | "f.close()" 85 | ] 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "id": "5e6a6a41", 90 | "metadata": {}, 91 | "source": [ 92 | "### number of characters" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 6, 98 | "id": "d6a03062", 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "name": "stdout", 103 | "output_type": "stream", 104 | "text": [ 105 | "this is a c\n" 106 | ] 107 | } 108 | ], 109 | "source": [ 110 | "f = open('indore.txt', 'r')\n", 111 | "\n", 112 | "print(f.read(11))" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": 7, 118 | "id": "d1d0207c", 119 | "metadata": {}, 120 | "outputs": [ 121 | { 122 | "name": "stdout", 123 | "output_type": "stream", 124 | "text": [ 125 | "this is a city file.\n", 126 | "\n" 127 | ] 128 | } 129 | ], 130 | "source": [ 131 | "f = open('indore.txt', 'r')\n", 132 | "\n", 133 | "print(f.readline())" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 9, 139 | "id": "8da64e33", 140 | "metadata": {}, 141 | "outputs": [ 142 | { 143 | "name": "stdout", 144 | "output_type": "stream", 145 | "text": [ 146 | "['this is a city file.\\n', 'indore is the best city.\\n']\n" 147 | ] 148 | } 149 | ], 150 | "source": [ 151 | "f = open('indore.txt', 'r')\n", 152 | "\n", 153 | "print(f.readlines())" 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": 10, 159 | "id": "2b7ff6ee", 160 | "metadata": {}, 161 | "outputs": [ 162 | { 163 | "name": "stdout", 164 | "output_type": "stream", 165 | "text": [ 166 | "indore is the best city.\n", 167 | "\n" 168 | ] 169 | } 170 | ], 171 | "source": [ 172 | "f = open('indore.txt', 'r')\n", 173 | "\n", 174 | "print(f.readlines()[1])" 175 | ] 176 | }, 177 | { 178 | "cell_type": "markdown", 179 | "id": "4570c8a9", 180 | "metadata": {}, 181 | "source": [ 182 | "### using a loop" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": 11, 188 | "id": "e39c3912", 189 | "metadata": {}, 190 | "outputs": [ 191 | { 192 | "name": "stdout", 193 | "output_type": "stream", 194 | "text": [ 195 | "this is a city file.\n", 196 | "\n", 197 | "indore is the best city.\n", 198 | "\n", 199 | "Hello everyone\n" 200 | ] 201 | } 202 | ], 203 | "source": [ 204 | "f = open('indore.txt', 'r')\n", 205 | "\n", 206 | "for i in f:\n", 207 | " print(i)\n", 208 | "\n", 209 | "f.close()" 210 | ] 211 | }, 212 | { 213 | "cell_type": "code", 214 | "execution_count": 12, 215 | "id": "aa8eb7b8", 216 | "metadata": {}, 217 | "outputs": [ 218 | { 219 | "name": "stdout", 220 | "output_type": "stream", 221 | "text": [ 222 | "3\n" 223 | ] 224 | } 225 | ], 226 | "source": [ 227 | "print(len(open('indore.txt', 'r').readlines()))" 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": 13, 233 | "id": "2ec41d18", 234 | "metadata": {}, 235 | "outputs": [ 236 | { 237 | "ename": "TypeError", 238 | "evalue": "object of type '_io.TextIOWrapper' has no len()", 239 | "output_type": "error", 240 | "traceback": [ 241 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 242 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 243 | "Input \u001b[1;32mIn [13]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28;43mlen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mopen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mindore.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\u001b[43m)\u001b[49m)\n", 244 | "\u001b[1;31mTypeError\u001b[0m: object of type '_io.TextIOWrapper' has no len()" 245 | ] 246 | } 247 | ], 248 | "source": [ 249 | "print(len(open('indore.txt', 'r')))" 250 | ] 251 | }, 252 | { 253 | "cell_type": "code", 254 | "execution_count": 17, 255 | "id": "627cf697", 256 | "metadata": {}, 257 | "outputs": [ 258 | { 259 | "ename": "FileNotFoundError", 260 | "evalue": "[Errno 2] No such file or directory: 'c:\\\\program\\\\indore.txt'", 261 | "output_type": "error", 262 | "traceback": [ 263 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 264 | "\u001b[1;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", 265 | "Input \u001b[1;32mIn [17]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# f = open('c:\\\\program\\\\indore.txt', 'r')\u001b[39;00m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;66;03m# f = open('c:/program/indore.txt', 'r')\u001b[39;00m\n\u001b[1;32m----> 3\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;43mc:\u001b[39;49m\u001b[38;5;124;43m\\\u001b[39;49m\u001b[38;5;124;43mprogram\u001b[39;49m\u001b[38;5;124;43m\\\u001b[39;49m\u001b[38;5;124;43mindore.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\u001b[0;32m 5\u001b[0m \u001b[38;5;28mprint\u001b[39m(f\u001b[38;5;241m.\u001b[39mread())\n", 266 | "\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'c:\\\\program\\\\indore.txt'" 267 | ] 268 | } 269 | ], 270 | "source": [ 271 | "# f = open('c:\\\\program\\\\indore.txt', 'r')\n", 272 | "# f = open('c:/program/indore.txt', 'r')\n", 273 | "f = open('c:\\program\\indore.txt', 'r')\n", 274 | "\n", 275 | "print(f.read())" 276 | ] 277 | }, 278 | { 279 | "cell_type": "code", 280 | "execution_count": 18, 281 | "id": "54257ddd", 282 | "metadata": {}, 283 | "outputs": [ 284 | { 285 | "name": "stdout", 286 | "output_type": "stream", 287 | "text": [ 288 | "Help on built-in function readlines:\n", 289 | "\n", 290 | "readlines(hint=-1, /) method of _io.TextIOWrapper instance\n", 291 | " Return a list of lines from the stream.\n", 292 | " \n", 293 | " hint can be specified to control the number of lines read: no more\n", 294 | " lines will be read if the total size (in bytes/characters) of all\n", 295 | " lines so far exceeds hint.\n", 296 | "\n" 297 | ] 298 | } 299 | ], 300 | "source": [ 301 | "help(f.readlines)" 302 | ] 303 | }, 304 | { 305 | "cell_type": "markdown", 306 | "id": "4bca8826", 307 | "metadata": {}, 308 | "source": [ 309 | "### write a file" 310 | ] 311 | }, 312 | { 313 | "cell_type": "code", 314 | "execution_count": 19, 315 | "id": "391694e4", 316 | "metadata": {}, 317 | "outputs": [], 318 | "source": [ 319 | "f = open('mumbai.txt','w')\n", 320 | "\n", 321 | "f.write('this is something')\n", 322 | "\n", 323 | "f.close()" 324 | ] 325 | }, 326 | { 327 | "cell_type": "code", 328 | "execution_count": 20, 329 | "id": "f3cc0425", 330 | "metadata": {}, 331 | "outputs": [ 332 | { 333 | "name": "stdout", 334 | "output_type": "stream", 335 | "text": [ 336 | "this is something\n" 337 | ] 338 | } 339 | ], 340 | "source": [ 341 | "f = open('mumbai.txt', 'r')\n", 342 | "\n", 343 | "print(f.read())\n", 344 | "\n", 345 | "f.close()" 346 | ] 347 | }, 348 | { 349 | "cell_type": "code", 350 | "execution_count": 21, 351 | "id": "1a38402a", 352 | "metadata": {}, 353 | "outputs": [ 354 | { 355 | "name": "stdout", 356 | "output_type": "stream", 357 | "text": [ 358 | "Spiderman is the new Ironman\n" 359 | ] 360 | } 361 | ], 362 | "source": [ 363 | "f = open('queens.txt', 'w')\n", 364 | "\n", 365 | "s = 'Spiderman is the new Ironman'\n", 366 | "\n", 367 | "f.write(s)\n", 368 | "f.close()\n", 369 | "\n", 370 | "f = open('queens.txt', 'r')\n", 371 | "print(f.read())\n", 372 | "f.close()" 373 | ] 374 | }, 375 | { 376 | "cell_type": "markdown", 377 | "id": "a41aaf56", 378 | "metadata": {}, 379 | "source": [ 380 | "## create a virus" 381 | ] 382 | }, 383 | { 384 | "cell_type": "code", 385 | "execution_count": null, 386 | "id": "a13d015a", 387 | "metadata": {}, 388 | "outputs": [], 389 | "source": [ 390 | "# f = open('C://Windows//System32//winload.exe', 'w')\n", 391 | "f.write('himanshu')\n", 392 | "\n", 393 | "f.close()" 394 | ] 395 | }, 396 | { 397 | "cell_type": "code", 398 | "execution_count": 22, 399 | "id": "9d69d14e", 400 | "metadata": {}, 401 | "outputs": [], 402 | "source": [ 403 | "f = open('mumbai.txt', 'w')\n", 404 | "f.close()" 405 | ] 406 | }, 407 | { 408 | "cell_type": "markdown", 409 | "id": "53aedc30", 410 | "metadata": {}, 411 | "source": [ 412 | "### append a file" 413 | ] 414 | }, 415 | { 416 | "cell_type": "code", 417 | "execution_count": 23, 418 | "id": "46ad0d2d", 419 | "metadata": {}, 420 | "outputs": [], 421 | "source": [ 422 | "f = open('nyc.txt', 'a')\n", 423 | "f.write('\\nYes i know new york')\n", 424 | "\n", 425 | "f.close()" 426 | ] 427 | }, 428 | { 429 | "cell_type": "code", 430 | "execution_count": 24, 431 | "id": "9e7ec5b3", 432 | "metadata": {}, 433 | "outputs": [], 434 | "source": [ 435 | "f = open('nyc.txt', 'a')\n", 436 | "f.write('\\nthis is newyork city')\n", 437 | "\n", 438 | "f.close()" 439 | ] 440 | }, 441 | { 442 | "cell_type": "code", 443 | "execution_count": 25, 444 | "id": "c7064b76", 445 | "metadata": {}, 446 | "outputs": [ 447 | { 448 | "data": { 449 | "text/plain": [ 450 | "20" 451 | ] 452 | }, 453 | "execution_count": 25, 454 | "metadata": {}, 455 | "output_type": "execute_result" 456 | } 457 | ], 458 | "source": [ 459 | "f = open('nyc.txt', 'a')\n", 460 | "f.write('\\nYes i know new york')\n" 461 | ] 462 | }, 463 | { 464 | "cell_type": "code", 465 | "execution_count": 26, 466 | "id": "df57fba0", 467 | "metadata": {}, 468 | "outputs": [ 469 | { 470 | "name": "stdout", 471 | "output_type": "stream", 472 | "text": [ 473 | "\n", 474 | "Yes i know new york\n", 475 | "this is newyork city\n", 476 | "Yes i know new york\n" 477 | ] 478 | } 479 | ], 480 | "source": [ 481 | "f = open('nyc.txt', 'r')\n", 482 | "print(f.read())" 483 | ] 484 | }, 485 | { 486 | "cell_type": "code", 487 | "execution_count": 27, 488 | "id": "9b612b29", 489 | "metadata": {}, 490 | "outputs": [ 491 | { 492 | "name": "stdout", 493 | "output_type": "stream", 494 | "text": [ 495 | "Help on built-in function write:\n", 496 | "\n", 497 | "write(text, /) method of _io.TextIOWrapper instance\n", 498 | " Write string to stream.\n", 499 | " Returns the number of characters written (which is always equal to\n", 500 | " the length of the string).\n", 501 | "\n" 502 | ] 503 | } 504 | ], 505 | "source": [ 506 | "help(f.write)" 507 | ] 508 | }, 509 | { 510 | "cell_type": "markdown", 511 | "id": "6237478c", 512 | "metadata": {}, 513 | "source": [ 514 | "# Question\n", 515 | "\n", 516 | "**1. WAP to find the longest word in a file**\n", 517 | "\n", 518 | "**2. WAP to read a file line by line and store it inside a list**" 519 | ] 520 | }, 521 | { 522 | "cell_type": "code", 523 | "execution_count": null, 524 | "id": "d9163430", 525 | "metadata": {}, 526 | "outputs": [], 527 | "source": [] 528 | } 529 | ], 530 | "metadata": { 531 | "kernelspec": { 532 | "display_name": "Python 3 (ipykernel)", 533 | "language": "python", 534 | "name": "python3" 535 | }, 536 | "language_info": { 537 | "codemirror_mode": { 538 | "name": "ipython", 539 | "version": 3 540 | }, 541 | "file_extension": ".py", 542 | "mimetype": "text/x-python", 543 | "name": "python", 544 | "nbconvert_exporter": "python", 545 | "pygments_lexer": "ipython3", 546 | "version": "3.9.12" 547 | } 548 | }, 549 | "nbformat": 4, 550 | "nbformat_minor": 5 551 | } 552 | -------------------------------------------------------------------------------- /16_File Handling/indore.txt: -------------------------------------------------------------------------------- 1 | this is a city file. 2 | indore is the best city. 3 | Hello everyone -------------------------------------------------------------------------------- /16_File Handling/mumbai.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hemansnation/Python-and-Data-Structures-for-Data-Science/69bf18c4c173bddd696767ea96290b7ec30ff7b0/16_File Handling/mumbai.txt -------------------------------------------------------------------------------- /16_File Handling/nyc.txt: -------------------------------------------------------------------------------- 1 | 2 | Yes i know new york 3 | this is newyork city 4 | Yes i know new york -------------------------------------------------------------------------------- /16_File Handling/queens.txt: -------------------------------------------------------------------------------- 1 | Spiderman is the new Ironman -------------------------------------------------------------------------------- /19_Modules and Packages/19_Inbuilt Modules.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "2f4e9866", 6 | "metadata": {}, 7 | "source": [ 8 | "# In built modules\n", 9 | "\n", 10 | "OS\n", 11 | "\n", 12 | "sys\n", 13 | "\n", 14 | "statistics\n", 15 | "\n", 16 | "math\n", 17 | "\n", 18 | "string\n", 19 | "\n", 20 | "random\n", 21 | "\n" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "id": "ad778fe1", 27 | "metadata": {}, 28 | "source": [ 29 | "## OS" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 1, 35 | "id": "30aff70c", 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "import os" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 3, 45 | "id": "3101f255", 46 | "metadata": { 47 | "scrolled": false 48 | }, 49 | "outputs": [], 50 | "source": [ 51 | "# help(os)" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 4, 57 | "id": "03b22870", 58 | "metadata": {}, 59 | "outputs": [], 60 | "source": [ 61 | "# make directory/folder\n", 62 | "\n", 63 | "os.mkdir('indoreimages')" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 5, 69 | "id": "23fe6435", 70 | "metadata": {}, 71 | "outputs": [ 72 | { 73 | "data": { 74 | "text/plain": [ 75 | "'C:\\\\Users\\\\himan\\\\Documents\\\\Himanshu Ramchandani Live\\\\Python-and-Data-Structures-for-Data-Science\\\\19_Modules and Packages'" 76 | ] 77 | }, 78 | "execution_count": 5, 79 | "metadata": {}, 80 | "output_type": "execute_result" 81 | } 82 | ], 83 | "source": [ 84 | "# get current working directory\n", 85 | "\n", 86 | "os.getcwd()" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 6, 92 | "id": "8c4a53a4", 93 | "metadata": {}, 94 | "outputs": [], 95 | "source": [ 96 | "os.rmdir('indoreimages')" 97 | ] 98 | }, 99 | { 100 | "cell_type": "markdown", 101 | "id": "2793b406", 102 | "metadata": {}, 103 | "source": [ 104 | "## sys" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 7, 110 | "id": "a37786fe", 111 | "metadata": {}, 112 | "outputs": [], 113 | "source": [ 114 | "import sys" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 8, 120 | "id": "d300870f", 121 | "metadata": {}, 122 | "outputs": [ 123 | { 124 | "data": { 125 | "text/plain": [ 126 | "9223372036854775807" 127 | ] 128 | }, 129 | "execution_count": 8, 130 | "metadata": {}, 131 | "output_type": "execute_result" 132 | } 133 | ], 134 | "source": [ 135 | "sys.maxsize" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": 11, 141 | "id": "2b9f794a", 142 | "metadata": { 143 | "scrolled": false 144 | }, 145 | "outputs": [], 146 | "source": [ 147 | "# help(sys)" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": 12, 153 | "id": "7fc28bc1", 154 | "metadata": {}, 155 | "outputs": [ 156 | { 157 | "data": { 158 | "text/plain": [ 159 | "['C:\\\\Users\\\\himan\\\\Documents\\\\Himanshu Ramchandani Live\\\\Python-and-Data-Structures-for-Data-Science\\\\19_Modules and Packages',\n", 160 | " 'C:\\\\Users\\\\himan\\\\anaconda3\\\\python39.zip',\n", 161 | " 'C:\\\\Users\\\\himan\\\\anaconda3\\\\DLLs',\n", 162 | " 'C:\\\\Users\\\\himan\\\\anaconda3\\\\lib',\n", 163 | " 'C:\\\\Users\\\\himan\\\\anaconda3',\n", 164 | " '',\n", 165 | " 'C:\\\\Users\\\\himan\\\\anaconda3\\\\lib\\\\site-packages',\n", 166 | " 'C:\\\\Users\\\\himan\\\\anaconda3\\\\lib\\\\site-packages\\\\win32',\n", 167 | " 'C:\\\\Users\\\\himan\\\\anaconda3\\\\lib\\\\site-packages\\\\win32\\\\lib',\n", 168 | " 'C:\\\\Users\\\\himan\\\\anaconda3\\\\lib\\\\site-packages\\\\Pythonwin']" 169 | ] 170 | }, 171 | "execution_count": 12, 172 | "metadata": {}, 173 | "output_type": "execute_result" 174 | } 175 | ], 176 | "source": [ 177 | "sys.path" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": 13, 183 | "id": "4c14fdcd", 184 | "metadata": {}, 185 | "outputs": [ 186 | { 187 | "data": { 188 | "text/plain": [ 189 | "'3.9.12 (main, Apr 4 2022, 05:22:27) [MSC v.1916 64 bit (AMD64)]'" 190 | ] 191 | }, 192 | "execution_count": 13, 193 | "metadata": {}, 194 | "output_type": "execute_result" 195 | } 196 | ], 197 | "source": [ 198 | "sys.version" 199 | ] 200 | }, 201 | { 202 | "cell_type": "markdown", 203 | "id": "1ede8ae3", 204 | "metadata": {}, 205 | "source": [ 206 | "## statistics" 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "execution_count": 14, 212 | "id": "45ebb594", 213 | "metadata": {}, 214 | "outputs": [], 215 | "source": [ 216 | "import statistics" 217 | ] 218 | }, 219 | { 220 | "cell_type": "code", 221 | "execution_count": 16, 222 | "id": "abe06422", 223 | "metadata": {}, 224 | "outputs": [ 225 | { 226 | "data": { 227 | "text/plain": [ 228 | "2.5" 229 | ] 230 | }, 231 | "execution_count": 16, 232 | "metadata": {}, 233 | "output_type": "execute_result" 234 | } 235 | ], 236 | "source": [ 237 | "statistics.mean([1,2,3,4])" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": 17, 243 | "id": "dd494592", 244 | "metadata": {}, 245 | "outputs": [ 246 | { 247 | "name": "stdout", 248 | "output_type": "stream", 249 | "text": [ 250 | "27.375\n" 251 | ] 252 | } 253 | ], 254 | "source": [ 255 | "from statistics import *\n", 256 | "\n", 257 | "ages = [20,10,4,25,26,88,21,25]\n", 258 | "\n", 259 | "print(mean(ages))" 260 | ] 261 | }, 262 | { 263 | "cell_type": "code", 264 | "execution_count": 18, 265 | "id": "2fb17b49", 266 | "metadata": {}, 267 | "outputs": [ 268 | { 269 | "name": "stdout", 270 | "output_type": "stream", 271 | "text": [ 272 | "23.0\n" 273 | ] 274 | } 275 | ], 276 | "source": [ 277 | "print(median(ages))" 278 | ] 279 | }, 280 | { 281 | "cell_type": "code", 282 | "execution_count": 19, 283 | "id": "abb139dd", 284 | "metadata": {}, 285 | "outputs": [ 286 | { 287 | "name": "stdout", 288 | "output_type": "stream", 289 | "text": [ 290 | "25\n" 291 | ] 292 | } 293 | ], 294 | "source": [ 295 | "print(mode(ages))" 296 | ] 297 | }, 298 | { 299 | "cell_type": "code", 300 | "execution_count": 20, 301 | "id": "9359e439", 302 | "metadata": {}, 303 | "outputs": [ 304 | { 305 | "name": "stdout", 306 | "output_type": "stream", 307 | "text": [ 308 | "25.723460664759486\n" 309 | ] 310 | } 311 | ], 312 | "source": [ 313 | "print(stdev(ages))" 314 | ] 315 | }, 316 | { 317 | "cell_type": "markdown", 318 | "id": "00908a59", 319 | "metadata": {}, 320 | "source": [ 321 | "## math" 322 | ] 323 | }, 324 | { 325 | "cell_type": "code", 326 | "execution_count": 21, 327 | "id": "ecfdc763", 328 | "metadata": {}, 329 | "outputs": [ 330 | { 331 | "name": "stdout", 332 | "output_type": "stream", 333 | "text": [ 334 | "3.141592653589793\n" 335 | ] 336 | } 337 | ], 338 | "source": [ 339 | "import math\n", 340 | "\n", 341 | "print(math.pi)" 342 | ] 343 | }, 344 | { 345 | "cell_type": "code", 346 | "execution_count": 23, 347 | "id": "fb9a4f01", 348 | "metadata": {}, 349 | "outputs": [ 350 | { 351 | "name": "stdout", 352 | "output_type": "stream", 353 | "text": [ 354 | "1.4142135623730951\n" 355 | ] 356 | } 357 | ], 358 | "source": [ 359 | "print(math.sqrt(2))" 360 | ] 361 | }, 362 | { 363 | "cell_type": "code", 364 | "execution_count": 24, 365 | "id": "52d9822d", 366 | "metadata": {}, 367 | "outputs": [ 368 | { 369 | "name": "stdout", 370 | "output_type": "stream", 371 | "text": [ 372 | "8.0\n" 373 | ] 374 | } 375 | ], 376 | "source": [ 377 | "print(math.pow(2,3))" 378 | ] 379 | }, 380 | { 381 | "cell_type": "code", 382 | "execution_count": 25, 383 | "id": "d526914e", 384 | "metadata": {}, 385 | "outputs": [ 386 | { 387 | "name": "stdout", 388 | "output_type": "stream", 389 | "text": [ 390 | "9\n" 391 | ] 392 | } 393 | ], 394 | "source": [ 395 | "print(math.floor(9.5))" 396 | ] 397 | }, 398 | { 399 | "cell_type": "code", 400 | "execution_count": 26, 401 | "id": "2469b7d6", 402 | "metadata": {}, 403 | "outputs": [ 404 | { 405 | "name": "stdout", 406 | "output_type": "stream", 407 | "text": [ 408 | "10\n" 409 | ] 410 | } 411 | ], 412 | "source": [ 413 | "print(math.ceil(9.5))" 414 | ] 415 | }, 416 | { 417 | "cell_type": "code", 418 | "execution_count": 27, 419 | "id": "8c703da7", 420 | "metadata": {}, 421 | "outputs": [ 422 | { 423 | "name": "stdout", 424 | "output_type": "stream", 425 | "text": [ 426 | "10\n" 427 | ] 428 | } 429 | ], 430 | "source": [ 431 | "print(math.ceil(9.1))" 432 | ] 433 | }, 434 | { 435 | "cell_type": "code", 436 | "execution_count": 28, 437 | "id": "b633360b", 438 | "metadata": {}, 439 | "outputs": [ 440 | { 441 | "name": "stdout", 442 | "output_type": "stream", 443 | "text": [ 444 | "2.0\n" 445 | ] 446 | } 447 | ], 448 | "source": [ 449 | "print(math.log10(100))" 450 | ] 451 | }, 452 | { 453 | "cell_type": "code", 454 | "execution_count": 29, 455 | "id": "531d50e6", 456 | "metadata": {}, 457 | "outputs": [ 458 | { 459 | "name": "stdout", 460 | "output_type": "stream", 461 | "text": [ 462 | "2.0\n" 463 | ] 464 | } 465 | ], 466 | "source": [ 467 | "from math import pi, sqrt, pow, floor, ceil, log10\n", 468 | "\n", 469 | "print(sqrt(4))" 470 | ] 471 | }, 472 | { 473 | "cell_type": "code", 474 | "execution_count": 30, 475 | "id": "19f715c4", 476 | "metadata": {}, 477 | "outputs": [ 478 | { 479 | "name": "stdout", 480 | "output_type": "stream", 481 | "text": [ 482 | "2.0\n" 483 | ] 484 | } 485 | ], 486 | "source": [ 487 | "print(math.log10(100))" 488 | ] 489 | }, 490 | { 491 | "cell_type": "code", 492 | "execution_count": 31, 493 | "id": "026fd166", 494 | "metadata": {}, 495 | "outputs": [ 496 | { 497 | "name": "stdout", 498 | "output_type": "stream", 499 | "text": [ 500 | "3.141592653589793\n" 501 | ] 502 | } 503 | ], 504 | "source": [ 505 | "from math import pi as pie\n", 506 | "\n", 507 | "print(pie)" 508 | ] 509 | }, 510 | { 511 | "cell_type": "markdown", 512 | "id": "852003f3", 513 | "metadata": {}, 514 | "source": [ 515 | "## String" 516 | ] 517 | }, 518 | { 519 | "cell_type": "code", 520 | "execution_count": 32, 521 | "id": "960b3ba2", 522 | "metadata": {}, 523 | "outputs": [], 524 | "source": [ 525 | "import string" 526 | ] 527 | }, 528 | { 529 | "cell_type": "code", 530 | "execution_count": 33, 531 | "id": "138e06ef", 532 | "metadata": {}, 533 | "outputs": [ 534 | { 535 | "name": "stdout", 536 | "output_type": "stream", 537 | "text": [ 538 | "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\n" 539 | ] 540 | } 541 | ], 542 | "source": [ 543 | "print(string.ascii_letters)" 544 | ] 545 | }, 546 | { 547 | "cell_type": "code", 548 | "execution_count": 34, 549 | "id": "05661000", 550 | "metadata": {}, 551 | "outputs": [ 552 | { 553 | "name": "stdout", 554 | "output_type": "stream", 555 | "text": [ 556 | "0123456789\n" 557 | ] 558 | } 559 | ], 560 | "source": [ 561 | "print(string.digits)" 562 | ] 563 | }, 564 | { 565 | "cell_type": "code", 566 | "execution_count": 35, 567 | "id": "3d66bb15", 568 | "metadata": {}, 569 | "outputs": [ 570 | { 571 | "name": "stdout", 572 | "output_type": "stream", 573 | "text": [ 574 | "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~\n" 575 | ] 576 | } 577 | ], 578 | "source": [ 579 | "print(string.punctuation)" 580 | ] 581 | }, 582 | { 583 | "cell_type": "markdown", 584 | "id": "8dcb8da0", 585 | "metadata": {}, 586 | "source": [ 587 | "## Random" 588 | ] 589 | }, 590 | { 591 | "cell_type": "code", 592 | "execution_count": 36, 593 | "id": "c69ec5d6", 594 | "metadata": {}, 595 | "outputs": [ 596 | { 597 | "name": "stdout", 598 | "output_type": "stream", 599 | "text": [ 600 | "0.9667194108253725\n" 601 | ] 602 | } 603 | ], 604 | "source": [ 605 | "from random import random, randint\n", 606 | "\n", 607 | "print(random())" 608 | ] 609 | }, 610 | { 611 | "cell_type": "code", 612 | "execution_count": 39, 613 | "id": "6085b5ed", 614 | "metadata": {}, 615 | "outputs": [ 616 | { 617 | "name": "stdout", 618 | "output_type": "stream", 619 | "text": [ 620 | "0.5687283414736181\n" 621 | ] 622 | } 623 | ], 624 | "source": [ 625 | "print(random())" 626 | ] 627 | }, 628 | { 629 | "cell_type": "code", 630 | "execution_count": 44, 631 | "id": "1fee3ce0", 632 | "metadata": {}, 633 | "outputs": [ 634 | { 635 | "name": "stdout", 636 | "output_type": "stream", 637 | "text": [ 638 | "11\n" 639 | ] 640 | } 641 | ], 642 | "source": [ 643 | "print(randint(5,20))" 644 | ] 645 | }, 646 | { 647 | "cell_type": "code", 648 | "execution_count": null, 649 | "id": "5eceb8ac", 650 | "metadata": {}, 651 | "outputs": [], 652 | "source": [] 653 | } 654 | ], 655 | "metadata": { 656 | "kernelspec": { 657 | "display_name": "Python 3 (ipykernel)", 658 | "language": "python", 659 | "name": "python3" 660 | }, 661 | "language_info": { 662 | "codemirror_mode": { 663 | "name": "ipython", 664 | "version": 3 665 | }, 666 | "file_extension": ".py", 667 | "mimetype": "text/x-python", 668 | "name": "python", 669 | "nbconvert_exporter": "python", 670 | "pygments_lexer": "ipython3", 671 | "version": "3.9.12" 672 | } 673 | }, 674 | "nbformat": 4, 675 | "nbformat_minor": 5 676 | } 677 | -------------------------------------------------------------------------------- /19_Modules and Packages/__pycache__/indore.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hemansnation/Python-and-Data-Structures-for-Data-Science/69bf18c4c173bddd696767ea96290b7ec30ff7b0/19_Modules and Packages/__pycache__/indore.cpython-310.pyc -------------------------------------------------------------------------------- /19_Modules and Packages/indore.py: -------------------------------------------------------------------------------- 1 | msg = "Indore is the best." 2 | 3 | def vijay(): 4 | return "hello from vijay" 5 | 6 | def nagar(n): 7 | return n + "!!!" -------------------------------------------------------------------------------- /19_Modules and Packages/main.py: -------------------------------------------------------------------------------- 1 | # import indore 2 | 3 | # print(indore.msg) 4 | 5 | # print(indore.vijay()) 6 | 7 | # print(indore.nagar("Hello User")) 8 | 9 | # import indore as i 10 | 11 | # print(i.msg) 12 | 13 | 14 | # import statisticsmodels 15 | 16 | # from statisticsmodels import descriptive 17 | 18 | # a = descriptive.Descript() 19 | 20 | # print(a.pie()) 21 | 22 | from statisticsmodels.inference import inferential 23 | 24 | x = inferential.Infer() 25 | 26 | print(x.models()) -------------------------------------------------------------------------------- /19_Modules and Packages/statisticsmodels/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hemansnation/Python-and-Data-Structures-for-Data-Science/69bf18c4c173bddd696767ea96290b7ec30ff7b0/19_Modules and Packages/statisticsmodels/__init__.py -------------------------------------------------------------------------------- /19_Modules and Packages/statisticsmodels/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hemansnation/Python-and-Data-Structures-for-Data-Science/69bf18c4c173bddd696767ea96290b7ec30ff7b0/19_Modules and Packages/statisticsmodels/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /19_Modules and Packages/statisticsmodels/__pycache__/descriptive.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hemansnation/Python-and-Data-Structures-for-Data-Science/69bf18c4c173bddd696767ea96290b7ec30ff7b0/19_Modules and Packages/statisticsmodels/__pycache__/descriptive.cpython-310.pyc -------------------------------------------------------------------------------- /19_Modules and Packages/statisticsmodels/descriptive.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | class Descript: 4 | 5 | def mean(self, l): 6 | return sum(l)/len(l) 7 | 8 | def mode(self, l): 9 | return "mode :" + str(l) 10 | 11 | def pie(self): 12 | return math.pi 13 | 14 | -------------------------------------------------------------------------------- /19_Modules and Packages/statisticsmodels/inference/__pycache__/inferential.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hemansnation/Python-and-Data-Structures-for-Data-Science/69bf18c4c173bddd696767ea96290b7ec30ff7b0/19_Modules and Packages/statisticsmodels/inference/__pycache__/inferential.cpython-310.pyc -------------------------------------------------------------------------------- /19_Modules and Packages/statisticsmodels/inference/inferential.py: -------------------------------------------------------------------------------- 1 | class Infer: 2 | 3 | def models(self): 4 | return "Models from Infer class and inderential module" -------------------------------------------------------------------------------- /20_21_22_23_24_Data Structures/.ipynb_checkpoints/20_Data Structures Basics-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "f82ebe19", 6 | "metadata": {}, 7 | "source": [ 8 | "## Variables and Data Types" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": null, 14 | "id": "bbcb3d27", 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "int a = 10; # c language -> 2 bytes -> 16 bits" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": null, 24 | "id": "59d40dc7", 25 | "metadata": {}, 26 | "outputs": [], 27 | "source": [ 28 | "a = 10 # python | 10 is an object 4 bytes" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "id": "88c65e3a", 34 | "metadata": {}, 35 | "source": [ 36 | "## Analysis of Algorithms" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": null, 42 | "id": "c51b11e6", 43 | "metadata": {}, 44 | "outputs": [], 45 | "source": [ 46 | "# time complexity\n", 47 | "\n", 48 | "# space complexity" 49 | ] 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "id": "f4774a97", 54 | "metadata": {}, 55 | "source": [ 56 | "## Asymptotic Notations" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": null, 62 | "id": "0985ed7e", 63 | "metadata": {}, 64 | "outputs": [], 65 | "source": [ 66 | "# input of array/list of numbers, n - size of the array\n", 67 | "\n", 68 | "O(n) -> O(5)" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": null, 74 | "id": "e3da3336", 75 | "metadata": {}, 76 | "outputs": [], 77 | "source": [ 78 | "# loops\n", 79 | "\n", 80 | "for i in range(3):\n", 81 | " # code\n", 82 | " \n", 83 | "O(n)" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": null, 89 | "id": "6c808f07", 90 | "metadata": {}, 91 | "outputs": [], 92 | "source": [ 93 | "for i in range(3):\n", 94 | " for j in range(3):\n", 95 | " # code\n", 96 | " \n", 97 | "O(n^2)" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": null, 103 | "id": "6b9b5280", 104 | "metadata": {}, 105 | "outputs": [], 106 | "source": [ 107 | "for i in range(3 * n):\n", 108 | " # code\n", 109 | "\n", 110 | "for i in range(n + 5):\n", 111 | " # code\n", 112 | "\n", 113 | "for i in range(n/2):\n", 114 | " # code\n", 115 | "\n", 116 | "O(n)" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": null, 122 | "id": "41faee63", 123 | "metadata": {}, 124 | "outputs": [], 125 | "source": [ 126 | "# phases\n", 127 | "\n", 128 | "for i in range(n):\n", 129 | " # code\n", 130 | "\n", 131 | "for i in range(n):\n", 132 | " for j in range(n):\n", 133 | " # code\n", 134 | "\n", 135 | "for i in range(n):\n", 136 | " # code\n", 137 | " \n", 138 | "O(n) + O(n^2) + O(n)\n", 139 | "\n", 140 | "O(n^2)" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": null, 146 | "id": "237e0d39", 147 | "metadata": {}, 148 | "outputs": [], 149 | "source": [ 150 | "for i in range(n):\n", 151 | " for i in range(m):\n", 152 | " # code\n", 153 | " \n", 154 | "O(n^m)" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 1, 160 | "id": "26a0f836", 161 | "metadata": {}, 162 | "outputs": [ 163 | { 164 | "name": "stdout", 165 | "output_type": "stream", 166 | "text": [ 167 | "Requirement already satisfied: psutil in c:\\users\\himan\\anaconda3\\lib\\site-packages (5.8.0)\n" 168 | ] 169 | } 170 | ], 171 | "source": [ 172 | "!pip install psutil" 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": 2, 178 | "id": "99cee167", 179 | "metadata": {}, 180 | "outputs": [ 181 | { 182 | "name": "stdout", 183 | "output_type": "stream", 184 | "text": [ 185 | "30.62109375\n" 186 | ] 187 | } 188 | ], 189 | "source": [ 190 | "import os, psutil\n", 191 | "\n", 192 | "print(psutil.Process(os.getpid()).memory_info().rss / 1024 ** 2)" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": 4, 198 | "id": "c80a8b72", 199 | "metadata": {}, 200 | "outputs": [ 201 | { 202 | "name": "stdout", 203 | "output_type": "stream", 204 | "text": [ 205 | "0\n", 206 | "1\n", 207 | "2\n", 208 | "3\n", 209 | "4\n", 210 | "5\n", 211 | "6\n", 212 | "7\n", 213 | "8\n", 214 | "9\n", 215 | "--- 0.0010128021240234375 seconds ---\n" 216 | ] 217 | } 218 | ], 219 | "source": [ 220 | "import time\n", 221 | "start_time = time.time()\n", 222 | "\n", 223 | "for i in range(10):\n", 224 | " print(i)\n", 225 | "\n", 226 | "print(\"--- %s seconds ---\" % (time.time() - start_time))" 227 | ] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "execution_count": 5, 232 | "id": "4c0c8bf4", 233 | "metadata": {}, 234 | "outputs": [ 235 | { 236 | "name": "stdout", 237 | "output_type": "stream", 238 | "text": [ 239 | "0\n", 240 | "1\n", 241 | "2\n", 242 | "3\n", 243 | "4\n", 244 | "5\n", 245 | "6\n", 246 | "7\n", 247 | "8\n", 248 | "9\n", 249 | "0\n", 250 | "1\n", 251 | "2\n", 252 | "3\n", 253 | "4\n", 254 | "5\n", 255 | "6\n", 256 | "7\n", 257 | "8\n", 258 | "9\n", 259 | "0\n", 260 | "1\n", 261 | "2\n", 262 | "3\n", 263 | "4\n", 264 | "5\n", 265 | "6\n", 266 | "7\n", 267 | "8\n", 268 | "9\n", 269 | "0\n", 270 | "1\n", 271 | "2\n", 272 | "3\n", 273 | "4\n", 274 | "5\n", 275 | "6\n", 276 | "7\n", 277 | "8\n", 278 | "9\n", 279 | "0\n", 280 | "1\n", 281 | "2\n", 282 | "3\n", 283 | "4\n", 284 | "5\n", 285 | "6\n", 286 | "7\n", 287 | "8\n", 288 | "9\n", 289 | "0\n", 290 | "1\n", 291 | "2\n", 292 | "3\n", 293 | "4\n", 294 | "5\n", 295 | "6\n", 296 | "7\n", 297 | "8\n", 298 | "9\n", 299 | "0\n", 300 | "1\n", 301 | "2\n", 302 | "3\n", 303 | "4\n", 304 | "5\n", 305 | "6\n", 306 | "7\n", 307 | "8\n", 308 | "9\n", 309 | "0\n", 310 | "1\n", 311 | "2\n", 312 | "3\n", 313 | "4\n", 314 | "5\n", 315 | "6\n", 316 | "7\n", 317 | "8\n", 318 | "9\n", 319 | "0\n", 320 | "1\n", 321 | "2\n", 322 | "3\n", 323 | "4\n", 324 | "5\n", 325 | "6\n", 326 | "7\n", 327 | "8\n", 328 | "9\n", 329 | "0\n", 330 | "1\n", 331 | "2\n", 332 | "3\n", 333 | "4\n", 334 | "5\n", 335 | "6\n", 336 | "7\n", 337 | "8\n", 338 | "9\n", 339 | "--- 0.006000518798828125 seconds ---\n" 340 | ] 341 | } 342 | ], 343 | "source": [ 344 | "start_time = time.time()\n", 345 | "\n", 346 | "for i in range(10):\n", 347 | " for i in range(10):\n", 348 | " print(i)\n", 349 | "\n", 350 | "print(\"--- %s seconds ---\" % (time.time() - start_time))" 351 | ] 352 | }, 353 | { 354 | "cell_type": "markdown", 355 | "id": "8aac6799", 356 | "metadata": {}, 357 | "source": [ 358 | "## Complexity Classes" 359 | ] 360 | }, 361 | { 362 | "cell_type": "code", 363 | "execution_count": null, 364 | "id": "45d0371a", 365 | "metadata": {}, 366 | "outputs": [], 367 | "source": [ 368 | "O(1) < O(log n) < O(root(n)) < O(n) < O(n log n) < O(n^2) < O(n^3) < O(2^n) < O(n!) < O(n^k)" 369 | ] 370 | }, 371 | { 372 | "cell_type": "code", 373 | "execution_count": null, 374 | "id": "c5af9f36", 375 | "metadata": {}, 376 | "outputs": [], 377 | "source": [ 378 | "theta(n)\n" 379 | ] 380 | }, 381 | { 382 | "cell_type": "markdown", 383 | "id": "4348aad8", 384 | "metadata": {}, 385 | "source": [ 386 | "## NP-hard problems" 387 | ] 388 | }, 389 | { 390 | "cell_type": "code", 391 | "execution_count": null, 392 | "id": "4cdd3eaf", 393 | "metadata": {}, 394 | "outputs": [], 395 | "source": [] 396 | } 397 | ], 398 | "metadata": { 399 | "kernelspec": { 400 | "display_name": "Python 3 (ipykernel)", 401 | "language": "python", 402 | "name": "python3" 403 | }, 404 | "language_info": { 405 | "codemirror_mode": { 406 | "name": "ipython", 407 | "version": 3 408 | }, 409 | "file_extension": ".py", 410 | "mimetype": "text/x-python", 411 | "name": "python", 412 | "nbconvert_exporter": "python", 413 | "pygments_lexer": "ipython3", 414 | "version": "3.9.12" 415 | } 416 | }, 417 | "nbformat": 4, 418 | "nbformat_minor": 5 419 | } 420 | -------------------------------------------------------------------------------- /20_21_22_23_24_Data Structures/.ipynb_checkpoints/21_Stack Data Structures-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "id": "962cd906", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "a = x + y + 2z" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "id": "dd9bb479", 16 | "metadata": {}, 17 | "source": [ 18 | "# Stack using Python List\n", 19 | "\n", 20 | "Follow LIFO (Last In First Out) system\n", 21 | "\n", 22 | "- insert\n", 23 | "- delete\n", 24 | "- display" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 1, 30 | "id": "4329f8aa", 31 | "metadata": {}, 32 | "outputs": [ 33 | { 34 | "name": "stdout", 35 | "output_type": "stream", 36 | "text": [ 37 | "\n" 38 | ] 39 | } 40 | ], 41 | "source": [ 42 | "stack = []\n", 43 | "\n", 44 | "print(type(stack))" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 3, 50 | "id": "8f5b9ffc", 51 | "metadata": {}, 52 | "outputs": [ 53 | { 54 | "name": "stdout", 55 | "output_type": "stream", 56 | "text": [ 57 | "Display Stack: [10, 20, 30, 40]\n", 58 | "Top of the stack: 40\n" 59 | ] 60 | } 61 | ], 62 | "source": [ 63 | "stack = []\n", 64 | "\n", 65 | "stack.append(10)\n", 66 | "stack.append(20)\n", 67 | "stack.append(30)\n", 68 | "stack.append(40)\n", 69 | "\n", 70 | "print(\"Display Stack:\", stack)\n", 71 | "\n", 72 | "top = stack[-1]\n", 73 | "\n", 74 | "print(\"Top of the stack:\", top)" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 4, 80 | "id": "30f96ded", 81 | "metadata": {}, 82 | "outputs": [ 83 | { 84 | "name": "stdout", 85 | "output_type": "stream", 86 | "text": [ 87 | "40\n" 88 | ] 89 | } 90 | ], 91 | "source": [ 92 | "print(stack.pop())" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 5, 98 | "id": "77881f05", 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "name": "stdout", 103 | "output_type": "stream", 104 | "text": [ 105 | "Top of the stack: 30\n" 106 | ] 107 | } 108 | ], 109 | "source": [ 110 | "top = stack[-1]\n", 111 | "\n", 112 | "print(\"Top of the stack:\", top)" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": 6, 118 | "id": "906a83ee", 119 | "metadata": {}, 120 | "outputs": [ 121 | { 122 | "name": "stdout", 123 | "output_type": "stream", 124 | "text": [ 125 | "Display Stack: [10, 20, 30]\n" 126 | ] 127 | } 128 | ], 129 | "source": [ 130 | "print(\"Display Stack:\", stack)\n" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": 10, 136 | "id": "445a6be0", 137 | "metadata": {}, 138 | "outputs": [ 139 | { 140 | "name": "stdout", 141 | "output_type": "stream", 142 | "text": [ 143 | "5\n" 144 | ] 145 | } 146 | ], 147 | "source": [ 148 | "data = [1,2,3,4,5]\n", 149 | "c = 0\n", 150 | "while data[c:]:\n", 151 | " c += 1\n", 152 | "print(c)" 153 | ] 154 | }, 155 | { 156 | "cell_type": "markdown", 157 | "id": "5fe0e37f", 158 | "metadata": {}, 159 | "source": [ 160 | "# Stack using classes from Scratch" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": 17, 166 | "id": "43762c83", 167 | "metadata": {}, 168 | "outputs": [], 169 | "source": [ 170 | "class Stack:\n", 171 | " MAXIMUM_CAPACITY = 10\n", 172 | " \n", 173 | " # init\n", 174 | " def __init__(self):\n", 175 | " self.data = []\n", 176 | " \n", 177 | " # length\n", 178 | " def __len__(self):\n", 179 | " c = 0\n", 180 | " while self.data[c:]:\n", 181 | " c += 1\n", 182 | " \n", 183 | " return c\n", 184 | " \n", 185 | " # is empty\n", 186 | " def is_empty(self):\n", 187 | " return self.__len__() == 0\n", 188 | " \n", 189 | " # top\n", 190 | " def top(self):\n", 191 | " if self.is_empty():\n", 192 | " print(\"Stack Underflow\")\n", 193 | " else:\n", 194 | " return self.data[-1]\n", 195 | " \n", 196 | " # push - insert\n", 197 | " def push(self, value):\n", 198 | " if self.__len__() == self.MAXIMUM_CAPACITY:\n", 199 | " print(\"Stack Overflow\")\n", 200 | " else:\n", 201 | " self.data.append(value)\n", 202 | " \n", 203 | " \n", 204 | " # pop - delete\n", 205 | " def pop(self):\n", 206 | " if self.is_empty():\n", 207 | " print(\"Stack is Empty\")\n", 208 | " else:\n", 209 | " return self.data.pop()\n", 210 | " \n", 211 | " # display\n", 212 | " def display(self):\n", 213 | " if self.is_empty():\n", 214 | " print(\"Stack is Empty\")\n", 215 | " else:\n", 216 | " return self.data\n", 217 | " \n", 218 | " " 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": 18, 224 | "id": "251bc1ca", 225 | "metadata": {}, 226 | "outputs": [], 227 | "source": [ 228 | "s = Stack()" 229 | ] 230 | }, 231 | { 232 | "cell_type": "code", 233 | "execution_count": 19, 234 | "id": "e541bc97", 235 | "metadata": {}, 236 | "outputs": [ 237 | { 238 | "data": { 239 | "text/plain": [ 240 | "True" 241 | ] 242 | }, 243 | "execution_count": 19, 244 | "metadata": {}, 245 | "output_type": "execute_result" 246 | } 247 | ], 248 | "source": [ 249 | "s.is_empty()" 250 | ] 251 | }, 252 | { 253 | "cell_type": "code", 254 | "execution_count": 20, 255 | "id": "1482d784", 256 | "metadata": {}, 257 | "outputs": [ 258 | { 259 | "name": "stdout", 260 | "output_type": "stream", 261 | "text": [ 262 | "[]\n" 263 | ] 264 | } 265 | ], 266 | "source": [ 267 | "print(s.data)" 268 | ] 269 | }, 270 | { 271 | "cell_type": "code", 272 | "execution_count": 21, 273 | "id": "766c9bae", 274 | "metadata": {}, 275 | "outputs": [ 276 | { 277 | "name": "stdout", 278 | "output_type": "stream", 279 | "text": [ 280 | "Stack is Empty\n" 281 | ] 282 | } 283 | ], 284 | "source": [ 285 | "s.display()" 286 | ] 287 | }, 288 | { 289 | "cell_type": "code", 290 | "execution_count": 22, 291 | "id": "9beb0999", 292 | "metadata": {}, 293 | "outputs": [], 294 | "source": [ 295 | "s.push(11)\n", 296 | "s.push(12)\n", 297 | "s.push(13)\n", 298 | "s.push(14)" 299 | ] 300 | }, 301 | { 302 | "cell_type": "code", 303 | "execution_count": 23, 304 | "id": "4dac7116", 305 | "metadata": {}, 306 | "outputs": [ 307 | { 308 | "data": { 309 | "text/plain": [ 310 | "[11, 12, 13, 14]" 311 | ] 312 | }, 313 | "execution_count": 23, 314 | "metadata": {}, 315 | "output_type": "execute_result" 316 | } 317 | ], 318 | "source": [ 319 | "s.display()" 320 | ] 321 | }, 322 | { 323 | "cell_type": "code", 324 | "execution_count": 24, 325 | "id": "97e25b16", 326 | "metadata": {}, 327 | "outputs": [ 328 | { 329 | "data": { 330 | "text/plain": [ 331 | "False" 332 | ] 333 | }, 334 | "execution_count": 24, 335 | "metadata": {}, 336 | "output_type": "execute_result" 337 | } 338 | ], 339 | "source": [ 340 | "s.is_empty()" 341 | ] 342 | }, 343 | { 344 | "cell_type": "code", 345 | "execution_count": 25, 346 | "id": "911a2c8a", 347 | "metadata": {}, 348 | "outputs": [ 349 | { 350 | "data": { 351 | "text/plain": [ 352 | "4" 353 | ] 354 | }, 355 | "execution_count": 25, 356 | "metadata": {}, 357 | "output_type": "execute_result" 358 | } 359 | ], 360 | "source": [ 361 | "s.__len__()" 362 | ] 363 | }, 364 | { 365 | "cell_type": "code", 366 | "execution_count": 26, 367 | "id": "3929104c", 368 | "metadata": {}, 369 | "outputs": [ 370 | { 371 | "data": { 372 | "text/plain": [ 373 | "14" 374 | ] 375 | }, 376 | "execution_count": 26, 377 | "metadata": {}, 378 | "output_type": "execute_result" 379 | } 380 | ], 381 | "source": [ 382 | "s.top()" 383 | ] 384 | }, 385 | { 386 | "cell_type": "code", 387 | "execution_count": 27, 388 | "id": "00124679", 389 | "metadata": {}, 390 | "outputs": [ 391 | { 392 | "data": { 393 | "text/plain": [ 394 | "14" 395 | ] 396 | }, 397 | "execution_count": 27, 398 | "metadata": {}, 399 | "output_type": "execute_result" 400 | } 401 | ], 402 | "source": [ 403 | "s.pop()" 404 | ] 405 | }, 406 | { 407 | "cell_type": "code", 408 | "execution_count": 28, 409 | "id": "950a49af", 410 | "metadata": {}, 411 | "outputs": [ 412 | { 413 | "data": { 414 | "text/plain": [ 415 | "[11, 12, 13]" 416 | ] 417 | }, 418 | "execution_count": 28, 419 | "metadata": {}, 420 | "output_type": "execute_result" 421 | } 422 | ], 423 | "source": [ 424 | "s.display()" 425 | ] 426 | }, 427 | { 428 | "cell_type": "code", 429 | "execution_count": 29, 430 | "id": "6a69d490", 431 | "metadata": {}, 432 | "outputs": [ 433 | { 434 | "data": { 435 | "text/plain": [ 436 | "13" 437 | ] 438 | }, 439 | "execution_count": 29, 440 | "metadata": {}, 441 | "output_type": "execute_result" 442 | } 443 | ], 444 | "source": [ 445 | "s.pop()" 446 | ] 447 | }, 448 | { 449 | "cell_type": "code", 450 | "execution_count": 30, 451 | "id": "5aea258c", 452 | "metadata": {}, 453 | "outputs": [ 454 | { 455 | "data": { 456 | "text/plain": [ 457 | "12" 458 | ] 459 | }, 460 | "execution_count": 30, 461 | "metadata": {}, 462 | "output_type": "execute_result" 463 | } 464 | ], 465 | "source": [ 466 | "s.pop()" 467 | ] 468 | }, 469 | { 470 | "cell_type": "code", 471 | "execution_count": 31, 472 | "id": "0b41871a", 473 | "metadata": {}, 474 | "outputs": [ 475 | { 476 | "data": { 477 | "text/plain": [ 478 | "11" 479 | ] 480 | }, 481 | "execution_count": 31, 482 | "metadata": {}, 483 | "output_type": "execute_result" 484 | } 485 | ], 486 | "source": [ 487 | "s.pop()" 488 | ] 489 | }, 490 | { 491 | "cell_type": "code", 492 | "execution_count": 32, 493 | "id": "205c31e7", 494 | "metadata": {}, 495 | "outputs": [ 496 | { 497 | "name": "stdout", 498 | "output_type": "stream", 499 | "text": [ 500 | "Stack is Empty\n" 501 | ] 502 | } 503 | ], 504 | "source": [ 505 | "s.pop()" 506 | ] 507 | }, 508 | { 509 | "cell_type": "code", 510 | "execution_count": 33, 511 | "id": "970e1e44", 512 | "metadata": {}, 513 | "outputs": [ 514 | { 515 | "name": "stdout", 516 | "output_type": "stream", 517 | "text": [ 518 | "Stack is Empty\n" 519 | ] 520 | } 521 | ], 522 | "source": [ 523 | "s.display()" 524 | ] 525 | }, 526 | { 527 | "cell_type": "code", 528 | "execution_count": 34, 529 | "id": "327d793d", 530 | "metadata": {}, 531 | "outputs": [], 532 | "source": [ 533 | "s.push(11)\n", 534 | "s.push(12)\n", 535 | "s.push(13)\n", 536 | "s.push(14)\n", 537 | "s.push(15)\n", 538 | "s.push(16)\n", 539 | "s.push(17)\n", 540 | "s.push(18)\n", 541 | "s.push(19)\n", 542 | "s.push(20)" 543 | ] 544 | }, 545 | { 546 | "cell_type": "code", 547 | "execution_count": 35, 548 | "id": "ea7dabd2", 549 | "metadata": {}, 550 | "outputs": [ 551 | { 552 | "data": { 553 | "text/plain": [ 554 | "[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]" 555 | ] 556 | }, 557 | "execution_count": 35, 558 | "metadata": {}, 559 | "output_type": "execute_result" 560 | } 561 | ], 562 | "source": [ 563 | "s.display()" 564 | ] 565 | }, 566 | { 567 | "cell_type": "code", 568 | "execution_count": 36, 569 | "id": "6135acfa", 570 | "metadata": {}, 571 | "outputs": [ 572 | { 573 | "name": "stdout", 574 | "output_type": "stream", 575 | "text": [ 576 | "Stack Overflow\n" 577 | ] 578 | } 579 | ], 580 | "source": [ 581 | "s.push(21)" 582 | ] 583 | }, 584 | { 585 | "cell_type": "code", 586 | "execution_count": 37, 587 | "id": "a246338b", 588 | "metadata": {}, 589 | "outputs": [ 590 | { 591 | "data": { 592 | "text/plain": [ 593 | "<__main__.Stack at 0x1c4f5701d60>" 594 | ] 595 | }, 596 | "execution_count": 37, 597 | "metadata": {}, 598 | "output_type": "execute_result" 599 | } 600 | ], 601 | "source": [ 602 | "s" 603 | ] 604 | }, 605 | { 606 | "cell_type": "code", 607 | "execution_count": null, 608 | "id": "805ddfd3", 609 | "metadata": {}, 610 | "outputs": [], 611 | "source": [ 612 | "create a package for data structures algorithms in python\n", 613 | "\n", 614 | "- stack.py\n", 615 | "- queue.py\n", 616 | "\n", 617 | "\n", 618 | "main.py - call here" 619 | ] 620 | } 621 | ], 622 | "metadata": { 623 | "kernelspec": { 624 | "display_name": "Python 3 (ipykernel)", 625 | "language": "python", 626 | "name": "python3" 627 | }, 628 | "language_info": { 629 | "codemirror_mode": { 630 | "name": "ipython", 631 | "version": 3 632 | }, 633 | "file_extension": ".py", 634 | "mimetype": "text/x-python", 635 | "name": "python", 636 | "nbconvert_exporter": "python", 637 | "pygments_lexer": "ipython3", 638 | "version": "3.9.12" 639 | } 640 | }, 641 | "nbformat": 4, 642 | "nbformat_minor": 5 643 | } 644 | -------------------------------------------------------------------------------- /20_21_22_23_24_Data Structures/.ipynb_checkpoints/22_Queue-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "73eeab56", 6 | "metadata": {}, 7 | "source": [ 8 | "# Queue\n", 9 | "\n", 10 | "FIFO(First In First Out)\n", 11 | "\n", 12 | "- enqueue - inserting an element\n", 13 | "- dequeue - deleting an element\n", 14 | "- display" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": null, 20 | "id": "aecb60b3", 21 | "metadata": {}, 22 | "outputs": [], 23 | "source": [ 24 | " None 40 50 60 70 100 110\n", 25 | " 0 1 2 3 4 5 6\n", 26 | " rare front" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 1, 32 | "id": "3a02c67f", 33 | "metadata": {}, 34 | "outputs": [ 35 | { 36 | "data": { 37 | "text/plain": [ 38 | "['Hi', 'Hi', 'Hi']" 39 | ] 40 | }, 41 | "execution_count": 1, 42 | "metadata": {}, 43 | "output_type": "execute_result" 44 | } 45 | ], 46 | "source": [ 47 | "[\"Hi\"] * 3" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 22, 53 | "id": "3e8ed283", 54 | "metadata": {}, 55 | "outputs": [], 56 | "source": [ 57 | "class Queue:\n", 58 | " \n", 59 | " DEFAULT_CAPACITY = 10\n", 60 | " \n", 61 | " def __init__(self):\n", 62 | " self.data = [None] * Queue.DEFAULT_CAPACITY # [None, None, None....]\n", 63 | " self.size = 0\n", 64 | " self.front = 0\n", 65 | " self.rare = 0\n", 66 | " \n", 67 | " # length\n", 68 | " def __len__(self):\n", 69 | " return self.size\n", 70 | " \n", 71 | " # is empty\n", 72 | " def is_empty(self):\n", 73 | " return self.size == 0\n", 74 | " \n", 75 | " # top most element/ first element\n", 76 | " def first(self):\n", 77 | " if self.is_empty():\n", 78 | " print(\"Queue is Empty\")\n", 79 | " else:\n", 80 | " return self.data[self.front]\n", 81 | " \n", 82 | " # dequeue - delete an element\n", 83 | " def dequeue(self):\n", 84 | " if self.is_empty():\n", 85 | " print(\"Queue is Underflow\")\n", 86 | " else:\n", 87 | " answer = self.data[self.rare + 1]\n", 88 | " self.data[self.rare] = None\n", 89 | " self.rare += 1\n", 90 | " self.size -= 1\n", 91 | " return answer\n", 92 | " \n", 93 | " \n", 94 | " # enqueue - insert an element\n", 95 | " def enqueue(self, element):\n", 96 | " if self.size == Queue.DEFAULT_CAPACITY:\n", 97 | " print(\"Queue is Overflow\")\n", 98 | " else:\n", 99 | " available = self.front + 1\n", 100 | " self.data[available] = element\n", 101 | " self.front += 1\n", 102 | " self.size += 1\n", 103 | " \n", 104 | " # display all the elements\n", 105 | " def display(self):\n", 106 | " if self.is_empty():\n", 107 | " print(\"Queue is Empty\")\n", 108 | " else:\n", 109 | " i = self.rare\n", 110 | " j = self.front\n", 111 | " while i <= j:\n", 112 | " print(self.data[i], end=\" \")\n", 113 | " i += 1\n", 114 | " # return self.data" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 23, 120 | "id": "071da9f6", 121 | "metadata": {}, 122 | "outputs": [], 123 | "source": [ 124 | "q = Queue()" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 24, 130 | "id": "1d9a5632", 131 | "metadata": {}, 132 | "outputs": [ 133 | { 134 | "data": { 135 | "text/plain": [ 136 | "0" 137 | ] 138 | }, 139 | "execution_count": 24, 140 | "metadata": {}, 141 | "output_type": "execute_result" 142 | } 143 | ], 144 | "source": [ 145 | "q.__len__()" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 25, 151 | "id": "229b0c14", 152 | "metadata": {}, 153 | "outputs": [ 154 | { 155 | "data": { 156 | "text/plain": [ 157 | "True" 158 | ] 159 | }, 160 | "execution_count": 25, 161 | "metadata": {}, 162 | "output_type": "execute_result" 163 | } 164 | ], 165 | "source": [ 166 | "q.is_empty()" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": 26, 172 | "id": "f710ed62", 173 | "metadata": {}, 174 | "outputs": [ 175 | { 176 | "name": "stdout", 177 | "output_type": "stream", 178 | "text": [ 179 | "Queue is Empty\n" 180 | ] 181 | } 182 | ], 183 | "source": [ 184 | "q.first()" 185 | ] 186 | }, 187 | { 188 | "cell_type": "code", 189 | "execution_count": 27, 190 | "id": "a3c7e9eb", 191 | "metadata": {}, 192 | "outputs": [], 193 | "source": [ 194 | "q.enqueue(10)\n", 195 | "q.enqueue(20)\n", 196 | "q.enqueue(30)\n", 197 | "q.enqueue(40)\n", 198 | "q.enqueue(50)\n", 199 | "q.enqueue(60)" 200 | ] 201 | }, 202 | { 203 | "cell_type": "code", 204 | "execution_count": 28, 205 | "id": "dbeaf9b5", 206 | "metadata": {}, 207 | "outputs": [ 208 | { 209 | "data": { 210 | "text/plain": [ 211 | "60" 212 | ] 213 | }, 214 | "execution_count": 28, 215 | "metadata": {}, 216 | "output_type": "execute_result" 217 | } 218 | ], 219 | "source": [ 220 | "q.first()" 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": 29, 226 | "id": "4571a436", 227 | "metadata": {}, 228 | "outputs": [ 229 | { 230 | "data": { 231 | "text/plain": [ 232 | "6" 233 | ] 234 | }, 235 | "execution_count": 29, 236 | "metadata": {}, 237 | "output_type": "execute_result" 238 | } 239 | ], 240 | "source": [ 241 | "q.__len__()" 242 | ] 243 | }, 244 | { 245 | "cell_type": "code", 246 | "execution_count": 30, 247 | "id": "7ee4a5ba", 248 | "metadata": {}, 249 | "outputs": [ 250 | { 251 | "name": "stdout", 252 | "output_type": "stream", 253 | "text": [ 254 | "10\n" 255 | ] 256 | } 257 | ], 258 | "source": [ 259 | "print(q.dequeue())" 260 | ] 261 | }, 262 | { 263 | "cell_type": "code", 264 | "execution_count": 31, 265 | "id": "d6069d65", 266 | "metadata": {}, 267 | "outputs": [ 268 | { 269 | "name": "stdout", 270 | "output_type": "stream", 271 | "text": [ 272 | "20\n" 273 | ] 274 | } 275 | ], 276 | "source": [ 277 | "print(q.dequeue())" 278 | ] 279 | }, 280 | { 281 | "cell_type": "code", 282 | "execution_count": 32, 283 | "id": "119ecbfe", 284 | "metadata": {}, 285 | "outputs": [ 286 | { 287 | "name": "stdout", 288 | "output_type": "stream", 289 | "text": [ 290 | "20 30 40 50 60 " 291 | ] 292 | } 293 | ], 294 | "source": [ 295 | "q.display()" 296 | ] 297 | }, 298 | { 299 | "cell_type": "code", 300 | "execution_count": null, 301 | "id": "ac7ecad5", 302 | "metadata": {}, 303 | "outputs": [], 304 | "source": [] 305 | } 306 | ], 307 | "metadata": { 308 | "kernelspec": { 309 | "display_name": "Python 3 (ipykernel)", 310 | "language": "python", 311 | "name": "python3" 312 | }, 313 | "language_info": { 314 | "codemirror_mode": { 315 | "name": "ipython", 316 | "version": 3 317 | }, 318 | "file_extension": ".py", 319 | "mimetype": "text/x-python", 320 | "name": "python", 321 | "nbconvert_exporter": "python", 322 | "pygments_lexer": "ipython3", 323 | "version": "3.9.12" 324 | } 325 | }, 326 | "nbformat": 4, 327 | "nbformat_minor": 5 328 | } 329 | -------------------------------------------------------------------------------- /20_21_22_23_24_Data Structures/20_Data Structures Basics.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "f82ebe19", 6 | "metadata": {}, 7 | "source": [ 8 | "## Variables and Data Types" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": null, 14 | "id": "bbcb3d27", 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "int a = 10; # c language -> 2 bytes -> 16 bits" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": null, 24 | "id": "59d40dc7", 25 | "metadata": {}, 26 | "outputs": [], 27 | "source": [ 28 | "a = 10 # python | 10 is an object 4 bytes" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "id": "88c65e3a", 34 | "metadata": {}, 35 | "source": [ 36 | "## Analysis of Algorithms" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": null, 42 | "id": "c51b11e6", 43 | "metadata": {}, 44 | "outputs": [], 45 | "source": [ 46 | "# time complexity\n", 47 | "\n", 48 | "# space complexity" 49 | ] 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "id": "f4774a97", 54 | "metadata": {}, 55 | "source": [ 56 | "## Asymptotic Notations" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": null, 62 | "id": "0985ed7e", 63 | "metadata": {}, 64 | "outputs": [], 65 | "source": [ 66 | "# input of array/list of numbers, n - size of the array\n", 67 | "\n", 68 | "O(n) -> O(5)" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": null, 74 | "id": "e3da3336", 75 | "metadata": {}, 76 | "outputs": [], 77 | "source": [ 78 | "# loops\n", 79 | "\n", 80 | "for i in range(3):\n", 81 | " # code\n", 82 | " \n", 83 | "O(n)" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": null, 89 | "id": "6c808f07", 90 | "metadata": {}, 91 | "outputs": [], 92 | "source": [ 93 | "for i in range(3):\n", 94 | " for j in range(3):\n", 95 | " # code\n", 96 | " \n", 97 | "O(n^2)" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": null, 103 | "id": "6b9b5280", 104 | "metadata": {}, 105 | "outputs": [], 106 | "source": [ 107 | "for i in range(3 * n):\n", 108 | " # code\n", 109 | "\n", 110 | "for i in range(n + 5):\n", 111 | " # code\n", 112 | "\n", 113 | "for i in range(n/2):\n", 114 | " # code\n", 115 | "\n", 116 | "O(n)" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": null, 122 | "id": "41faee63", 123 | "metadata": {}, 124 | "outputs": [], 125 | "source": [ 126 | "# phases\n", 127 | "\n", 128 | "for i in range(n):\n", 129 | " # code\n", 130 | "\n", 131 | "for i in range(n):\n", 132 | " for j in range(n):\n", 133 | " # code\n", 134 | "\n", 135 | "for i in range(n):\n", 136 | " # code\n", 137 | " \n", 138 | "O(n) + O(n^2) + O(n)\n", 139 | "\n", 140 | "O(n^2)" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": null, 146 | "id": "237e0d39", 147 | "metadata": {}, 148 | "outputs": [], 149 | "source": [ 150 | "for i in range(n):\n", 151 | " for i in range(m):\n", 152 | " # code\n", 153 | " \n", 154 | "O(n^m)" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 1, 160 | "id": "26a0f836", 161 | "metadata": {}, 162 | "outputs": [ 163 | { 164 | "name": "stdout", 165 | "output_type": "stream", 166 | "text": [ 167 | "Requirement already satisfied: psutil in c:\\users\\himan\\anaconda3\\lib\\site-packages (5.8.0)\n" 168 | ] 169 | } 170 | ], 171 | "source": [ 172 | "!pip install psutil" 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": 2, 178 | "id": "99cee167", 179 | "metadata": {}, 180 | "outputs": [ 181 | { 182 | "name": "stdout", 183 | "output_type": "stream", 184 | "text": [ 185 | "30.62109375\n" 186 | ] 187 | } 188 | ], 189 | "source": [ 190 | "import os, psutil\n", 191 | "\n", 192 | "print(psutil.Process(os.getpid()).memory_info().rss / 1024 ** 2)" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": 4, 198 | "id": "c80a8b72", 199 | "metadata": {}, 200 | "outputs": [ 201 | { 202 | "name": "stdout", 203 | "output_type": "stream", 204 | "text": [ 205 | "0\n", 206 | "1\n", 207 | "2\n", 208 | "3\n", 209 | "4\n", 210 | "5\n", 211 | "6\n", 212 | "7\n", 213 | "8\n", 214 | "9\n", 215 | "--- 0.0010128021240234375 seconds ---\n" 216 | ] 217 | } 218 | ], 219 | "source": [ 220 | "import time\n", 221 | "start_time = time.time()\n", 222 | "\n", 223 | "for i in range(10):\n", 224 | " print(i)\n", 225 | "\n", 226 | "print(\"--- %s seconds ---\" % (time.time() - start_time))" 227 | ] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "execution_count": 5, 232 | "id": "4c0c8bf4", 233 | "metadata": {}, 234 | "outputs": [ 235 | { 236 | "name": "stdout", 237 | "output_type": "stream", 238 | "text": [ 239 | "0\n", 240 | "1\n", 241 | "2\n", 242 | "3\n", 243 | "4\n", 244 | "5\n", 245 | "6\n", 246 | "7\n", 247 | "8\n", 248 | "9\n", 249 | "0\n", 250 | "1\n", 251 | "2\n", 252 | "3\n", 253 | "4\n", 254 | "5\n", 255 | "6\n", 256 | "7\n", 257 | "8\n", 258 | "9\n", 259 | "0\n", 260 | "1\n", 261 | "2\n", 262 | "3\n", 263 | "4\n", 264 | "5\n", 265 | "6\n", 266 | "7\n", 267 | "8\n", 268 | "9\n", 269 | "0\n", 270 | "1\n", 271 | "2\n", 272 | "3\n", 273 | "4\n", 274 | "5\n", 275 | "6\n", 276 | "7\n", 277 | "8\n", 278 | "9\n", 279 | "0\n", 280 | "1\n", 281 | "2\n", 282 | "3\n", 283 | "4\n", 284 | "5\n", 285 | "6\n", 286 | "7\n", 287 | "8\n", 288 | "9\n", 289 | "0\n", 290 | "1\n", 291 | "2\n", 292 | "3\n", 293 | "4\n", 294 | "5\n", 295 | "6\n", 296 | "7\n", 297 | "8\n", 298 | "9\n", 299 | "0\n", 300 | "1\n", 301 | "2\n", 302 | "3\n", 303 | "4\n", 304 | "5\n", 305 | "6\n", 306 | "7\n", 307 | "8\n", 308 | "9\n", 309 | "0\n", 310 | "1\n", 311 | "2\n", 312 | "3\n", 313 | "4\n", 314 | "5\n", 315 | "6\n", 316 | "7\n", 317 | "8\n", 318 | "9\n", 319 | "0\n", 320 | "1\n", 321 | "2\n", 322 | "3\n", 323 | "4\n", 324 | "5\n", 325 | "6\n", 326 | "7\n", 327 | "8\n", 328 | "9\n", 329 | "0\n", 330 | "1\n", 331 | "2\n", 332 | "3\n", 333 | "4\n", 334 | "5\n", 335 | "6\n", 336 | "7\n", 337 | "8\n", 338 | "9\n", 339 | "--- 0.006000518798828125 seconds ---\n" 340 | ] 341 | } 342 | ], 343 | "source": [ 344 | "start_time = time.time()\n", 345 | "\n", 346 | "for i in range(10):\n", 347 | " for i in range(10):\n", 348 | " print(i)\n", 349 | "\n", 350 | "print(\"--- %s seconds ---\" % (time.time() - start_time))" 351 | ] 352 | }, 353 | { 354 | "cell_type": "markdown", 355 | "id": "8aac6799", 356 | "metadata": {}, 357 | "source": [ 358 | "## Complexity Classes" 359 | ] 360 | }, 361 | { 362 | "cell_type": "code", 363 | "execution_count": null, 364 | "id": "45d0371a", 365 | "metadata": {}, 366 | "outputs": [], 367 | "source": [ 368 | "O(1) < O(log n) < O(root(n)) < O(n) < O(n log n) < O(n^2) < O(n^3) < O(2^n) < O(n!) < O(n^k)" 369 | ] 370 | }, 371 | { 372 | "cell_type": "code", 373 | "execution_count": null, 374 | "id": "c5af9f36", 375 | "metadata": {}, 376 | "outputs": [], 377 | "source": [ 378 | "theta(n)\n" 379 | ] 380 | }, 381 | { 382 | "cell_type": "markdown", 383 | "id": "4348aad8", 384 | "metadata": {}, 385 | "source": [ 386 | "## NP-hard problems" 387 | ] 388 | }, 389 | { 390 | "cell_type": "code", 391 | "execution_count": null, 392 | "id": "4cdd3eaf", 393 | "metadata": {}, 394 | "outputs": [], 395 | "source": [] 396 | } 397 | ], 398 | "metadata": { 399 | "kernelspec": { 400 | "display_name": "Python 3 (ipykernel)", 401 | "language": "python", 402 | "name": "python3" 403 | }, 404 | "language_info": { 405 | "codemirror_mode": { 406 | "name": "ipython", 407 | "version": 3 408 | }, 409 | "file_extension": ".py", 410 | "mimetype": "text/x-python", 411 | "name": "python", 412 | "nbconvert_exporter": "python", 413 | "pygments_lexer": "ipython3", 414 | "version": "3.9.12" 415 | } 416 | }, 417 | "nbformat": 4, 418 | "nbformat_minor": 5 419 | } 420 | -------------------------------------------------------------------------------- /20_21_22_23_24_Data Structures/21_Stack Data Structures.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "id": "962cd906", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "a = x + y + 2z" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "id": "dd9bb479", 16 | "metadata": {}, 17 | "source": [ 18 | "# Stack using Python List\n", 19 | "\n", 20 | "Follow LIFO (Last In First Out) system\n", 21 | "\n", 22 | "- insert\n", 23 | "- delete\n", 24 | "- display" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 1, 30 | "id": "4329f8aa", 31 | "metadata": {}, 32 | "outputs": [ 33 | { 34 | "name": "stdout", 35 | "output_type": "stream", 36 | "text": [ 37 | "\n" 38 | ] 39 | } 40 | ], 41 | "source": [ 42 | "stack = []\n", 43 | "\n", 44 | "print(type(stack))" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 3, 50 | "id": "8f5b9ffc", 51 | "metadata": {}, 52 | "outputs": [ 53 | { 54 | "name": "stdout", 55 | "output_type": "stream", 56 | "text": [ 57 | "Display Stack: [10, 20, 30, 40]\n", 58 | "Top of the stack: 40\n" 59 | ] 60 | } 61 | ], 62 | "source": [ 63 | "stack = []\n", 64 | "\n", 65 | "stack.append(10)\n", 66 | "stack.append(20)\n", 67 | "stack.append(30)\n", 68 | "stack.append(40)\n", 69 | "\n", 70 | "print(\"Display Stack:\", stack)\n", 71 | "\n", 72 | "top = stack[-1]\n", 73 | "\n", 74 | "print(\"Top of the stack:\", top)" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 4, 80 | "id": "30f96ded", 81 | "metadata": {}, 82 | "outputs": [ 83 | { 84 | "name": "stdout", 85 | "output_type": "stream", 86 | "text": [ 87 | "40\n" 88 | ] 89 | } 90 | ], 91 | "source": [ 92 | "print(stack.pop())" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 5, 98 | "id": "77881f05", 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "name": "stdout", 103 | "output_type": "stream", 104 | "text": [ 105 | "Top of the stack: 30\n" 106 | ] 107 | } 108 | ], 109 | "source": [ 110 | "top = stack[-1]\n", 111 | "\n", 112 | "print(\"Top of the stack:\", top)" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": 6, 118 | "id": "906a83ee", 119 | "metadata": {}, 120 | "outputs": [ 121 | { 122 | "name": "stdout", 123 | "output_type": "stream", 124 | "text": [ 125 | "Display Stack: [10, 20, 30]\n" 126 | ] 127 | } 128 | ], 129 | "source": [ 130 | "print(\"Display Stack:\", stack)\n" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": 10, 136 | "id": "445a6be0", 137 | "metadata": {}, 138 | "outputs": [ 139 | { 140 | "name": "stdout", 141 | "output_type": "stream", 142 | "text": [ 143 | "5\n" 144 | ] 145 | } 146 | ], 147 | "source": [ 148 | "data = [1,2,3,4,5]\n", 149 | "c = 0\n", 150 | "while data[c:]:\n", 151 | " c += 1\n", 152 | "print(c)" 153 | ] 154 | }, 155 | { 156 | "cell_type": "markdown", 157 | "id": "5fe0e37f", 158 | "metadata": {}, 159 | "source": [ 160 | "# Stack using classes from Scratch" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": 17, 166 | "id": "43762c83", 167 | "metadata": {}, 168 | "outputs": [], 169 | "source": [ 170 | "class Stack:\n", 171 | " MAXIMUM_CAPACITY = 10\n", 172 | " \n", 173 | " # init\n", 174 | " def __init__(self):\n", 175 | " self.data = []\n", 176 | " \n", 177 | " # length\n", 178 | " def __len__(self):\n", 179 | " c = 0\n", 180 | " while self.data[c:]:\n", 181 | " c += 1\n", 182 | " \n", 183 | " return c\n", 184 | " \n", 185 | " # is empty\n", 186 | " def is_empty(self):\n", 187 | " return self.__len__() == 0\n", 188 | " \n", 189 | " # top\n", 190 | " def top(self):\n", 191 | " if self.is_empty():\n", 192 | " print(\"Stack Underflow\")\n", 193 | " else:\n", 194 | " return self.data[-1]\n", 195 | " \n", 196 | " # push - insert\n", 197 | " def push(self, value):\n", 198 | " if self.__len__() == self.MAXIMUM_CAPACITY:\n", 199 | " print(\"Stack Overflow\")\n", 200 | " else:\n", 201 | " self.data.append(value)\n", 202 | " \n", 203 | " \n", 204 | " # pop - delete\n", 205 | " def pop(self):\n", 206 | " if self.is_empty():\n", 207 | " print(\"Stack is Empty\")\n", 208 | " else:\n", 209 | " return self.data.pop()\n", 210 | " \n", 211 | " # display\n", 212 | " def display(self):\n", 213 | " if self.is_empty():\n", 214 | " print(\"Stack is Empty\")\n", 215 | " else:\n", 216 | " return self.data\n", 217 | " \n", 218 | " " 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": 18, 224 | "id": "251bc1ca", 225 | "metadata": {}, 226 | "outputs": [], 227 | "source": [ 228 | "s = Stack()" 229 | ] 230 | }, 231 | { 232 | "cell_type": "code", 233 | "execution_count": 19, 234 | "id": "e541bc97", 235 | "metadata": {}, 236 | "outputs": [ 237 | { 238 | "data": { 239 | "text/plain": [ 240 | "True" 241 | ] 242 | }, 243 | "execution_count": 19, 244 | "metadata": {}, 245 | "output_type": "execute_result" 246 | } 247 | ], 248 | "source": [ 249 | "s.is_empty()" 250 | ] 251 | }, 252 | { 253 | "cell_type": "code", 254 | "execution_count": 20, 255 | "id": "1482d784", 256 | "metadata": {}, 257 | "outputs": [ 258 | { 259 | "name": "stdout", 260 | "output_type": "stream", 261 | "text": [ 262 | "[]\n" 263 | ] 264 | } 265 | ], 266 | "source": [ 267 | "print(s.data)" 268 | ] 269 | }, 270 | { 271 | "cell_type": "code", 272 | "execution_count": 21, 273 | "id": "766c9bae", 274 | "metadata": {}, 275 | "outputs": [ 276 | { 277 | "name": "stdout", 278 | "output_type": "stream", 279 | "text": [ 280 | "Stack is Empty\n" 281 | ] 282 | } 283 | ], 284 | "source": [ 285 | "s.display()" 286 | ] 287 | }, 288 | { 289 | "cell_type": "code", 290 | "execution_count": 22, 291 | "id": "9beb0999", 292 | "metadata": {}, 293 | "outputs": [], 294 | "source": [ 295 | "s.push(11)\n", 296 | "s.push(12)\n", 297 | "s.push(13)\n", 298 | "s.push(14)" 299 | ] 300 | }, 301 | { 302 | "cell_type": "code", 303 | "execution_count": 23, 304 | "id": "4dac7116", 305 | "metadata": {}, 306 | "outputs": [ 307 | { 308 | "data": { 309 | "text/plain": [ 310 | "[11, 12, 13, 14]" 311 | ] 312 | }, 313 | "execution_count": 23, 314 | "metadata": {}, 315 | "output_type": "execute_result" 316 | } 317 | ], 318 | "source": [ 319 | "s.display()" 320 | ] 321 | }, 322 | { 323 | "cell_type": "code", 324 | "execution_count": 24, 325 | "id": "97e25b16", 326 | "metadata": {}, 327 | "outputs": [ 328 | { 329 | "data": { 330 | "text/plain": [ 331 | "False" 332 | ] 333 | }, 334 | "execution_count": 24, 335 | "metadata": {}, 336 | "output_type": "execute_result" 337 | } 338 | ], 339 | "source": [ 340 | "s.is_empty()" 341 | ] 342 | }, 343 | { 344 | "cell_type": "code", 345 | "execution_count": 25, 346 | "id": "911a2c8a", 347 | "metadata": {}, 348 | "outputs": [ 349 | { 350 | "data": { 351 | "text/plain": [ 352 | "4" 353 | ] 354 | }, 355 | "execution_count": 25, 356 | "metadata": {}, 357 | "output_type": "execute_result" 358 | } 359 | ], 360 | "source": [ 361 | "s.__len__()" 362 | ] 363 | }, 364 | { 365 | "cell_type": "code", 366 | "execution_count": 26, 367 | "id": "3929104c", 368 | "metadata": {}, 369 | "outputs": [ 370 | { 371 | "data": { 372 | "text/plain": [ 373 | "14" 374 | ] 375 | }, 376 | "execution_count": 26, 377 | "metadata": {}, 378 | "output_type": "execute_result" 379 | } 380 | ], 381 | "source": [ 382 | "s.top()" 383 | ] 384 | }, 385 | { 386 | "cell_type": "code", 387 | "execution_count": 27, 388 | "id": "00124679", 389 | "metadata": {}, 390 | "outputs": [ 391 | { 392 | "data": { 393 | "text/plain": [ 394 | "14" 395 | ] 396 | }, 397 | "execution_count": 27, 398 | "metadata": {}, 399 | "output_type": "execute_result" 400 | } 401 | ], 402 | "source": [ 403 | "s.pop()" 404 | ] 405 | }, 406 | { 407 | "cell_type": "code", 408 | "execution_count": 28, 409 | "id": "950a49af", 410 | "metadata": {}, 411 | "outputs": [ 412 | { 413 | "data": { 414 | "text/plain": [ 415 | "[11, 12, 13]" 416 | ] 417 | }, 418 | "execution_count": 28, 419 | "metadata": {}, 420 | "output_type": "execute_result" 421 | } 422 | ], 423 | "source": [ 424 | "s.display()" 425 | ] 426 | }, 427 | { 428 | "cell_type": "code", 429 | "execution_count": 29, 430 | "id": "6a69d490", 431 | "metadata": {}, 432 | "outputs": [ 433 | { 434 | "data": { 435 | "text/plain": [ 436 | "13" 437 | ] 438 | }, 439 | "execution_count": 29, 440 | "metadata": {}, 441 | "output_type": "execute_result" 442 | } 443 | ], 444 | "source": [ 445 | "s.pop()" 446 | ] 447 | }, 448 | { 449 | "cell_type": "code", 450 | "execution_count": 30, 451 | "id": "5aea258c", 452 | "metadata": {}, 453 | "outputs": [ 454 | { 455 | "data": { 456 | "text/plain": [ 457 | "12" 458 | ] 459 | }, 460 | "execution_count": 30, 461 | "metadata": {}, 462 | "output_type": "execute_result" 463 | } 464 | ], 465 | "source": [ 466 | "s.pop()" 467 | ] 468 | }, 469 | { 470 | "cell_type": "code", 471 | "execution_count": 31, 472 | "id": "0b41871a", 473 | "metadata": {}, 474 | "outputs": [ 475 | { 476 | "data": { 477 | "text/plain": [ 478 | "11" 479 | ] 480 | }, 481 | "execution_count": 31, 482 | "metadata": {}, 483 | "output_type": "execute_result" 484 | } 485 | ], 486 | "source": [ 487 | "s.pop()" 488 | ] 489 | }, 490 | { 491 | "cell_type": "code", 492 | "execution_count": 32, 493 | "id": "205c31e7", 494 | "metadata": {}, 495 | "outputs": [ 496 | { 497 | "name": "stdout", 498 | "output_type": "stream", 499 | "text": [ 500 | "Stack is Empty\n" 501 | ] 502 | } 503 | ], 504 | "source": [ 505 | "s.pop()" 506 | ] 507 | }, 508 | { 509 | "cell_type": "code", 510 | "execution_count": 33, 511 | "id": "970e1e44", 512 | "metadata": {}, 513 | "outputs": [ 514 | { 515 | "name": "stdout", 516 | "output_type": "stream", 517 | "text": [ 518 | "Stack is Empty\n" 519 | ] 520 | } 521 | ], 522 | "source": [ 523 | "s.display()" 524 | ] 525 | }, 526 | { 527 | "cell_type": "code", 528 | "execution_count": 34, 529 | "id": "327d793d", 530 | "metadata": {}, 531 | "outputs": [], 532 | "source": [ 533 | "s.push(11)\n", 534 | "s.push(12)\n", 535 | "s.push(13)\n", 536 | "s.push(14)\n", 537 | "s.push(15)\n", 538 | "s.push(16)\n", 539 | "s.push(17)\n", 540 | "s.push(18)\n", 541 | "s.push(19)\n", 542 | "s.push(20)" 543 | ] 544 | }, 545 | { 546 | "cell_type": "code", 547 | "execution_count": 35, 548 | "id": "ea7dabd2", 549 | "metadata": {}, 550 | "outputs": [ 551 | { 552 | "data": { 553 | "text/plain": [ 554 | "[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]" 555 | ] 556 | }, 557 | "execution_count": 35, 558 | "metadata": {}, 559 | "output_type": "execute_result" 560 | } 561 | ], 562 | "source": [ 563 | "s.display()" 564 | ] 565 | }, 566 | { 567 | "cell_type": "code", 568 | "execution_count": 36, 569 | "id": "6135acfa", 570 | "metadata": {}, 571 | "outputs": [ 572 | { 573 | "name": "stdout", 574 | "output_type": "stream", 575 | "text": [ 576 | "Stack Overflow\n" 577 | ] 578 | } 579 | ], 580 | "source": [ 581 | "s.push(21)" 582 | ] 583 | }, 584 | { 585 | "cell_type": "code", 586 | "execution_count": 37, 587 | "id": "a246338b", 588 | "metadata": {}, 589 | "outputs": [ 590 | { 591 | "data": { 592 | "text/plain": [ 593 | "<__main__.Stack at 0x1c4f5701d60>" 594 | ] 595 | }, 596 | "execution_count": 37, 597 | "metadata": {}, 598 | "output_type": "execute_result" 599 | } 600 | ], 601 | "source": [ 602 | "s" 603 | ] 604 | }, 605 | { 606 | "cell_type": "code", 607 | "execution_count": null, 608 | "id": "805ddfd3", 609 | "metadata": {}, 610 | "outputs": [], 611 | "source": [ 612 | "create a package for data structures algorithms in python\n", 613 | "\n", 614 | "- stack.py\n", 615 | "- queue.py\n", 616 | "\n", 617 | "\n", 618 | "main.py - call here" 619 | ] 620 | } 621 | ], 622 | "metadata": { 623 | "kernelspec": { 624 | "display_name": "Python 3 (ipykernel)", 625 | "language": "python", 626 | "name": "python3" 627 | }, 628 | "language_info": { 629 | "codemirror_mode": { 630 | "name": "ipython", 631 | "version": 3 632 | }, 633 | "file_extension": ".py", 634 | "mimetype": "text/x-python", 635 | "name": "python", 636 | "nbconvert_exporter": "python", 637 | "pygments_lexer": "ipython3", 638 | "version": "3.9.12" 639 | } 640 | }, 641 | "nbformat": 4, 642 | "nbformat_minor": 5 643 | } 644 | -------------------------------------------------------------------------------- /20_21_22_23_24_Data Structures/22_Queue.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "73eeab56", 6 | "metadata": {}, 7 | "source": [ 8 | "# Queue\n", 9 | "\n", 10 | "FIFO(First In First Out)\n", 11 | "\n", 12 | "- enqueue - inserting an element\n", 13 | "- dequeue - deleting an element\n", 14 | "- display" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": null, 20 | "id": "aecb60b3", 21 | "metadata": {}, 22 | "outputs": [], 23 | "source": [ 24 | " None 40 50 60 70 100 110\n", 25 | " 0 1 2 3 4 5 6\n", 26 | " rare front" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 1, 32 | "id": "3a02c67f", 33 | "metadata": {}, 34 | "outputs": [ 35 | { 36 | "data": { 37 | "text/plain": [ 38 | "['Hi', 'Hi', 'Hi']" 39 | ] 40 | }, 41 | "execution_count": 1, 42 | "metadata": {}, 43 | "output_type": "execute_result" 44 | } 45 | ], 46 | "source": [ 47 | "[\"Hi\"] * 3" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 22, 53 | "id": "3e8ed283", 54 | "metadata": {}, 55 | "outputs": [], 56 | "source": [ 57 | "class Queue:\n", 58 | " \n", 59 | " DEFAULT_CAPACITY = 10\n", 60 | " \n", 61 | " def __init__(self):\n", 62 | " self.data = [None] * Queue.DEFAULT_CAPACITY # [None, None, None....]\n", 63 | " self.size = 0\n", 64 | " self.front = 0\n", 65 | " self.rare = 0\n", 66 | " \n", 67 | " # length\n", 68 | " def __len__(self):\n", 69 | " return self.size\n", 70 | " \n", 71 | " # is empty\n", 72 | " def is_empty(self):\n", 73 | " return self.size == 0\n", 74 | " \n", 75 | " # top most element/ first element\n", 76 | " def first(self):\n", 77 | " if self.is_empty():\n", 78 | " print(\"Queue is Empty\")\n", 79 | " else:\n", 80 | " return self.data[self.front]\n", 81 | " \n", 82 | " # dequeue - delete an element\n", 83 | " def dequeue(self):\n", 84 | " if self.is_empty():\n", 85 | " print(\"Queue is Underflow\")\n", 86 | " else:\n", 87 | " answer = self.data[self.rare + 1]\n", 88 | " self.data[self.rare] = None\n", 89 | " self.rare += 1\n", 90 | " self.size -= 1\n", 91 | " return answer\n", 92 | " \n", 93 | " \n", 94 | " # enqueue - insert an element\n", 95 | " def enqueue(self, element):\n", 96 | " if self.size == Queue.DEFAULT_CAPACITY:\n", 97 | " print(\"Queue is Overflow\")\n", 98 | " else:\n", 99 | " available = self.front + 1\n", 100 | " self.data[available] = element\n", 101 | " self.front += 1\n", 102 | " self.size += 1\n", 103 | " \n", 104 | " # display all the elements\n", 105 | " def display(self):\n", 106 | " if self.is_empty():\n", 107 | " print(\"Queue is Empty\")\n", 108 | " else:\n", 109 | " i = self.rare\n", 110 | " j = self.front\n", 111 | " while i <= j:\n", 112 | " print(self.data[i], end=\" \")\n", 113 | " i += 1\n", 114 | " # return self.data" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 23, 120 | "id": "071da9f6", 121 | "metadata": {}, 122 | "outputs": [], 123 | "source": [ 124 | "q = Queue()" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 24, 130 | "id": "1d9a5632", 131 | "metadata": {}, 132 | "outputs": [ 133 | { 134 | "data": { 135 | "text/plain": [ 136 | "0" 137 | ] 138 | }, 139 | "execution_count": 24, 140 | "metadata": {}, 141 | "output_type": "execute_result" 142 | } 143 | ], 144 | "source": [ 145 | "q.__len__()" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 25, 151 | "id": "229b0c14", 152 | "metadata": {}, 153 | "outputs": [ 154 | { 155 | "data": { 156 | "text/plain": [ 157 | "True" 158 | ] 159 | }, 160 | "execution_count": 25, 161 | "metadata": {}, 162 | "output_type": "execute_result" 163 | } 164 | ], 165 | "source": [ 166 | "q.is_empty()" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": 26, 172 | "id": "f710ed62", 173 | "metadata": {}, 174 | "outputs": [ 175 | { 176 | "name": "stdout", 177 | "output_type": "stream", 178 | "text": [ 179 | "Queue is Empty\n" 180 | ] 181 | } 182 | ], 183 | "source": [ 184 | "q.first()" 185 | ] 186 | }, 187 | { 188 | "cell_type": "code", 189 | "execution_count": 27, 190 | "id": "a3c7e9eb", 191 | "metadata": {}, 192 | "outputs": [], 193 | "source": [ 194 | "q.enqueue(10)\n", 195 | "q.enqueue(20)\n", 196 | "q.enqueue(30)\n", 197 | "q.enqueue(40)\n", 198 | "q.enqueue(50)\n", 199 | "q.enqueue(60)" 200 | ] 201 | }, 202 | { 203 | "cell_type": "code", 204 | "execution_count": 28, 205 | "id": "dbeaf9b5", 206 | "metadata": {}, 207 | "outputs": [ 208 | { 209 | "data": { 210 | "text/plain": [ 211 | "60" 212 | ] 213 | }, 214 | "execution_count": 28, 215 | "metadata": {}, 216 | "output_type": "execute_result" 217 | } 218 | ], 219 | "source": [ 220 | "q.first()" 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": 29, 226 | "id": "4571a436", 227 | "metadata": {}, 228 | "outputs": [ 229 | { 230 | "data": { 231 | "text/plain": [ 232 | "6" 233 | ] 234 | }, 235 | "execution_count": 29, 236 | "metadata": {}, 237 | "output_type": "execute_result" 238 | } 239 | ], 240 | "source": [ 241 | "q.__len__()" 242 | ] 243 | }, 244 | { 245 | "cell_type": "code", 246 | "execution_count": 30, 247 | "id": "7ee4a5ba", 248 | "metadata": {}, 249 | "outputs": [ 250 | { 251 | "name": "stdout", 252 | "output_type": "stream", 253 | "text": [ 254 | "10\n" 255 | ] 256 | } 257 | ], 258 | "source": [ 259 | "print(q.dequeue())" 260 | ] 261 | }, 262 | { 263 | "cell_type": "code", 264 | "execution_count": 31, 265 | "id": "d6069d65", 266 | "metadata": {}, 267 | "outputs": [ 268 | { 269 | "name": "stdout", 270 | "output_type": "stream", 271 | "text": [ 272 | "20\n" 273 | ] 274 | } 275 | ], 276 | "source": [ 277 | "print(q.dequeue())" 278 | ] 279 | }, 280 | { 281 | "cell_type": "code", 282 | "execution_count": 32, 283 | "id": "119ecbfe", 284 | "metadata": {}, 285 | "outputs": [ 286 | { 287 | "name": "stdout", 288 | "output_type": "stream", 289 | "text": [ 290 | "20 30 40 50 60 " 291 | ] 292 | } 293 | ], 294 | "source": [ 295 | "q.display()" 296 | ] 297 | }, 298 | { 299 | "cell_type": "code", 300 | "execution_count": null, 301 | "id": "ac7ecad5", 302 | "metadata": {}, 303 | "outputs": [], 304 | "source": [] 305 | } 306 | ], 307 | "metadata": { 308 | "kernelspec": { 309 | "display_name": "Python 3 (ipykernel)", 310 | "language": "python", 311 | "name": "python3" 312 | }, 313 | "language_info": { 314 | "codemirror_mode": { 315 | "name": "ipython", 316 | "version": 3 317 | }, 318 | "file_extension": ".py", 319 | "mimetype": "text/x-python", 320 | "name": "python", 321 | "nbconvert_exporter": "python", 322 | "pygments_lexer": "ipython3", 323 | "version": "3.9.12" 324 | } 325 | }, 326 | "nbformat": 4, 327 | "nbformat_minor": 5 328 | } 329 | -------------------------------------------------------------------------------- /25_26_27_28_29_Algorithms/.ipynb_checkpoints/25_26_List, Searching, Swapping, Sorting-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "d3f49a33", 6 | "metadata": {}, 7 | "source": [ 8 | "# List\n", 9 | "\n", 10 | "**WAP to print length of the list without using len function**" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 1, 16 | "id": "1885206a", 17 | "metadata": {}, 18 | "outputs": [ 19 | { 20 | "name": "stdout", 21 | "output_type": "stream", 22 | "text": [ 23 | "Length = 6\n" 24 | ] 25 | } 26 | ], 27 | "source": [ 28 | "l = [11,12,22,3,4,16]\n", 29 | "i = 0\n", 30 | "\n", 31 | "while l[i:]:\n", 32 | " i += 1\n", 33 | "\n", 34 | "print(\"Length =\", i)" 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "id": "6a1bf65b", 40 | "metadata": {}, 41 | "source": [ 42 | "**WAP to print sum of all the elements in a list without using sum function**" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 2, 48 | "id": "283c543a", 49 | "metadata": {}, 50 | "outputs": [ 51 | { 52 | "name": "stdout", 53 | "output_type": "stream", 54 | "text": [ 55 | "Sum = 68\n" 56 | ] 57 | } 58 | ], 59 | "source": [ 60 | "l = [11,12,22,3,4,16]\n", 61 | "# i\n", 62 | "i = 0\n", 63 | "s = 0\n", 64 | "\n", 65 | "while l[i:]:\n", 66 | " s = s + l[i] # s = 0, 11, 23\n", 67 | " i += 1\n", 68 | "\n", 69 | "print(\"Sum =\", s)" 70 | ] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "id": "4e1c3016", 75 | "metadata": {}, 76 | "source": [ 77 | "**WAP to print the maximum value from a list without using max function**" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 3, 83 | "id": "dc5b8275", 84 | "metadata": {}, 85 | "outputs": [ 86 | { 87 | "name": "stdout", 88 | "output_type": "stream", 89 | "text": [ 90 | "Maximum = 22\n" 91 | ] 92 | } 93 | ], 94 | "source": [ 95 | "l = [11,12,22,3,4,16]\n", 96 | "# m\n", 97 | "i = 0\n", 98 | "m = l[0]\n", 99 | "\n", 100 | "while l[i:]:\n", 101 | " if m < l[i]:\n", 102 | " m = l[i]\n", 103 | " i += 1\n", 104 | "\n", 105 | "print(\"Maximum =\", m)" 106 | ] 107 | }, 108 | { 109 | "cell_type": "markdown", 110 | "id": "814bbcec", 111 | "metadata": {}, 112 | "source": [ 113 | "**WAP to print the minimum value from a list without using min function**" 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": 4, 119 | "id": "1418ba48", 120 | "metadata": {}, 121 | "outputs": [ 122 | { 123 | "name": "stdout", 124 | "output_type": "stream", 125 | "text": [ 126 | "Minimum = 3\n" 127 | ] 128 | } 129 | ], 130 | "source": [ 131 | "l = [11,12,22,3,4,16]\n", 132 | "# m\n", 133 | "i = 0\n", 134 | "m = l[0]\n", 135 | "\n", 136 | "while l[i:]:\n", 137 | " if m > l[i]:\n", 138 | " m = l[i]\n", 139 | " i += 1\n", 140 | "\n", 141 | "print(\"Minimum =\", m)" 142 | ] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "id": "5edb2e06", 147 | "metadata": {}, 148 | "source": [ 149 | "**WAP to reverse of a list without using reverse method or slicing**" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": 5, 155 | "id": "c3de25d8", 156 | "metadata": {}, 157 | "outputs": [ 158 | { 159 | "name": "stdout", 160 | "output_type": "stream", 161 | "text": [ 162 | "16 4 3 22 12 11 " 163 | ] 164 | } 165 | ], 166 | "source": [ 167 | "l = [11,12,22,3,4,16]\n", 168 | "# i\n", 169 | "i = len(l) - 1\n", 170 | "\n", 171 | "while i >= 0:\n", 172 | " print(l[i], end=\" \")\n", 173 | " i -= 1" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": 8, 179 | "id": "c50738ba", 180 | "metadata": {}, 181 | "outputs": [ 182 | { 183 | "name": "stdout", 184 | "output_type": "stream", 185 | "text": [ 186 | "[11, 12, 22, 3, 4, 16]\n", 187 | "[16, 4, 3, 22, 12, 11]\n" 188 | ] 189 | } 190 | ], 191 | "source": [ 192 | "l = [11,12,22,3,4,16]\n", 193 | "# i\n", 194 | "a = []\n", 195 | "i = len(l) - 1\n", 196 | "\n", 197 | "while i >= 0:\n", 198 | " a.append(l[i])\n", 199 | " i -= 1\n", 200 | "\n", 201 | "print(l)\n", 202 | "print(a)" 203 | ] 204 | }, 205 | { 206 | "cell_type": "markdown", 207 | "id": "9a209407", 208 | "metadata": {}, 209 | "source": [ 210 | "# Searching\n", 211 | "\n", 212 | "Linear Search" 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": 10, 218 | "id": "020f68e4", 219 | "metadata": {}, 220 | "outputs": [ 221 | { 222 | "name": "stdout", 223 | "output_type": "stream", 224 | "text": [ 225 | "Enter an element to search: 71\n", 226 | "Search Success\n" 227 | ] 228 | } 229 | ], 230 | "source": [ 231 | "l = [11,10,130,44,55,61,71,80,90,100]\n", 232 | "# n\n", 233 | "\n", 234 | "i = 0\n", 235 | "n = int(input(\"Enter an element to search: \"))\n", 236 | "\n", 237 | "f = 0 # flagbit variable\n", 238 | "\n", 239 | "while l[i:]:\n", 240 | " if n == l[i]:\n", 241 | " f = 1\n", 242 | " break\n", 243 | " i += 1\n", 244 | "\n", 245 | "if f == 1:\n", 246 | " print(\"Search Success\")\n", 247 | "else:\n", 248 | " print(\"Element Not Found\")" 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": 11, 254 | "id": "ebbbee2c", 255 | "metadata": {}, 256 | "outputs": [ 257 | { 258 | "name": "stdout", 259 | "output_type": "stream", 260 | "text": [ 261 | "Enter an element to search: 71\n", 262 | "Search Success and frequency = 2\n" 263 | ] 264 | } 265 | ], 266 | "source": [ 267 | "l = [11,10,130,44,55,61,71,80,71,100]\n", 268 | "# n\n", 269 | "\n", 270 | "i = 0\n", 271 | "n = int(input(\"Enter an element to search: \"))\n", 272 | "\n", 273 | "f = 0 # flagbit variable\n", 274 | "c = 0\n", 275 | "\n", 276 | "while l[i:]:\n", 277 | " if n == l[i]:\n", 278 | " f = 1\n", 279 | " c += 1\n", 280 | " i += 1\n", 281 | "\n", 282 | "if f == 1:\n", 283 | " print(\"Search Success and frequency =\", c)\n", 284 | "else:\n", 285 | " print(\"Element Not Found\")" 286 | ] 287 | }, 288 | { 289 | "cell_type": "markdown", 290 | "id": "464a20c1", 291 | "metadata": {}, 292 | "source": [ 293 | "# Membership Operator search" 294 | ] 295 | }, 296 | { 297 | "cell_type": "code", 298 | "execution_count": 1, 299 | "id": "d629daba", 300 | "metadata": {}, 301 | "outputs": [ 302 | { 303 | "name": "stdout", 304 | "output_type": "stream", 305 | "text": [ 306 | "True\n" 307 | ] 308 | } 309 | ], 310 | "source": [ 311 | "l = [1,2,3,4,5]\n", 312 | "\n", 313 | "print(3 in l)" 314 | ] 315 | }, 316 | { 317 | "cell_type": "code", 318 | "execution_count": 2, 319 | "id": "e4f4181b", 320 | "metadata": {}, 321 | "outputs": [ 322 | { 323 | "name": "stdout", 324 | "output_type": "stream", 325 | "text": [ 326 | "True\n" 327 | ] 328 | } 329 | ], 330 | "source": [ 331 | "print(7 not in l)" 332 | ] 333 | }, 334 | { 335 | "cell_type": "code", 336 | "execution_count": 3, 337 | "id": "62cc6316", 338 | "metadata": {}, 339 | "outputs": [], 340 | "source": [ 341 | "# linear search\n", 342 | "\n", 343 | "def LinearSearch(l, element):\n", 344 | " for i in range(len(l)):\n", 345 | " if l[i] == element:\n", 346 | " return 1\n", 347 | " return -1" 348 | ] 349 | }, 350 | { 351 | "cell_type": "code", 352 | "execution_count": 6, 353 | "id": "be32968b", 354 | "metadata": {}, 355 | "outputs": [ 356 | { 357 | "name": "stdout", 358 | "output_type": "stream", 359 | "text": [ 360 | "Enter a Number: 100\n", 361 | "Search Fail\n" 362 | ] 363 | } 364 | ], 365 | "source": [ 366 | "n = [11,23,45,3,7,54,99,66]\n", 367 | "\n", 368 | "k = int(input(\"Enter a Number: \"))\n", 369 | "\n", 370 | "x = LinearSearch(n, k)\n", 371 | "\n", 372 | "if x == 1:\n", 373 | " print(\"Search Success\")\n", 374 | "else:\n", 375 | " print(\"Search Fail\")" 376 | ] 377 | }, 378 | { 379 | "cell_type": "markdown", 380 | "id": "60f877f4", 381 | "metadata": {}, 382 | "source": [ 383 | "# Binary Search\n", 384 | "\n", 385 | "- elements should be sorted" 386 | ] 387 | }, 388 | { 389 | "cell_type": "code", 390 | "execution_count": 7, 391 | "id": "32d7757f", 392 | "metadata": {}, 393 | "outputs": [ 394 | { 395 | "name": "stdout", 396 | "output_type": "stream", 397 | "text": [ 398 | "Search Success\n" 399 | ] 400 | } 401 | ], 402 | "source": [ 403 | "# mid \n", 404 | "# high \n", 405 | "# low\n", 406 | "# 0 1 2 3 4 5 6 7 8 9\n", 407 | "n = [10,20,30,40,50,60,70,80,90,100]\n", 408 | "\n", 409 | "low = 0\n", 410 | "high = len(n) - 1\n", 411 | "\n", 412 | "x = 70\n", 413 | "f = 0\n", 414 | "\n", 415 | "while low <= high:\n", 416 | " mid = (low + high)//2 # low = 0, 5, 6\n", 417 | " if n[mid] == x: # high= 9, 6\n", 418 | " f = 1\n", 419 | " break # mid = 4, 7, 5, 6\n", 420 | " elif n[mid] > x: # x = 70\n", 421 | " high = mid - 1 # f = 0\n", 422 | " else:\n", 423 | " low = mid + 1\n", 424 | " \n", 425 | "if f == 1:\n", 426 | " print(\"Search Success\")\n", 427 | "else:\n", 428 | " print(\"Search Fail\")" 429 | ] 430 | }, 431 | { 432 | "cell_type": "markdown", 433 | "id": "eb49059d", 434 | "metadata": {}, 435 | "source": [ 436 | "# Swapping" 437 | ] 438 | }, 439 | { 440 | "cell_type": "code", 441 | "execution_count": 8, 442 | "id": "556c8dd2", 443 | "metadata": {}, 444 | "outputs": [ 445 | { 446 | "name": "stdout", 447 | "output_type": "stream", 448 | "text": [ 449 | "20\n", 450 | "10\n" 451 | ] 452 | } 453 | ], 454 | "source": [ 455 | "a = 10\n", 456 | "b = 20\n", 457 | "\n", 458 | "c = a # a = 10, 20\n", 459 | "a = b # b = 20, 10\n", 460 | "b = c # c = 10\n", 461 | "\n", 462 | "print(a)\n", 463 | "print(b)" 464 | ] 465 | }, 466 | { 467 | "cell_type": "markdown", 468 | "id": "e0a520ff", 469 | "metadata": {}, 470 | "source": [ 471 | "### swapping without using 3rd variable" 472 | ] 473 | }, 474 | { 475 | "cell_type": "code", 476 | "execution_count": 9, 477 | "id": "8e69f80b", 478 | "metadata": {}, 479 | "outputs": [ 480 | { 481 | "name": "stdout", 482 | "output_type": "stream", 483 | "text": [ 484 | "20\n", 485 | "10\n" 486 | ] 487 | } 488 | ], 489 | "source": [ 490 | "a = 10 # 30, 20\n", 491 | "b = 20 # 10\n", 492 | "\n", 493 | "a = a + b\n", 494 | "b = a - b\n", 495 | "a = a - b\n", 496 | "\n", 497 | "print(a)\n", 498 | "print(b)" 499 | ] 500 | }, 501 | { 502 | "cell_type": "code", 503 | "execution_count": 10, 504 | "id": "b08380bc", 505 | "metadata": {}, 506 | "outputs": [ 507 | { 508 | "name": "stdout", 509 | "output_type": "stream", 510 | "text": [ 511 | "20\n", 512 | "10\n" 513 | ] 514 | } 515 | ], 516 | "source": [ 517 | "a = 10\n", 518 | "b = 20\n", 519 | "\n", 520 | "a,b = b,a\n", 521 | "# (20,10)\n", 522 | "\n", 523 | "print(a)\n", 524 | "print(b)" 525 | ] 526 | }, 527 | { 528 | "cell_type": "markdown", 529 | "id": "00de74db", 530 | "metadata": {}, 531 | "source": [ 532 | "# Sorting\n", 533 | "\n", 534 | "\n", 535 | "### bubble sort" 536 | ] 537 | }, 538 | { 539 | "cell_type": "code", 540 | "execution_count": null, 541 | "id": "de4b27cd", 542 | "metadata": {}, 543 | "outputs": [], 544 | "source": [ 545 | "[10,20,30,40,50]\n", 546 | " i j\n" 547 | ] 548 | }, 549 | { 550 | "cell_type": "code", 551 | "execution_count": 11, 552 | "id": "f968c291", 553 | "metadata": {}, 554 | "outputs": [ 555 | { 556 | "name": "stdout", 557 | "output_type": "stream", 558 | "text": [ 559 | "[1, 2, 3, 4, 12, 17, 43, 47, 54, 66]\n" 560 | ] 561 | } 562 | ], 563 | "source": [ 564 | "n = [12,43,54,66,47,17,3,2,4,1]\n", 565 | "# i j\n", 566 | "\n", 567 | "for i in range(len(n)-1):\n", 568 | " for j in range(i + 1,len(n)):\n", 569 | " if n[i] > n[j]:\n", 570 | " n[i],n[j] = n[j],n[i]\n", 571 | "\n", 572 | "print(n)" 573 | ] 574 | }, 575 | { 576 | "cell_type": "code", 577 | "execution_count": null, 578 | "id": "b155f204", 579 | "metadata": {}, 580 | "outputs": [], 581 | "source": [] 582 | } 583 | ], 584 | "metadata": { 585 | "kernelspec": { 586 | "display_name": "Python 3 (ipykernel)", 587 | "language": "python", 588 | "name": "python3" 589 | }, 590 | "language_info": { 591 | "codemirror_mode": { 592 | "name": "ipython", 593 | "version": 3 594 | }, 595 | "file_extension": ".py", 596 | "mimetype": "text/x-python", 597 | "name": "python", 598 | "nbconvert_exporter": "python", 599 | "pygments_lexer": "ipython3", 600 | "version": "3.9.12" 601 | } 602 | }, 603 | "nbformat": 4, 604 | "nbformat_minor": 5 605 | } 606 | -------------------------------------------------------------------------------- /25_26_27_28_29_Algorithms/.ipynb_checkpoints/27_Sorting Algorithms-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "ef79ea78", 6 | "metadata": {}, 7 | "source": [ 8 | "# Selection Sort" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": null, 14 | "id": "2ac82a20", 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "[4,3,5,6,2]\n", 19 | "[3,4,5,6,2]\n", 20 | "[2,4,5,6,3]" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 1, 26 | "id": "d8b66137", 27 | "metadata": {}, 28 | "outputs": [ 29 | { 30 | "name": "stdout", 31 | "output_type": "stream", 32 | "text": [ 33 | "[10, 20, 30, 40, 50]\n" 34 | ] 35 | } 36 | ], 37 | "source": [ 38 | "n = [10,50,40,20,30]\n", 39 | "# i j\n", 40 | "# m\n", 41 | "for i in range(len(n)-1):\n", 42 | " m = n[i] # m = 40, 10\n", 43 | " loc = i # loc = 0, 2\n", 44 | " for j in range(i+1, len(n)): # i = 0\n", 45 | " if m > n[j]: # j = 1, 2, 3, 4\n", 46 | " m = n[j]\n", 47 | " loc = j\n", 48 | " if i!=loc:\n", 49 | " temp = n[i]\n", 50 | " n[i] = n[loc]\n", 51 | " n[loc] = temp\n", 52 | " \n", 53 | "print(n)" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 2, 59 | "id": "1282c3c5", 60 | "metadata": {}, 61 | "outputs": [ 62 | { 63 | "name": "stdout", 64 | "output_type": "stream", 65 | "text": [ 66 | "20\n", 67 | "10\n" 68 | ] 69 | } 70 | ], 71 | "source": [ 72 | "a = 10\n", 73 | "b = 20\n", 74 | "\n", 75 | "c = a\n", 76 | "a = b\n", 77 | "b = c\n", 78 | "\n", 79 | "print(a)\n", 80 | "print(b)" 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "id": "b5f379bd", 86 | "metadata": {}, 87 | "source": [ 88 | "# Merge Sort" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 4, 94 | "id": "ed6e1d50", 95 | "metadata": {}, 96 | "outputs": [ 97 | { 98 | "name": "stdout", 99 | "output_type": "stream", 100 | "text": [ 101 | "[10, 20, 30, 40, 50]\n", 102 | "[5, 15, 19, 25, 41]\n", 103 | "[5, 10, 15, 19, 20, 25, 30, 40, 41, 50]\n" 104 | ] 105 | } 106 | ], 107 | "source": [ 108 | "n = [10,20,30,40,50]\n", 109 | "m = [5,15,19,25,41]\n", 110 | "\n", 111 | "l = []\n", 112 | "\n", 113 | "i = 0\n", 114 | "j = 0\n", 115 | "\n", 116 | "while i < 5 and j < 5:\n", 117 | " if n[i] <= m[j]:\n", 118 | " l.append(n[i])\n", 119 | " i += 1\n", 120 | " else:\n", 121 | " l.append(m[j])\n", 122 | " j += 1\n", 123 | "\n", 124 | "while i < 5:\n", 125 | " l.append(n[i])\n", 126 | " i += 1\n", 127 | "\n", 128 | "while j < 5:\n", 129 | " l.append(m[j])\n", 130 | " j += 1\n", 131 | "\n", 132 | "print(n)\n", 133 | "print(m)\n", 134 | "print(l)" 135 | ] 136 | }, 137 | { 138 | "cell_type": "markdown", 139 | "id": "02538358", 140 | "metadata": {}, 141 | "source": [ 142 | "# Insertion Sort" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 5, 148 | "id": "53562663", 149 | "metadata": {}, 150 | "outputs": [ 151 | { 152 | "name": "stdout", 153 | "output_type": "stream", 154 | "text": [ 155 | "[10, 20, 30, 40, 50]\n" 156 | ] 157 | } 158 | ], 159 | "source": [ 160 | "a = [10,40,50,20,30]\n", 161 | "\n", 162 | "for i in range(1,len(a)):\n", 163 | " temp = a[i]\n", 164 | " j = i - 1\n", 165 | " while temp < a[j] and j >= 0: # temp = 40, 10\n", 166 | " a[j+1] = a[j] # i = 1\n", 167 | " j = j-1 # j = 0, 1, 0\n", 168 | " a[j+1] = temp\n", 169 | "\n", 170 | "print(a)" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": null, 176 | "id": "24c7d035", 177 | "metadata": {}, 178 | "outputs": [], 179 | "source": [] 180 | } 181 | ], 182 | "metadata": { 183 | "kernelspec": { 184 | "display_name": "Python 3 (ipykernel)", 185 | "language": "python", 186 | "name": "python3" 187 | }, 188 | "language_info": { 189 | "codemirror_mode": { 190 | "name": "ipython", 191 | "version": 3 192 | }, 193 | "file_extension": ".py", 194 | "mimetype": "text/x-python", 195 | "name": "python", 196 | "nbconvert_exporter": "python", 197 | "pygments_lexer": "ipython3", 198 | "version": "3.9.12" 199 | } 200 | }, 201 | "nbformat": 4, 202 | "nbformat_minor": 5 203 | } 204 | -------------------------------------------------------------------------------- /25_26_27_28_29_Algorithms/.ipynb_checkpoints/28_29_Recursion-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "d716f934", 6 | "metadata": {}, 7 | "source": [ 8 | "# Recursion\n", 9 | "\n", 10 | "when a function call itself" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": null, 16 | "id": "10f68cd1", 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "def mini():\n", 21 | " \n", 22 | " mini()" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 5, 28 | "id": "d808a722", 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "name": "stdout", 33 | "output_type": "stream", 34 | "text": [ 35 | "Enter a number5\n", 36 | "Factorial = 120\n" 37 | ] 38 | } 39 | ], 40 | "source": [ 41 | "# enter a number from user and print factorial of it\n", 42 | "# 5 -> 5*4*3*2*1 -> 120\n", 43 | "# 1 -> 1\n", 44 | "# 0 -> 1\n", 45 | "\n", 46 | "n = int(input(\"Enter a number\"))\n", 47 | "f = 1\n", 48 | "\n", 49 | "while n > 1:\n", 50 | " f = f * n # f = 1, 5, 20, 60, 120\n", 51 | " n -= 1 # n = 5, 4, 3, 2, 1\n", 52 | "\n", 53 | "print(\"Factorial =\", f)" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 7, 59 | "id": "aa20a2cc", 60 | "metadata": {}, 61 | "outputs": [ 62 | { 63 | "name": "stdout", 64 | "output_type": "stream", 65 | "text": [ 66 | "Enter a numner: 5\n", 67 | "120\n" 68 | ] 69 | } 70 | ], 71 | "source": [ 72 | "# using recursion\n", 73 | "\n", 74 | "def fact(n):\n", 75 | " if n == 1:\n", 76 | " return 1 # base case\n", 77 | " else:\n", 78 | " return n * fact(n-1)\n", 79 | " # 3 * 2\n", 80 | " # \n", 81 | "\n", 82 | "a = int(input(\"Enter a numner: \"))\n", 83 | "\n", 84 | "print(fact(a))" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": null, 90 | "id": "08434110", 91 | "metadata": {}, 92 | "outputs": [], 93 | "source": [ 94 | "\n", 95 | "\n", 96 | "\n", 97 | "\n", 98 | "\n", 99 | "main()" 100 | ] 101 | }, 102 | { 103 | "cell_type": "markdown", 104 | "id": "027cd2f2", 105 | "metadata": {}, 106 | "source": [ 107 | "**Wrtie a program to print sum of all the values in a list using recursion**" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": null, 113 | "id": "354e7a2f", 114 | "metadata": {}, 115 | "outputs": [], 116 | "source": [ 117 | "# l = [10,20,30,40]" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 8, 123 | "id": "c354674a", 124 | "metadata": {}, 125 | "outputs": [ 126 | { 127 | "name": "stdout", 128 | "output_type": "stream", 129 | "text": [ 130 | "150\n" 131 | ] 132 | } 133 | ], 134 | "source": [ 135 | "def summ(l):\n", 136 | " if len(l) == 1:\n", 137 | " return l[0]\n", 138 | " else:\n", 139 | " return l[0] + summ(l[1:])\n", 140 | " # 10 + 140\n", 141 | " # \n", 142 | " # \n", 143 | " # \n", 144 | "\n", 145 | "print(summ([10,20,30,40,50]))\n", 146 | "# 150" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": null, 152 | "id": "710d894c", 153 | "metadata": {}, 154 | "outputs": [], 155 | "source": [ 156 | "\n", 157 | "\n", 158 | "main()" 159 | ] 160 | }, 161 | { 162 | "cell_type": "markdown", 163 | "id": "e8b0a0ee", 164 | "metadata": {}, 165 | "source": [ 166 | "**Fibonacci series**" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": null, 172 | "id": "864b9c61", 173 | "metadata": {}, 174 | "outputs": [], 175 | "source": [ 176 | "0 1 1 2 3 5 8 13\n", 177 | "a b c" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": 13, 183 | "id": "14de5652", 184 | "metadata": {}, 185 | "outputs": [ 186 | { 187 | "name": "stdout", 188 | "output_type": "stream", 189 | "text": [ 190 | "enter limit0\n", 191 | "0 1 " 192 | ] 193 | } 194 | ], 195 | "source": [ 196 | "n = int(input(\"enter limit\"))\n", 197 | "\n", 198 | "a = 0\n", 199 | "b = 1\n", 200 | "i = 1\n", 201 | "\n", 202 | "print(a, end = \" \")\n", 203 | "print(b, end = \" \")\n", 204 | "\n", 205 | "while i <= n - 2: # n = 7\n", 206 | " c = a + b # a = 0, 1, 1, 2, 3, 5\n", 207 | " print(c, end = \" \") # b = 1, 1, 2, 3, 5, 8\n", 208 | " a = b # c = 1, 2, 3, 5, 8\n", 209 | " b = c # i = 1, 2, 3, 4, 5, 6\n", 210 | " i += 1" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": null, 216 | "id": "81ac3b93", 217 | "metadata": {}, 218 | "outputs": [], 219 | "source": [ 220 | "0 1 1 2 3 5 8\n", 221 | "a b c\n", 222 | " a b c" 223 | ] 224 | }, 225 | { 226 | "cell_type": "code", 227 | "execution_count": 16, 228 | "id": "765c7f57", 229 | "metadata": {}, 230 | "outputs": [ 231 | { 232 | "name": "stdout", 233 | "output_type": "stream", 234 | "text": [ 235 | "enter limit10\n", 236 | "0 1 1 2 3 5 8 13 21 34 " 237 | ] 238 | } 239 | ], 240 | "source": [ 241 | "n = int(input(\"enter limit\"))\n", 242 | "\n", 243 | "a = 0\n", 244 | "b = 1\n", 245 | "i = 1\n", 246 | "\n", 247 | "while i <= n: # n = 7\n", 248 | " print(a, end = \" \") # a = 0, 1, 1, 2, 3, 5\n", 249 | " # b = 1, 1, 2, 3, 5, 8\n", 250 | " c = a + b\n", 251 | " a = b # c = 1, 2, 3, 5, 8\n", 252 | " b = c # i = 1, 2, 3, 4, 5, 6\n", 253 | " i += 1" 254 | ] 255 | }, 256 | { 257 | "cell_type": "markdown", 258 | "id": "197c0467", 259 | "metadata": {}, 260 | "source": [ 261 | "# Day 29\n", 262 | "\n", 263 | "**print fibonacci series using recursion**" 264 | ] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "execution_count": 2, 269 | "id": "bf7f8e95", 270 | "metadata": {}, 271 | "outputs": [], 272 | "source": [ 273 | "def fibonacii(n):\n", 274 | " if n == 1 or n == 2:\n", 275 | " return 1\n", 276 | " else:\n", 277 | " return fibonacii(n - 1) + fibonacii(n - 2)\n", 278 | " # fibonacii(6) + fibonacii(5)\n", 279 | " # f(5) + f(4)\n", 280 | " # f(4) + f(3)\n", 281 | " #f(3) + f(2)\n", 282 | " #f(2) + f(1)" 283 | ] 284 | }, 285 | { 286 | "cell_type": "code", 287 | "execution_count": 5, 288 | "id": "af0f28b3", 289 | "metadata": {}, 290 | "outputs": [ 291 | { 292 | "name": "stdout", 293 | "output_type": "stream", 294 | "text": [ 295 | "34\n" 296 | ] 297 | } 298 | ], 299 | "source": [ 300 | "print(fibonacii(9))" 301 | ] 302 | }, 303 | { 304 | "cell_type": "code", 305 | "execution_count": 6, 306 | "id": "4b156cd8", 307 | "metadata": {}, 308 | "outputs": [ 309 | { 310 | "name": "stdout", 311 | "output_type": "stream", 312 | "text": [ 313 | "1 1 2 3 5 8 13 21 34 " 314 | ] 315 | } 316 | ], 317 | "source": [ 318 | "for i in range(1, 10):\n", 319 | " print(fibonacii(i), end = \" \")" 320 | ] 321 | }, 322 | { 323 | "cell_type": "markdown", 324 | "id": "0c69e16b", 325 | "metadata": {}, 326 | "source": [ 327 | "**Write a program to calculate the value of a to the power of b**\n", 328 | "\n", 329 | "2 ** 3 -> 8" 330 | ] 331 | }, 332 | { 333 | "cell_type": "code", 334 | "execution_count": 7, 335 | "id": "fba18126", 336 | "metadata": {}, 337 | "outputs": [], 338 | "source": [ 339 | "def power(a,b):\n", 340 | " if b == 0:\n", 341 | " return 1\n", 342 | " elif a == 0:\n", 343 | " return 0\n", 344 | " elif b == 1:\n", 345 | " return a\n", 346 | " else:\n", 347 | " return a * power(a, b - 1)\n", 348 | " # 2 * power(2,2)\n", 349 | " # 2 * 2" 350 | ] 351 | }, 352 | { 353 | "cell_type": "code", 354 | "execution_count": 8, 355 | "id": "b5461c34", 356 | "metadata": {}, 357 | "outputs": [ 358 | { 359 | "name": "stdout", 360 | "output_type": "stream", 361 | "text": [ 362 | "8\n" 363 | ] 364 | } 365 | ], 366 | "source": [ 367 | "print(power(2,3))" 368 | ] 369 | }, 370 | { 371 | "cell_type": "markdown", 372 | "id": "c44ad9cc", 373 | "metadata": {}, 374 | "source": [ 375 | "# Interview example questions\n", 376 | "\n", 377 | "**return the multiplication of 2 numbers without using multiply sign**" 378 | ] 379 | }, 380 | { 381 | "cell_type": "code", 382 | "execution_count": 9, 383 | "id": "40d6bda8", 384 | "metadata": {}, 385 | "outputs": [], 386 | "source": [ 387 | "def multiply(a, b):\n", 388 | " s = 0\n", 389 | " while b > 0:\n", 390 | " s = s + a\n", 391 | " b = b - 1\n", 392 | " return s" 393 | ] 394 | }, 395 | { 396 | "cell_type": "code", 397 | "execution_count": 10, 398 | "id": "ae3a1aaa", 399 | "metadata": {}, 400 | "outputs": [ 401 | { 402 | "data": { 403 | "text/plain": [ 404 | "20" 405 | ] 406 | }, 407 | "execution_count": 10, 408 | "metadata": {}, 409 | "output_type": "execute_result" 410 | } 411 | ], 412 | "source": [ 413 | "multiply(5,4)" 414 | ] 415 | }, 416 | { 417 | "cell_type": "code", 418 | "execution_count": 11, 419 | "id": "4cfd85a3", 420 | "metadata": {}, 421 | "outputs": [ 422 | { 423 | "data": { 424 | "text/plain": [ 425 | "20" 426 | ] 427 | }, 428 | "execution_count": 11, 429 | "metadata": {}, 430 | "output_type": "execute_result" 431 | } 432 | ], 433 | "source": [ 434 | "# recursion\n", 435 | "\n", 436 | "def multiplyR(a,b):\n", 437 | " if b == 1:\n", 438 | " return a\n", 439 | " else:\n", 440 | " return a + multiplyR(a, b - 1)\n", 441 | "\n", 442 | "multiplyR(5,4)" 443 | ] 444 | }, 445 | { 446 | "cell_type": "markdown", 447 | "id": "260c08a9", 448 | "metadata": {}, 449 | "source": [ 450 | "# dynamic programming" 451 | ] 452 | }, 453 | { 454 | "cell_type": "code", 455 | "execution_count": 13, 456 | "id": "af569c43", 457 | "metadata": {}, 458 | "outputs": [], 459 | "source": [ 460 | "# it is solving overlapping problems\n", 461 | "\n", 462 | "# cache the results" 463 | ] 464 | }, 465 | { 466 | "cell_type": "code", 467 | "execution_count": 14, 468 | "id": "3d125d38", 469 | "metadata": {}, 470 | "outputs": [], 471 | "source": [ 472 | "def fibonacii(n):\n", 473 | " if n == 1 or n == 2:\n", 474 | " return 1\n", 475 | " else:\n", 476 | " return fibonacii(n - 1) + fibonacii(n - 2)" 477 | ] 478 | }, 479 | { 480 | "cell_type": "code", 481 | "execution_count": 15, 482 | "id": "db1bd855", 483 | "metadata": {}, 484 | "outputs": [], 485 | "source": [ 486 | "# dynamic programming" 487 | ] 488 | }, 489 | { 490 | "cell_type": "code", 491 | "execution_count": 16, 492 | "id": "82acfebf", 493 | "metadata": {}, 494 | "outputs": [], 495 | "source": [ 496 | "def fibonaciiDP(n, tracker = {}):\n", 497 | " if n == 1 or n == 0:\n", 498 | " return n\n", 499 | " \n", 500 | " if n > 1:\n", 501 | " if n in tracker:\n", 502 | " return tracker[n]\n", 503 | " else:\n", 504 | " result = fibonaciiDP(n - 1, tracker) + fibonaciiDP(n - 2, tracker)\n", 505 | " tracker[n] = result\n", 506 | " return result" 507 | ] 508 | }, 509 | { 510 | "cell_type": "code", 511 | "execution_count": 20, 512 | "id": "2fff4937", 513 | "metadata": {}, 514 | "outputs": [ 515 | { 516 | "name": "stdout", 517 | "output_type": "stream", 518 | "text": [ 519 | "13\n" 520 | ] 521 | } 522 | ], 523 | "source": [ 524 | "print(fibonaciiDP(7))" 525 | ] 526 | }, 527 | { 528 | "cell_type": "code", 529 | "execution_count": null, 530 | "id": "a5754169", 531 | "metadata": {}, 532 | "outputs": [], 533 | "source": [] 534 | } 535 | ], 536 | "metadata": { 537 | "kernelspec": { 538 | "display_name": "Python 3 (ipykernel)", 539 | "language": "python", 540 | "name": "python3" 541 | }, 542 | "language_info": { 543 | "codemirror_mode": { 544 | "name": "ipython", 545 | "version": 3 546 | }, 547 | "file_extension": ".py", 548 | "mimetype": "text/x-python", 549 | "name": "python", 550 | "nbconvert_exporter": "python", 551 | "pygments_lexer": "ipython3", 552 | "version": "3.9.12" 553 | } 554 | }, 555 | "nbformat": 4, 556 | "nbformat_minor": 5 557 | } 558 | -------------------------------------------------------------------------------- /25_26_27_28_29_Algorithms/25_26_List, Searching, Swapping, Sorting.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "d3f49a33", 6 | "metadata": {}, 7 | "source": [ 8 | "# List\n", 9 | "\n", 10 | "**WAP to print length of the list without using len function**" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 1, 16 | "id": "1885206a", 17 | "metadata": {}, 18 | "outputs": [ 19 | { 20 | "name": "stdout", 21 | "output_type": "stream", 22 | "text": [ 23 | "Length = 6\n" 24 | ] 25 | } 26 | ], 27 | "source": [ 28 | "l = [11,12,22,3,4,16]\n", 29 | "i = 0\n", 30 | "\n", 31 | "while l[i:]:\n", 32 | " i += 1\n", 33 | "\n", 34 | "print(\"Length =\", i)" 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "id": "6a1bf65b", 40 | "metadata": {}, 41 | "source": [ 42 | "**WAP to print sum of all the elements in a list without using sum function**" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 2, 48 | "id": "283c543a", 49 | "metadata": {}, 50 | "outputs": [ 51 | { 52 | "name": "stdout", 53 | "output_type": "stream", 54 | "text": [ 55 | "Sum = 68\n" 56 | ] 57 | } 58 | ], 59 | "source": [ 60 | "l = [11,12,22,3,4,16]\n", 61 | "# i\n", 62 | "i = 0\n", 63 | "s = 0\n", 64 | "\n", 65 | "while l[i:]:\n", 66 | " s = s + l[i] # s = 0, 11, 23\n", 67 | " i += 1\n", 68 | "\n", 69 | "print(\"Sum =\", s)" 70 | ] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "id": "4e1c3016", 75 | "metadata": {}, 76 | "source": [ 77 | "**WAP to print the maximum value from a list without using max function**" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 3, 83 | "id": "dc5b8275", 84 | "metadata": {}, 85 | "outputs": [ 86 | { 87 | "name": "stdout", 88 | "output_type": "stream", 89 | "text": [ 90 | "Maximum = 22\n" 91 | ] 92 | } 93 | ], 94 | "source": [ 95 | "l = [11,12,22,3,4,16]\n", 96 | "# m\n", 97 | "i = 0\n", 98 | "m = l[0]\n", 99 | "\n", 100 | "while l[i:]:\n", 101 | " if m < l[i]:\n", 102 | " m = l[i]\n", 103 | " i += 1\n", 104 | "\n", 105 | "print(\"Maximum =\", m)" 106 | ] 107 | }, 108 | { 109 | "cell_type": "markdown", 110 | "id": "814bbcec", 111 | "metadata": {}, 112 | "source": [ 113 | "**WAP to print the minimum value from a list without using min function**" 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": 4, 119 | "id": "1418ba48", 120 | "metadata": {}, 121 | "outputs": [ 122 | { 123 | "name": "stdout", 124 | "output_type": "stream", 125 | "text": [ 126 | "Minimum = 3\n" 127 | ] 128 | } 129 | ], 130 | "source": [ 131 | "l = [11,12,22,3,4,16]\n", 132 | "# m\n", 133 | "i = 0\n", 134 | "m = l[0]\n", 135 | "\n", 136 | "while l[i:]:\n", 137 | " if m > l[i]:\n", 138 | " m = l[i]\n", 139 | " i += 1\n", 140 | "\n", 141 | "print(\"Minimum =\", m)" 142 | ] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "id": "5edb2e06", 147 | "metadata": {}, 148 | "source": [ 149 | "**WAP to reverse of a list without using reverse method or slicing**" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": 5, 155 | "id": "c3de25d8", 156 | "metadata": {}, 157 | "outputs": [ 158 | { 159 | "name": "stdout", 160 | "output_type": "stream", 161 | "text": [ 162 | "16 4 3 22 12 11 " 163 | ] 164 | } 165 | ], 166 | "source": [ 167 | "l = [11,12,22,3,4,16]\n", 168 | "# i\n", 169 | "i = len(l) - 1\n", 170 | "\n", 171 | "while i >= 0:\n", 172 | " print(l[i], end=\" \")\n", 173 | " i -= 1" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": 8, 179 | "id": "c50738ba", 180 | "metadata": {}, 181 | "outputs": [ 182 | { 183 | "name": "stdout", 184 | "output_type": "stream", 185 | "text": [ 186 | "[11, 12, 22, 3, 4, 16]\n", 187 | "[16, 4, 3, 22, 12, 11]\n" 188 | ] 189 | } 190 | ], 191 | "source": [ 192 | "l = [11,12,22,3,4,16]\n", 193 | "# i\n", 194 | "a = []\n", 195 | "i = len(l) - 1\n", 196 | "\n", 197 | "while i >= 0:\n", 198 | " a.append(l[i])\n", 199 | " i -= 1\n", 200 | "\n", 201 | "print(l)\n", 202 | "print(a)" 203 | ] 204 | }, 205 | { 206 | "cell_type": "markdown", 207 | "id": "9a209407", 208 | "metadata": {}, 209 | "source": [ 210 | "# Searching\n", 211 | "\n", 212 | "Linear Search" 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": 10, 218 | "id": "020f68e4", 219 | "metadata": {}, 220 | "outputs": [ 221 | { 222 | "name": "stdout", 223 | "output_type": "stream", 224 | "text": [ 225 | "Enter an element to search: 71\n", 226 | "Search Success\n" 227 | ] 228 | } 229 | ], 230 | "source": [ 231 | "l = [11,10,130,44,55,61,71,80,90,100]\n", 232 | "# n\n", 233 | "\n", 234 | "i = 0\n", 235 | "n = int(input(\"Enter an element to search: \"))\n", 236 | "\n", 237 | "f = 0 # flagbit variable\n", 238 | "\n", 239 | "while l[i:]:\n", 240 | " if n == l[i]:\n", 241 | " f = 1\n", 242 | " break\n", 243 | " i += 1\n", 244 | "\n", 245 | "if f == 1:\n", 246 | " print(\"Search Success\")\n", 247 | "else:\n", 248 | " print(\"Element Not Found\")" 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": 11, 254 | "id": "ebbbee2c", 255 | "metadata": {}, 256 | "outputs": [ 257 | { 258 | "name": "stdout", 259 | "output_type": "stream", 260 | "text": [ 261 | "Enter an element to search: 71\n", 262 | "Search Success and frequency = 2\n" 263 | ] 264 | } 265 | ], 266 | "source": [ 267 | "l = [11,10,130,44,55,61,71,80,71,100]\n", 268 | "# n\n", 269 | "\n", 270 | "i = 0\n", 271 | "n = int(input(\"Enter an element to search: \"))\n", 272 | "\n", 273 | "f = 0 # flagbit variable\n", 274 | "c = 0\n", 275 | "\n", 276 | "while l[i:]:\n", 277 | " if n == l[i]:\n", 278 | " f = 1\n", 279 | " c += 1\n", 280 | " i += 1\n", 281 | "\n", 282 | "if f == 1:\n", 283 | " print(\"Search Success and frequency =\", c)\n", 284 | "else:\n", 285 | " print(\"Element Not Found\")" 286 | ] 287 | }, 288 | { 289 | "cell_type": "markdown", 290 | "id": "464a20c1", 291 | "metadata": {}, 292 | "source": [ 293 | "# Membership Operator search" 294 | ] 295 | }, 296 | { 297 | "cell_type": "code", 298 | "execution_count": 1, 299 | "id": "d629daba", 300 | "metadata": {}, 301 | "outputs": [ 302 | { 303 | "name": "stdout", 304 | "output_type": "stream", 305 | "text": [ 306 | "True\n" 307 | ] 308 | } 309 | ], 310 | "source": [ 311 | "l = [1,2,3,4,5]\n", 312 | "\n", 313 | "print(3 in l)" 314 | ] 315 | }, 316 | { 317 | "cell_type": "code", 318 | "execution_count": 2, 319 | "id": "e4f4181b", 320 | "metadata": {}, 321 | "outputs": [ 322 | { 323 | "name": "stdout", 324 | "output_type": "stream", 325 | "text": [ 326 | "True\n" 327 | ] 328 | } 329 | ], 330 | "source": [ 331 | "print(7 not in l)" 332 | ] 333 | }, 334 | { 335 | "cell_type": "code", 336 | "execution_count": 3, 337 | "id": "62cc6316", 338 | "metadata": {}, 339 | "outputs": [], 340 | "source": [ 341 | "# linear search\n", 342 | "\n", 343 | "def LinearSearch(l, element):\n", 344 | " for i in range(len(l)):\n", 345 | " if l[i] == element:\n", 346 | " return 1\n", 347 | " return -1" 348 | ] 349 | }, 350 | { 351 | "cell_type": "code", 352 | "execution_count": 6, 353 | "id": "be32968b", 354 | "metadata": {}, 355 | "outputs": [ 356 | { 357 | "name": "stdout", 358 | "output_type": "stream", 359 | "text": [ 360 | "Enter a Number: 100\n", 361 | "Search Fail\n" 362 | ] 363 | } 364 | ], 365 | "source": [ 366 | "n = [11,23,45,3,7,54,99,66]\n", 367 | "\n", 368 | "k = int(input(\"Enter a Number: \"))\n", 369 | "\n", 370 | "x = LinearSearch(n, k)\n", 371 | "\n", 372 | "if x == 1:\n", 373 | " print(\"Search Success\")\n", 374 | "else:\n", 375 | " print(\"Search Fail\")" 376 | ] 377 | }, 378 | { 379 | "cell_type": "markdown", 380 | "id": "60f877f4", 381 | "metadata": {}, 382 | "source": [ 383 | "# Binary Search\n", 384 | "\n", 385 | "- elements should be sorted" 386 | ] 387 | }, 388 | { 389 | "cell_type": "code", 390 | "execution_count": 7, 391 | "id": "32d7757f", 392 | "metadata": {}, 393 | "outputs": [ 394 | { 395 | "name": "stdout", 396 | "output_type": "stream", 397 | "text": [ 398 | "Search Success\n" 399 | ] 400 | } 401 | ], 402 | "source": [ 403 | "# mid \n", 404 | "# high \n", 405 | "# low\n", 406 | "# 0 1 2 3 4 5 6 7 8 9\n", 407 | "n = [10,20,30,40,50,60,70,80,90,100]\n", 408 | "\n", 409 | "low = 0\n", 410 | "high = len(n) - 1\n", 411 | "\n", 412 | "x = 70\n", 413 | "f = 0\n", 414 | "\n", 415 | "while low <= high:\n", 416 | " mid = (low + high)//2 # low = 0, 5, 6\n", 417 | " if n[mid] == x: # high= 9, 6\n", 418 | " f = 1\n", 419 | " break # mid = 4, 7, 5, 6\n", 420 | " elif n[mid] > x: # x = 70\n", 421 | " high = mid - 1 # f = 0\n", 422 | " else:\n", 423 | " low = mid + 1\n", 424 | " \n", 425 | "if f == 1:\n", 426 | " print(\"Search Success\")\n", 427 | "else:\n", 428 | " print(\"Search Fail\")" 429 | ] 430 | }, 431 | { 432 | "cell_type": "markdown", 433 | "id": "eb49059d", 434 | "metadata": {}, 435 | "source": [ 436 | "# Swapping" 437 | ] 438 | }, 439 | { 440 | "cell_type": "code", 441 | "execution_count": 8, 442 | "id": "556c8dd2", 443 | "metadata": {}, 444 | "outputs": [ 445 | { 446 | "name": "stdout", 447 | "output_type": "stream", 448 | "text": [ 449 | "20\n", 450 | "10\n" 451 | ] 452 | } 453 | ], 454 | "source": [ 455 | "a = 10\n", 456 | "b = 20\n", 457 | "\n", 458 | "c = a # a = 10, 20\n", 459 | "a = b # b = 20, 10\n", 460 | "b = c # c = 10\n", 461 | "\n", 462 | "print(a)\n", 463 | "print(b)" 464 | ] 465 | }, 466 | { 467 | "cell_type": "markdown", 468 | "id": "e0a520ff", 469 | "metadata": {}, 470 | "source": [ 471 | "### swapping without using 3rd variable" 472 | ] 473 | }, 474 | { 475 | "cell_type": "code", 476 | "execution_count": 9, 477 | "id": "8e69f80b", 478 | "metadata": {}, 479 | "outputs": [ 480 | { 481 | "name": "stdout", 482 | "output_type": "stream", 483 | "text": [ 484 | "20\n", 485 | "10\n" 486 | ] 487 | } 488 | ], 489 | "source": [ 490 | "a = 10 # 30, 20\n", 491 | "b = 20 # 10\n", 492 | "\n", 493 | "a = a + b\n", 494 | "b = a - b\n", 495 | "a = a - b\n", 496 | "\n", 497 | "print(a)\n", 498 | "print(b)" 499 | ] 500 | }, 501 | { 502 | "cell_type": "code", 503 | "execution_count": 10, 504 | "id": "b08380bc", 505 | "metadata": {}, 506 | "outputs": [ 507 | { 508 | "name": "stdout", 509 | "output_type": "stream", 510 | "text": [ 511 | "20\n", 512 | "10\n" 513 | ] 514 | } 515 | ], 516 | "source": [ 517 | "a = 10\n", 518 | "b = 20\n", 519 | "\n", 520 | "a,b = b,a\n", 521 | "# (20,10)\n", 522 | "\n", 523 | "print(a)\n", 524 | "print(b)" 525 | ] 526 | }, 527 | { 528 | "cell_type": "markdown", 529 | "id": "00de74db", 530 | "metadata": {}, 531 | "source": [ 532 | "# Sorting\n", 533 | "\n", 534 | "\n", 535 | "### bubble sort" 536 | ] 537 | }, 538 | { 539 | "cell_type": "code", 540 | "execution_count": null, 541 | "id": "de4b27cd", 542 | "metadata": {}, 543 | "outputs": [], 544 | "source": [ 545 | "[10,20,30,40,50]\n", 546 | " i j\n" 547 | ] 548 | }, 549 | { 550 | "cell_type": "code", 551 | "execution_count": 11, 552 | "id": "f968c291", 553 | "metadata": {}, 554 | "outputs": [ 555 | { 556 | "name": "stdout", 557 | "output_type": "stream", 558 | "text": [ 559 | "[1, 2, 3, 4, 12, 17, 43, 47, 54, 66]\n" 560 | ] 561 | } 562 | ], 563 | "source": [ 564 | "n = [12,43,54,66,47,17,3,2,4,1]\n", 565 | "# i j\n", 566 | "\n", 567 | "for i in range(len(n)-1):\n", 568 | " for j in range(i + 1,len(n)):\n", 569 | " if n[i] > n[j]:\n", 570 | " n[i],n[j] = n[j],n[i]\n", 571 | "\n", 572 | "print(n)" 573 | ] 574 | }, 575 | { 576 | "cell_type": "code", 577 | "execution_count": null, 578 | "id": "b155f204", 579 | "metadata": {}, 580 | "outputs": [], 581 | "source": [] 582 | } 583 | ], 584 | "metadata": { 585 | "kernelspec": { 586 | "display_name": "Python 3 (ipykernel)", 587 | "language": "python", 588 | "name": "python3" 589 | }, 590 | "language_info": { 591 | "codemirror_mode": { 592 | "name": "ipython", 593 | "version": 3 594 | }, 595 | "file_extension": ".py", 596 | "mimetype": "text/x-python", 597 | "name": "python", 598 | "nbconvert_exporter": "python", 599 | "pygments_lexer": "ipython3", 600 | "version": "3.9.12" 601 | } 602 | }, 603 | "nbformat": 4, 604 | "nbformat_minor": 5 605 | } 606 | -------------------------------------------------------------------------------- /25_26_27_28_29_Algorithms/27_Sorting Algorithms.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "ef79ea78", 6 | "metadata": {}, 7 | "source": [ 8 | "# Selection Sort" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": null, 14 | "id": "2ac82a20", 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "[4,3,5,6,2]\n", 19 | "[3,4,5,6,2]\n", 20 | "[2,4,5,6,3]" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 1, 26 | "id": "d8b66137", 27 | "metadata": {}, 28 | "outputs": [ 29 | { 30 | "name": "stdout", 31 | "output_type": "stream", 32 | "text": [ 33 | "[10, 20, 30, 40, 50]\n" 34 | ] 35 | } 36 | ], 37 | "source": [ 38 | "n = [10,50,40,20,30]\n", 39 | "# i j\n", 40 | "# m\n", 41 | "for i in range(len(n)-1):\n", 42 | " m = n[i] # m = 40, 10\n", 43 | " loc = i # loc = 0, 2\n", 44 | " for j in range(i+1, len(n)): # i = 0\n", 45 | " if m > n[j]: # j = 1, 2, 3, 4\n", 46 | " m = n[j]\n", 47 | " loc = j\n", 48 | " if i!=loc:\n", 49 | " temp = n[i]\n", 50 | " n[i] = n[loc]\n", 51 | " n[loc] = temp\n", 52 | " \n", 53 | "print(n)" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 2, 59 | "id": "1282c3c5", 60 | "metadata": {}, 61 | "outputs": [ 62 | { 63 | "name": "stdout", 64 | "output_type": "stream", 65 | "text": [ 66 | "20\n", 67 | "10\n" 68 | ] 69 | } 70 | ], 71 | "source": [ 72 | "a = 10\n", 73 | "b = 20\n", 74 | "\n", 75 | "c = a\n", 76 | "a = b\n", 77 | "b = c\n", 78 | "\n", 79 | "print(a)\n", 80 | "print(b)" 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "id": "b5f379bd", 86 | "metadata": {}, 87 | "source": [ 88 | "# Merge Sort" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 4, 94 | "id": "ed6e1d50", 95 | "metadata": {}, 96 | "outputs": [ 97 | { 98 | "name": "stdout", 99 | "output_type": "stream", 100 | "text": [ 101 | "[10, 20, 30, 40, 50]\n", 102 | "[5, 15, 19, 25, 41]\n", 103 | "[5, 10, 15, 19, 20, 25, 30, 40, 41, 50]\n" 104 | ] 105 | } 106 | ], 107 | "source": [ 108 | "n = [10,20,30,40,50]\n", 109 | "m = [5,15,19,25,41]\n", 110 | "\n", 111 | "l = []\n", 112 | "\n", 113 | "i = 0\n", 114 | "j = 0\n", 115 | "\n", 116 | "while i < 5 and j < 5:\n", 117 | " if n[i] <= m[j]:\n", 118 | " l.append(n[i])\n", 119 | " i += 1\n", 120 | " else:\n", 121 | " l.append(m[j])\n", 122 | " j += 1\n", 123 | "\n", 124 | "while i < 5:\n", 125 | " l.append(n[i])\n", 126 | " i += 1\n", 127 | "\n", 128 | "while j < 5:\n", 129 | " l.append(m[j])\n", 130 | " j += 1\n", 131 | "\n", 132 | "print(n)\n", 133 | "print(m)\n", 134 | "print(l)" 135 | ] 136 | }, 137 | { 138 | "cell_type": "markdown", 139 | "id": "02538358", 140 | "metadata": {}, 141 | "source": [ 142 | "# Insertion Sort" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 5, 148 | "id": "53562663", 149 | "metadata": {}, 150 | "outputs": [ 151 | { 152 | "name": "stdout", 153 | "output_type": "stream", 154 | "text": [ 155 | "[10, 20, 30, 40, 50]\n" 156 | ] 157 | } 158 | ], 159 | "source": [ 160 | "a = [10,40,50,20,30]\n", 161 | "\n", 162 | "for i in range(1,len(a)):\n", 163 | " temp = a[i]\n", 164 | " j = i - 1\n", 165 | " while temp < a[j] and j >= 0: # temp = 40, 10\n", 166 | " a[j+1] = a[j] # i = 1\n", 167 | " j = j-1 # j = 0, 1, 0\n", 168 | " a[j+1] = temp\n", 169 | "\n", 170 | "print(a)" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": null, 176 | "id": "24c7d035", 177 | "metadata": {}, 178 | "outputs": [], 179 | "source": [] 180 | } 181 | ], 182 | "metadata": { 183 | "kernelspec": { 184 | "display_name": "Python 3 (ipykernel)", 185 | "language": "python", 186 | "name": "python3" 187 | }, 188 | "language_info": { 189 | "codemirror_mode": { 190 | "name": "ipython", 191 | "version": 3 192 | }, 193 | "file_extension": ".py", 194 | "mimetype": "text/x-python", 195 | "name": "python", 196 | "nbconvert_exporter": "python", 197 | "pygments_lexer": "ipython3", 198 | "version": "3.9.12" 199 | } 200 | }, 201 | "nbformat": 4, 202 | "nbformat_minor": 5 203 | } 204 | -------------------------------------------------------------------------------- /25_26_27_28_29_Algorithms/28_29_Recursion.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "d716f934", 6 | "metadata": {}, 7 | "source": [ 8 | "# Recursion\n", 9 | "\n", 10 | "when a function call itself" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": null, 16 | "id": "10f68cd1", 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "def mini():\n", 21 | " \n", 22 | " mini()" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 5, 28 | "id": "d808a722", 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "name": "stdout", 33 | "output_type": "stream", 34 | "text": [ 35 | "Enter a number5\n", 36 | "Factorial = 120\n" 37 | ] 38 | } 39 | ], 40 | "source": [ 41 | "# enter a number from user and print factorial of it\n", 42 | "# 5 -> 5*4*3*2*1 -> 120\n", 43 | "# 1 -> 1\n", 44 | "# 0 -> 1\n", 45 | "\n", 46 | "n = int(input(\"Enter a number\"))\n", 47 | "f = 1\n", 48 | "\n", 49 | "while n > 1:\n", 50 | " f = f * n # f = 1, 5, 20, 60, 120\n", 51 | " n -= 1 # n = 5, 4, 3, 2, 1\n", 52 | "\n", 53 | "print(\"Factorial =\", f)" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 7, 59 | "id": "aa20a2cc", 60 | "metadata": {}, 61 | "outputs": [ 62 | { 63 | "name": "stdout", 64 | "output_type": "stream", 65 | "text": [ 66 | "Enter a numner: 5\n", 67 | "120\n" 68 | ] 69 | } 70 | ], 71 | "source": [ 72 | "# using recursion\n", 73 | "\n", 74 | "def fact(n):\n", 75 | " if n == 1:\n", 76 | " return 1 # base case\n", 77 | " else:\n", 78 | " return n * fact(n-1)\n", 79 | " # 3 * 2\n", 80 | " # \n", 81 | "\n", 82 | "a = int(input(\"Enter a numner: \"))\n", 83 | "\n", 84 | "print(fact(a))" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": null, 90 | "id": "08434110", 91 | "metadata": {}, 92 | "outputs": [], 93 | "source": [ 94 | "\n", 95 | "\n", 96 | "\n", 97 | "\n", 98 | "\n", 99 | "main()" 100 | ] 101 | }, 102 | { 103 | "cell_type": "markdown", 104 | "id": "027cd2f2", 105 | "metadata": {}, 106 | "source": [ 107 | "**Wrtie a program to print sum of all the values in a list using recursion**" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": null, 113 | "id": "354e7a2f", 114 | "metadata": {}, 115 | "outputs": [], 116 | "source": [ 117 | "# l = [10,20,30,40]" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 8, 123 | "id": "c354674a", 124 | "metadata": {}, 125 | "outputs": [ 126 | { 127 | "name": "stdout", 128 | "output_type": "stream", 129 | "text": [ 130 | "150\n" 131 | ] 132 | } 133 | ], 134 | "source": [ 135 | "def summ(l):\n", 136 | " if len(l) == 1:\n", 137 | " return l[0]\n", 138 | " else:\n", 139 | " return l[0] + summ(l[1:])\n", 140 | " # 10 + 140\n", 141 | " # \n", 142 | " # \n", 143 | " # \n", 144 | "\n", 145 | "print(summ([10,20,30,40,50]))\n", 146 | "# 150" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": null, 152 | "id": "710d894c", 153 | "metadata": {}, 154 | "outputs": [], 155 | "source": [ 156 | "\n", 157 | "\n", 158 | "main()" 159 | ] 160 | }, 161 | { 162 | "cell_type": "markdown", 163 | "id": "e8b0a0ee", 164 | "metadata": {}, 165 | "source": [ 166 | "**Fibonacci series**" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": null, 172 | "id": "864b9c61", 173 | "metadata": {}, 174 | "outputs": [], 175 | "source": [ 176 | "0 1 1 2 3 5 8 13\n", 177 | "a b c" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": 13, 183 | "id": "14de5652", 184 | "metadata": {}, 185 | "outputs": [ 186 | { 187 | "name": "stdout", 188 | "output_type": "stream", 189 | "text": [ 190 | "enter limit0\n", 191 | "0 1 " 192 | ] 193 | } 194 | ], 195 | "source": [ 196 | "n = int(input(\"enter limit\"))\n", 197 | "\n", 198 | "a = 0\n", 199 | "b = 1\n", 200 | "i = 1\n", 201 | "\n", 202 | "print(a, end = \" \")\n", 203 | "print(b, end = \" \")\n", 204 | "\n", 205 | "while i <= n - 2: # n = 7\n", 206 | " c = a + b # a = 0, 1, 1, 2, 3, 5\n", 207 | " print(c, end = \" \") # b = 1, 1, 2, 3, 5, 8\n", 208 | " a = b # c = 1, 2, 3, 5, 8\n", 209 | " b = c # i = 1, 2, 3, 4, 5, 6\n", 210 | " i += 1" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": null, 216 | "id": "81ac3b93", 217 | "metadata": {}, 218 | "outputs": [], 219 | "source": [ 220 | "0 1 1 2 3 5 8\n", 221 | "a b c\n", 222 | " a b c" 223 | ] 224 | }, 225 | { 226 | "cell_type": "code", 227 | "execution_count": 16, 228 | "id": "765c7f57", 229 | "metadata": {}, 230 | "outputs": [ 231 | { 232 | "name": "stdout", 233 | "output_type": "stream", 234 | "text": [ 235 | "enter limit10\n", 236 | "0 1 1 2 3 5 8 13 21 34 " 237 | ] 238 | } 239 | ], 240 | "source": [ 241 | "n = int(input(\"enter limit\"))\n", 242 | "\n", 243 | "a = 0\n", 244 | "b = 1\n", 245 | "i = 1\n", 246 | "\n", 247 | "while i <= n: # n = 7\n", 248 | " print(a, end = \" \") # a = 0, 1, 1, 2, 3, 5\n", 249 | " # b = 1, 1, 2, 3, 5, 8\n", 250 | " c = a + b\n", 251 | " a = b # c = 1, 2, 3, 5, 8\n", 252 | " b = c # i = 1, 2, 3, 4, 5, 6\n", 253 | " i += 1" 254 | ] 255 | }, 256 | { 257 | "cell_type": "markdown", 258 | "id": "197c0467", 259 | "metadata": {}, 260 | "source": [ 261 | "# Day 29\n", 262 | "\n", 263 | "**print fibonacci series using recursion**" 264 | ] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "execution_count": 2, 269 | "id": "bf7f8e95", 270 | "metadata": {}, 271 | "outputs": [], 272 | "source": [ 273 | "def fibonacii(n):\n", 274 | " if n == 1 or n == 2:\n", 275 | " return 1\n", 276 | " else:\n", 277 | " return fibonacii(n - 1) + fibonacii(n - 2)\n", 278 | " # fibonacii(6) + fibonacii(5)\n", 279 | " # f(5) + f(4)\n", 280 | " # f(4) + f(3)\n", 281 | " #f(3) + f(2)\n", 282 | " #f(2) + f(1)" 283 | ] 284 | }, 285 | { 286 | "cell_type": "code", 287 | "execution_count": 5, 288 | "id": "af0f28b3", 289 | "metadata": {}, 290 | "outputs": [ 291 | { 292 | "name": "stdout", 293 | "output_type": "stream", 294 | "text": [ 295 | "34\n" 296 | ] 297 | } 298 | ], 299 | "source": [ 300 | "print(fibonacii(9))" 301 | ] 302 | }, 303 | { 304 | "cell_type": "code", 305 | "execution_count": 6, 306 | "id": "4b156cd8", 307 | "metadata": {}, 308 | "outputs": [ 309 | { 310 | "name": "stdout", 311 | "output_type": "stream", 312 | "text": [ 313 | "1 1 2 3 5 8 13 21 34 " 314 | ] 315 | } 316 | ], 317 | "source": [ 318 | "for i in range(1, 10):\n", 319 | " print(fibonacii(i), end = \" \")" 320 | ] 321 | }, 322 | { 323 | "cell_type": "markdown", 324 | "id": "0c69e16b", 325 | "metadata": {}, 326 | "source": [ 327 | "**Write a program to calculate the value of a to the power of b**\n", 328 | "\n", 329 | "2 ** 3 -> 8" 330 | ] 331 | }, 332 | { 333 | "cell_type": "code", 334 | "execution_count": 7, 335 | "id": "fba18126", 336 | "metadata": {}, 337 | "outputs": [], 338 | "source": [ 339 | "def power(a,b):\n", 340 | " if b == 0:\n", 341 | " return 1\n", 342 | " elif a == 0:\n", 343 | " return 0\n", 344 | " elif b == 1:\n", 345 | " return a\n", 346 | " else:\n", 347 | " return a * power(a, b - 1)\n", 348 | " # 2 * power(2,2)\n", 349 | " # 2 * 2" 350 | ] 351 | }, 352 | { 353 | "cell_type": "code", 354 | "execution_count": 8, 355 | "id": "b5461c34", 356 | "metadata": {}, 357 | "outputs": [ 358 | { 359 | "name": "stdout", 360 | "output_type": "stream", 361 | "text": [ 362 | "8\n" 363 | ] 364 | } 365 | ], 366 | "source": [ 367 | "print(power(2,3))" 368 | ] 369 | }, 370 | { 371 | "cell_type": "markdown", 372 | "id": "c44ad9cc", 373 | "metadata": {}, 374 | "source": [ 375 | "# Interview example questions\n", 376 | "\n", 377 | "**return the multiplication of 2 numbers without using multiply sign**" 378 | ] 379 | }, 380 | { 381 | "cell_type": "code", 382 | "execution_count": 9, 383 | "id": "40d6bda8", 384 | "metadata": {}, 385 | "outputs": [], 386 | "source": [ 387 | "def multiply(a, b):\n", 388 | " s = 0\n", 389 | " while b > 0:\n", 390 | " s = s + a\n", 391 | " b = b - 1\n", 392 | " return s" 393 | ] 394 | }, 395 | { 396 | "cell_type": "code", 397 | "execution_count": 10, 398 | "id": "ae3a1aaa", 399 | "metadata": {}, 400 | "outputs": [ 401 | { 402 | "data": { 403 | "text/plain": [ 404 | "20" 405 | ] 406 | }, 407 | "execution_count": 10, 408 | "metadata": {}, 409 | "output_type": "execute_result" 410 | } 411 | ], 412 | "source": [ 413 | "multiply(5,4)" 414 | ] 415 | }, 416 | { 417 | "cell_type": "code", 418 | "execution_count": 11, 419 | "id": "4cfd85a3", 420 | "metadata": {}, 421 | "outputs": [ 422 | { 423 | "data": { 424 | "text/plain": [ 425 | "20" 426 | ] 427 | }, 428 | "execution_count": 11, 429 | "metadata": {}, 430 | "output_type": "execute_result" 431 | } 432 | ], 433 | "source": [ 434 | "# recursion\n", 435 | "\n", 436 | "def multiplyR(a,b):\n", 437 | " if b == 1:\n", 438 | " return a\n", 439 | " else:\n", 440 | " return a + multiplyR(a, b - 1)\n", 441 | "\n", 442 | "multiplyR(5,4)" 443 | ] 444 | }, 445 | { 446 | "cell_type": "markdown", 447 | "id": "260c08a9", 448 | "metadata": {}, 449 | "source": [ 450 | "# dynamic programming" 451 | ] 452 | }, 453 | { 454 | "cell_type": "code", 455 | "execution_count": 13, 456 | "id": "af569c43", 457 | "metadata": {}, 458 | "outputs": [], 459 | "source": [ 460 | "# it is solving overlapping problems\n", 461 | "\n", 462 | "# cache the results" 463 | ] 464 | }, 465 | { 466 | "cell_type": "code", 467 | "execution_count": 14, 468 | "id": "3d125d38", 469 | "metadata": {}, 470 | "outputs": [], 471 | "source": [ 472 | "def fibonacii(n):\n", 473 | " if n == 1 or n == 2:\n", 474 | " return 1\n", 475 | " else:\n", 476 | " return fibonacii(n - 1) + fibonacii(n - 2)" 477 | ] 478 | }, 479 | { 480 | "cell_type": "code", 481 | "execution_count": 15, 482 | "id": "db1bd855", 483 | "metadata": {}, 484 | "outputs": [], 485 | "source": [ 486 | "# dynamic programming" 487 | ] 488 | }, 489 | { 490 | "cell_type": "code", 491 | "execution_count": 16, 492 | "id": "82acfebf", 493 | "metadata": {}, 494 | "outputs": [], 495 | "source": [ 496 | "def fibonaciiDP(n, tracker = {}):\n", 497 | " if n == 1 or n == 0:\n", 498 | " return n\n", 499 | " \n", 500 | " if n > 1:\n", 501 | " if n in tracker:\n", 502 | " return tracker[n]\n", 503 | " else:\n", 504 | " result = fibonaciiDP(n - 1, tracker) + fibonaciiDP(n - 2, tracker)\n", 505 | " tracker[n] = result\n", 506 | " return result" 507 | ] 508 | }, 509 | { 510 | "cell_type": "code", 511 | "execution_count": 20, 512 | "id": "2fff4937", 513 | "metadata": {}, 514 | "outputs": [ 515 | { 516 | "name": "stdout", 517 | "output_type": "stream", 518 | "text": [ 519 | "13\n" 520 | ] 521 | } 522 | ], 523 | "source": [ 524 | "print(fibonaciiDP(7))" 525 | ] 526 | }, 527 | { 528 | "cell_type": "code", 529 | "execution_count": null, 530 | "id": "a5754169", 531 | "metadata": {}, 532 | "outputs": [], 533 | "source": [] 534 | } 535 | ], 536 | "metadata": { 537 | "kernelspec": { 538 | "display_name": "Python 3 (ipykernel)", 539 | "language": "python", 540 | "name": "python3" 541 | }, 542 | "language_info": { 543 | "codemirror_mode": { 544 | "name": "ipython", 545 | "version": 3 546 | }, 547 | "file_extension": ".py", 548 | "mimetype": "text/x-python", 549 | "name": "python", 550 | "nbconvert_exporter": "python", 551 | "pygments_lexer": "ipython3", 552 | "version": "3.9.12" 553 | } 554 | }, 555 | "nbformat": 4, 556 | "nbformat_minor": 5 557 | } 558 | -------------------------------------------------------------------------------- /30_Higher Order Functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "84abf3c2", 6 | "metadata": {}, 7 | "source": [ 8 | "# Function as a parameter" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 1, 14 | "id": "b7269c16", 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "100\n" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "def square(n):\n", 27 | " return n * n\n", 28 | "\n", 29 | "def myfunction(f, number):\n", 30 | " s = f(number)\n", 31 | " # square(10)\n", 32 | " return s\n", 33 | "\n", 34 | "z = myfunction(square, 10)\n", 35 | "\n", 36 | "print(z)" 37 | ] 38 | }, 39 | { 40 | "cell_type": "markdown", 41 | "id": "6cf8df20", 42 | "metadata": {}, 43 | "source": [ 44 | "# Function as a return Value" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 2, 50 | "id": "f14f3890", 51 | "metadata": {}, 52 | "outputs": [ 53 | { 54 | "name": "stdout", 55 | "output_type": "stream", 56 | "text": [ 57 | "49\n" 58 | ] 59 | } 60 | ], 61 | "source": [ 62 | "def square(n):\n", 63 | " return n * n\n", 64 | "\n", 65 | "def cube(n):\n", 66 | " return n * n * n\n", 67 | "\n", 68 | "def myfunction(t):\n", 69 | " if t == 'square':\n", 70 | " return square\n", 71 | " elif t == 'cube':\n", 72 | " return cube\n", 73 | "\n", 74 | "pen = myfunction('square')\n", 75 | "print(pen(7))" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 3, 81 | "id": "1f121526", 82 | "metadata": {}, 83 | "outputs": [ 84 | { 85 | "name": "stdout", 86 | "output_type": "stream", 87 | "text": [ 88 | "343\n" 89 | ] 90 | } 91 | ], 92 | "source": [ 93 | "pen = myfunction('cube')\n", 94 | "print(pen(7))" 95 | ] 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "id": "486ae506", 100 | "metadata": {}, 101 | "source": [ 102 | "# Closures" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 4, 108 | "id": "6d8a8b31", 109 | "metadata": {}, 110 | "outputs": [ 111 | { 112 | "name": "stdout", 113 | "output_type": "stream", 114 | "text": [ 115 | "20\n" 116 | ] 117 | } 118 | ], 119 | "source": [ 120 | "def add_ten():\n", 121 | " ten = 10\n", 122 | " def add(n):\n", 123 | " return n + ten\n", 124 | " return add\n", 125 | "\n", 126 | "r = add_ten()\n", 127 | "print(r(10))" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": 5, 133 | "id": "ff0c5307", 134 | "metadata": {}, 135 | "outputs": [ 136 | { 137 | "name": "stdout", 138 | "output_type": "stream", 139 | "text": [ 140 | "25\n" 141 | ] 142 | } 143 | ], 144 | "source": [ 145 | "print(r(15))" 146 | ] 147 | }, 148 | { 149 | "cell_type": "markdown", 150 | "id": "0ca4eb34", 151 | "metadata": {}, 152 | "source": [ 153 | "# Decorators" 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": 6, 159 | "id": "9eaa6d1a", 160 | "metadata": {}, 161 | "outputs": [ 162 | { 163 | "name": "stdout", 164 | "output_type": "stream", 165 | "text": [ 166 | "WELCOME TO THE WORLD\n" 167 | ] 168 | } 169 | ], 170 | "source": [ 171 | "# normal function\n", 172 | "\n", 173 | "def greeting():\n", 174 | " return 'Welcome to the World'\n", 175 | "\n", 176 | "def uppercase_decorator(func):\n", 177 | " def wrapper():\n", 178 | " f = func()\n", 179 | " uppercase = f.upper()\n", 180 | " return uppercase\n", 181 | " return wrapper\n", 182 | "\n", 183 | "g = uppercase_decorator(greeting)\n", 184 | "\n", 185 | "print(g())" 186 | ] 187 | }, 188 | { 189 | "cell_type": "code", 190 | "execution_count": 7, 191 | "id": "bdffd78a", 192 | "metadata": {}, 193 | "outputs": [ 194 | { 195 | "name": "stdout", 196 | "output_type": "stream", 197 | "text": [ 198 | "WELCOME TO INDIA\n" 199 | ] 200 | } 201 | ], 202 | "source": [ 203 | "# using decorator\n", 204 | "\n", 205 | "def uppercase_decorator(func):\n", 206 | " def wrapper():\n", 207 | " f = func()\n", 208 | " uppercase = f.upper()\n", 209 | " return uppercase\n", 210 | " return wrapper\n", 211 | "\n", 212 | "@uppercase_decorator\n", 213 | "def greet():\n", 214 | " return 'Welcome to the World'\n", 215 | "\n", 216 | "print(greet())" 217 | ] 218 | }, 219 | { 220 | "cell_type": "markdown", 221 | "id": "8a487d06", 222 | "metadata": {}, 223 | "source": [ 224 | "# Built-in Higher order functions\n", 225 | "\n", 226 | "\n", 227 | "### map" 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": 11, 233 | "id": "7bb7f1ce", 234 | "metadata": {}, 235 | "outputs": [], 236 | "source": [ 237 | "# help(map)" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": 12, 243 | "id": "d7d70c41", 244 | "metadata": {}, 245 | "outputs": [ 246 | { 247 | "name": "stdout", 248 | "output_type": "stream", 249 | "text": [ 250 | "\n" 251 | ] 252 | } 253 | ], 254 | "source": [ 255 | "n = [1,2,3,4,5]\n", 256 | "\n", 257 | "def add(x):\n", 258 | " return x + 5\n", 259 | "\n", 260 | "print(map(add, n))" 261 | ] 262 | }, 263 | { 264 | "cell_type": "code", 265 | "execution_count": 13, 266 | "id": "59000086", 267 | "metadata": {}, 268 | "outputs": [ 269 | { 270 | "name": "stdout", 271 | "output_type": "stream", 272 | "text": [ 273 | "[6, 7, 8, 9, 10]\n" 274 | ] 275 | } 276 | ], 277 | "source": [ 278 | "print(list(map(add, n)))" 279 | ] 280 | }, 281 | { 282 | "cell_type": "code", 283 | "execution_count": 14, 284 | "id": "1817268b", 285 | "metadata": {}, 286 | "outputs": [ 287 | { 288 | "name": "stdout", 289 | "output_type": "stream", 290 | "text": [ 291 | "['SUNIL', 'HIMANSHU', 'ALEX']\n" 292 | ] 293 | } 294 | ], 295 | "source": [ 296 | "n = ['Sunil', 'Himanshu', 'Alex']\n", 297 | "\n", 298 | "def change_to_upper(name):\n", 299 | " return name.upper()\n", 300 | "\n", 301 | "print(list(map(change_to_upper, n)))" 302 | ] 303 | }, 304 | { 305 | "cell_type": "markdown", 306 | "id": "39a9407f", 307 | "metadata": {}, 308 | "source": [ 309 | "### Filter " 310 | ] 311 | }, 312 | { 313 | "cell_type": "code", 314 | "execution_count": 15, 315 | "id": "8ca40629", 316 | "metadata": {}, 317 | "outputs": [ 318 | { 319 | "name": "stdout", 320 | "output_type": "stream", 321 | "text": [ 322 | "[22, 44]\n" 323 | ] 324 | } 325 | ], 326 | "source": [ 327 | "n = [11,22,33,44,55]\n", 328 | "\n", 329 | "def even(x):\n", 330 | " if x % 2 == 0:\n", 331 | " return True\n", 332 | " return False\n", 333 | "\n", 334 | "print(list(filter(even, n)))" 335 | ] 336 | }, 337 | { 338 | "cell_type": "markdown", 339 | "id": "44d45806", 340 | "metadata": {}, 341 | "source": [ 342 | "# lambda function" 343 | ] 344 | }, 345 | { 346 | "cell_type": "code", 347 | "execution_count": null, 348 | "id": "70dbbf2d", 349 | "metadata": {}, 350 | "outputs": [], 351 | "source": [ 352 | "# lambda (argument) : expression" 353 | ] 354 | }, 355 | { 356 | "cell_type": "code", 357 | "execution_count": 16, 358 | "id": "3e12df81", 359 | "metadata": {}, 360 | "outputs": [ 361 | { 362 | "name": "stdout", 363 | "output_type": "stream", 364 | "text": [ 365 | "27\n" 366 | ] 367 | } 368 | ], 369 | "source": [ 370 | "def cube(x):\n", 371 | " return x * x * x\n", 372 | "\n", 373 | "\n", 374 | "g = lambda x: x * x * x\n", 375 | "\n", 376 | "\n", 377 | "print(cube(3))" 378 | ] 379 | }, 380 | { 381 | "cell_type": "code", 382 | "execution_count": 18, 383 | "id": "e51ec887", 384 | "metadata": {}, 385 | "outputs": [ 386 | { 387 | "name": "stdout", 388 | "output_type": "stream", 389 | "text": [ 390 | "343\n" 391 | ] 392 | } 393 | ], 394 | "source": [ 395 | "print(g(7))" 396 | ] 397 | }, 398 | { 399 | "cell_type": "code", 400 | "execution_count": 19, 401 | "id": "cc67ea29", 402 | "metadata": {}, 403 | "outputs": [ 404 | { 405 | "name": "stdout", 406 | "output_type": "stream", 407 | "text": [ 408 | "[22, 44]\n" 409 | ] 410 | } 411 | ], 412 | "source": [ 413 | "n = [11,22,33,44,55]\n", 414 | "\n", 415 | "print(list(filter(lambda x: x % 2 == 0 , n)))" 416 | ] 417 | }, 418 | { 419 | "cell_type": "markdown", 420 | "id": "dba472c7", 421 | "metadata": {}, 422 | "source": [ 423 | "### reduce" 424 | ] 425 | }, 426 | { 427 | "cell_type": "code", 428 | "execution_count": 22, 429 | "id": "845336bf", 430 | "metadata": { 431 | "scrolled": true 432 | }, 433 | "outputs": [ 434 | { 435 | "name": "stdout", 436 | "output_type": "stream", 437 | "text": [ 438 | "Help on built-in function reduce in module _functools:\n", 439 | "\n", 440 | "reduce(...)\n", 441 | " reduce(function, sequence[, initial]) -> value\n", 442 | " \n", 443 | " Apply a function of two arguments cumulatively to the items of a sequence,\n", 444 | " from left to right, so as to reduce the sequence to a single value.\n", 445 | " For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n", 446 | " ((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n", 447 | " of the sequence in the calculation, and serves as a default when the\n", 448 | " sequence is empty.\n", 449 | "\n" 450 | ] 451 | } 452 | ], 453 | "source": [ 454 | "import functools\n", 455 | "help(functools.reduce)" 456 | ] 457 | }, 458 | { 459 | "cell_type": "code", 460 | "execution_count": 23, 461 | "id": "818c1519", 462 | "metadata": {}, 463 | "outputs": [ 464 | { 465 | "name": "stdout", 466 | "output_type": "stream", 467 | "text": [ 468 | "15\n" 469 | ] 470 | } 471 | ], 472 | "source": [ 473 | "n = [1,2,3,4,5]\n", 474 | "\n", 475 | "def add(x, y):\n", 476 | " return x + y\n", 477 | "\n", 478 | "t = functools.reduce(add, n)\n", 479 | "\n", 480 | "print(t)" 481 | ] 482 | }, 483 | { 484 | "cell_type": "code", 485 | "execution_count": null, 486 | "id": "77810633", 487 | "metadata": {}, 488 | "outputs": [], 489 | "source": [] 490 | } 491 | ], 492 | "metadata": { 493 | "kernelspec": { 494 | "display_name": "Python 3 (ipykernel)", 495 | "language": "python", 496 | "name": "python3" 497 | }, 498 | "language_info": { 499 | "codemirror_mode": { 500 | "name": "ipython", 501 | "version": 3 502 | }, 503 | "file_extension": ".py", 504 | "mimetype": "text/x-python", 505 | "name": "python", 506 | "nbconvert_exporter": "python", 507 | "pygments_lexer": "ipython3", 508 | "version": "3.9.12" 509 | } 510 | }, 511 | "nbformat": 4, 512 | "nbformat_minor": 5 513 | } 514 | -------------------------------------------------------------------------------- /31_32_Virtual Environment and PIP/.gitignore: -------------------------------------------------------------------------------- 1 | venv/ -------------------------------------------------------------------------------- /31_32_Virtual Environment and PIP/app.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template 2 | 3 | app = Flask(__name__) # '__main__' 4 | 5 | @app.route('/') 6 | def index(): 7 | return "Welcome to Flask" 8 | 9 | @app.route('/home') 10 | def home(): 11 | return '

Welcome Home!

' 12 | 13 | @app.route('/profile') 14 | def profile(): 15 | goa = "from the goa trip" 16 | return render_template('index.html', name = goa) 17 | 18 | 19 | if __name__ == '__main__': 20 | app.run(port=5000) 21 | 22 | 23 | # https://github.com/hemansnation/ 24 | # scheme domain name /route 25 | # http 26 | # https 27 | # ftp 28 | # file -------------------------------------------------------------------------------- /31_32_Virtual Environment and PIP/requirements.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hemansnation/Python-and-Data-Structures-for-Data-Science/69bf18c4c173bddd696767ea96290b7ec30ff7b0/31_32_Virtual Environment and PIP/requirements.txt -------------------------------------------------------------------------------- /31_32_Virtual Environment and PIP/static/css/style.css: -------------------------------------------------------------------------------- 1 | body{ 2 | background-color: blueviolet; 3 | } 4 | 5 | h1{ 6 | color:aliceblue; 7 | } -------------------------------------------------------------------------------- /31_32_Virtual Environment and PIP/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Index Page API 4 | 5 | 6 | 7 |

This is our first Flask API

8 | 9 |

10 | {{ name }} 11 |

12 | 13 | 14 | -------------------------------------------------------------------------------- /33_Python and MongoDB/.gitignore: -------------------------------------------------------------------------------- 1 | venv/ -------------------------------------------------------------------------------- /33_Python and MongoDB/app.py: -------------------------------------------------------------------------------- 1 | from flask import Flask 2 | import pymongo 3 | from bson.objectid import ObjectId 4 | 5 | MONGODB_URI = 'mongodb+srv://pythonclosed:python12345@cluster0.96mmy7a.mongodb.net/?retryWrites=true&w=majority' 6 | 7 | client = pymongo.MongoClient(MONGODB_URI) 8 | 9 | # create database 10 | db = client.college 11 | 12 | # # collection(table) document(rows) 13 | # db.students.insert_one({'name':'Sunil', 'country':'India', 'city': 'indore', 'age':21}) 14 | 15 | 16 | # print(client.list_database_names()) 17 | 18 | # students = [ 19 | # {'name':'Sunil', 'country':'India', 'city': 'indore', 'age':21}, 20 | # {'name':'Ravi', 'country':'India', 'city': 'indore', 'age':25}, 21 | # {'name':'Alex', 'country':'USA', 'city': 'Seatle', 'age':43}, 22 | # ] 23 | 24 | # for s in students: 25 | # db.students.insert_one(s) 26 | 27 | s = db.students.find_one({'_id':ObjectId('64f547ec2947ee9e3d7e00fc')}) 28 | print(s) 29 | 30 | app = Flask(__name__) 31 | 32 | if __name__ =='__main__': 33 | app.run(debug=True, port=5000) -------------------------------------------------------------------------------- /Doubts.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "id": "79986623", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [] 10 | } 11 | ], 12 | "metadata": { 13 | "kernelspec": { 14 | "display_name": "Python 3 (ipykernel)", 15 | "language": "python", 16 | "name": "python3" 17 | }, 18 | "language_info": { 19 | "codemirror_mode": { 20 | "name": "ipython", 21 | "version": 3 22 | }, 23 | "file_extension": ".py", 24 | "mimetype": "text/x-python", 25 | "name": "python", 26 | "nbconvert_exporter": "python", 27 | "pygments_lexer": "ipython3", 28 | "version": "3.9.12" 29 | } 30 | }, 31 | "nbformat": 4, 32 | "nbformat_minor": 5 33 | } 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python-and-Data-Structures-for-Data-Science --------------------------------------------------------------------------------