├── 1 Recursion-1 ├── 1 Intro to Recursion.ipynb ├── 2 Fibonacci Number.ipynb ├── 3 Recursion Depth.ipynb ├── 4 List is sorted or not .ipynb ├── 5 Sum of Array.ipynb ├── 6 Check Number in Array.ipynb ├── 7 Find Index of number.ipynb └── 8 Last Index of Number.ipynb ├── 10 Linked List-2 ├── 10.01 Reverse LL - O(N^2) [Recursive].ipynb ├── 10.02 Reverse LL - 2.ipynb ├── 10.03 Reverse LL - 3 [Best].ipynb ├── 10.04 Reverse LL (Iterative).ipynb ├── 10.05 Mid Point of a Linked List.ipynb ├── 10.06 Merge Two Sorted LL.ipynb ├── 10.07 Merge Sort in LL.ipynb ├── 10.08 Variations in LL.ipynb ├── 10.09 Find a node in LL (recursive).ipynb ├── 10.10 Even nodes after Odd nodes in LL.ipynb ├── 10.11 Delete every N nodes .ipynb ├── 10.12 Swap two nodes of a LL.ipynb ├── 10.13 kReverse.ipynb └── 10.14 Bubble Sort Using Linked List.ipynb ├── 11 Stacks ├── 11.1 Intro to Stack.ipynb ├── 11.2 Stack Using Array.ipynb ├── 11.3 Stack Using Linked List.ipynb ├── 11.4 Inbuilt Stack & Queues.ipynb ├── 11.5 Balanced Paranthesis.ipynb ├── 11.6 Reverse a Stack using Another Stack.ipynb ├── 11.7 Check Redundant Brackets.ipynb ├── 11.8 Stock Span.ipynb └── 11.9 Minimum Bracket Reversal.ipynb ├── 12 Queue ├── 12.1 Intro to Queue.ipynb ├── 12.2 Queue using Array.ipynb ├── 12.3 Queue using LL.ipynb └── 12.4 Inbuilt Stacks and Queues.ipynb ├── 13 Trees ├── 13.01 Introduction to Trees.ipynb ├── 13.02 Print a Tree.ipynb ├── 13.03 Input of Binary Tree.ipynb ├── 13.04 Number of Nodes in a tree.ipynb ├── 13.05 Sum of Nodes.ipynb ├── 13.06 Tree Traversals.ipynb ├── 13.07 Node with Largest Data.ipynb ├── 13.08 Nodes Greater Than X.ipynb ├── 13.09 Height of a tree.ipynb ├── 13.10 Number of Leaf Nodes.ipynb ├── 13.11 Print Nodes at Depth K.ipynb ├── 13.12 Replace Node with Depth .ipynb ├── 13.13 Is node present.ipynb └── 13.14 Nodes without sibling.ipynb ├── 14 Binary Trees - 2 ├── 14.01 Time Complexity of Binary Tree Problem.ipynb ├── 14.02 Remove Leaf Nodes.ipynb ├── 14.03 Mirror Binary Tree.ipynb ├── 14.04 Check tree is balanced or not.ipynb ├── 14.05 Check Balanced {Optimized One}.ipynb ├── 14.06 Diameter of a Binary Tree.ipynb ├── 14.07 Level wise Input Binary Tree.ipynb ├── 14.08 Print Level wise Binary Tree.ipynb ├── 14.09 Construct a tree using inorder and preorder.ipynb ├── 14.10 Construct Tree using In-order and Post-Order.ipynb ├── 14.12 Min and max.ipynb └── 14.13 Level order traversal.ipynb ├── 15 BST-1 ├── 15.1 Intro to Binary Search Tree.ipynb ├── 15.2 Search in BST.ipynb ├── 15.3 Print Elements in range K1 and K2.ipynb ├── 15.4 Convert sorted arrays to BST.ipynb ├── 15.5 Check is BST.ipynb ├── 15.6 isBST optimised.ipynb └── 15.7 isBST (Another solution).ipynb ├── 16 BST-2 ├── 16.1 Root to node path in Binary Tree.ipynb ├── 16.2 Structure of BST class.ipynb ├── 16.3 BST class - search and print.ipynb ├── 16.4 BST class - inserting an element.ipynb ├── 16.5 BST class - delete.ipynb └── 16.6 BST Class Code.ipynb ├── 17 Generic Trees ├── 17.01 Generic Trees.ipynb ├── 17.02 Print a generic tree recursively.ipynb ├── 17.03 Take Tree Input (Recursively).ipynb ├── 17.04 Number of nodes in generic tree.ipynb ├── 17.05 Sum of all nodes.ipynb ├── 17.06 Node with Largest Data.ipynb ├── 17.07 Height of tree.ipynb ├── 17.08 Level wise Input.ipynb ├── 17.09 Print Levelwise.ipynb ├── 17.10 Contains X.ipynb ├── 17.11 Count leaf nodes.ipynb ├── 17.12 Node with maximum child sum.ipynb ├── 17.13 Structurally identical.ipynb └── 17.14 Next larger.ipynb ├── 18 Dictionaries ├── 18.01 Intro to Dictionaries.ipynb ├── 18.02 Accessing or Looping elements in dictionaries.ipynb ├── 18.03 Adding or Removing data in dictionary.ipynb ├── 18.04 Print all words with frequency k.ipynb ├── 18.05 Maximum Frequency.ipynb ├── 18.07 Make our own Hashmap.ipynb ├── 18.08 Collision Handling.ipynb ├── 18.09 Steps for implementing our own Map.ipynb ├── 18.10 How to insert in Map.ipynb ├── 18.11 Search and Remove from Map.ipynb ├── 18.12 Time Complexity for Map Functions.ipynb ├── 18.13 Rehashing.ipynb ├── 18.14 Extract Unique characters.ipynb ├── 18.15 Longest Increasing Subsequence.ipynb ├── 18.17 Longest subset zero sum.ipynb └── img │ ├── README.md │ ├── collision resolution techniques.png │ ├── download (1).png │ ├── download (2).png │ ├── download (3).png │ ├── download (4).png │ ├── download.png │ ├── hash.png │ ├── hashFunction.png │ └── hashTable.png ├── 19 Graphs - 1 ├── 19.01 Intro to Graphs.ipynb ├── 19.02 Terminologies in Graphs.ipynb ├── 19.03 Complexity in terms of edges.ipynb ├── 19.04 How to implement Graphs.ipynb ├── 19.05 Implementation of Graph.ipynb ├── 19.06 DFS.ipynb ├── 19.07 BFS.ipynb ├── 19.08 Has Path.ipynb ├── 19.09 BFS or DFS for disconnected graph.ipynb ├── 19.10 Get Path DFS.ipynb ├── 19.11 Get Path BFS.ipynb ├── 19.12 Is Graph Connected.ipynb ├── 19.13 All Connected Components.ipynb ├── 19.14 Variations of Graph.ipynb └── img │ ├── README.md │ ├── adjacency list.png │ ├── adjacency matrix.png │ ├── bfs.png │ ├── connected.png │ ├── dfs 2.png │ ├── dfs.png │ ├── diagram graph.png │ ├── directed.png │ ├── download (1).png │ ├── download (2).png │ ├── download (3).png │ ├── download.png │ ├── edge list.png │ ├── graph types.png │ ├── graph vertex array.png │ ├── table.png │ ├── tree graph.png │ ├── tree.png │ ├── two component.png │ ├── undirected.png │ ├── weighted directed.png │ └── weighted.png ├── 2 Recursion-2 ├── 1 Recursion in Strings.ipynb ├── 2 Replace pi with 3.14.ipynb ├── 3 Remove duplicates recursively.ipynb ├── 4 Binary Search using Recursion.ipynb ├── 5 Merge Sort.ipynb ├── 6 Quick Sort.ipynb └── 7 Towers of Hanoi.ipynb ├── 20 Graphs - 2 ├── 20.1 Minimum Spanning Trees.ipynb ├── 20.2 Kruskal's Algorithm.ipynb ├── 20.3 Detect Cycle.ipynb ├── 20.4 Union Find Algorithm.ipynb ├── 20.5 Kruskal's Algorithm.ipynb ├── 20.6 Prim's Algorithm.ipynb └── 20.7 Dijkstra's Algorithm.ipynb ├── 24 DP-1 ├── 24.01 Intro to DP.ipynb ├── 24.02 Fibonacci Memoization.ipynb ├── 24.03 Time Complexity of Memoization.ipynb ├── 24.04 Iterative Dynamic Programming.ipynb ├── 24.05 Why Iterative solutions are better.ipynb ├── 24.06 Min steps to 1.ipynb ├── 24.07 Min squares to represent N.ipynb ├── 24.08 longest Increasing Subsequence.ipynb └── img │ ├── IMG_20190918_145913.jpg │ ├── README.md │ ├── fibdiagram.jpg │ ├── fibonacci__binary_tree_recursive.svg │ ├── fibonacci_tree.png │ ├── lis.jpg │ └── memoization-12.png ├── 25 Recursion-3 ├── 25.01 Return All Subsequences Intro.ipynb ├── 25.02 Keypad combinations.ipynb ├── 25.03 Print the output instead of returning.ipynb ├── 25.04 Minimum of Array.ipynb ├── 25.05 Print All Subsequences.ipynb ├── 25.06 Print keypad combinations.ipynb ├── 25.07 Return subsets sum to K.ipynb ├── 25.07 return subset to sum k.txt ├── 25.08 Return Subsets.txt ├── 25.08 Return subsets.ipynb ├── 25.09 Return Permutations of a String.ipynb ├── 25.09 return all permutations.txt └── img │ ├── keypad.png │ ├── permutation recursion tree.gif │ └── substring and subsequence.jpg ├── 3 OOPS-1 ├── 3.01 Intro to OOPS.ipynb ├── 3.02 How to create classes & objects.ipynb ├── 3.03 Instance Attributes.ipynb ├── 3.04 Init Method.ipynb ├── 3.05 Instance Methods.ipynb ├── 3.06 Fraction Class - I.ipynb ├── 3.07 Fraction Class - II.ipynb ├── 3.08 Fraction Class = III.ipynb ├── 3.09 Complex Number.ipynb ├── 3.10 Complex Number Addition, Multiplication and Conjunction.ipynb └── 3.11 Complex Number.ipynb ├── 4 OOPS-2 ├── 4.1 [OOPS - 2] Inheritance.ipynb ├── 4.2 Inheritance and Private Members.ipynb ├── 4.3 Inheritance cont.....ipynb ├── 4.4 Polymorphism (Method overriding).ipynb ├── 4.5 Protected Members.ipynb ├── 4.6 Object class.ipynb ├── 4.7 Multiple Inheritance.ipynb ├── 4.8 Method Resolution Order.ipynb └── 4.9 Operator Overloading.ipynb ├── 5 OOPS-3 ├── 5.1 Abstract Classes.ipynb ├── 5.2 Abstract class cont.....ipynb ├── 5.3 Errors and Exceptions.ipynb ├── 5.4 Intro to Exception Handling.ipynb ├── 5.5 Handling Multiple Exceptions.ipynb ├── 5.6 Custom Exceptions (Own Exceptions).ipynb ├── 5.7 Except Functionality.ipynb ├── 5.8 Else and Finally.ipynb └── 5.9 More About Finally.ipynb ├── 6 Time Complexity ├── 6.1 Time Complexity Analysis.ipynb └── 6.2 Excercises.ipynb ├── 7 Space Complexity └── Space Complexity Analysis.ipynb ├── 8 Time Complexity Improvement ├── 8.1 Time Complexity Improvement - power of a number (x^n).ipynb ├── 8.2 Array Intersection.ipynb ├── 8.3 Equilibrium Index.ipynb ├── 8.4 Find a unique number in a list.ipynb ├── 8.5 Duplicate element in an array.ipynb ├── 8.6 Pair sum in a list.ipynb ├── 8.7 Triplet Sum.ipynb └── 8.8 Rotate array.ipynb ├── 9 Linked List-1 ├── 9.01 Intro to Linked List.ipynb ├── 9.02 Creating a linked list by taking input from the user.ipynb ├── 9.03 Printing Linked List.ipynb ├── 9.04 Time Complexity for Taking Input .ipynb ├── 9.05 length of Linked List.ipynb ├── 9.06 Print ith node.ipynb ├── 9.07 Insert a node at ith position.ipynb ├── 9.08 Delete node.ipynb ├── 9.09 Length of a LL (Recursive).ipynb ├── 9.10 Insert at ith postition (Recursively).ipynb ├── 9.11 Deletion of a node using recursion.ipynb ├── 9.12 Find a node in LL.ipynb ├── 9.13 Append Last N elements to First.ipynb ├── 9.14 Eliminate Duplicates from LL.ipynb ├── 9.15 Print Reverse Linked List.ipynb └── 9.16 Palindrome Linked List.ipynb ├── README.md ├── _config.yml ├── docs ├── README.md └── [Heap] Time complexity of various data structures.docx ├── img ├── 1 Recursion Stack.png ├── 2 Graph.png ├── 3 BFS.png ├── Actual.JPG ├── README.md └── fibonacci_tree.png └── index.md /1 Recursion-1/2 Fibonacci Number.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Fibonacci Number using recursion:\n", 8 | "\n", 9 | "### fib(n) = fib(n-1) + fib(n-2)\n", 10 | "\n", 11 | "### Base case: fib(1) = 1" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 12, 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "def fib(n):\n", 21 | " if n == 1 or n == 2:\n", 22 | " return 1 \n", 23 | " fib_n_1 = fib(n-1)\n", 24 | " fib_n_2 = fib(n-2)\n", 25 | " return fib_n_1 + fib_n_2" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 13, 31 | "metadata": {}, 32 | "outputs": [ 33 | { 34 | "name": "stdout", 35 | "output_type": "stream", 36 | "text": [ 37 | "6\n", 38 | "2\n", 39 | "3\n", 40 | "2\n", 41 | "5\n", 42 | "2\n", 43 | "3\n", 44 | "8\n", 45 | "8\n" 46 | ] 47 | } 48 | ], 49 | "source": [ 50 | "n = int(input())\n", 51 | "print(fib(n))" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 2, 57 | "metadata": {}, 58 | "outputs": [ 59 | { 60 | "data": { 61 | "text/plain": [ 62 | "2" 63 | ] 64 | }, 65 | "execution_count": 2, 66 | "metadata": {}, 67 | "output_type": "execute_result" 68 | } 69 | ], 70 | "source": [ 71 | "def fib(n):\n", 72 | " if n == 1 or n == 2:\n", 73 | " return 1 \n", 74 | " return fib(n-1) + fib(n-2)\n", 75 | "fib(3)" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 3, 81 | "metadata": {}, 82 | "outputs": [ 83 | { 84 | "name": "stdout", 85 | "output_type": "stream", 86 | "text": [ 87 | "16\n" 88 | ] 89 | } 90 | ], 91 | "source": [ 92 | "#Predict the output:\n", 93 | "def fun(n):\n", 94 | " if(n == 4):\n", 95 | " return n\n", 96 | " else:\n", 97 | " return 2*fun(n+1)\n", 98 | "\n", 99 | "print(fun(2))" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": null, 105 | "metadata": {}, 106 | "outputs": [], 107 | "source": [] 108 | } 109 | ], 110 | "metadata": { 111 | "kernelspec": { 112 | "display_name": "Python 3", 113 | "language": "python", 114 | "name": "python3" 115 | }, 116 | "language_info": { 117 | "codemirror_mode": { 118 | "name": "ipython", 119 | "version": 3 120 | }, 121 | "file_extension": ".py", 122 | "mimetype": "text/x-python", 123 | "name": "python", 124 | "nbconvert_exporter": "python", 125 | "pygments_lexer": "ipython3", 126 | "version": "3.7.3" 127 | } 128 | }, 129 | "nbformat": 4, 130 | "nbformat_minor": 2 131 | } 132 | -------------------------------------------------------------------------------- /1 Recursion-1/5 Sum of Array.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "5\n", 13 | "1 2 3 4 5\n", 14 | "\n", 15 | "[1, 2, 3, 4, 5]\n", 16 | "\n", 17 | "[2, 3, 4, 5]\n", 18 | "\n", 19 | "[3, 4, 5]\n", 20 | "\n", 21 | "[4, 5]\n", 22 | "\n", 23 | "[5]\n", 24 | "\n", 25 | "[]\n", 26 | "15\n", 27 | "[1, 2, 3, 4, 5]\n" 28 | ] 29 | } 30 | ], 31 | "source": [ 32 | "def sumArray(arr): \n", 33 | " print(type(arr)) \n", 34 | " print(arr)\n", 35 | " if len(arr) == 0:\n", 36 | " return 0\n", 37 | " else:\n", 38 | " return arr[0] + sumArray(arr[1:])\n", 39 | " \n", 40 | "\n", 41 | "from sys import setrecursionlimit\n", 42 | "setrecursionlimit(11000)\n", 43 | "n=int(input())\n", 44 | "arr=list(int(i) for i in input().strip().split(' '))\n", 45 | "print(sumArray(arr))\n", 46 | "print(arr)\n" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "Note:- Here the sum is 15 and the list remained the same as [1,2,3,4,5] even after making a function call." 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": null, 59 | "metadata": {}, 60 | "outputs": [], 61 | "source": [] 62 | } 63 | ], 64 | "metadata": { 65 | "kernelspec": { 66 | "display_name": "Python 3", 67 | "language": "python", 68 | "name": "python3" 69 | }, 70 | "language_info": { 71 | "codemirror_mode": { 72 | "name": "ipython", 73 | "version": 3 74 | }, 75 | "file_extension": ".py", 76 | "mimetype": "text/x-python", 77 | "name": "python", 78 | "nbconvert_exporter": "python", 79 | "pygments_lexer": "ipython3", 80 | "version": "3.7.3" 81 | } 82 | }, 83 | "nbformat": 4, 84 | "nbformat_minor": 2 85 | } 86 | -------------------------------------------------------------------------------- /1 Recursion-1/6 Check Number in Array.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "def checkNumber(arr, x):\n", 10 | " # Please add your code here\n", 11 | " pass\n", 12 | "\n", 13 | "# Main\n", 14 | "from sys import setrecursionlimit\n", 15 | "setrecursionlimit(11000)\n", 16 | "n=int(input())\n", 17 | "arr=list(int(i) for i in input().strip().split(' '))\n", 18 | "x=int(input())\n", 19 | "if checkNumber(arr, x):\n", 20 | " print('true')\n", 21 | "else:\n", 22 | " print('false')\n" 23 | ] 24 | } 25 | ], 26 | "metadata": { 27 | "kernelspec": { 28 | "display_name": "Python 3", 29 | "language": "python", 30 | "name": "python3" 31 | }, 32 | "language_info": { 33 | "codemirror_mode": { 34 | "name": "ipython", 35 | "version": 3 36 | }, 37 | "file_extension": ".py", 38 | "mimetype": "text/x-python", 39 | "name": "python", 40 | "nbconvert_exporter": "python", 41 | "pygments_lexer": "ipython3", 42 | "version": "3.7.3" 43 | } 44 | }, 45 | "nbformat": 4, 46 | "nbformat_minor": 2 47 | } 48 | -------------------------------------------------------------------------------- /1 Recursion-1/8 Last Index of Number.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 13, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "#copy the smaller part\n", 10 | "def lastIndex(arr, x):\n", 11 | " l = len(arr)\n", 12 | " if l == 0:\n", 13 | " return -1\n", 14 | " \n", 15 | " smallerList = arr[1:]\n", 16 | " smallerListOutput = lastIndex(smallerList, x)\n", 17 | " if smallerListOutput != -1:\n", 18 | " return smallerListOutput +1\n", 19 | " else:\n", 20 | " if arr[0] == x:\n", 21 | " return 0\n", 22 | " else:\n", 23 | " return -1" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 15, 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "name": "stdout", 33 | "output_type": "stream", 34 | "text": [ 35 | "7\n" 36 | ] 37 | } 38 | ], 39 | "source": [ 40 | "a = [1,2,3,4,1,2,3,4]\n", 41 | "x = 4\n", 42 | "print(lastIndex(a,x))" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 21, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [ 51 | "# Using starting index\n", 52 | "\n", 53 | "def lastIndexBetter(arr, si, x):\n", 54 | " l = len(arr)\n", 55 | " if si == l:\n", 56 | " return -1\n", 57 | " smallerListOutput = lastIndexBetter(arr, si+1, x)\n", 58 | " if smallerListOutput != -1:\n", 59 | " return smallerListOutput \n", 60 | " else:\n", 61 | " if arr[si] == x:\n", 62 | " return si\n", 63 | " else:\n", 64 | " return -1" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 24, 70 | "metadata": {}, 71 | "outputs": [ 72 | { 73 | "name": "stdout", 74 | "output_type": "stream", 75 | "text": [ 76 | "-1\n" 77 | ] 78 | } 79 | ], 80 | "source": [ 81 | "a = [1,2,3,4,1,2,3,4]\n", 82 | "x = 15\n", 83 | "print(lastIndexBetter(a,0,x))" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": null, 89 | "metadata": {}, 90 | "outputs": [], 91 | "source": [] 92 | } 93 | ], 94 | "metadata": { 95 | "kernelspec": { 96 | "display_name": "Python 3", 97 | "language": "python", 98 | "name": "python3" 99 | }, 100 | "language_info": { 101 | "codemirror_mode": { 102 | "name": "ipython", 103 | "version": 3 104 | }, 105 | "file_extension": ".py", 106 | "mimetype": "text/x-python", 107 | "name": "python", 108 | "nbconvert_exporter": "python", 109 | "pygments_lexer": "ipython3", 110 | "version": "3.7.3" 111 | } 112 | }, 113 | "nbformat": 4, 114 | "nbformat_minor": 2 115 | } 116 | -------------------------------------------------------------------------------- /10 Linked List-2/10.01 Reverse LL - O(N^2) [Recursive].ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Reversing a LL, not printing a LL in reverse order but we're actually reversing the LL\n", 8 | "#### For an example:- If LL is 1-->2-->3-->4-->5-->None then the output should be 5-->4-->3-->2-->1-->None " 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 20, 14 | "metadata": {}, 15 | "outputs": [ 16 | { 17 | "name": "stdout", 18 | "output_type": "stream", 19 | "text": [ 20 | "1 --> 2 --> 3 --> 4 --> 5 --> None\n", 21 | "5 --> 4 --> 3 --> 2 --> 1 --> None\n" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "class Node:\n", 27 | " def __init__(self, data):\n", 28 | " self.data = data\n", 29 | " self.next = None\n", 30 | " \n", 31 | " \n", 32 | "def printll(head):\n", 33 | " while head is not None:\n", 34 | " print(head.data, \"-->\", end = \" \")\n", 35 | " head = head.next\n", 36 | " print(\"None\")\n", 37 | " \n", 38 | "def ll(arr):\n", 39 | " if len(arr) == 0:\n", 40 | " return None\n", 41 | " head = Node(arr[0])\n", 42 | " last = head\n", 43 | " for data in arr[1:]:\n", 44 | " last.next = Node(data)\n", 45 | " last = last.next\n", 46 | " return head \n", 47 | "\n", 48 | "def reversell(head):\n", 49 | " \n", 50 | " if head is None or head.next is None:\n", 51 | " return head\n", 52 | " \n", 53 | " smallhead = reversell(head.next) \n", 54 | " curr = smallhead\n", 55 | " \n", 56 | " while curr.next is not None:\n", 57 | " curr = curr.next\n", 58 | " \n", 59 | " curr.next = head\n", 60 | " head.next = None\n", 61 | " return smallhead\n", 62 | "\n", 63 | "#arr = list(int(x) for x in input().split())\n", 64 | "arr = [1,2,3,4,5,-1]\n", 65 | "l = ll(arr[:-1])\n", 66 | "printll(l)\n", 67 | "\n", 68 | "x = reversell(l)\n", 69 | "printll(x)" 70 | ] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "metadata": {}, 75 | "source": [ 76 | "#### Time Complexity is O(N^2)" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": null, 82 | "metadata": {}, 83 | "outputs": [], 84 | "source": [] 85 | } 86 | ], 87 | "metadata": { 88 | "kernelspec": { 89 | "display_name": "Python 3", 90 | "language": "python", 91 | "name": "python3" 92 | }, 93 | "language_info": { 94 | "codemirror_mode": { 95 | "name": "ipython", 96 | "version": 3 97 | }, 98 | "file_extension": ".py", 99 | "mimetype": "text/x-python", 100 | "name": "python", 101 | "nbconvert_exporter": "python", 102 | "pygments_lexer": "ipython3", 103 | "version": "3.7.3" 104 | } 105 | }, 106 | "nbformat": 4, 107 | "nbformat_minor": 2 108 | } 109 | -------------------------------------------------------------------------------- /10 Linked List-2/10.02 Reverse LL - 2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Maintain tail as well to reduce the time complexity from O(N^2) to O(N)" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 2, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "name": "stdout", 17 | "output_type": "stream", 18 | "text": [ 19 | "1 --> 2 --> 3 --> 4 --> 5 --> None\n", 20 | "5 --> 4 --> 3 --> 2 --> 1 --> None\n" 21 | ] 22 | } 23 | ], 24 | "source": [ 25 | "class Node:\n", 26 | " def __init__(self, data):\n", 27 | " self.data = data\n", 28 | " self.next = None\n", 29 | " \n", 30 | " \n", 31 | "def printll(head):\n", 32 | " while head is not None:\n", 33 | " print(head.data, \"-->\", end = \" \")\n", 34 | " head = head.next\n", 35 | " print(\"None\")\n", 36 | " \n", 37 | "def ll(arr):\n", 38 | " if len(arr) == 0:\n", 39 | " return None\n", 40 | " head = Node(arr[0])\n", 41 | " last = head\n", 42 | " for data in arr[1:]:\n", 43 | " last.next = Node(data)\n", 44 | " last = last.next\n", 45 | " return head \n", 46 | "\n", 47 | "\n", 48 | "def reverse2(head):\n", 49 | " if head is None or head.next is None:\n", 50 | " return head, head\n", 51 | " smallHead, smallTail = reverse2(head.next) # Getting head and tail of the reversed LL\n", 52 | " smallTail.next = head\n", 53 | " head.next = None\n", 54 | " \n", 55 | " return smallHead, head \n", 56 | "\n", 57 | "#arr = list(int(x) for x in input().split())\n", 58 | "arr = [1,2,3,4,5,-1]\n", 59 | "l = ll(arr[:-1])\n", 60 | "printll(l)\n", 61 | "\n", 62 | "head, tail = reverse2(l)\n", 63 | "printll(head)" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": null, 69 | "metadata": {}, 70 | "outputs": [], 71 | "source": [] 72 | } 73 | ], 74 | "metadata": { 75 | "kernelspec": { 76 | "display_name": "Python 3", 77 | "language": "python", 78 | "name": "python3" 79 | }, 80 | "language_info": { 81 | "codemirror_mode": { 82 | "name": "ipython", 83 | "version": 3 84 | }, 85 | "file_extension": ".py", 86 | "mimetype": "text/x-python", 87 | "name": "python", 88 | "nbconvert_exporter": "python", 89 | "pygments_lexer": "ipython3", 90 | "version": "3.7.3" 91 | } 92 | }, 93 | "nbformat": 4, 94 | "nbformat_minor": 2 95 | } 96 | -------------------------------------------------------------------------------- /10 Linked List-2/10.03 Reverse LL - 3 [Best].ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### This is the best way to create a reverse LL" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 2, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "name": "stdout", 17 | "output_type": "stream", 18 | "text": [ 19 | "1 --> 2 --> 3 --> 4 --> 5 --> None\n", 20 | "5 --> 4 --> 3 --> 2 --> 1 --> None\n" 21 | ] 22 | } 23 | ], 24 | "source": [ 25 | "class Node:\n", 26 | " def __init__(self, data):\n", 27 | " self.data = data\n", 28 | " self.next = None\n", 29 | " \n", 30 | " \n", 31 | "def printll(head):\n", 32 | " while head is not None:\n", 33 | " print(head.data, \"-->\", end = \" \")\n", 34 | " head = head.next\n", 35 | " print(\"None\")\n", 36 | " \n", 37 | "def ll(arr):\n", 38 | " if len(arr) == 0:\n", 39 | " return None\n", 40 | " head = Node(arr[0])\n", 41 | " last = head\n", 42 | " for data in arr[1:]:\n", 43 | " last.next = Node(data)\n", 44 | " last = last.next\n", 45 | " return head \n", 46 | "\n", 47 | "def reverse3(head): \n", 48 | " if head is None or head.next is None:\n", 49 | " return head\n", 50 | " smallHead = reverse3(head.next)\n", 51 | " # tail = head.next\n", 52 | " # tail.next = head\n", 53 | " # The above 3 lines can be written in one line as the following\n", 54 | " head.next.next = head\n", 55 | " head.next = None\n", 56 | " \n", 57 | " return smallHead\n", 58 | "\n", 59 | "\n", 60 | "#arr = list(int(x) for x in input().split())\n", 61 | "arr = [1,2,3,4,5,-1]\n", 62 | "l = ll(arr[:-1])\n", 63 | "printll(l)\n", 64 | "\n", 65 | "head = reverse3(l)\n", 66 | "printll(head)" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": null, 72 | "metadata": {}, 73 | "outputs": [], 74 | "source": [] 75 | } 76 | ], 77 | "metadata": { 78 | "kernelspec": { 79 | "display_name": "Python 3", 80 | "language": "python", 81 | "name": "python3" 82 | }, 83 | "language_info": { 84 | "codemirror_mode": { 85 | "name": "ipython", 86 | "version": 3 87 | }, 88 | "file_extension": ".py", 89 | "mimetype": "text/x-python", 90 | "name": "python", 91 | "nbconvert_exporter": "python", 92 | "pygments_lexer": "ipython3", 93 | "version": "3.7.3" 94 | } 95 | }, 96 | "nbformat": 4, 97 | "nbformat_minor": 2 98 | } 99 | -------------------------------------------------------------------------------- /10 Linked List-2/10.04 Reverse LL (Iterative).ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 5, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "1 --> 2 --> 3 --> 4 --> 5 --> None\n", 13 | "5 --> 4 --> 3 --> 2 --> 1 --> None\n" 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "class Node:\n", 19 | " def __init__(self, data):\n", 20 | " self.data = data\n", 21 | " self.next = None\n", 22 | " \n", 23 | " \n", 24 | "def printll(head):\n", 25 | " while head is not None:\n", 26 | " print(head.data, \"-->\", end = \" \")\n", 27 | " head = head.next\n", 28 | " print(\"None\")\n", 29 | " \n", 30 | "def ll(arr):\n", 31 | " if len(arr) == 0:\n", 32 | " return None\n", 33 | " head = Node(arr[0])\n", 34 | " last = head\n", 35 | " for data in arr[1:]:\n", 36 | " last.next = Node(data)\n", 37 | " last = last.next\n", 38 | " return head \n", 39 | "\n", 40 | "def reverse(head): \n", 41 | " curr = head\n", 42 | " prev = None\n", 43 | " while curr is not None:\n", 44 | " next = curr.next\n", 45 | " curr.next = prev\n", 46 | " prev = curr\n", 47 | " curr = next\n", 48 | " head = prev \n", 49 | " return head\n", 50 | "\n", 51 | "\n", 52 | "#arr = list(int(x) for x in input().split())\n", 53 | "arr = [1,2,3,4,5,-1]\n", 54 | "l = ll(arr[:-1])\n", 55 | "printll(l)\n", 56 | "\n", 57 | "head = reverse(l)\n", 58 | "printll(head)" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": null, 64 | "metadata": {}, 65 | "outputs": [], 66 | "source": [] 67 | } 68 | ], 69 | "metadata": { 70 | "kernelspec": { 71 | "display_name": "Python 3", 72 | "language": "python", 73 | "name": "python3" 74 | }, 75 | "language_info": { 76 | "codemirror_mode": { 77 | "name": "ipython", 78 | "version": 3 79 | }, 80 | "file_extension": ".py", 81 | "mimetype": "text/x-python", 82 | "name": "python", 83 | "nbconvert_exporter": "python", 84 | "pygments_lexer": "ipython3", 85 | "version": "3.7.3" 86 | } 87 | }, 88 | "nbformat": 4, 89 | "nbformat_minor": 2 90 | } 91 | -------------------------------------------------------------------------------- /10 Linked List-2/10.05 Mid Point of a Linked List.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "#### Midpoint = (length - 1)/2" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "#### Maintain two pointers fast and slow where fast goes (2 x speed) of slow. When fast will reach the end then the slow will reach the midpoint.\n", 15 | "\n", 16 | "When fast.next.next == null (for even case) and fast.next == null (for odd case) then we need to stop till then we need to increment slow = slow.next and fast = fast.next.next. && which keeps on iterating till the end. || doesn't works as the even condition fails.\n", 17 | "** How can we reach the midpoint in one pass?" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 5, 23 | "metadata": {}, 24 | "outputs": [ 25 | { 26 | "name": "stdout", 27 | "output_type": "stream", 28 | "text": [ 29 | "1 2 3 4 5 6 -1\n", 30 | "3\n" 31 | ] 32 | } 33 | ], 34 | "source": [ 35 | "class Node:\n", 36 | " def __init__(self, data):\n", 37 | " self.data = data\n", 38 | " self.next = None\n", 39 | "\n", 40 | "def midpoint_linkedlist(head):\n", 41 | " slow = head\n", 42 | " fast = head\n", 43 | " while fast.next != None and fast.next.next != None:\n", 44 | " slow = slow.next\n", 45 | " fast = fast.next.next\n", 46 | " \n", 47 | " return slow\n", 48 | "\n", 49 | "def ll(arr):\n", 50 | " if len(arr)==0:\n", 51 | " return None\n", 52 | " head = Node(arr[0])\n", 53 | " last = head\n", 54 | " for data in arr[1:]:\n", 55 | " last.next = Node(data)\n", 56 | " last = last.next\n", 57 | " return head\n", 58 | "\n", 59 | "# Main\n", 60 | "# Read the link list elements including -1\n", 61 | "arr=list(int(i) for i in input().strip().split(' '))\n", 62 | "# Create a Linked list after removing -1 from list\n", 63 | "l = ll(arr[:-1])\n", 64 | "node = midpoint_linkedlist(l)\n", 65 | "if node:\n", 66 | " print(node.data)\n" 67 | ] 68 | } 69 | ], 70 | "metadata": { 71 | "kernelspec": { 72 | "display_name": "Python 3", 73 | "language": "python", 74 | "name": "python3" 75 | }, 76 | "language_info": { 77 | "codemirror_mode": { 78 | "name": "ipython", 79 | "version": 3 80 | }, 81 | "file_extension": ".py", 82 | "mimetype": "text/x-python", 83 | "name": "python", 84 | "nbconvert_exporter": "python", 85 | "pygments_lexer": "ipython3", 86 | "version": "3.7.3" 87 | } 88 | }, 89 | "nbformat": 4, 90 | "nbformat_minor": 2 91 | } 92 | -------------------------------------------------------------------------------- /10 Linked List-2/10.09 Find a node in LL (recursive).ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 16, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "2 6 5 3 -1\n", 13 | "23\n", 14 | "-1\n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "class Node:\n", 20 | " def __init__(self, data):\n", 21 | " self.data = data\n", 22 | " self.next = None\n", 23 | "\n", 24 | "def linearSearchRecursive(head, n):\n", 25 | " \n", 26 | " if head is None:\n", 27 | " return -1\n", 28 | " if head.data == n:\n", 29 | " return 0\n", 30 | " \n", 31 | " value = linearSearchRecursive(head.next, n) \n", 32 | " \n", 33 | " if value == -1:\n", 34 | " return -1\n", 35 | " else:\n", 36 | " return 1 + value \n", 37 | "\n", 38 | "def ll(arr):\n", 39 | " if len(arr)==0:\n", 40 | " return None\n", 41 | " head = Node(arr[0])\n", 42 | " last = head\n", 43 | " for data in arr[1:]:\n", 44 | " last.next = Node(data)\n", 45 | " last = last.next\n", 46 | " return head\n", 47 | "\n", 48 | "from sys import setrecursionlimit\n", 49 | "setrecursionlimit(11000)\n", 50 | "arr=list(int(i) for i in input().strip().split(' '))\n", 51 | "l = ll(arr[:-1])\n", 52 | "data=int(input())\n", 53 | "index = linearSearchRecursive(l, data)\n", 54 | "print(index)\n" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": null, 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": null, 67 | "metadata": {}, 68 | "outputs": [], 69 | "source": [] 70 | } 71 | ], 72 | "metadata": { 73 | "kernelspec": { 74 | "display_name": "Python 3", 75 | "language": "python", 76 | "name": "python3" 77 | }, 78 | "language_info": { 79 | "codemirror_mode": { 80 | "name": "ipython", 81 | "version": 3 82 | }, 83 | "file_extension": ".py", 84 | "mimetype": "text/x-python", 85 | "name": "python", 86 | "nbconvert_exporter": "python", 87 | "pygments_lexer": "ipython3", 88 | "version": "3.7.3" 89 | } 90 | }, 91 | "nbformat": 4, 92 | "nbformat_minor": 2 93 | } 94 | -------------------------------------------------------------------------------- /10 Linked List-2/10.13 kReverse.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "1 2 3 4 5 6 7 8 9 10 -1\n", 13 | "4\n", 14 | "4 3 2 1 8 7 6 5 10 9 \n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "class Node:\n", 20 | " def __init__(self, data):\n", 21 | " self.data = data\n", 22 | " self.next = None\n", 23 | "\n", 24 | "def reverse(head):\n", 25 | " if head is None or head.next is None:\n", 26 | " return head, head\n", 27 | " smallHead, smallTail = reverse(head.next) \n", 28 | " smallTail.next = head\n", 29 | " head.next = None\n", 30 | " \n", 31 | " return smallHead, head \n", 32 | " \n", 33 | "def kReverse(head, n):\n", 34 | " \n", 35 | " if head is None or head.next is None or n <= 0:\n", 36 | " return head\n", 37 | " \n", 38 | " curr = head\n", 39 | " tail = head\n", 40 | "\n", 41 | " cnt = 1\n", 42 | " while tail.next is not None:\n", 43 | " if cnt == n:\n", 44 | " break\n", 45 | " cnt += 1\n", 46 | " tail = tail.next\n", 47 | " \n", 48 | " nextHead = tail.next\n", 49 | " tail.next = None\n", 50 | " newHead, newTail = reverse(curr)\n", 51 | " newTail.next = kReverse(nextHead, n)\n", 52 | " \n", 53 | " return newHead\n", 54 | "\n", 55 | "\n", 56 | "def ll(arr):\n", 57 | " if len(arr)==0:\n", 58 | " return None\n", 59 | " head = Node(arr[0])\n", 60 | " last = head\n", 61 | " for data in arr[1:]:\n", 62 | " last.next = Node(data)\n", 63 | " last = last.next\n", 64 | " return head\n", 65 | "\n", 66 | "def printll(head):\n", 67 | " while head:\n", 68 | " print(head.data, end=' ')\n", 69 | " head = head.next\n", 70 | " print()\n", 71 | "\n", 72 | "# Main\n", 73 | "# Read the link list elements including -1\n", 74 | "arr=list(int(i) for i in input().strip().split(' '))\n", 75 | "# Create a Linked list after removing -1 from list\n", 76 | "l = ll(arr[:-1])\n", 77 | "i=int(input())\n", 78 | "l = kReverse(l, i)\n", 79 | "printll(l)\n" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": null, 85 | "metadata": {}, 86 | "outputs": [], 87 | "source": [] 88 | } 89 | ], 90 | "metadata": { 91 | "kernelspec": { 92 | "display_name": "Python 3", 93 | "language": "python", 94 | "name": "python3" 95 | }, 96 | "language_info": { 97 | "codemirror_mode": { 98 | "name": "ipython", 99 | "version": 3 100 | }, 101 | "file_extension": ".py", 102 | "mimetype": "text/x-python", 103 | "name": "python", 104 | "nbconvert_exporter": "python", 105 | "pygments_lexer": "ipython3", 106 | "version": "3.7.3" 107 | } 108 | }, 109 | "nbformat": 4, 110 | "nbformat_minor": 2 111 | } 112 | -------------------------------------------------------------------------------- /11 Stacks/11.2 Stack Using Array.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "### Stack Using Array\n", 10 | "### This part is hidden from the User\n", 11 | "class Stack:\n", 12 | " def __init__(self):\n", 13 | " self.__data = [] #Private array: not to give access to the user\n", 14 | " \n", 15 | " def push(self, item):\n", 16 | " self.__data.append(item)\n", 17 | " \n", 18 | " def pop(self):\n", 19 | " if self.isEmpty():\n", 20 | " print(\"Stack is Empty!\")\n", 21 | " return\n", 22 | " return self.__data.pop()\n", 23 | " \n", 24 | " def top(self):\n", 25 | " if self.isEmpty():\n", 26 | " print(\"Stack is Empty!\")\n", 27 | " return\n", 28 | " return self.__data[len(self.__data) - 1]\n", 29 | " \n", 30 | " def size(self):\n", 31 | " return len(self.__data)\n", 32 | " \n", 33 | " def isEmpty(self):\n", 34 | " return self.size() == 0" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 2, 40 | "metadata": {}, 41 | "outputs": [ 42 | { 43 | "name": "stdout", 44 | "output_type": "stream", 45 | "text": [ 46 | "14\n", 47 | "13\n", 48 | "12\n", 49 | "Stack is Empty!\n" 50 | ] 51 | } 52 | ], 53 | "source": [ 54 | "#### This part is visible to the user\n", 55 | "## Actually we need to write the above program in another fila and here in the below code we can import the Stack class using import statement\n", 56 | "\n", 57 | "# from _11.2StackUsingArray import Stack\n", 58 | "\n", 59 | "s = Stack()\n", 60 | "\n", 61 | "s.push(12)\n", 62 | "s.push(13)\n", 63 | "s.push(14)\n", 64 | "\n", 65 | "while s.isEmpty() is False:\n", 66 | " print(s.pop())\n", 67 | "s.top() " 68 | ] 69 | } 70 | ], 71 | "metadata": { 72 | "kernelspec": { 73 | "display_name": "Python 3", 74 | "language": "python", 75 | "name": "python3" 76 | }, 77 | "language_info": { 78 | "codemirror_mode": { 79 | "name": "ipython", 80 | "version": 3 81 | }, 82 | "file_extension": ".py", 83 | "mimetype": "text/x-python", 84 | "name": "python", 85 | "nbconvert_exporter": "python", 86 | "pygments_lexer": "ipython3", 87 | "version": "3.7.3" 88 | } 89 | }, 90 | "nbformat": 4, 91 | "nbformat_minor": 2 92 | } 93 | -------------------------------------------------------------------------------- /11 Stacks/11.7 Check Redundant Brackets.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "(a+b) is not redundant ---- False\n", 8 | "\n", 9 | "((a+b)) is redendant ---- False\n", 10 | "\n", 11 | "(a+(b+c)) is not redundant- False \n", 12 | "\n", 13 | "Is there any useful information between a pair of brackets then it is not redundant else it's redundant." 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 1, 19 | "metadata": {}, 20 | "outputs": [ 21 | { 22 | "name": "stdout", 23 | "output_type": "stream", 24 | "text": [ 25 | "(a + (b - c))\n", 26 | "false\n" 27 | ] 28 | } 29 | ], 30 | "source": [ 31 | "import queue\n", 32 | "\n", 33 | "def checkRedundant(string):\n", 34 | " q=queue.LifoQueue()\n", 35 | " temp=0\n", 36 | " l=len(string)\n", 37 | " for i in range(l-1):\n", 38 | " c=string[i]\n", 39 | " n=string[i+1]\n", 40 | " if c=='(' and n==')':\n", 41 | " return True\n", 42 | " elif c=='(' and n=='(':\n", 43 | " temp=2\n", 44 | " elif c==')' and n==')' and temp==2:\n", 45 | " return True\n", 46 | " return False\n", 47 | "#### Implement Your Code Here\n", 48 | " \n", 49 | "\n", 50 | "string = input()\n", 51 | "ans = checkRedundant(string)\n", 52 | "if ans is True:\n", 53 | " print('true')\n", 54 | "else:\n", 55 | " print('false')" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": null, 61 | "metadata": {}, 62 | "outputs": [], 63 | "source": [] 64 | } 65 | ], 66 | "metadata": { 67 | "kernelspec": { 68 | "display_name": "Python 3", 69 | "language": "python", 70 | "name": "python3" 71 | }, 72 | "language_info": { 73 | "codemirror_mode": { 74 | "name": "ipython", 75 | "version": 3 76 | }, 77 | "file_extension": ".py", 78 | "mimetype": "text/x-python", 79 | "name": "python", 80 | "nbconvert_exporter": "python", 81 | "pygments_lexer": "ipython3", 82 | "version": "3.7.3" 83 | } 84 | }, 85 | "nbformat": 4, 86 | "nbformat_minor": 2 87 | } 88 | -------------------------------------------------------------------------------- /11 Stacks/11.8 Stock Span.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "8\n", 13 | "60 70 80 100 90 75 80 120\n", 14 | "1 2 3 4 1 1 2 8 " 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "\n", 20 | "\n", 21 | "def stockSpan(price,S):\n", 22 | " n = len(price) \n", 23 | " st = [] \n", 24 | " st.append(0) \n", 25 | " S[0] = 1\n", 26 | " for i in range(1, n): \n", 27 | " while( len(st) > 0 and price[st[-1]] < price[i]): \n", 28 | " st.pop() \n", 29 | " \n", 30 | " S[i] = i + 1 if len(st) <= 0 else (i - st[-1]) \n", 31 | " st.append(i) \n", 32 | " \n", 33 | "def printArray(arr, n): \n", 34 | " for i in range(0, n): \n", 35 | " print (arr[i], end =\" \") \n", 36 | " \n", 37 | " \n", 38 | "n = int(input())\n", 39 | "price = list(int(x) for x in input().split()[:n]) \n", 40 | "\n", 41 | "S = [0 for i in range(len(price)+1)] \n", 42 | " \n", 43 | "stockSpan(price, S) \n", 44 | " \n", 45 | "printArray(S, len(price)) \n", 46 | "\n", 47 | "\n", 48 | "\n", 49 | "\n", 50 | "\n" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": null, 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [] 59 | } 60 | ], 61 | "metadata": { 62 | "kernelspec": { 63 | "display_name": "Python 3", 64 | "language": "python", 65 | "name": "python3" 66 | }, 67 | "language_info": { 68 | "codemirror_mode": { 69 | "name": "ipython", 70 | "version": 3 71 | }, 72 | "file_extension": ".py", 73 | "mimetype": "text/x-python", 74 | "name": "python", 75 | "nbconvert_exporter": "python", 76 | "pygments_lexer": "ipython3", 77 | "version": "3.7.3" 78 | } 79 | }, 80 | "nbformat": 4, 81 | "nbformat_minor": 2 82 | } 83 | -------------------------------------------------------------------------------- /11 Stacks/11.9 Minimum Bracket Reversal.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "{{{{}}\n", 13 | "1\n" 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "def countBracketReversals(string):\n", 19 | " if(len(string) == 0):\n", 20 | " return 0\n", 21 | " if(len(string)%2 != 0):\n", 22 | " return -1\n", 23 | " s = []\n", 24 | " for char in string:\n", 25 | " if char == '{':\n", 26 | " s.append(char)\n", 27 | " else:\n", 28 | " if(len(s) > 0 and s[-1] == '{'):\n", 29 | " s.pop()\n", 30 | " else:\n", 31 | " s.append(char)\n", 32 | " count = 0\n", 33 | " while(len(s) != 0):\n", 34 | " c1 = s.pop()\n", 35 | " c2 = s.pop()\n", 36 | " if c1!=c2:\n", 37 | " count+=2\n", 38 | " else:\n", 39 | " count+=1\n", 40 | " return count\n", 41 | "string = input()\n", 42 | "ans = countBracketReversals(string)\n", 43 | "print(ans)\n", 44 | "\n", 45 | "\n", 46 | "\n" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": null, 52 | "metadata": {}, 53 | "outputs": [], 54 | "source": [] 55 | } 56 | ], 57 | "metadata": { 58 | "kernelspec": { 59 | "display_name": "Python 3", 60 | "language": "python", 61 | "name": "python3" 62 | }, 63 | "language_info": { 64 | "codemirror_mode": { 65 | "name": "ipython", 66 | "version": 3 67 | }, 68 | "file_extension": ".py", 69 | "mimetype": "text/x-python", 70 | "name": "python", 71 | "nbconvert_exporter": "python", 72 | "pygments_lexer": "ipython3", 73 | "version": "3.7.3" 74 | } 75 | }, 76 | "nbformat": 4, 77 | "nbformat_minor": 2 78 | } 79 | -------------------------------------------------------------------------------- /12 Queue/12.1 Intro to Queue.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Queue: follows FIFO mechanism\n", 8 | "\n", 9 | "1) Enqueue() - Inserting an element to the queue\n", 10 | "\n", 11 | "2) Dequeue() - Deleting an element from the queue\n", 12 | "\n", 13 | "3) size() - returns the size of the queue\n", 14 | "\n", 15 | "4) isEmpty() - returns queue is empty or not\n", 16 | "\n", 17 | "5) front() - returns front element of queue\n", 18 | "\n", 19 | "**Elements are inserted at the rear part of the queue and elements are removed from the front end." 20 | ] 21 | } 22 | ], 23 | "metadata": { 24 | "kernelspec": { 25 | "display_name": "Python 3", 26 | "language": "python", 27 | "name": "python3" 28 | }, 29 | "language_info": { 30 | "codemirror_mode": { 31 | "name": "ipython", 32 | "version": 3 33 | }, 34 | "file_extension": ".py", 35 | "mimetype": "text/x-python", 36 | "name": "python", 37 | "nbconvert_exporter": "python", 38 | "pygments_lexer": "ipython3", 39 | "version": "3.7.3" 40 | } 41 | }, 42 | "nbformat": 4, 43 | "nbformat_minor": 2 44 | } 45 | -------------------------------------------------------------------------------- /12 Queue/12.2 Queue using Array.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# Whenever I deque then I +1 the front index so that Dequeue is acheived in O(1) time\n", 10 | "# For size maintain a count\n", 11 | "\n", 12 | "class QueueUsingArray:\n", 13 | " def __init__(self):\n", 14 | " self.__arr = []\n", 15 | " self.__count = 0\n", 16 | " self.__front = 0\n", 17 | " \n", 18 | " def enqueue(self, data):\n", 19 | " self.__arr.append(data)\n", 20 | " self.__count += 1\n", 21 | " \n", 22 | " def dequeue(self):\n", 23 | " if self.__count == 0:\n", 24 | " return -1\n", 25 | " \n", 26 | " element = self.__arr[self.__front]\n", 27 | " self.__front += 1\n", 28 | " self.__count -= 1\n", 29 | " return element\n", 30 | " \n", 31 | " def front(self):\n", 32 | " if self.__count == 0:\n", 33 | " return -1\n", 34 | " return self.__arr[self.__front]\n", 35 | " \n", 36 | " def size(self):\n", 37 | " return self.__count\n", 38 | " \n", 39 | " def isEmpty(self):\n", 40 | " return self.size() == 0" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 2, 46 | "metadata": {}, 47 | "outputs": [ 48 | { 49 | "name": "stdout", 50 | "output_type": "stream", 51 | "text": [ 52 | "1\n", 53 | "2\n", 54 | "3\n", 55 | "4\n", 56 | "-1\n" 57 | ] 58 | } 59 | ], 60 | "source": [ 61 | "q = QueueUsingArray()\n", 62 | "\n", 63 | "q.enqueue(1)\n", 64 | "q.enqueue(2)\n", 65 | "q.enqueue(3)\n", 66 | "q.enqueue(4)\n", 67 | "\n", 68 | "while q.isEmpty() is False:\n", 69 | " print(q.front())\n", 70 | " q.dequeue()\n", 71 | " \n", 72 | "print(q.dequeue())" 73 | ] 74 | } 75 | ], 76 | "metadata": { 77 | "kernelspec": { 78 | "display_name": "Python 3", 79 | "language": "python", 80 | "name": "python3" 81 | }, 82 | "language_info": { 83 | "codemirror_mode": { 84 | "name": "ipython", 85 | "version": 3 86 | }, 87 | "file_extension": ".py", 88 | "mimetype": "text/x-python", 89 | "name": "python", 90 | "nbconvert_exporter": "python", 91 | "pygments_lexer": "ipython3", 92 | "version": "3.7.3" 93 | } 94 | }, 95 | "nbformat": 4, 96 | "nbformat_minor": 2 97 | } 98 | -------------------------------------------------------------------------------- /12 Queue/12.4 Inbuilt Stacks and Queues.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### For inbuilt stack we can use list" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 8, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "name": "stdout", 17 | "output_type": "stream", 18 | "text": [ 19 | "5\n", 20 | "4\n" 21 | ] 22 | } 23 | ], 24 | "source": [ 25 | "# inbuilt stack as list\n", 26 | "s = [1,2,3]\n", 27 | "\n", 28 | "s.append(4)\n", 29 | "s.append(5)\n", 30 | "\n", 31 | "print(s.pop())\n", 32 | "print(s.pop())" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": {}, 38 | "source": [ 39 | "#### We shouldn't use list for queue in Python instead we've an inbuilt library queue from Python 3 onwards.\n" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 5, 45 | "metadata": {}, 46 | "outputs": [ 47 | { 48 | "name": "stdout", 49 | "output_type": "stream", 50 | "text": [ 51 | "1\n", 52 | "2\n", 53 | "3\n", 54 | "4\n" 55 | ] 56 | } 57 | ], 58 | "source": [ 59 | "# Inbuilt Queue Library\n", 60 | "\n", 61 | "import queue # importing queue inbuilt library\n", 62 | "q = queue.Queue()\n", 63 | "\n", 64 | "q.put(1) # put enqueues the element \n", 65 | "q.put(2)\n", 66 | "q.put(3)\n", 67 | "q.put(4)\n", 68 | "\n", 69 | "while not q.empty():\n", 70 | " print(q.get()) # get dequeue the element" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 2, 76 | "metadata": {}, 77 | "outputs": [ 78 | { 79 | "name": "stdout", 80 | "output_type": "stream", 81 | "text": [ 82 | "3\n", 83 | "2\n", 84 | "1\n" 85 | ] 86 | } 87 | ], 88 | "source": [ 89 | "# Inbuilt Stack Library\n", 90 | "import queue\n", 91 | "q = queue.LifoQueue() # LifoQueue class is actually a stack\n", 92 | "q.put(1)\n", 93 | "q.put(2)\n", 94 | "q.put(3)\n", 95 | "\n", 96 | "while not q.empty():\n", 97 | " print(q.get())" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": null, 103 | "metadata": {}, 104 | "outputs": [], 105 | "source": [] 106 | } 107 | ], 108 | "metadata": { 109 | "kernelspec": { 110 | "display_name": "Python 3", 111 | "language": "python", 112 | "name": "python3" 113 | }, 114 | "language_info": { 115 | "codemirror_mode": { 116 | "name": "ipython", 117 | "version": 3 118 | }, 119 | "file_extension": ".py", 120 | "mimetype": "text/x-python", 121 | "name": "python", 122 | "nbconvert_exporter": "python", 123 | "pygments_lexer": "ipython3", 124 | "version": "3.7.3" 125 | } 126 | }, 127 | "nbformat": 4, 128 | "nbformat_minor": 2 129 | } 130 | -------------------------------------------------------------------------------- /13 Trees/13.07 Node with Largest Data.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Return the node with largest number:" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 4, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "name": "stdout", 17 | "output_type": "stream", 18 | "text": [ 19 | "8\n", 20 | "3\n", 21 | "10\n", 22 | "1\n", 23 | "6\n", 24 | "-1\n", 25 | "14\n", 26 | "-1\n", 27 | "-1\n", 28 | "4\n", 29 | "7\n", 30 | "13\n", 31 | "-1\n", 32 | "-1\n", 33 | "-1\n", 34 | "-1\n", 35 | "-1\n", 36 | "-1\n", 37 | "-1\n", 38 | "14\n" 39 | ] 40 | } 41 | ], 42 | "source": [ 43 | "class BinaryTreeNode:\n", 44 | " def __init__(self, data):\n", 45 | " self.data = data\n", 46 | " self.left = None\n", 47 | " self.right = None\n", 48 | " \n", 49 | "def treeInput():\n", 50 | " \n", 51 | " rootData = int(input())\n", 52 | " if rootData == -1:\n", 53 | " return None\n", 54 | " \n", 55 | " root = BinaryTreeNode(rootData)\n", 56 | " root.left = treeInput()\n", 57 | " root.right = treeInput()\n", 58 | "\n", 59 | " return root\n", 60 | "\n", 61 | "def printTreeDetailed(root):\n", 62 | " if root == None:\n", 63 | " return\n", 64 | " print(root.data, end = \":\")\n", 65 | " \n", 66 | " if root.left is not None:\n", 67 | " print(\"L\", root.left.data, end = \",\")\n", 68 | " \n", 69 | " if root.right is not None:\n", 70 | " print(\"R\", root.right.data, end = \" \")\n", 71 | " \n", 72 | " print()\n", 73 | " printTreeDetailed(root.left)\n", 74 | " printTreeDetailed(root.right)\n", 75 | " \n", 76 | "def findLargest(root):\n", 77 | " if root == None:\n", 78 | " return -1 # Ideally return -infinity \n", 79 | " \n", 80 | " leftLargest = findLargest(root.left)\n", 81 | " rightLargest = findLargest(root.right)\n", 82 | " \n", 83 | " m = max(root.data, leftLargest, rightLargest) # Max functions works for 3 numbers as well\n", 84 | " \n", 85 | " return m\n", 86 | "\n", 87 | "root = treeInput()\n", 88 | "print(findLargest(root))\n", 89 | " \n", 90 | " " 91 | ] 92 | } 93 | ], 94 | "metadata": { 95 | "kernelspec": { 96 | "display_name": "Python 3", 97 | "language": "python", 98 | "name": "python3" 99 | }, 100 | "language_info": { 101 | "codemirror_mode": { 102 | "name": "ipython", 103 | "version": 3 104 | }, 105 | "file_extension": ".py", 106 | "mimetype": "text/x-python", 107 | "name": "python", 108 | "nbconvert_exporter": "python", 109 | "pygments_lexer": "ipython3", 110 | "version": "3.7.3" 111 | } 112 | }, 113 | "nbformat": 4, 114 | "nbformat_minor": 2 115 | } 116 | -------------------------------------------------------------------------------- /13 Trees/13.08 Nodes Greater Than X.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 16, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "8 3 10 1 6 -1 14 -1 -1 4 7 13 -1 -1 -1 -1 -1 -1 -1\n", 13 | "8\n", 14 | "3\n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "import queue\n", 20 | "class BinaryTreeNode:\n", 21 | " def __init__(self, data):\n", 22 | " self.data = data\n", 23 | " self.left = None\n", 24 | " self.right = None\n", 25 | "\n", 26 | "def countNodesGreaterThanX(root, x):\n", 27 | " if root == None:\n", 28 | " return 0\n", 29 | " \n", 30 | " leftX = countNodesGreaterThanX(root.left, x)\n", 31 | " rightX = countNodesGreaterThanX(root.right, x)\n", 32 | " \n", 33 | " if root.data > x:\n", 34 | " return 1 + leftX + rightX\n", 35 | " return leftX + rightX \n", 36 | " \n", 37 | "def buildLevelTree(levelorder):\n", 38 | " index = 0\n", 39 | " length = len(levelorder)\n", 40 | " if length<=0 or levelorder[0]==-1:\n", 41 | " return None\n", 42 | " root = BinaryTreeNode(levelorder[index])\n", 43 | " index += 1\n", 44 | " q = queue.Queue()\n", 45 | " q.put(root)\n", 46 | " while not q.empty():\n", 47 | " currentNode = q.get()\n", 48 | " leftChild = levelorder[index]\n", 49 | " index += 1\n", 50 | " if leftChild != -1:\n", 51 | " leftNode = BinaryTreeNode(leftChild)\n", 52 | " currentNode.left =leftNode\n", 53 | " q.put(leftNode)\n", 54 | " rightChild = levelorder[index]\n", 55 | " index += 1\n", 56 | " if rightChild != -1:\n", 57 | " rightNode = BinaryTreeNode(rightChild)\n", 58 | " currentNode.right =rightNode\n", 59 | " q.put(rightNode)\n", 60 | " return root\n", 61 | "\n", 62 | "# Main\n", 63 | "levelOrder = [int(i) for i in input().strip().split()]\n", 64 | "x=int(input())\n", 65 | "root = buildLevelTree(levelOrder)\n", 66 | "print(countNodesGreaterThanX(root, x))\n" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": null, 72 | "metadata": {}, 73 | "outputs": [], 74 | "source": [] 75 | } 76 | ], 77 | "metadata": { 78 | "kernelspec": { 79 | "display_name": "Python 3", 80 | "language": "python", 81 | "name": "python3" 82 | }, 83 | "language_info": { 84 | "codemirror_mode": { 85 | "name": "ipython", 86 | "version": 3 87 | }, 88 | "file_extension": ".py", 89 | "mimetype": "text/x-python", 90 | "name": "python", 91 | "nbconvert_exporter": "python", 92 | "pygments_lexer": "ipython3", 93 | "version": "3.7.3" 94 | } 95 | }, 96 | "nbformat": 4, 97 | "nbformat_minor": 2 98 | } 99 | -------------------------------------------------------------------------------- /13 Trees/13.09 Height of a tree.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "None tree has a height 0\n", 8 | "\n", 9 | "Single node tree has a height 1" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "10\n", 22 | "9\n", 23 | "4\n", 24 | "-1\n", 25 | "-1\n", 26 | "5\n", 27 | "8\n", 28 | "-1\n", 29 | "6\n", 30 | "-1\n", 31 | "-1\n", 32 | "3\n", 33 | "-1\n", 34 | "-1\n", 35 | "-1\n", 36 | "5\n" 37 | ] 38 | } 39 | ], 40 | "source": [ 41 | "import queue\n", 42 | "class BinaryTreeNode:\n", 43 | " def __init__(self, data):\n", 44 | " self.data = data\n", 45 | " self.left = None\n", 46 | " self.right = None\n", 47 | "\n", 48 | "def height(root):\n", 49 | " if root == None:\n", 50 | " return 0\n", 51 | " \n", 52 | " leftHeight = height(root.left)\n", 53 | " rightHeight = height(root.right)\n", 54 | " \n", 55 | " return 1 + max(leftHeight, rightHeight) \n", 56 | " \n", 57 | "\n", 58 | "def buildLevelTree():\n", 59 | " \n", 60 | " root = BinaryTreeNode(int(input()))\n", 61 | " q = queue.Queue()\n", 62 | " q.put(root)\n", 63 | " while not q.empty():\n", 64 | " currentNode = q.get()\n", 65 | " leftChild = int(input())\n", 66 | " if leftChild != -1:\n", 67 | " leftNode = BinaryTreeNode(leftChild)\n", 68 | " currentNode.left =leftNode\n", 69 | " q.put(leftNode)\n", 70 | " rightChild = int(input())\n", 71 | " if rightChild != -1:\n", 72 | " rightNode = BinaryTreeNode(rightChild)\n", 73 | " currentNode.right =rightNode\n", 74 | " q.put(rightNode)\n", 75 | " return root\n", 76 | "\n", 77 | "# Main\n", 78 | "root = buildLevelTree()\n", 79 | "print(height(root))\n", 80 | "\n" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": null, 86 | "metadata": {}, 87 | "outputs": [], 88 | "source": [] 89 | } 90 | ], 91 | "metadata": { 92 | "kernelspec": { 93 | "display_name": "Python 3", 94 | "language": "python", 95 | "name": "python3" 96 | }, 97 | "language_info": { 98 | "codemirror_mode": { 99 | "name": "ipython", 100 | "version": 3 101 | }, 102 | "file_extension": ".py", 103 | "mimetype": "text/x-python", 104 | "name": "python", 105 | "nbconvert_exporter": "python", 106 | "pygments_lexer": "ipython3", 107 | "version": "3.7.3" 108 | } 109 | }, 110 | "nbformat": 4, 111 | "nbformat_minor": 2 112 | } 113 | -------------------------------------------------------------------------------- /13 Trees/13.13 Is node present.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "8 3 10 1 6 -1 14 -1 -1 4 7 13 -1 -1 -1 -1 -1 -1 -1\n", 13 | "7\n", 14 | "true\n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "import queue\n", 20 | "class BinaryTreeNode:\n", 21 | " def __init__(self, data):\n", 22 | " self.data = data\n", 23 | " self.left = None\n", 24 | " self.right = None\n", 25 | "\n", 26 | "def isNodePresent(root, x):\n", 27 | " if root == None:\n", 28 | " return \n", 29 | " if root.data == x:\n", 30 | " return True\n", 31 | " left = isNodePresent(root.left, x)\n", 32 | " right = isNodePresent(root.right, x)\n", 33 | " if left:\n", 34 | " return True\n", 35 | " if right:\n", 36 | " return True\n", 37 | "\n", 38 | "def buildLevelTree(levelorder):\n", 39 | " index = 0\n", 40 | " length = len(levelorder)\n", 41 | " if length<=0 or levelorder[0]==-1:\n", 42 | " return None\n", 43 | " root = BinaryTreeNode(levelorder[index])\n", 44 | " index += 1\n", 45 | " q = queue.Queue()\n", 46 | " q.put(root)\n", 47 | " while not q.empty():\n", 48 | " currentNode = q.get()\n", 49 | " leftChild = levelorder[index]\n", 50 | " index += 1\n", 51 | " if leftChild != -1:\n", 52 | " leftNode = BinaryTreeNode(leftChild)\n", 53 | " currentNode.left =leftNode\n", 54 | " q.put(leftNode)\n", 55 | " rightChild = levelorder[index]\n", 56 | " index += 1\n", 57 | " if rightChild != -1:\n", 58 | " rightNode = BinaryTreeNode(rightChild)\n", 59 | " currentNode.right =rightNode\n", 60 | " q.put(rightNode)\n", 61 | " return root\n", 62 | "\n", 63 | "# Main\n", 64 | "levelOrder = [int(i) for i in input().strip().split()]\n", 65 | "root = buildLevelTree(levelOrder)\n", 66 | "x=int(input())\n", 67 | "present=isNodePresent(root, x)\n", 68 | "if present:\n", 69 | " print('true')\n", 70 | "else:\n", 71 | " print('false')\n" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": null, 77 | "metadata": {}, 78 | "outputs": [], 79 | "source": [] 80 | } 81 | ], 82 | "metadata": { 83 | "kernelspec": { 84 | "display_name": "Python 3", 85 | "language": "python", 86 | "name": "python3" 87 | }, 88 | "language_info": { 89 | "codemirror_mode": { 90 | "name": "ipython", 91 | "version": 3 92 | }, 93 | "file_extension": ".py", 94 | "mimetype": "text/x-python", 95 | "name": "python", 96 | "nbconvert_exporter": "python", 97 | "pygments_lexer": "ipython3", 98 | "version": "3.7.3" 99 | } 100 | }, 101 | "nbformat": 4, 102 | "nbformat_minor": 2 103 | } 104 | -------------------------------------------------------------------------------- /14 Binary Trees - 2/14.01 Time Complexity of Binary Tree Problem.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Number of Nodes: Each node is doing some constant k amount of work --> n*(k1 + k2) = O(N)" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "def numNodes(root):\n", 17 | " if root == None:\n", 18 | " return 0\n", 19 | " leftCount = numNodes(root.left)\n", 20 | " rightCount = numNodes(root.right)\n", 21 | " \n", 22 | " return 1 + leftCount + rightCount # Why 1?\n", 23 | " # If you don't add 1 then you'll get the result as 0 no matter how many nodes are there in a tree" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": null, 29 | "metadata": {}, 30 | "outputs": [], 31 | "source": [ 32 | "Root - constant time\n", 33 | "root.left = constant time\n", 34 | "root.right = constant time\n", 35 | "So, time complexity is O(N)" 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": {}, 41 | "source": [ 42 | "### Height of a Tree: Each node is doing some constant k amount of work --> O(N)" 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "### Print all at depth k --> O(N)" 50 | ] 51 | } 52 | ], 53 | "metadata": { 54 | "kernelspec": { 55 | "display_name": "Python 3", 56 | "language": "python", 57 | "name": "python3" 58 | }, 59 | "language_info": { 60 | "codemirror_mode": { 61 | "name": "ipython", 62 | "version": 3 63 | }, 64 | "file_extension": ".py", 65 | "mimetype": "text/x-python", 66 | "name": "python", 67 | "nbconvert_exporter": "python", 68 | "pygments_lexer": "ipython3", 69 | "version": "3.7.3" 70 | } 71 | }, 72 | "nbformat": 4, 73 | "nbformat_minor": 2 74 | } 75 | -------------------------------------------------------------------------------- /15 BST-1/15.1 Intro to Binary Search Tree.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Binary Search Tree: is a binary tree in which elements are stored in a particular pattern i.e. left < root < right. And the same thing applies to it's childrens. \n", 8 | "\n", 9 | " In case of BST, for each node with data d its left subtree has to be < d and its right subtree has to be > d." 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "### Searching an element in a BST :" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 3, 22 | "metadata": {}, 23 | "outputs": [], 24 | "source": [ 25 | "def search(root, x):\n", 26 | " if root is None:\n", 27 | " return None\n", 28 | " if x == root.data:\n", 29 | " return root\n", 30 | " if x < root.data:\n", 31 | " return search(root.left, x)\n", 32 | " if x > root.data:\n", 33 | " return search(root.right, x)" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "metadata": {}, 39 | "source": [ 40 | "O(h) - height of the tree \n", 41 | "\n", 42 | "In a beautiful tree, the height is log n so the time complexity would be O(log h)\n", 43 | "\n", 44 | "In a left skewed or right skewed trees, the height will be O(N)" 45 | ] 46 | } 47 | ], 48 | "metadata": { 49 | "kernelspec": { 50 | "display_name": "Python 3", 51 | "language": "python", 52 | "name": "python3" 53 | }, 54 | "language_info": { 55 | "codemirror_mode": { 56 | "name": "ipython", 57 | "version": 3 58 | }, 59 | "file_extension": ".py", 60 | "mimetype": "text/x-python", 61 | "name": "python", 62 | "nbconvert_exporter": "python", 63 | "pygments_lexer": "ipython3", 64 | "version": "3.7.3" 65 | } 66 | }, 67 | "nbformat": 4, 68 | "nbformat_minor": 2 69 | } 70 | -------------------------------------------------------------------------------- /15 BST-1/15.2 Search in BST.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "8 5 10 2 6 -1 -1 -1 -1 -1 7 -1 -1\n", 13 | "2\n", 14 | "2\n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "import queue\n", 20 | "class BinaryTreeNode:\n", 21 | " def __init__(self, data):\n", 22 | " self.data = data\n", 23 | " self.left = None\n", 24 | " self.right = None\n", 25 | "\n", 26 | "def searchInBST(root, k):\n", 27 | " if root is None:\n", 28 | " return None\n", 29 | " if k == root.data:\n", 30 | " return root\n", 31 | " if k < root.data:\n", 32 | " return searchInBST(root.left, k)\n", 33 | " if k > root.data:\n", 34 | " return searchInBST(root.right, k)\n", 35 | " \n", 36 | " \n", 37 | "def buildLevelTree(levelorder):\n", 38 | " index = 0\n", 39 | " length = len(levelorder)\n", 40 | " if length<=0 or levelorder[0]==-1:\n", 41 | " return None\n", 42 | " root = BinaryTreeNode(levelorder[index])\n", 43 | " index += 1\n", 44 | " q = queue.Queue()\n", 45 | " q.put(root)\n", 46 | " while not q.empty():\n", 47 | " currentNode = q.get()\n", 48 | " leftChild = levelorder[index]\n", 49 | " index += 1\n", 50 | " if leftChild != -1:\n", 51 | " leftNode = BinaryTreeNode(leftChild)\n", 52 | " currentNode.left =leftNode\n", 53 | " q.put(leftNode)\n", 54 | " rightChild = levelorder[index]\n", 55 | " index += 1\n", 56 | " if rightChild != -1:\n", 57 | " rightNode = BinaryTreeNode(rightChild)\n", 58 | " currentNode.right =rightNode\n", 59 | " q.put(rightNode)\n", 60 | " return root\n", 61 | "\n", 62 | "# Main\n", 63 | "levelOrder = [int(i) for i in input().strip().split()]\n", 64 | "root = buildLevelTree(levelOrder)\n", 65 | "k=int(input())\n", 66 | "node=searchInBST(root, k)\n", 67 | "if node:\n", 68 | " print(node.data)" 69 | ] 70 | } 71 | ], 72 | "metadata": { 73 | "kernelspec": { 74 | "display_name": "Python 3", 75 | "language": "python", 76 | "name": "python3" 77 | }, 78 | "language_info": { 79 | "codemirror_mode": { 80 | "name": "ipython", 81 | "version": 3 82 | }, 83 | "file_extension": ".py", 84 | "mimetype": "text/x-python", 85 | "name": "python", 86 | "nbconvert_exporter": "python", 87 | "pygments_lexer": "ipython3", 88 | "version": "3.7.3" 89 | } 90 | }, 91 | "nbformat": 4, 92 | "nbformat_minor": 2 93 | } 94 | -------------------------------------------------------------------------------- /15 BST-1/15.4 Convert sorted arrays to BST.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "1) Take the middle element and create root node\n", 8 | "\n", 9 | "2) Take left side of array pass them recursively to get left subtree\n", 10 | "\n", 11 | "3) Use right side elements to get right subtree\n", 12 | "\n", 13 | "4) Connect these subtrees with the root node" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 6, 19 | "metadata": {}, 20 | "outputs": [ 21 | { 22 | "name": "stdout", 23 | "output_type": "stream", 24 | "text": [ 25 | "4 2 1 3 6 5 7 " 26 | ] 27 | } 28 | ], 29 | "source": [ 30 | "import queue\n", 31 | "class BinaryTreeNode:\n", 32 | " def __init__(self, data):\n", 33 | " self.data = data\n", 34 | " self.left = None\n", 35 | " self.right = None\n", 36 | "\n", 37 | "def constructBST(lst):\n", 38 | " if not lst:\n", 39 | " return None\n", 40 | " \n", 41 | " mid = len(lst)//2\n", 42 | " root = BinaryTreeNode(lst[mid])\n", 43 | " root.left = constructBST(lst[:mid])\n", 44 | " root.right = constructBST(lst[mid+1:])\n", 45 | " \n", 46 | " return root\n", 47 | " \n", 48 | " \n", 49 | "def preOrder(root):\n", 50 | " if root==None:\n", 51 | " return\n", 52 | " print(root.data, end=' ')\n", 53 | " preOrder(root.left)\n", 54 | " preOrder(root.right)\n", 55 | "\n", 56 | "# Main\n", 57 | "#n=int(input())\n", 58 | "n = 7\n", 59 | "#lst=[int(i) for i in input().strip().split()]\n", 60 | "lst = [1,2,3,4,5,6,7]\n", 61 | "root=constructBST(lst)\n", 62 | "preOrder(root)\n" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": null, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [] 71 | } 72 | ], 73 | "metadata": { 74 | "kernelspec": { 75 | "display_name": "Python 3", 76 | "language": "python", 77 | "name": "python3" 78 | }, 79 | "language_info": { 80 | "codemirror_mode": { 81 | "name": "ipython", 82 | "version": 3 83 | }, 84 | "file_extension": ".py", 85 | "mimetype": "text/x-python", 86 | "name": "python", 87 | "nbconvert_exporter": "python", 88 | "pygments_lexer": "ipython3", 89 | "version": "3.7.3" 90 | } 91 | }, 92 | "nbformat": 4, 93 | "nbformat_minor": 2 94 | } 95 | -------------------------------------------------------------------------------- /16 BST-2/16.2 Structure of BST class.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "We need to create a BST class which has the following four functions:\n", 8 | "\n", 9 | "b = BST()\n", 10 | "\n", 11 | "b.insert(1)\n", 12 | "\n", 13 | "b.delete(1)\n", 14 | "\n", 15 | "b.isPresent(1)\n", 16 | "\n", 17 | "b.count()" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 1, 23 | "metadata": {}, 24 | "outputs": [], 25 | "source": [ 26 | "class BinaryTreeNode:\n", 27 | " def __init__(self, data):\n", 28 | " self.data = data\n", 29 | " self.left = None\n", 30 | " self.right = None" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": null, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "# Basic structure of BST class\n", 40 | "class BST:\n", 41 | " \n", 42 | " def __init__(self):\n", 43 | " self.root = None\n", 44 | " self.numNodes = 0\n", 45 | " \n", 46 | " def printTree(self):\n", 47 | " return \n", 48 | " \n", 49 | " def isPresent(self, data):\n", 50 | " return False\n", 51 | " \n", 52 | " def insert(self, data):\n", 53 | " return \n", 54 | " \n", 55 | " def deleteData(self, data):\n", 56 | " return False\n", 57 | " \n", 58 | " def count(self):\n", 59 | " return 0" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": null, 65 | "metadata": {}, 66 | "outputs": [], 67 | "source": [ 68 | "b = BST()\n", 69 | "b.insert(10)\n", 70 | "b.insert(5)\n", 71 | "b.insert(12)\n", 72 | "print(b.isPresent(10))\n", 73 | "print(b.isPresent(7))\n", 74 | "print(b.deleteData(10))\n", 75 | "print(b.count())\n", 76 | "b.printTree()" 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "metadata": {}, 82 | "source": [ 83 | "### This is the basic structure of a BST class and from the next session we're going to write all these functions" 84 | ] 85 | } 86 | ], 87 | "metadata": { 88 | "kernelspec": { 89 | "display_name": "Python 3", 90 | "language": "python", 91 | "name": "python3" 92 | }, 93 | "language_info": { 94 | "codemirror_mode": { 95 | "name": "ipython", 96 | "version": 3 97 | }, 98 | "file_extension": ".py", 99 | "mimetype": "text/x-python", 100 | "name": "python", 101 | "nbconvert_exporter": "python", 102 | "pygments_lexer": "ipython3", 103 | "version": "3.7.3" 104 | } 105 | }, 106 | "nbformat": 4, 107 | "nbformat_minor": 2 108 | } 109 | -------------------------------------------------------------------------------- /17 Generic Trees/17.01 Generic Trees.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Generic trees can have as many childrens as they can unlike Binary trees which are restricted to have atmost 2 childrens.\n", 8 | "\n", 9 | "In Binary trees the childrens are stored as left and right but here in generic tree we have an array or list of childrens\n", 10 | "\n", 11 | "Node contains of data and an array of integers. \n", 12 | "\n", 13 | "Now, we need to iterate the array and that's the only dirfference." 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 1, 19 | "metadata": {}, 20 | "outputs": [], 21 | "source": [ 22 | "class TreeNode:\n", 23 | " def __init__(self, data):\n", 24 | " self.data = data\n", 25 | " self.children = list()" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 3, 31 | "metadata": {}, 32 | "outputs": [ 33 | { 34 | "data": { 35 | "text/plain": [ 36 | "5" 37 | ] 38 | }, 39 | "execution_count": 3, 40 | "metadata": {}, 41 | "output_type": "execute_result" 42 | } 43 | ], 44 | "source": [ 45 | "root = TreeNode(5)\n", 46 | "root.data" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 6, 52 | "metadata": {}, 53 | "outputs": [], 54 | "source": [ 55 | "n1 = TreeNode(5)\n", 56 | "n2 = TreeNode(2)\n", 57 | "n3 = TreeNode(9)\n", 58 | "n4 = TreeNode(8)\n", 59 | "n5 = TreeNode(7)\n", 60 | "n6 = TreeNode(15)\n", 61 | "n7 = TreeNode(1)\n", 62 | "\n", 63 | "n1.children.append(n2)\n", 64 | "n1.children.append(n3)\n", 65 | "n1.children.append(n4)\n", 66 | "n1.children.append(n5)\n", 67 | "\n", 68 | "n3.children.append(n6)\n", 69 | "n3.children.append(n7)" 70 | ] 71 | } 72 | ], 73 | "metadata": { 74 | "kernelspec": { 75 | "display_name": "Python 3", 76 | "language": "python", 77 | "name": "python3" 78 | }, 79 | "language_info": { 80 | "codemirror_mode": { 81 | "name": "ipython", 82 | "version": 3 83 | }, 84 | "file_extension": ".py", 85 | "mimetype": "text/x-python", 86 | "name": "python", 87 | "nbconvert_exporter": "python", 88 | "pygments_lexer": "ipython3", 89 | "version": "3.7.3" 90 | } 91 | }, 92 | "nbformat": 4, 93 | "nbformat_minor": 2 94 | } 95 | -------------------------------------------------------------------------------- /17 Generic Trees/17.05 Sum of all nodes.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 7, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "10 3 20 30 40 2 40 50 0 0 0 0\n", 13 | "190\n" 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "class treeNode:\n", 19 | " def __init__(self, data):\n", 20 | " self.data = data\n", 21 | " self.children = []\n", 22 | " def __str__(self):\n", 23 | " return str(self.data)\n", 24 | "\n", 25 | "def sumofNodes(tree):\n", 26 | " if tree == None:\n", 27 | " return \n", 28 | " sum = tree.data\n", 29 | " for child in tree.children:\n", 30 | " sum = sum + sumofNodes(child)\n", 31 | " \n", 32 | " return sum \n", 33 | " \n", 34 | "def createLevelWiseTree(arr):\n", 35 | " root = treeNode(int(arr[0]))\n", 36 | " q = [root]\n", 37 | " size = len(arr)\n", 38 | " i = 1\n", 39 | " while i n:\n", 29 | " ans = tree\n", 30 | " else:\n", 31 | " ans = None\n", 32 | " \n", 33 | " q = queue.Queue()\n", 34 | " q.put(tree)\n", 35 | " \n", 36 | " while(not(q.empty())):\n", 37 | " current_node = q.get() \n", 38 | " \n", 39 | " for child in current_node.children:\n", 40 | " if child.data > n and child.data < ans:\n", 41 | " ans = child.data\n", 42 | " return child\n", 43 | " \n", 44 | "\n", 45 | "def createLevelWiseTree(arr):\n", 46 | " root = treeNode(int(arr[0]))\n", 47 | " q = [root]\n", 48 | " size = len(arr)\n", 49 | " i = 1\n", 50 | " while i max:\n", 29 | " max = d[x]\n", 30 | " num = x \n", 31 | " \n", 32 | " return num\n", 33 | " \n", 34 | " \n", 35 | "n=int(input())\n", 36 | "l=list(int(i) for i in input().strip().split(' ')[:n])\n", 37 | "print(maxFreq(l))\n" 38 | ] 39 | } 40 | ], 41 | "metadata": { 42 | "kernelspec": { 43 | "display_name": "Python 3", 44 | "language": "python", 45 | "name": "python3" 46 | }, 47 | "language_info": { 48 | "codemirror_mode": { 49 | "name": "ipython", 50 | "version": 3 51 | }, 52 | "file_extension": ".py", 53 | "mimetype": "text/x-python", 54 | "name": "python", 55 | "nbconvert_exporter": "python", 56 | "pygments_lexer": "ipython3", 57 | "version": "3.7.3" 58 | } 59 | }, 60 | "nbformat": 4, 61 | "nbformat_minor": 2 62 | } 63 | -------------------------------------------------------------------------------- /18 Dictionaries/18.09 Steps for implementing our own Map.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Each map will have buckets, each bucket will have map nodes and each map node will have key, value and next\n", 8 | "\n", 9 | "class Map:\n", 10 | "\n", 11 | "1) buckets\n", 12 | "\n", 13 | "2) bucketSize\n", 14 | "\n", 15 | "3) count" 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": {}, 21 | "source": [ 22 | "class MapNode:\n", 23 | "\n", 24 | "1) key\n", 25 | "\n", 26 | "2) value\n", 27 | "\n", 28 | "3) next" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "Implement these 3 functions:\n", 36 | "\n", 37 | "1) insert(key, value) : You'll be storing value according to the key\n", 38 | "\n", 39 | "2) Remove(key): Removes the elemnt of the key\n", 40 | "\n", 41 | "3) search(key) : return the element stored in that key" 42 | ] 43 | } 44 | ], 45 | "metadata": { 46 | "kernelspec": { 47 | "display_name": "Python 3", 48 | "language": "python", 49 | "name": "python3" 50 | }, 51 | "language_info": { 52 | "codemirror_mode": { 53 | "name": "ipython", 54 | "version": 3 55 | }, 56 | "file_extension": ".py", 57 | "mimetype": "text/x-python", 58 | "name": "python", 59 | "nbconvert_exporter": "python", 60 | "pygments_lexer": "ipython3", 61 | "version": "3.7.3" 62 | } 63 | }, 64 | "nbformat": 4, 65 | "nbformat_minor": 2 66 | } 67 | -------------------------------------------------------------------------------- /18 Dictionaries/18.10 How to insert in Map.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "class MapNode:\n", 10 | " def __init__(self, key, value):\n", 11 | " self.key = key\n", 12 | " self.value = value\n", 13 | " self.next = None\n", 14 | " \n", 15 | "class Map:\n", 16 | " def __init__(self):\n", 17 | " self.bucketSize = 10\n", 18 | " self.buckets = [None for i in range(self.bucketSize)]\n", 19 | " self.count = 0\n", 20 | " \n", 21 | " def size(self):\n", 22 | " return self.count\n", 23 | " \n", 24 | " def getBucketIndex(self, hc):\n", 25 | " return (abs(hc) % (self.bucketSize))\n", 26 | " \n", 27 | " def insert(self, key, value): # First task is to find the index\n", 28 | " # At the index, if same key is present then change the corresponding value of it\n", 29 | " # If same key is not present then create a new Node, make newNode.next = head, self.buckets[index] = newNode and \n", 30 | " # increment the count of that bucket\n", 31 | " \n", 32 | " hc = hash(key)\n", 33 | " index = self.getBucketIndex(hc)\n", 34 | " head = self.buckets[index]\n", 35 | " while head is not None:\n", 36 | " if head.key == key:\n", 37 | " head.value = value\n", 38 | " return \n", 39 | " head = head.next\n", 40 | " \n", 41 | " newNode = MapNode(key, value)\n", 42 | " newNode.next = head\n", 43 | " self.buckets[index] = newNode\n", 44 | " self.count += 1 " 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 6, 50 | "metadata": {}, 51 | "outputs": [ 52 | { 53 | "name": "stdout", 54 | "output_type": "stream", 55 | "text": [ 56 | "1\n", 57 | "2\n", 58 | "2\n" 59 | ] 60 | } 61 | ], 62 | "source": [ 63 | "m = Map()\n", 64 | "m.insert('Fazeel', 2)\n", 65 | "print(m.size())\n", 66 | "m.insert('Usmani', 7)\n", 67 | "print(m.size())\n", 68 | "m.insert('Fazeel', 4) # It hasn't inserted the new node of key 'Fazeel' instead it just changed the value for of that key\n", 69 | "print(m.size())" 70 | ] 71 | } 72 | ], 73 | "metadata": { 74 | "kernelspec": { 75 | "display_name": "Python 3", 76 | "language": "python", 77 | "name": "python3" 78 | }, 79 | "language_info": { 80 | "codemirror_mode": { 81 | "name": "ipython", 82 | "version": 3 83 | }, 84 | "file_extension": ".py", 85 | "mimetype": "text/x-python", 86 | "name": "python", 87 | "nbconvert_exporter": "python", 88 | "pygments_lexer": "ipython3", 89 | "version": "3.7.3" 90 | } 91 | }, 92 | "nbformat": 4, 93 | "nbformat_minor": 2 94 | } 95 | -------------------------------------------------------------------------------- /18 Dictionaries/18.12 Time Complexity for Map Functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Time Complexity for Map Functions:" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "1) Insert(key, value)\n", 17 | "\n", 18 | " i) Calculating HashCode --> O(L) time\n", 19 | " ii) Iterating on LL --> O(N) time in worst case if it needs to traverse the whole LL till the end \n", 20 | " O(L) <<< O(N) so we consider O(L) as constant time i.e. O(1) time\n", 21 | " \n", 22 | "If there are b buckets then each bucket has n/b entries. Then we should make sure that n/b < 0.7.\n", 23 | "For example, if there are 100 buckets then the buckets should be in between 130 or 140\n", 24 | "\n", 25 | " Load Factor = 0.7\n", 26 | "\n", 27 | "If we maintain n/b < 0.7 (Load Factor) then we can say that for inserting it takes constant time. \n", 28 | "\n", 29 | "n = 14\n", 30 | "b = 20 then it's okay\n", 31 | "\n", 32 | "Rehashing: Initializes the new array of double size and whatever the previous element was we try to store in the new array.\n", 33 | " \n", 34 | "--> But Rehashing will not occur everytime it occurs very less times. SO, it is ignored as it comes under Amortized \n", 35 | "complexity of algorithms." 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": {}, 41 | "source": [ 42 | "## Steps for Rehashing:" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [ 51 | "Whenever n/b becomes >= 0.7 we need to rehash. That means, we need to increase (double) our bucket size. \n", 52 | "Copy these elements to the new list. Before the new buckets list are None and then copy the elements by again \n", 53 | "passing through the hash function. \n", 54 | "When you create a new list then we need to insert each element to the new buckets. " 55 | ] 56 | }, 57 | { 58 | "attachments": {}, 59 | "cell_type": "markdown", 60 | "metadata": {}, 61 | "source": [ 62 | "![download%20%284%29.png](img/download%20%284%29.png)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": null, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [] 71 | } 72 | ], 73 | "metadata": { 74 | "kernelspec": { 75 | "display_name": "Python 3", 76 | "language": "python", 77 | "name": "python3" 78 | }, 79 | "language_info": { 80 | "codemirror_mode": { 81 | "name": "ipython", 82 | "version": 3 83 | }, 84 | "file_extension": ".py", 85 | "mimetype": "text/x-python", 86 | "name": "python", 87 | "nbconvert_exporter": "python", 88 | "pygments_lexer": "ipython3", 89 | "version": "3.7.3" 90 | } 91 | }, 92 | "nbformat": 4, 93 | "nbformat_minor": 2 94 | } 95 | -------------------------------------------------------------------------------- /18 Dictionaries/18.14 Extract Unique characters.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Naive Approach:" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 6, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "name": "stdout", 17 | "output_type": "stream", 18 | "text": [ 19 | "ababacd\n", 20 | "abcd\n" 21 | ] 22 | } 23 | ], 24 | "source": [ 25 | "def uniqueChars(string):\n", 26 | " new_string = \"\"\n", 27 | " for i in range(len(string)):\n", 28 | " if string[i] not in new_string:\n", 29 | " new_string += string[i]\n", 30 | " \n", 31 | " return new_string \n", 32 | " \n", 33 | " \n", 34 | "string = \"ababacd\"\n", 35 | "print(uniqueChars(string))\n" 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": {}, 41 | "source": [ 42 | "### HashMap Approach:" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [ 51 | "In Progress..." 52 | ] 53 | } 54 | ], 55 | "metadata": { 56 | "kernelspec": { 57 | "display_name": "Python 3", 58 | "language": "python", 59 | "name": "python3" 60 | }, 61 | "language_info": { 62 | "codemirror_mode": { 63 | "name": "ipython", 64 | "version": 3 65 | }, 66 | "file_extension": ".py", 67 | "mimetype": "text/x-python", 68 | "name": "python", 69 | "nbconvert_exporter": "python", 70 | "pygments_lexer": "ipython3", 71 | "version": "3.7.3" 72 | } 73 | }, 74 | "nbformat": 4, 75 | "nbformat_minor": 2 76 | } 77 | -------------------------------------------------------------------------------- /18 Dictionaries/18.17 Longest subset zero sum.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "10\n", 13 | "95 -97 -387 -435 -5 -70 897 127 23 284\n", 14 | "5\n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "def subsetSum(l):\n", 20 | " \n", 21 | " hash_map = {}\n", 22 | " \n", 23 | " max_len = 0\n", 24 | " curr_sum = 0\n", 25 | " \n", 26 | " for i in range(len(l)):\n", 27 | " curr_sum += l[i]\n", 28 | " \n", 29 | " if l[i] is 0 and max_len is 0:\n", 30 | " max_len = i + 1\n", 31 | " \n", 32 | " if curr_sum in hash_map:\n", 33 | " max_len = max(max_len, i - hash_map[curr_sum])\n", 34 | " \n", 35 | " else:\n", 36 | " hash_map[curr_sum] = i\n", 37 | " \n", 38 | " return max_len \n", 39 | " \n", 40 | "\n", 41 | "\n", 42 | "n=int(input())\n", 43 | "l=list(int(i) for i in input().strip().split(' '))\n", 44 | "finalLen= subsetSum(l)\n", 45 | "print(finalLen)" 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "metadata": {}, 51 | "source": [ 52 | "### HashMap Approach InProgress....." 53 | ] 54 | } 55 | ], 56 | "metadata": { 57 | "kernelspec": { 58 | "display_name": "Python 3", 59 | "language": "python", 60 | "name": "python3" 61 | }, 62 | "language_info": { 63 | "codemirror_mode": { 64 | "name": "ipython", 65 | "version": 3 66 | }, 67 | "file_extension": ".py", 68 | "mimetype": "text/x-python", 69 | "name": "python", 70 | "nbconvert_exporter": "python", 71 | "pygments_lexer": "ipython3", 72 | "version": "3.7.3" 73 | } 74 | }, 75 | "nbformat": 4, 76 | "nbformat_minor": 2 77 | } 78 | -------------------------------------------------------------------------------- /18 Dictionaries/img/README.md: -------------------------------------------------------------------------------- 1 | This folder contains all the images used in the jupyter notebooks of **18 Dictionaries** topic. 2 | -------------------------------------------------------------------------------- /18 Dictionaries/img/collision resolution techniques.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/18 Dictionaries/img/collision resolution techniques.png -------------------------------------------------------------------------------- /18 Dictionaries/img/download (1).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/18 Dictionaries/img/download (1).png -------------------------------------------------------------------------------- /18 Dictionaries/img/download (2).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/18 Dictionaries/img/download (2).png -------------------------------------------------------------------------------- /18 Dictionaries/img/download (3).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/18 Dictionaries/img/download (3).png -------------------------------------------------------------------------------- /18 Dictionaries/img/download (4).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/18 Dictionaries/img/download (4).png -------------------------------------------------------------------------------- /18 Dictionaries/img/download.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/18 Dictionaries/img/download.png -------------------------------------------------------------------------------- /18 Dictionaries/img/hash.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/18 Dictionaries/img/hash.png -------------------------------------------------------------------------------- /18 Dictionaries/img/hashFunction.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/18 Dictionaries/img/hashFunction.png -------------------------------------------------------------------------------- /18 Dictionaries/img/hashTable.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/18 Dictionaries/img/hashTable.png -------------------------------------------------------------------------------- /19 Graphs - 1/19.01 Intro to Graphs.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "![download.png](img/download.png)" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Graphs has vertices and edges and it's not necessary that there will be a connection between each vertex." 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "Graph is commonly used in Social Media Networks (Mutual friends) or Road Networks" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "# Mutual Friend Network in Social Media:" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "![download%20%281%29.png](img/download%20%281%29.png)" 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": {}, 41 | "source": [ 42 | "### Road Network" 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "![download%20%282%29.png](img/download%20%282%29.png)" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": {}, 55 | "source": [ 56 | "Unlike trees there is no hierarchy (left and right child) type structure in graphs but it has only has vertices and edges" 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": {}, 62 | "source": [ 63 | "![download%20%283%29.png](img/download%20%283%29.png)" 64 | ] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "metadata": {}, 69 | "source": [ 70 | "### There are 3 islands in the above graph" 71 | ] 72 | } 73 | ], 74 | "metadata": { 75 | "kernelspec": { 76 | "display_name": "Python 3", 77 | "language": "python", 78 | "name": "python3" 79 | }, 80 | "language_info": { 81 | "codemirror_mode": { 82 | "name": "ipython", 83 | "version": 3 84 | }, 85 | "file_extension": ".py", 86 | "mimetype": "text/x-python", 87 | "name": "python", 88 | "nbconvert_exporter": "python", 89 | "pygments_lexer": "ipython3", 90 | "version": "3.7.3" 91 | } 92 | }, 93 | "nbformat": 4, 94 | "nbformat_minor": 2 95 | } 96 | -------------------------------------------------------------------------------- /19 Graphs - 1/19.03 Complexity in terms of edges.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "1) Minimum edges in a graph is 0\n", 8 | "\n", 9 | "2) Minimum edges in a connected graph for n vetices is (n-1) edges \n", 10 | " #P.S: A tree is a connected graph in which it has n vertices and (n-1) edges\n", 11 | " \n", 12 | "3) Maximum number of edges in a connected graph fo n vertices is (n-1)!\n", 13 | " (n-1) + (n-2) + (n-3) + (n-4) + --- + 1\n", 14 | " \n", 15 | " Complexity:\n", 16 | " \n", 17 | " = n(n-1)/2 = O(n**2) = O(E) [Edges]" 18 | ] 19 | } 20 | ], 21 | "metadata": { 22 | "kernelspec": { 23 | "display_name": "Python 3", 24 | "language": "python", 25 | "name": "python3" 26 | }, 27 | "language_info": { 28 | "codemirror_mode": { 29 | "name": "ipython", 30 | "version": 3 31 | }, 32 | "file_extension": ".py", 33 | "mimetype": "text/x-python", 34 | "name": "python", 35 | "nbconvert_exporter": "python", 36 | "pygments_lexer": "ipython3", 37 | "version": "3.7.3" 38 | } 39 | }, 40 | "nbformat": 4, 41 | "nbformat_minor": 2 42 | } 43 | -------------------------------------------------------------------------------- /19 Graphs - 1/19.05 Implementation of Graph.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "class Graph:\n", 10 | " def __init__(self, nVertices):\n", 11 | " self.nVertices = nVertices\n", 12 | " self.adjMatrix = [[0 for i in range(nVertices)] for j in range(nVertices)]\n", 13 | " \n", 14 | " \n", 15 | " def addEdge(self, v1, v2):\n", 16 | " self.adjMatrix[v1][v2] = 1\n", 17 | " self.adjMatrix[v2][v1] = 1\n", 18 | " \n", 19 | " \n", 20 | " def removeEdge(self, v1, v2):\n", 21 | " if self.containEdge(v1, v2) is False:\n", 22 | " return\n", 23 | " self.adjMatrix[v1][v2] = 0\n", 24 | " self.adjMatrix[v2][v1] = 0\n", 25 | " \n", 26 | " \n", 27 | " def containsEdge(self, v1, v2):\n", 28 | " return True if self.adjMatrix[v1][v2] > 0 else False\n", 29 | " \n", 30 | " # The [below] 4 lines code can be written as [above] single line code\n", 31 | " \n", 32 | "# if self.adjMatrix[v1][v2] > 0:\n", 33 | "# return True\n", 34 | "# else:\n", 35 | "# return False\n", 36 | " \n", 37 | "\n", 38 | " def __str__(self):\n", 39 | " return str(self.adjMatrix)\n", 40 | " " 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 4, 46 | "metadata": {}, 47 | "outputs": [ 48 | { 49 | "name": "stdout", 50 | "output_type": "stream", 51 | "text": [ 52 | "[[0, 1, 0, 1, 0], [1, 0, 0, 1, 0], [0, 0, 0, 0, 1], [1, 1, 0, 0, 0], [0, 0, 1, 0, 0]]\n" 53 | ] 54 | } 55 | ], 56 | "source": [ 57 | "g = Graph(5)\n", 58 | "g.addEdge(0, 1)\n", 59 | "g.addEdge(1, 3)\n", 60 | "g.addEdge(2, 4)\n", 61 | "g.addEdge(3, 0)\n", 62 | "print(g)" 63 | ] 64 | } 65 | ], 66 | "metadata": { 67 | "kernelspec": { 68 | "display_name": "Python 3", 69 | "language": "python", 70 | "name": "python3" 71 | }, 72 | "language_info": { 73 | "codemirror_mode": { 74 | "name": "ipython", 75 | "version": 3 76 | }, 77 | "file_extension": ".py", 78 | "mimetype": "text/x-python", 79 | "name": "python", 80 | "nbconvert_exporter": "python", 81 | "pygments_lexer": "ipython3", 82 | "version": "3.7.3" 83 | } 84 | }, 85 | "nbformat": 4, 86 | "nbformat_minor": 2 87 | } 88 | -------------------------------------------------------------------------------- /19 Graphs - 1/19.10 Get Path DFS.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 95, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "4 4\n", 13 | "0 1\n", 14 | "0 3\n", 15 | "1 2\n", 16 | "2 3\n", 17 | "1 3\n", 18 | "3 0 1 " 19 | ] 20 | } 21 | ], 22 | "source": [ 23 | "class Graph:\n", 24 | " def __init__(self, nVertices):\n", 25 | " self.nVertices = nVertices\n", 26 | " self.adjMatrix = [[0 for i in range(nVertices)] for j in range(nVertices)]\n", 27 | " \n", 28 | " \n", 29 | " def addEdge(self, v1, v2):\n", 30 | " self.adjMatrix[v1][v2] = 1\n", 31 | " self.adjMatrix[v2][v1] = 1\n", 32 | " \n", 33 | " def getPathHelper(self, v1, v2, visited, list):\n", 34 | "\n", 35 | " if self.adjMatrix[v1][v2] > 0:\n", 36 | " list.append(v2)\n", 37 | " list.append(v1)\n", 38 | " \n", 39 | " return list\n", 40 | " \n", 41 | " visited[v1] = True\n", 42 | " \n", 43 | " for i in range(self.nVertices):\n", 44 | " if self.adjMatrix[v1][i] > 0 and visited[i] is False:\n", 45 | " if self.getPathHelper(i, v2, visited, list):\n", 46 | " list.append(v1)\n", 47 | " \n", 48 | " return list \n", 49 | " \n", 50 | " return False\n", 51 | " \n", 52 | " \n", 53 | " def getPath(self, v1, v2, n):\n", 54 | " visited = [False] * n\n", 55 | " return self.getPathHelper(v1, v2, visited, [])\n", 56 | " \n", 57 | "\n", 58 | "v, e = [int (i) for i in input().split()[:2]]\n", 59 | "g = Graph(v)\n", 60 | "for i in range(e):\n", 61 | " a, b = [int(x) for x in input().split()[:2]]\n", 62 | " g.addEdge(a, b)\n", 63 | " \n", 64 | "v1, v2 = [int (r) for r in input().split()[:2]]\n", 65 | "list = g.getPath(v1, v2, v)\n", 66 | "if list:\n", 67 | " for i in g.getPath(v1, v2, v):\n", 68 | " print(i, end = \" \")" 69 | ] 70 | } 71 | ], 72 | "metadata": { 73 | "kernelspec": { 74 | "display_name": "Python 3", 75 | "language": "python", 76 | "name": "python3" 77 | }, 78 | "language_info": { 79 | "codemirror_mode": { 80 | "name": "ipython", 81 | "version": 3 82 | }, 83 | "file_extension": ".py", 84 | "mimetype": "text/x-python", 85 | "name": "python", 86 | "nbconvert_exporter": "python", 87 | "pygments_lexer": "ipython3", 88 | "version": "3.7.3" 89 | } 90 | }, 91 | "nbformat": 4, 92 | "nbformat_minor": 2 93 | } 94 | -------------------------------------------------------------------------------- /19 Graphs - 1/19.14 Variations of Graph.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Variations of Graph:" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## 1) UNDIRECTED GRAPH:" 15 | ] 16 | }, 17 | { 18 | "attachments": {}, 19 | "cell_type": "markdown", 20 | "metadata": {}, 21 | "source": [ 22 | "![undirected.png](img/undirected.png)" 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "metadata": {}, 28 | "source": [ 29 | "## 2) DIRECTED GRAPH:" 30 | ] 31 | }, 32 | { 33 | "attachments": {}, 34 | "cell_type": "markdown", 35 | "metadata": {}, 36 | "source": [ 37 | "![directed.png](img/directed.png)" 38 | ] 39 | }, 40 | { 41 | "cell_type": "markdown", 42 | "metadata": {}, 43 | "source": [ 44 | "## 3) WEIGHTED GRAPH:\n", 45 | "Length of Road\n", 46 | "Friendship in Social Media Platform" 47 | ] 48 | }, 49 | { 50 | "attachments": {}, 51 | "cell_type": "markdown", 52 | "metadata": {}, 53 | "source": [ 54 | "![weighted.png](img/weighted.png)" 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "metadata": {}, 60 | "source": [ 61 | "## 4) WEIGHTED DIRECTED GRAPH:" 62 | ] 63 | }, 64 | { 65 | "attachments": {}, 66 | "cell_type": "markdown", 67 | "metadata": {}, 68 | "source": [ 69 | "![weighted%20directed.png](img/weighted%20directed.png)" 70 | ] 71 | } 72 | ], 73 | "metadata": { 74 | "kernelspec": { 75 | "display_name": "Python 3", 76 | "language": "python", 77 | "name": "python3" 78 | }, 79 | "language_info": { 80 | "codemirror_mode": { 81 | "name": "ipython", 82 | "version": 3 83 | }, 84 | "file_extension": ".py", 85 | "mimetype": "text/x-python", 86 | "name": "python", 87 | "nbconvert_exporter": "python", 88 | "pygments_lexer": "ipython3", 89 | "version": "3.7.3" 90 | } 91 | }, 92 | "nbformat": 4, 93 | "nbformat_minor": 2 94 | } 95 | -------------------------------------------------------------------------------- /19 Graphs - 1/img/README.md: -------------------------------------------------------------------------------- 1 | This folder contains all the images used in Graphs - 1 2 | -------------------------------------------------------------------------------- /19 Graphs - 1/img/adjacency list.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/19 Graphs - 1/img/adjacency list.png -------------------------------------------------------------------------------- /19 Graphs - 1/img/adjacency matrix.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/19 Graphs - 1/img/adjacency matrix.png -------------------------------------------------------------------------------- /19 Graphs - 1/img/bfs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/19 Graphs - 1/img/bfs.png -------------------------------------------------------------------------------- /19 Graphs - 1/img/connected.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/19 Graphs - 1/img/connected.png -------------------------------------------------------------------------------- /19 Graphs - 1/img/dfs 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/19 Graphs - 1/img/dfs 2.png -------------------------------------------------------------------------------- /19 Graphs - 1/img/dfs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/19 Graphs - 1/img/dfs.png -------------------------------------------------------------------------------- /19 Graphs - 1/img/diagram graph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/19 Graphs - 1/img/diagram graph.png -------------------------------------------------------------------------------- /19 Graphs - 1/img/directed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/19 Graphs - 1/img/directed.png -------------------------------------------------------------------------------- /19 Graphs - 1/img/download (1).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/19 Graphs - 1/img/download (1).png -------------------------------------------------------------------------------- /19 Graphs - 1/img/download (2).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/19 Graphs - 1/img/download (2).png -------------------------------------------------------------------------------- /19 Graphs - 1/img/download (3).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/19 Graphs - 1/img/download (3).png -------------------------------------------------------------------------------- /19 Graphs - 1/img/download.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/19 Graphs - 1/img/download.png -------------------------------------------------------------------------------- /19 Graphs - 1/img/edge list.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/19 Graphs - 1/img/edge list.png -------------------------------------------------------------------------------- /19 Graphs - 1/img/graph types.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/19 Graphs - 1/img/graph types.png -------------------------------------------------------------------------------- /19 Graphs - 1/img/graph vertex array.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/19 Graphs - 1/img/graph vertex array.png -------------------------------------------------------------------------------- /19 Graphs - 1/img/table.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/19 Graphs - 1/img/table.png -------------------------------------------------------------------------------- /19 Graphs - 1/img/tree graph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/19 Graphs - 1/img/tree graph.png -------------------------------------------------------------------------------- /19 Graphs - 1/img/tree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/19 Graphs - 1/img/tree.png -------------------------------------------------------------------------------- /19 Graphs - 1/img/two component.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/19 Graphs - 1/img/two component.png -------------------------------------------------------------------------------- /19 Graphs - 1/img/undirected.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/19 Graphs - 1/img/undirected.png -------------------------------------------------------------------------------- /19 Graphs - 1/img/weighted directed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/19 Graphs - 1/img/weighted directed.png -------------------------------------------------------------------------------- /19 Graphs - 1/img/weighted.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/19 Graphs - 1/img/weighted.png -------------------------------------------------------------------------------- /2 Recursion-2/1 Recursion in Strings.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Replace a character with another character" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "def replaceChar(s, a, b):\n", 17 | " if len(s) == 0:\n", 18 | " return s\n", 19 | " \n", 20 | " smallOutput = replaceChar(a[1:], a, b)\n", 21 | " if s[0] == 'a':\n", 22 | " return b + smallOutput\n", 23 | " else:\n", 24 | " return s[0] + smallOutput\n", 25 | " " 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 4, 31 | "metadata": {}, 32 | "outputs": [ 33 | { 34 | "name": "stdout", 35 | "output_type": "stream", 36 | "text": [ 37 | "c\n" 38 | ] 39 | } 40 | ], 41 | "source": [ 42 | "print(replaceChar(\"cxcccx\", 'c', 'x'))" 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "### Remove x from the string" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 5, 55 | "metadata": {}, 56 | "outputs": [ 57 | { 58 | "name": "stdout", 59 | "output_type": "stream", 60 | "text": [ 61 | "axlkbxkljdfxxxxx\n", 62 | "alkbkljdf\n" 63 | ] 64 | } 65 | ], 66 | "source": [ 67 | "# Problem: Remove x from string\n", 68 | "def removeX(string): \n", 69 | " if len(string) == 0:\n", 70 | " return string\n", 71 | " smallOutput = removeX(string[1:])\n", 72 | " if string[0] == 'x':\n", 73 | " return smallOutput\n", 74 | " else:\n", 75 | " return string[0] + smallOutput\n", 76 | "\n", 77 | "# Main\n", 78 | "string = input()\n", 79 | "print(removeX(string))\n" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": null, 85 | "metadata": {}, 86 | "outputs": [], 87 | "source": [] 88 | } 89 | ], 90 | "metadata": { 91 | "kernelspec": { 92 | "display_name": "Python 3", 93 | "language": "python", 94 | "name": "python3" 95 | }, 96 | "language_info": { 97 | "codemirror_mode": { 98 | "name": "ipython", 99 | "version": 3 100 | }, 101 | "file_extension": ".py", 102 | "mimetype": "text/x-python", 103 | "name": "python", 104 | "nbconvert_exporter": "python", 105 | "pygments_lexer": "ipython3", 106 | "version": "3.7.3" 107 | } 108 | }, 109 | "nbformat": 4, 110 | "nbformat_minor": 2 111 | } 112 | -------------------------------------------------------------------------------- /2 Recursion-2/2 Replace pi with 3.14.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | " If l == 0 or l==1:\n", 8 | "return the same string\n", 9 | "If pi then replace with 3.14" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 3, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "def replacePi(s):\n", 19 | " if len(s) == 0 or len(s) == 1:\n", 20 | " return s\n", 21 | " if s[0] == 'p' and s[1] == 'i':\n", 22 | " smallOutput = replacePi(s[2:])\n", 23 | " return \"3.14\" + smallOutput\n", 24 | " else:\n", 25 | " smallOutput = replacePi(s[1:])\n", 26 | " return s[0] + smallOutput" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 5, 32 | "metadata": {}, 33 | "outputs": [ 34 | { 35 | "data": { 36 | "text/plain": [ 37 | "'3.14 hona kya 3.14'" 38 | ] 39 | }, 40 | "execution_count": 5, 41 | "metadata": {}, 42 | "output_type": "execute_result" 43 | } 44 | ], 45 | "source": [ 46 | "replacePi(\"pi hona kya pi\")" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": null, 52 | "metadata": {}, 53 | "outputs": [], 54 | "source": [] 55 | } 56 | ], 57 | "metadata": { 58 | "kernelspec": { 59 | "display_name": "Python 3", 60 | "language": "python", 61 | "name": "python3" 62 | }, 63 | "language_info": { 64 | "codemirror_mode": { 65 | "name": "ipython", 66 | "version": 3 67 | }, 68 | "file_extension": ".py", 69 | "mimetype": "text/x-python", 70 | "name": "python", 71 | "nbconvert_exporter": "python", 72 | "pygments_lexer": "ipython3", 73 | "version": "3.7.3" 74 | } 75 | }, 76 | "nbformat": 4, 77 | "nbformat_minor": 2 78 | } 79 | -------------------------------------------------------------------------------- /2 Recursion-2/4 Binary Search using Recursion.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Binary Search using Recursion:\n", 8 | "1000 can reached 10 in just 10 steps. You don't need to compare all the 1000 elements" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 2, 14 | "metadata": {}, 15 | "outputs": [], 16 | "source": [ 17 | "def binary_search(a, x, si, ei):\n", 18 | " if si > ei:\n", 19 | " return -1\n", 20 | " \n", 21 | " mid = (si + ei)//2\n", 22 | " if a[mid] == x:\n", 23 | " return mid\n", 24 | " elif a[mid] > x:\n", 25 | " return binary_search(a, x, si, mid-1)\n", 26 | " else:\n", 27 | " return binary_search(a, x, mid+1, ei)\n", 28 | " \n", 29 | " " 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 5, 35 | "metadata": {}, 36 | "outputs": [ 37 | { 38 | "data": { 39 | "text/plain": [ 40 | "1" 41 | ] 42 | }, 43 | "execution_count": 5, 44 | "metadata": {}, 45 | "output_type": "execute_result" 46 | } 47 | ], 48 | "source": [ 49 | "binary_search([1,3,5,7,9,11,13,15, 17, 19], 3, 0, 9)" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": null, 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [] 58 | } 59 | ], 60 | "metadata": { 61 | "kernelspec": { 62 | "display_name": "Python 3", 63 | "language": "python", 64 | "name": "python3" 65 | }, 66 | "language_info": { 67 | "codemirror_mode": { 68 | "name": "ipython", 69 | "version": 3 70 | }, 71 | "file_extension": ".py", 72 | "mimetype": "text/x-python", 73 | "name": "python", 74 | "nbconvert_exporter": "python", 75 | "pygments_lexer": "ipython3", 76 | "version": "3.7.3" 77 | } 78 | }, 79 | "nbformat": 4, 80 | "nbformat_minor": 2 81 | } 82 | -------------------------------------------------------------------------------- /2 Recursion-2/5 Merge Sort.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 15, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "def merge(a1, a2, a):\n", 10 | " i = 0\n", 11 | " j = 0\n", 12 | " k = 0\n", 13 | " \n", 14 | " while i < len(a1) and j = pivot:\n", 28 | " j = j - 1\n", 29 | " else:\n", 30 | " a[i], a[j] = a[j], a[i]\n", 31 | " i = i + 1\n", 32 | " j = j - 1\n", 33 | " \n", 34 | " return pivot_index" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 29, 40 | "metadata": {}, 41 | "outputs": [], 42 | "source": [ 43 | "\n", 44 | "def quick_sort(a, si, ei):\n", 45 | " if si >= ei:\n", 46 | " return \n", 47 | " \n", 48 | " pivot_index = partition(a, si, ei)\n", 49 | " quick_sort(a, si, pivot_index-1)\n", 50 | " quick_sort(a, pivot_index+1, ei)\n", 51 | " " 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 30, 57 | "metadata": {}, 58 | "outputs": [ 59 | { 60 | "data": { 61 | "text/plain": [ 62 | "[1, 2, 3, 4, 5, 7, 8, 9, 10]" 63 | ] 64 | }, 65 | "execution_count": 30, 66 | "metadata": {}, 67 | "output_type": "execute_result" 68 | } 69 | ], 70 | "source": [ 71 | "a = [10,9,8,7,1,3,5,4,2]\n", 72 | "quick_sort(a, 0, len(a)-1)\n", 73 | "a" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": null, 79 | "metadata": {}, 80 | "outputs": [], 81 | "source": [] 82 | } 83 | ], 84 | "metadata": { 85 | "kernelspec": { 86 | "display_name": "Python 3", 87 | "language": "python", 88 | "name": "python3" 89 | }, 90 | "language_info": { 91 | "codemirror_mode": { 92 | "name": "ipython", 93 | "version": 3 94 | }, 95 | "file_extension": ".py", 96 | "mimetype": "text/x-python", 97 | "name": "python", 98 | "nbconvert_exporter": "python", 99 | "pygments_lexer": "ipython3", 100 | "version": "3.7.3" 101 | } 102 | }, 103 | "nbformat": 4, 104 | "nbformat_minor": 2 105 | } 106 | -------------------------------------------------------------------------------- /2 Recursion-2/7 Towers of Hanoi.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "def tower_hanoi(n,a,b,c):\n", 10 | " if n == 1:\n", 11 | " print(\"move 1st disk from \", a, \" to \", c)\n", 12 | " return\n", 13 | " tower_hanoi(n-1, a, c, b)\n", 14 | " print(\"move \", n, \"th disk from \", a, \" to \", c)\n", 15 | " tower_hanoi(n-1, b,a,c)" 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": 3, 21 | "metadata": {}, 22 | "outputs": [ 23 | { 24 | "name": "stdout", 25 | "output_type": "stream", 26 | "text": [ 27 | "move 1st disk from a to c\n", 28 | "move 2 th disk from a to b\n", 29 | "move 1st disk from c to b\n", 30 | "move 3 th disk from a to c\n", 31 | "move 1st disk from b to a\n", 32 | "move 2 th disk from b to c\n", 33 | "move 1st disk from a to c\n" 34 | ] 35 | } 36 | ], 37 | "source": [ 38 | "tower_hanoi(3, \"a\", \"b\", \"c\")" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 5, 44 | "metadata": {}, 45 | "outputs": [ 46 | { 47 | "name": "stdout", 48 | "output_type": "stream", 49 | "text": [ 50 | "1\n", 51 | "a c\n" 52 | ] 53 | } 54 | ], 55 | "source": [ 56 | "def towerofhanoi(n, source, aux, dest):\n", 57 | "\n", 58 | " if n == 1:\n", 59 | " print(source,\" \", dest)\n", 60 | " return\n", 61 | "\n", 62 | " towerofhanoi(n-1, source, dest, aux)\n", 63 | " print(source,\" \", dest)\n", 64 | " towerofhanoi(n-1, aux, source, dest)\n", 65 | " \n", 66 | "\n", 67 | "\n", 68 | "n=int(input())\n", 69 | "towerofhanoi(n, 'a', 'b', 'c')\n" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": null, 75 | "metadata": {}, 76 | "outputs": [], 77 | "source": [] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": null, 82 | "metadata": {}, 83 | "outputs": [], 84 | "source": [] 85 | } 86 | ], 87 | "metadata": { 88 | "kernelspec": { 89 | "display_name": "Python 3", 90 | "language": "python", 91 | "name": "python3" 92 | }, 93 | "language_info": { 94 | "codemirror_mode": { 95 | "name": "ipython", 96 | "version": 3 97 | }, 98 | "file_extension": ".py", 99 | "mimetype": "text/x-python", 100 | "name": "python", 101 | "nbconvert_exporter": "python", 102 | "pygments_lexer": "ipython3", 103 | "version": "3.7.3" 104 | } 105 | }, 106 | "nbformat": 4, 107 | "nbformat_minor": 2 108 | } 109 | -------------------------------------------------------------------------------- /20 Graphs - 2/20.1 Minimum Spanning Trees.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Minimum Spanning Tree:" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Trees:\n", 15 | " 1) Graph is Connected\n", 16 | " 2) Graph has no cycles\n", 17 | " \n", 18 | "**Tree has n vertices and n-1 edges" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": {}, 24 | "source": [ 25 | "Minimum Spanning Tree has the minimum weight connecting all vertices and satisfies the following 3 properties:\n", 26 | " 1) Weighted\n", 27 | " 2) Undirected\n", 28 | " 3) Connected" 29 | ] 30 | } 31 | ], 32 | "metadata": { 33 | "kernelspec": { 34 | "display_name": "Python 3", 35 | "language": "python", 36 | "name": "python3" 37 | }, 38 | "language_info": { 39 | "codemirror_mode": { 40 | "name": "ipython", 41 | "version": 3 42 | }, 43 | "file_extension": ".py", 44 | "mimetype": "text/x-python", 45 | "name": "python", 46 | "nbconvert_exporter": "python", 47 | "pygments_lexer": "ipython3", 48 | "version": "3.7.3" 49 | } 50 | }, 51 | "nbformat": 4, 52 | "nbformat_minor": 2 53 | } 54 | -------------------------------------------------------------------------------- /20 Graphs - 2/20.2 Kruskal's Algorithm.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Pick the edges with minimum weight and skip the edges who're forming a cycle" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [] 16 | } 17 | ], 18 | "metadata": { 19 | "kernelspec": { 20 | "display_name": "Python 3", 21 | "language": "python", 22 | "name": "python3" 23 | }, 24 | "language_info": { 25 | "codemirror_mode": { 26 | "name": "ipython", 27 | "version": 3 28 | }, 29 | "file_extension": ".py", 30 | "mimetype": "text/x-python", 31 | "name": "python", 32 | "nbconvert_exporter": "python", 33 | "pygments_lexer": "ipython3", 34 | "version": "3.7.3" 35 | } 36 | }, 37 | "nbformat": 4, 38 | "nbformat_minor": 2 39 | } 40 | -------------------------------------------------------------------------------- /20 Graphs - 2/20.3 Detect Cycle.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Check whether a path exists between v1 and v2 if exists then if form a cylce else it won't" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "hasPath(v1, v2) has a complexity of O(E) --> O(V**2)" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": null, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [] 23 | } 24 | ], 25 | "metadata": { 26 | "kernelspec": { 27 | "display_name": "Python 3", 28 | "language": "python", 29 | "name": "python3" 30 | }, 31 | "language_info": { 32 | "codemirror_mode": { 33 | "name": "ipython", 34 | "version": 3 35 | }, 36 | "file_extension": ".py", 37 | "mimetype": "text/x-python", 38 | "name": "python", 39 | "nbconvert_exporter": "python", 40 | "pygments_lexer": "ipython3", 41 | "version": "3.7.3" 42 | } 43 | }, 44 | "nbformat": 4, 45 | "nbformat_minor": 2 46 | } 47 | -------------------------------------------------------------------------------- /20 Graphs - 2/20.4 Union Find Algorithm.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Union Find Algorithm is used to detect cycle in a graph" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "Check whether v1 and v2\n", 17 | "Whether v1 and v2 are in same component or not. \n", 18 | "If they're in the same component then it forms a cylce else it won't\n", 19 | "\n", 20 | "Maintain a parent matrix: Maintain the parent of each matrix\n", 21 | "Time Complexity: O(v)" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": null, 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [] 30 | } 31 | ], 32 | "metadata": { 33 | "kernelspec": { 34 | "display_name": "Python 3", 35 | "language": "python", 36 | "name": "python3" 37 | }, 38 | "language_info": { 39 | "codemirror_mode": { 40 | "name": "ipython", 41 | "version": 3 42 | }, 43 | "file_extension": ".py", 44 | "mimetype": "text/x-python", 45 | "name": "python", 46 | "nbconvert_exporter": "python", 47 | "pygments_lexer": "ipython3", 48 | "version": "3.7.3" 49 | } 50 | }, 51 | "nbformat": 4, 52 | "nbformat_minor": 2 53 | } 54 | -------------------------------------------------------------------------------- /24 DP-1/24.01 Intro to DP.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "An concept which is applied where the following two things are happening:\n", 10 | " 1) Optimal Substructure - {Recurtion} \n", 11 | " wherever recurrence relation is present it is optimal substructure\n", 12 | " 2) Overlapping subproblems - \n", 13 | " \n", 14 | " Problem depends on subproblems. \n", 15 | " \n", 16 | "In fib(n):\n", 17 | " if n == 0 or n == 1:\n", 18 | " return n\n", 19 | " \n", 20 | " return fib(n-1) + fib(n-2)" 21 | ] 22 | }, 23 | { 24 | "attachments": {}, 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "![fibonacci_tree.png](img/fibonacci_tree.png)" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": null, 34 | "metadata": {}, 35 | "outputs": [], 36 | "source": [ 37 | "Here in the above tree:\n", 38 | " --> f4 is called 2 times\n", 39 | " --> f3 is called 3 times\n", 40 | " --> f2 is called 5 times\n", 41 | " \n", 42 | "DP uses a storage so that you do not call on the same sub problem again and again\n", 43 | "\n", 44 | "Every recursive problem is not an overlapping subproblem. For example, factorial of a number\n", 45 | "\n", 46 | "Fact(5) --> Fact(4) --> Fact(3) --> Fact(2) --> Fact(1) --> Fact(0) \n", 47 | "\n", 48 | "Recursion will always have optimal substructure but DP is applied where it also has overlapping sub problem" 49 | ] 50 | } 51 | ], 52 | "metadata": { 53 | "kernelspec": { 54 | "display_name": "Python 3", 55 | "language": "python", 56 | "name": "python3" 57 | }, 58 | "language_info": { 59 | "codemirror_mode": { 60 | "name": "ipython", 61 | "version": 3 62 | }, 63 | "file_extension": ".py", 64 | "mimetype": "text/x-python", 65 | "name": "python", 66 | "nbconvert_exporter": "python", 67 | "pygments_lexer": "ipython3", 68 | "version": "3.7.3" 69 | } 70 | }, 71 | "nbformat": 4, 72 | "nbformat_minor": 2 73 | } 74 | -------------------------------------------------------------------------------- /24 DP-1/24.03 Time Complexity of Memoization.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "attachments": {}, 5 | "cell_type": "markdown", 6 | "metadata": {}, 7 | "source": [ 8 | "![memoization-12.png](img/memoization-12.png)" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "Normal Recursion has a time complexity of O(2^n). Space complexity is O(n)\n", 16 | "\n", 17 | "Memoized has a time complexity of O(n+1) = O(n). Space complexity is O(n)for path +O(n)for array \n", 18 | "\n", 19 | "So, your time complexity has decreased from O(2^n) to O(n)" 20 | ] 21 | } 22 | ], 23 | "metadata": { 24 | "kernelspec": { 25 | "display_name": "Python 3", 26 | "language": "python", 27 | "name": "python3" 28 | }, 29 | "language_info": { 30 | "codemirror_mode": { 31 | "name": "ipython", 32 | "version": 3 33 | }, 34 | "file_extension": ".py", 35 | "mimetype": "text/x-python", 36 | "name": "python", 37 | "nbconvert_exporter": "python", 38 | "pygments_lexer": "ipython3", 39 | "version": "3.7.3" 40 | } 41 | }, 42 | "nbformat": 4, 43 | "nbformat_minor": 2 44 | } 45 | -------------------------------------------------------------------------------- /24 DP-1/24.04 Iterative Dynamic Programming.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Iterative solutions are better than recursive solutions\n", 8 | "\n", 9 | "Iterative solution uses the concept of recurrence relation" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 7, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "8\n", 22 | "21\n" 23 | ] 24 | } 25 | ], 26 | "source": [ 27 | "def fib(n):\n", 28 | " dp = [0 for i in range(n + 1)]\n", 29 | " dp[0] = 0\n", 30 | " dp[1] = 1\n", 31 | " \n", 32 | " for i in range(2, n+1):\n", 33 | " dp[i] = dp[i-1] + dp[i-2] \n", 34 | " \n", 35 | " return dp[n]\n", 36 | " \n", 37 | "n = int(input())\n", 38 | "print(fib(n))" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "Time complexity - O(n)\n", 46 | "\n", 47 | "Space complexity - O(n)" 48 | ] 49 | } 50 | ], 51 | "metadata": { 52 | "kernelspec": { 53 | "display_name": "Python 3", 54 | "language": "python", 55 | "name": "python3" 56 | }, 57 | "language_info": { 58 | "codemirror_mode": { 59 | "name": "ipython", 60 | "version": 3 61 | }, 62 | "file_extension": ".py", 63 | "mimetype": "text/x-python", 64 | "name": "python", 65 | "nbconvert_exporter": "python", 66 | "pygments_lexer": "ipython3", 67 | "version": "3.7.3" 68 | } 69 | }, 70 | "nbformat": 4, 71 | "nbformat_minor": 2 72 | } 73 | -------------------------------------------------------------------------------- /24 DP-1/24.05 Why Iterative solutions are better.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Why Iterative solutions are better?" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "Don't skip to iterative solution directly you may face issues as you're doing if for the first \n", 17 | "time. First try for 15-20 problems (by following the below steps) then you can directly jump to \n", 18 | "Iterative solution\n", 19 | "\n", 20 | "First find \n", 21 | "1) Calculate the recursion or recurrence relation [70%]\n", 22 | "2) Memoization [30%]\n", 23 | "3) Iterative DP" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": null, 29 | "metadata": {}, 30 | "outputs": [], 31 | "source": [ 32 | " Recursion Iterative\n", 33 | "Time O(n) O(n)\n", 34 | "Space O(n) O(n)\n", 35 | "\n", 36 | "\n", 37 | "In recursion, one call is waited for another call and so on so there are chances for \n", 38 | "stack overflow.\n", 39 | "\n", 40 | "Limit for calls:-\n", 41 | " In C++, Java the recursion limit is 10**4 \n", 42 | " In Python, the recursion limit is 10**3 but you can change upto 10**4 by using \n", 43 | " setRecursionLimit().\n", 44 | " \n", 45 | "Iterative solution does not have this stack overflow headache." 46 | ] 47 | } 48 | ], 49 | "metadata": { 50 | "kernelspec": { 51 | "display_name": "Python 3", 52 | "language": "python", 53 | "name": "python3" 54 | }, 55 | "language_info": { 56 | "codemirror_mode": { 57 | "name": "ipython", 58 | "version": 3 59 | }, 60 | "file_extension": ".py", 61 | "mimetype": "text/x-python", 62 | "name": "python", 63 | "nbconvert_exporter": "python", 64 | "pygments_lexer": "ipython3", 65 | "version": "3.7.3" 66 | } 67 | }, 68 | "nbformat": 4, 69 | "nbformat_minor": 2 70 | } 71 | -------------------------------------------------------------------------------- /24 DP-1/img/IMG_20190918_145913.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/24 DP-1/img/IMG_20190918_145913.jpg -------------------------------------------------------------------------------- /24 DP-1/img/README.md: -------------------------------------------------------------------------------- 1 | This folder contains all the images which are used in Dynamic Programming - 1. 2 | 3 | Source: GeeksForGeeks, Coding Ninjas and so on and so forth 4 | -------------------------------------------------------------------------------- /24 DP-1/img/fibdiagram.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/24 DP-1/img/fibdiagram.jpg -------------------------------------------------------------------------------- /24 DP-1/img/fibonacci_tree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/24 DP-1/img/fibonacci_tree.png -------------------------------------------------------------------------------- /24 DP-1/img/lis.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/24 DP-1/img/lis.jpg -------------------------------------------------------------------------------- /24 DP-1/img/memoization-12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/24 DP-1/img/memoization-12.png -------------------------------------------------------------------------------- /25 Recursion-3/25.01 Return All Subsequences Intro.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "A subsequence is a part of string need not be continuous whereas substrings are a part of a \n", 8 | "string which is continuous. \n", 9 | "\n", 10 | "Subsequences and substring are different. " 11 | ] 12 | }, 13 | { 14 | "attachments": {}, 15 | "cell_type": "markdown", 16 | "metadata": {}, 17 | "source": [ 18 | "![substring%20and%20subsequence.jpg](img/substring%20and%20subsequence.jpg)" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": {}, 24 | "source": [ 25 | "For a string of length n there are $2^n$ subsequences " 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 17, 31 | "metadata": {}, 32 | "outputs": [ 33 | { 34 | "name": "stdout", 35 | "output_type": "stream", 36 | "text": [ 37 | "[' ', 'c ', 'b ', 'bc ', 'a ', 'ac ', 'ab ', 'abc ']\n" 38 | ] 39 | } 40 | ], 41 | "source": [ 42 | "def subs(str):\n", 43 | " \n", 44 | " if len(str) == 0:\n", 45 | " output = []\n", 46 | " output.append(\" \")\n", 47 | " return output\n", 48 | " \n", 49 | " smallerString = str[1:]\n", 50 | " smallerOutput = subs(smallerString)\n", 51 | " \n", 52 | " output = []\n", 53 | " for sub in smallerOutput:\n", 54 | " output.append(sub)\n", 55 | " \n", 56 | " for sub in smallerOutput:\n", 57 | " subs_with_zeroth_char = str[0] + sub\n", 58 | " output.append(subs_with_zeroth_char)\n", 59 | " \n", 60 | " return output\n", 61 | " \n", 62 | "print(subs(\"abc\"))\n" 63 | ] 64 | } 65 | ], 66 | "metadata": { 67 | "kernelspec": { 68 | "display_name": "Python 3", 69 | "language": "python", 70 | "name": "python3" 71 | }, 72 | "language_info": { 73 | "codemirror_mode": { 74 | "name": "ipython", 75 | "version": 3 76 | }, 77 | "file_extension": ".py", 78 | "mimetype": "text/x-python", 79 | "name": "python", 80 | "nbconvert_exporter": "python", 81 | "pygments_lexer": "ipython3", 82 | "version": "3.7.3" 83 | } 84 | }, 85 | "nbformat": 4, 86 | "nbformat_minor": 2 87 | } 88 | -------------------------------------------------------------------------------- /25 Recursion-3/25.04 Minimum of Array.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### The following is how we find the minimum number in an array using recursion:" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 2, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "data": { 17 | "text/plain": [ 18 | "-5" 19 | ] 20 | }, 21 | "execution_count": 2, 22 | "metadata": {}, 23 | "output_type": "execute_result" 24 | } 25 | ], 26 | "source": [ 27 | "def minArray(arr):\n", 28 | " if len(arr) == 1:\n", 29 | " return arr[0]\n", 30 | " \n", 31 | " smallMin = minArray(arr[1:])\n", 32 | " overallMin = min(smallMin, arr[0])\n", 33 | " return overallMin\n", 34 | "\n", 35 | "minArray([2,5,4,1,2,3,6,8,7,4,-5]) " 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 7, 41 | "metadata": {}, 42 | "outputs": [ 43 | { 44 | "name": "stdout", 45 | "output_type": "stream", 46 | "text": [ 47 | "-9\n" 48 | ] 49 | } 50 | ], 51 | "source": [ 52 | "def printMin(arr, minSoFar = 999999):\n", 53 | " if len(arr) == 1:\n", 54 | " print(minSoFar) \n", 55 | " return \n", 56 | " \n", 57 | " newMin = min(minSoFar, arr[0])\n", 58 | " printMin(arr[1:], newMin)\n", 59 | " \n", 60 | "printMin([4,2,54,7,3,-2,5,-8,-4,-9,-7,52,36])" 61 | ] 62 | } 63 | ], 64 | "metadata": { 65 | "kernelspec": { 66 | "display_name": "Python 3", 67 | "language": "python", 68 | "name": "python3" 69 | }, 70 | "language_info": { 71 | "codemirror_mode": { 72 | "name": "ipython", 73 | "version": 3 74 | }, 75 | "file_extension": ".py", 76 | "mimetype": "text/x-python", 77 | "name": "python", 78 | "nbconvert_exporter": "python", 79 | "pygments_lexer": "ipython3", 80 | "version": "3.7.3" 81 | } 82 | }, 83 | "nbformat": 4, 84 | "nbformat_minor": 2 85 | } 86 | -------------------------------------------------------------------------------- /25 Recursion-3/25.05 Print All Subsequences.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### abc --> bc --> c --> \" \"\n", 8 | "\n", 9 | "Each character should take decision whether including a character or not\n", 10 | "\n" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 16, 16 | "metadata": {}, 17 | "outputs": [ 18 | { 19 | "name": "stdout", 20 | "output_type": "stream", 21 | "text": [ 22 | "\n", 23 | "c\n", 24 | "b\n", 25 | "bc\n", 26 | "a\n", 27 | "ac\n", 28 | "ab\n", 29 | "abc\n" 30 | ] 31 | } 32 | ], 33 | "source": [ 34 | "def printSub(str, output):\n", 35 | " if len(str) == 0:\n", 36 | " print(output)\n", 37 | " return\n", 38 | " \n", 39 | " # Not including 0th char\n", 40 | " printSub(str[1:], output)\n", 41 | " \n", 42 | " # Including 0th char\n", 43 | " printSub(str[1:], output+str[0])\n", 44 | " \n", 45 | "printSub(\"abc\", \"\")\n", 46 | " " 47 | ] 48 | } 49 | ], 50 | "metadata": { 51 | "kernelspec": { 52 | "display_name": "Python 3", 53 | "language": "python", 54 | "name": "python3" 55 | }, 56 | "language_info": { 57 | "codemirror_mode": { 58 | "name": "ipython", 59 | "version": 3 60 | }, 61 | "file_extension": ".py", 62 | "mimetype": "text/x-python", 63 | "name": "python", 64 | "nbconvert_exporter": "python", 65 | "pygments_lexer": "ipython3", 66 | "version": "3.7.3" 67 | } 68 | }, 69 | "nbformat": 4, 70 | "nbformat_minor": 2 71 | } 72 | -------------------------------------------------------------------------------- /25 Recursion-3/25.06 Print keypad combinations.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 11, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "adg\n", 13 | "bdg\n", 14 | "cdg\n", 15 | "aeg\n", 16 | "beg\n", 17 | "ceg\n", 18 | "afg\n", 19 | "bfg\n", 20 | "cfg\n", 21 | "adh\n", 22 | "bdh\n", 23 | "cdh\n", 24 | "aeh\n", 25 | "beh\n", 26 | "ceh\n", 27 | "afh\n", 28 | "bfh\n", 29 | "cfh\n", 30 | "adi\n", 31 | "bdi\n", 32 | "cdi\n", 33 | "aei\n", 34 | "bei\n", 35 | "cei\n", 36 | "afi\n", 37 | "bfi\n", 38 | "cfi\n" 39 | ] 40 | } 41 | ], 42 | "source": [ 43 | "def getString(d):\n", 44 | " if d == 2:\n", 45 | " return \"abc\"\n", 46 | " if d == 3:\n", 47 | " return \"def\"\n", 48 | " if d == 4:\n", 49 | " return \"ghi\"\n", 50 | " if d == 5:\n", 51 | " return \"jkl\"\n", 52 | " if d == 6:\n", 53 | " return \"mno\"\n", 54 | " if d == 7:\n", 55 | " return \"pqrs\"\n", 56 | " if d == 8:\n", 57 | " return \"tuv\"\n", 58 | " if d == 9:\n", 59 | " return \"wxyz\"\n", 60 | " return \" \" \n", 61 | " \n", 62 | "\n", 63 | "def keypad(input, output):\n", 64 | " if input == 0:\n", 65 | " print(output)\n", 66 | " return \n", 67 | " \n", 68 | " remainder = input%10 \n", 69 | " input = input//10\n", 70 | " \n", 71 | " text = getString(remainder)\n", 72 | " \n", 73 | " for s in text: \n", 74 | " keypad(input, s+output)\n", 75 | " \n", 76 | "keypad(234, \"\")" 77 | ] 78 | } 79 | ], 80 | "metadata": { 81 | "kernelspec": { 82 | "display_name": "Python 3", 83 | "language": "python", 84 | "name": "python3" 85 | }, 86 | "language_info": { 87 | "codemirror_mode": { 88 | "name": "ipython", 89 | "version": 3 90 | }, 91 | "file_extension": ".py", 92 | "mimetype": "text/x-python", 93 | "name": "python", 94 | "nbconvert_exporter": "python", 95 | "pygments_lexer": "ipython3", 96 | "version": "3.7.3" 97 | } 98 | }, 99 | "nbformat": 4, 100 | "nbformat_minor": 2 101 | } 102 | -------------------------------------------------------------------------------- /25 Recursion-3/25.07 return subset to sum k.txt: -------------------------------------------------------------------------------- 1 | # This solution is more optimized. Please have a look at this solution and merge. 2 | 3 | def subset(arr,k): 4 | n=len(arr) 5 | if n == 1: 6 | if arr[0]==k: 7 | output = [ [k] ] 8 | return output 9 | else: 10 | output = [] 11 | return output 12 | 13 | output1 = subset(arr[:-1], k) 14 | output2 = subset(arr[:-1], k - arr[-1]) 15 | 16 | for lst in output2: 17 | lst.append(arr[-1]) 18 | 19 | if arr[-1] == k: 20 | output2.append([k]) 21 | 22 | return output1 + output2 23 | 24 | print(subset([2,3,4,3], 6)) 25 | -------------------------------------------------------------------------------- /25 Recursion-3/25.08 Return Subsets.txt: -------------------------------------------------------------------------------- 1 | # This solution is more optimised and easily understandable. Please have a look and merge 2 | def subset(arr): 3 | n = len(arr) 4 | if n <= 0: 5 | output = [ [] ] 6 | return output 7 | 8 | output = subset(arr[:n-1]) 9 | outputLen = len(output) 10 | for i in range(outputLen): 11 | output.append(output[i].copy()) 12 | output[outputLen + i].append(arr[n-1]) 13 | 14 | return output 15 | 16 | for j in subset([1,2,3,4]): 17 | print(j) 18 | -------------------------------------------------------------------------------- /25 Recursion-3/25.09 return all permutations.txt: -------------------------------------------------------------------------------- 1 | # We can also use join operator 2 | from itertools import permutations 3 | 4 | def totalPermutations(str): 5 | 6 | permList = permutations(str) 7 | for perm in list(permList): 8 | print (''.join(perm)) 9 | 10 | str = 'abcd' 11 | totalPermutations(str) 12 | -------------------------------------------------------------------------------- /25 Recursion-3/img/keypad.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/25 Recursion-3/img/keypad.png -------------------------------------------------------------------------------- /25 Recursion-3/img/permutation recursion tree.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/25 Recursion-3/img/permutation recursion tree.gif -------------------------------------------------------------------------------- /25 Recursion-3/img/substring and subsequence.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/25 Recursion-3/img/substring and subsequence.jpg -------------------------------------------------------------------------------- /3 OOPS-1/3.01 Intro to OOPS.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "#### Basic idea behind OOP \n", 8 | "\n", 9 | "batch.giveAdmission(student)\n", 10 | "\n", 11 | "batch --> object\n", 12 | "\n", 13 | "giveAdmission --> function\n", 14 | "\n", 15 | "student --> an argument\n", 16 | "\n", 17 | "We have classes and objects:\n", 18 | "\n", 19 | "Objects like batch, student and so on and so forth\n", 20 | "\n", 21 | "There may have lots of batches and students\n", 22 | "\n", 23 | "** Class is basically a template\n", 24 | "\n", 25 | "** Object is the instance of the class\n", 26 | "\n" 27 | ] 28 | } 29 | ], 30 | "metadata": { 31 | "kernelspec": { 32 | "display_name": "Python 3", 33 | "language": "python", 34 | "name": "python3" 35 | }, 36 | "language_info": { 37 | "codemirror_mode": { 38 | "name": "ipython", 39 | "version": 3 40 | }, 41 | "file_extension": ".py", 42 | "mimetype": "text/x-python", 43 | "name": "python", 44 | "nbconvert_exporter": "python", 45 | "pygments_lexer": "ipython3", 46 | "version": "3.7.3" 47 | } 48 | }, 49 | "nbformat": 4, 50 | "nbformat_minor": 2 51 | } 52 | -------------------------------------------------------------------------------- /3 OOPS-1/3.02 How to create classes & objects.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 6, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# Classs canot be empty you must write something in it atleast pass\n", 10 | "class Student: \n", 11 | " pass\n", 12 | "s1 = Student()" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 2, 18 | "metadata": {}, 19 | "outputs": [ 20 | { 21 | "name": "stdout", 22 | "output_type": "stream", 23 | "text": [ 24 | "<__main__.Student object at 0x0000021CEA65DEF0>\n" 25 | ] 26 | } 27 | ], 28 | "source": [ 29 | "print(s1)" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 3, 35 | "metadata": {}, 36 | "outputs": [ 37 | { 38 | "name": "stdout", 39 | "output_type": "stream", 40 | "text": [ 41 | "\n" 42 | ] 43 | } 44 | ], 45 | "source": [ 46 | "print(type(s1))" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 4, 52 | "metadata": {}, 53 | "outputs": [ 54 | { 55 | "name": "stdout", 56 | "output_type": "stream", 57 | "text": [ 58 | "\n" 59 | ] 60 | } 61 | ], 62 | "source": [ 63 | "list = [1,22,4]\n", 64 | "print(type(list))" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 5, 70 | "metadata": {}, 71 | "outputs": [ 72 | { 73 | "name": "stdout", 74 | "output_type": "stream", 75 | "text": [ 76 | "<__main__.Student object at 0x0000021CEA70D3C8> <__main__.Student object at 0x0000021CEA70D400>\n" 77 | ] 78 | } 79 | ], 80 | "source": [ 81 | "s2 = Student()\n", 82 | "s3 = Student()\n", 83 | "print(s2, s3)" 84 | ] 85 | } 86 | ], 87 | "metadata": { 88 | "kernelspec": { 89 | "display_name": "Python 3", 90 | "language": "python", 91 | "name": "python3" 92 | }, 93 | "language_info": { 94 | "codemirror_mode": { 95 | "name": "ipython", 96 | "version": 3 97 | }, 98 | "file_extension": ".py", 99 | "mimetype": "text/x-python", 100 | "name": "python", 101 | "nbconvert_exporter": "python", 102 | "pygments_lexer": "ipython3", 103 | "version": "3.7.3" 104 | } 105 | }, 106 | "nbformat": 4, 107 | "nbformat_minor": 2 108 | } 109 | -------------------------------------------------------------------------------- /3 OOPS-1/3.05 Instance Methods.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 16, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "class Student:\n", 10 | " def __init__(self, name, rollNumber): #self - the object we're creating\n", 11 | " self.name = name\n", 12 | " self.rollNumber = rollNumber \n", 13 | " def printStudent(self): #While s1.printStudent() is called\n", 14 | " print(\"My name is \", self.name, \"and my roll number is \", self.rollNumber)" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 17, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "s1 = Student(\"Fazeel\", 36)" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 18, 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "data": { 33 | "text/plain": [ 34 | "{'name': 'Fazeel', 'rollNumber': 36}" 35 | ] 36 | }, 37 | "execution_count": 18, 38 | "metadata": {}, 39 | "output_type": "execute_result" 40 | } 41 | ], 42 | "source": [ 43 | "s1.__dict__" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 19, 49 | "metadata": {}, 50 | "outputs": [ 51 | { 52 | "name": "stdout", 53 | "output_type": "stream", 54 | "text": [ 55 | "My name is Fazeel and my roll number is 36\n" 56 | ] 57 | } 58 | ], 59 | "source": [ 60 | "s1.printStudent() # s1 is passed to you as the argument to the function (self is being passed)" 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": {}, 66 | "source": [ 67 | "### Calling class with s1 as an attribute Student.printStudent(s1) " 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 20, 73 | "metadata": {}, 74 | "outputs": [ 75 | { 76 | "name": "stdout", 77 | "output_type": "stream", 78 | "text": [ 79 | "My name is Fazeel and my roll number is 36\n" 80 | ] 81 | } 82 | ], 83 | "source": [ 84 | "Student.printStudent(s1)" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": null, 90 | "metadata": {}, 91 | "outputs": [], 92 | "source": [] 93 | } 94 | ], 95 | "metadata": { 96 | "kernelspec": { 97 | "display_name": "Python 3", 98 | "language": "python", 99 | "name": "python3" 100 | }, 101 | "language_info": { 102 | "codemirror_mode": { 103 | "name": "ipython", 104 | "version": 3 105 | }, 106 | "file_extension": ".py", 107 | "mimetype": "text/x-python", 108 | "name": "python", 109 | "nbconvert_exporter": "python", 110 | "pygments_lexer": "ipython3", 111 | "version": "3.7.3" 112 | } 113 | }, 114 | "nbformat": 4, 115 | "nbformat_minor": 2 116 | } 117 | -------------------------------------------------------------------------------- /3 OOPS-1/3.06 Fraction Class - I.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### f = Fraction(2,3)\n", 8 | "f.print() ---> 2/3\n", 9 | " #You can have only one init function in a class\n", 10 | "f.add(f2) " 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 17, 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "class Fraction:\n", 20 | " def __init__(self, num = 0, den = 1): # Here we're using default argument values concept\n", 21 | " self.num = num\n", 22 | " self.den = den\n", 23 | " \n", 24 | " def print(self):\n", 25 | " print(num, \"/\", den)\n", 26 | " \n", 27 | " " 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 18, 33 | "metadata": {}, 34 | "outputs": [ 35 | { 36 | "data": { 37 | "text/plain": [ 38 | "{'num': 2, 'den': 3}" 39 | ] 40 | }, 41 | "execution_count": 18, 42 | "metadata": {}, 43 | "output_type": "execute_result" 44 | } 45 | ], 46 | "source": [ 47 | "f = Fraction(2,3)\n", 48 | "f.__dict__" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 19, 54 | "metadata": {}, 55 | "outputs": [ 56 | { 57 | "data": { 58 | "text/plain": [ 59 | "{'num': 0, 'den': 1}" 60 | ] 61 | }, 62 | "execution_count": 19, 63 | "metadata": {}, 64 | "output_type": "execute_result" 65 | } 66 | ], 67 | "source": [ 68 | "f2 = Fraction()\n", 69 | "f2.__dict__" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 20, 75 | "metadata": {}, 76 | "outputs": [ 77 | { 78 | "data": { 79 | "text/plain": [ 80 | "{'num': 3, 'den': 1}" 81 | ] 82 | }, 83 | "execution_count": 20, 84 | "metadata": {}, 85 | "output_type": "execute_result" 86 | } 87 | ], 88 | "source": [ 89 | "f3 = Fraction(3)\n", 90 | "f3.__dict__" 91 | ] 92 | } 93 | ], 94 | "metadata": { 95 | "kernelspec": { 96 | "display_name": "Python 3", 97 | "language": "python", 98 | "name": "python3" 99 | }, 100 | "language_info": { 101 | "codemirror_mode": { 102 | "name": "ipython", 103 | "version": 3 104 | }, 105 | "file_extension": ".py", 106 | "mimetype": "text/x-python", 107 | "name": "python", 108 | "nbconvert_exporter": "python", 109 | "pygments_lexer": "ipython3", 110 | "version": "3.7.3" 111 | } 112 | }, 113 | "nbformat": 4, 114 | "nbformat_minor": 2 115 | } 116 | -------------------------------------------------------------------------------- /3 OOPS-1/3.09 Complex Number.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 14, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "class cNumber:\n", 10 | " def __init__(self, real, imag):\n", 11 | " self.real = real\n", 12 | " self.imag = imag\n", 13 | " \n", 14 | " def print(self):\n", 15 | " print(self.real, '+ i', end = \"\")\n", 16 | " print(self.imag)\n", 17 | " " 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 17, 23 | "metadata": {}, 24 | "outputs": [ 25 | { 26 | "data": { 27 | "text/plain": [ 28 | "{'real': 4, 'imag': 5}" 29 | ] 30 | }, 31 | "execution_count": 17, 32 | "metadata": {}, 33 | "output_type": "execute_result" 34 | } 35 | ], 36 | "source": [ 37 | "c = cNumber(4,5)\n", 38 | "c.__dict__" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 18, 44 | "metadata": {}, 45 | "outputs": [ 46 | { 47 | "name": "stdout", 48 | "output_type": "stream", 49 | "text": [ 50 | "4 + i5\n" 51 | ] 52 | } 53 | ], 54 | "source": [ 55 | "c.print()" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": null, 61 | "metadata": {}, 62 | "outputs": [], 63 | "source": [] 64 | } 65 | ], 66 | "metadata": { 67 | "kernelspec": { 68 | "display_name": "Python 3", 69 | "language": "python", 70 | "name": "python3" 71 | }, 72 | "language_info": { 73 | "codemirror_mode": { 74 | "name": "ipython", 75 | "version": 3 76 | }, 77 | "file_extension": ".py", 78 | "mimetype": "text/x-python", 79 | "name": "python", 80 | "nbconvert_exporter": "python", 81 | "pygments_lexer": "ipython3", 82 | "version": "3.7.3" 83 | } 84 | }, 85 | "nbformat": 4, 86 | "nbformat_minor": 2 87 | } 88 | -------------------------------------------------------------------------------- /3 OOPS-1/3.11 Complex Number.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "4 5\n", 13 | "1 2\n", 14 | "1\n", 15 | "5 + i7\n" 16 | ] 17 | } 18 | ], 19 | "source": [ 20 | "# Based on the assignment\n", 21 | "\n", 22 | "class cNumber:\n", 23 | " def __init__(self, real, imag):\n", 24 | " self.real = real\n", 25 | " self.imag = imag\n", 26 | " \n", 27 | " def print(self):\n", 28 | " print(self.real, '+ i', end = \"\")\n", 29 | " print(self.imag)\n", 30 | " \n", 31 | " def add(self, otherNumber):\n", 32 | " newReal = self.real + otherNumber.real\n", 33 | " newImag = self.imag + otherNumber.imag\n", 34 | " self.real = newReal\n", 35 | " self.imag = newImag\n", 36 | " self.print()\n", 37 | " \n", 38 | " def multiply(self, otherNumber):\n", 39 | " newReal = self.real * otherNumber.real - self.imag * otherNumber.imag\n", 40 | " newImag = self.imag * otherNumber.real + self.real * otherNumber.imag\n", 41 | " self.real = newReal\n", 42 | " self.imag = newImag\n", 43 | " self.print()\n", 44 | "\n", 45 | "str1 = input().split()\n", 46 | "str2 = input().split()\n", 47 | "\n", 48 | "a,b = int(str1[0]), int(str1[1])\n", 49 | "c,d = int(str2[0]), int(str2[1])\n", 50 | "operation = int(input()) #1-addition | 2-multiplication\n", 51 | "\n", 52 | "c1 = cNumber(a,b)\n", 53 | "c2 = cNumber(c,d)\n", 54 | "\n", 55 | "if operation == 1:\n", 56 | " c1.add(c2)\n", 57 | "elif operation == 2:\n", 58 | " c1.multiply(c2)\n", 59 | "\n" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": null, 65 | "metadata": {}, 66 | "outputs": [], 67 | "source": [] 68 | } 69 | ], 70 | "metadata": { 71 | "kernelspec": { 72 | "display_name": "Python 3", 73 | "language": "python", 74 | "name": "python3" 75 | }, 76 | "language_info": { 77 | "codemirror_mode": { 78 | "name": "ipython", 79 | "version": 3 80 | }, 81 | "file_extension": ".py", 82 | "mimetype": "text/x-python", 83 | "name": "python", 84 | "nbconvert_exporter": "python", 85 | "pygments_lexer": "ipython3", 86 | "version": "3.7.3" 87 | } 88 | }, 89 | "nbformat": 4, 90 | "nbformat_minor": 2 91 | } 92 | -------------------------------------------------------------------------------- /4 OOPS-2/4.6 Object class.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "#### By default, every class is inherited from Object class.\n", 8 | "\n", 9 | "There are 3 methods in object class:\n", 10 | "\n", 11 | "1) __new__ --> create new object. Used to override an object\n", 12 | "\n", 13 | "2) __init__ --> We use it in every class\n", 14 | "\n", 15 | "3) __str__ --> Used to give the description of a class\n", 16 | "\n" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 1, 22 | "metadata": {}, 23 | "outputs": [ 24 | { 25 | "name": "stdout", 26 | "output_type": "stream", 27 | "text": [ 28 | "<__main__.Circle object at 0x000001D0C67B0390>\n" 29 | ] 30 | } 31 | ], 32 | "source": [ 33 | "class Circle(object):\n", 34 | " \n", 35 | " def __init__(self, radius):\n", 36 | " self.__radius = radius\n", 37 | " \n", 38 | "c = Circle(3)\n", 39 | "print(c)" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 3, 45 | "metadata": {}, 46 | "outputs": [ 47 | { 48 | "name": "stdout", 49 | "output_type": "stream", 50 | "text": [ 51 | "This is a Circle class which takes radius as an argument\n" 52 | ] 53 | } 54 | ], 55 | "source": [ 56 | "# If you need description of the class the use __str__()\n", 57 | "class Circle(object):\n", 58 | " \n", 59 | " def __init__(self, radius):\n", 60 | " self.__radius = radius\n", 61 | " \n", 62 | " def __str__(self):\n", 63 | " return \"This is a Circle class which takes radius as an argument\"\n", 64 | " \n", 65 | "c = Circle(3)\n", 66 | "print(c)" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 6, 72 | "metadata": {}, 73 | "outputs": [ 74 | { 75 | "name": "stdout", 76 | "output_type": "stream", 77 | "text": [ 78 | "This is a Circle Class\n" 79 | ] 80 | } 81 | ], 82 | "source": [ 83 | "#Predict the output:\n", 84 | "class Circle(object):\n", 85 | " def __str__(self):\n", 86 | " return \"This is a Circle Class\"\n", 87 | " \n", 88 | "c = Circle()\n", 89 | "print(c)" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": null, 95 | "metadata": {}, 96 | "outputs": [], 97 | "source": [] 98 | } 99 | ], 100 | "metadata": { 101 | "kernelspec": { 102 | "display_name": "Python 3", 103 | "language": "python", 104 | "name": "python3" 105 | }, 106 | "language_info": { 107 | "codemirror_mode": { 108 | "name": "ipython", 109 | "version": 3 110 | }, 111 | "file_extension": ".py", 112 | "mimetype": "text/x-python", 113 | "name": "python", 114 | "nbconvert_exporter": "python", 115 | "pygments_lexer": "ipython3", 116 | "version": "3.7.3" 117 | } 118 | }, 119 | "nbformat": 4, 120 | "nbformat_minor": 2 121 | } 122 | -------------------------------------------------------------------------------- /6 Time Complexity/6.2 Excercises.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# Time Complexity?\n", 10 | "\n", 11 | "def multiplyRec(m, n):\n", 12 | " if n==1:\n", 13 | " return m\n", 14 | " return m + multiplyRec(m, n - 1)\n", 15 | "\n", 16 | "#Ans:- " 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": null, 22 | "metadata": {}, 23 | "outputs": [], 24 | "source": [] 25 | } 26 | ], 27 | "metadata": { 28 | "kernelspec": { 29 | "display_name": "Python 3", 30 | "language": "python", 31 | "name": "python3" 32 | }, 33 | "language_info": { 34 | "codemirror_mode": { 35 | "name": "ipython", 36 | "version": 3 37 | }, 38 | "file_extension": ".py", 39 | "mimetype": "text/x-python", 40 | "name": "python", 41 | "nbconvert_exporter": "python", 42 | "pygments_lexer": "ipython3", 43 | "version": "3.7.3" 44 | } 45 | }, 46 | "nbformat": 4, 47 | "nbformat_minor": 2 48 | } 49 | -------------------------------------------------------------------------------- /8 Time Complexity Improvement/8.4 Find a unique number in a list.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "5\n", 13 | "3 2 5 5 3\n", 14 | "2\n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "# Here we need to find a unique element in a list in < 10^6 time\n", 20 | "\n", 21 | "def FindUnique(arr):\n", 22 | " unique = arr[0]\n", 23 | " for i in range(1, len(arr)):\n", 24 | " unique = unique ^ arr[i]\n", 25 | " \n", 26 | " return unique\n", 27 | " \n", 28 | "n=int(input())\n", 29 | "arr=list(int(i) for i in input().strip().split(' '))\n", 30 | "unique=FindUnique(arr)\n", 31 | "print(unique)" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": null, 37 | "metadata": {}, 38 | "outputs": [], 39 | "source": [] 40 | } 41 | ], 42 | "metadata": { 43 | "kernelspec": { 44 | "display_name": "Python 3", 45 | "language": "python", 46 | "name": "python3" 47 | }, 48 | "language_info": { 49 | "codemirror_mode": { 50 | "name": "ipython", 51 | "version": 3 52 | }, 53 | "file_extension": ".py", 54 | "mimetype": "text/x-python", 55 | "name": "python", 56 | "nbconvert_exporter": "python", 57 | "pygments_lexer": "ipython3", 58 | "version": "3.7.3" 59 | } 60 | }, 61 | "nbformat": 4, 62 | "nbformat_minor": 2 63 | } 64 | -------------------------------------------------------------------------------- /8 Time Complexity Improvement/8.5 Duplicate element in an array.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 4, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "9\n", 13 | "1 0 8 9 -3 3 8 5 3\n", 14 | "11\n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "# Optimised solution < 10^6 \n", 20 | "\n", 21 | "def MissingNumber(arr):\n", 22 | " sum1 = sum(arr)\n", 23 | " x = set()\n", 24 | " for i in arr:\n", 25 | " x.add(i)\n", 26 | " return sum1 - sum(x)\n", 27 | " \n", 28 | "\n", 29 | "n=int(input())\n", 30 | "arr=list(int(i) for i in input().strip().split(' '))\n", 31 | "ans=MissingNumber(arr)\n", 32 | "print(ans)" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": null, 38 | "metadata": {}, 39 | "outputs": [], 40 | "source": [] 41 | } 42 | ], 43 | "metadata": { 44 | "kernelspec": { 45 | "display_name": "Python 3", 46 | "language": "python", 47 | "name": "python3" 48 | }, 49 | "language_info": { 50 | "codemirror_mode": { 51 | "name": "ipython", 52 | "version": 3 53 | }, 54 | "file_extension": ".py", 55 | "mimetype": "text/x-python", 56 | "name": "python", 57 | "nbconvert_exporter": "python", 58 | "pygments_lexer": "ipython3", 59 | "version": "3.7.3" 60 | } 61 | }, 62 | "nbformat": 4, 63 | "nbformat_minor": 2 64 | } 65 | -------------------------------------------------------------------------------- /8 Time Complexity Improvement/8.6 Pair sum in a list.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "7\n", 13 | "1 2 5 4 3 6 8 \n", 14 | "6\n", 15 | "1 5\n", 16 | "2 4\n", 17 | "2 4\n", 18 | "3 3\n" 19 | ] 20 | } 21 | ], 22 | "source": [ 23 | "def pairSum(arr, x):\n", 24 | " for i in range(0, len(arr)-1):\n", 25 | " for j in range(1, len(arr)):\n", 26 | " if arr[i] + arr[j] == x:\n", 27 | " if arr[i] > arr[j]:\n", 28 | " print(arr[j], arr[i])\n", 29 | " else:\n", 30 | " print(arr[i], arr[j])\n", 31 | " \n", 32 | " \n", 33 | " \n", 34 | " \n", 35 | "n=int(input())\n", 36 | "arr=list(int(i) for i in input().strip().split(' '))\n", 37 | "sum=int(input())\n", 38 | "pairSum(arr, sum)\n" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": null, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [] 47 | } 48 | ], 49 | "metadata": { 50 | "kernelspec": { 51 | "display_name": "Python 3", 52 | "language": "python", 53 | "name": "python3" 54 | }, 55 | "language_info": { 56 | "codemirror_mode": { 57 | "name": "ipython", 58 | "version": 3 59 | }, 60 | "file_extension": ".py", 61 | "mimetype": "text/x-python", 62 | "name": "python", 63 | "nbconvert_exporter": "python", 64 | "pygments_lexer": "ipython3", 65 | "version": "3.7.3" 66 | } 67 | }, 68 | "nbformat": 4, 69 | "nbformat_minor": 2 70 | } 71 | -------------------------------------------------------------------------------- /8 Time Complexity Improvement/8.8 Rotate array.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "5\n", 13 | "2 36 1 4 2\n", 14 | "12\n", 15 | "1 4 2 2 36 " 16 | ] 17 | } 18 | ], 19 | "source": [ 20 | "def Rotate(arr, d):\n", 21 | " d = d % len(arr)\n", 22 | " for i in range(d, len(arr)):\n", 23 | " print(arr[i], end=\" \")\n", 24 | " for i in range(0, d):\n", 25 | " print(arr[i], end=\" \")\n", 26 | " \n", 27 | "\n", 28 | "n=int(input())\n", 29 | "arr=list(int(i) for i in input().strip().split(' '))\n", 30 | "d=int(input())\n", 31 | "Rotate(arr, d)\n" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": null, 37 | "metadata": {}, 38 | "outputs": [], 39 | "source": [] 40 | } 41 | ], 42 | "metadata": { 43 | "kernelspec": { 44 | "display_name": "Python 3", 45 | "language": "python", 46 | "name": "python3" 47 | }, 48 | "language_info": { 49 | "codemirror_mode": { 50 | "name": "ipython", 51 | "version": 3 52 | }, 53 | "file_extension": ".py", 54 | "mimetype": "text/x-python", 55 | "name": "python", 56 | "nbconvert_exporter": "python", 57 | "pygments_lexer": "ipython3", 58 | "version": "3.7.3" 59 | } 60 | }, 61 | "nbformat": 4, 62 | "nbformat_minor": 2 63 | } 64 | -------------------------------------------------------------------------------- /9 Linked List-1/9.02 Creating a linked list by taking input from the user.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 4, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "1 2 3 45\n", 13 | "1\n", 14 | "2\n", 15 | "3\n", 16 | "45\n" 17 | ] 18 | } 19 | ], 20 | "source": [ 21 | "class Node:\n", 22 | " \n", 23 | " def __init__(self, data):\n", 24 | " self.data = data\n", 25 | " self.next = None\n", 26 | "\n", 27 | "def takeInput():\n", 28 | " \n", 29 | " arr = list(int(ele) for ele in input().split()) \n", 30 | " head = None\n", 31 | " for currData in arr:\n", 32 | " if currData == -1:\n", 33 | " break\n", 34 | " \n", 35 | " newNode = Node(currData) # Creation of a node\n", 36 | " \n", 37 | " if head is None:\n", 38 | " head = newNode\n", 39 | " else: \n", 40 | " curr = head\n", 41 | " while curr.next is not None:\n", 42 | " curr = curr.next\n", 43 | " curr.next = newNode \n", 44 | " return head\n", 45 | "\n", 46 | "head = takeInput()\n", 47 | "\n", 48 | "curr = head\n", 49 | "while curr != None:\n", 50 | " print(curr.data)\n", 51 | " curr = curr.next" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": null, 57 | "metadata": {}, 58 | "outputs": [], 59 | "source": [] 60 | } 61 | ], 62 | "metadata": { 63 | "kernelspec": { 64 | "display_name": "Python 3", 65 | "language": "python", 66 | "name": "python3" 67 | }, 68 | "language_info": { 69 | "codemirror_mode": { 70 | "name": "ipython", 71 | "version": 3 72 | }, 73 | "file_extension": ".py", 74 | "mimetype": "text/x-python", 75 | "name": "python", 76 | "nbconvert_exporter": "python", 77 | "pygments_lexer": "ipython3", 78 | "version": "3.7.3" 79 | } 80 | }, 81 | "nbformat": 4, 82 | "nbformat_minor": 2 83 | } 84 | -------------------------------------------------------------------------------- /9 Linked List-1/9.03 Printing Linked List.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 7, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "1 2 3 4 5\n", 13 | "1->2->3->4->5->None\n" 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "class Node:\n", 19 | " \n", 20 | " def __init__(self, data):\n", 21 | " self.data = data\n", 22 | " self.next = None\n", 23 | "\n", 24 | "def printLL(head):\n", 25 | " while head is not None:\n", 26 | " print(str(head.data) + \"->\", end = \"\") # We're also printing -> so we need to convert head.data to string\n", 27 | " head = head.next\n", 28 | " print(\"None\")\n", 29 | " return\n", 30 | "\n", 31 | "def takeInput():\n", 32 | " \n", 33 | " arr = list(int(ele) for ele in input().split()) \n", 34 | " head = None\n", 35 | " for currData in arr:\n", 36 | " if currData == -1:\n", 37 | " break\n", 38 | " \n", 39 | " newNode = Node(currData) # Creation of a node\n", 40 | " \n", 41 | " if head is None:\n", 42 | " head = newNode\n", 43 | " else: \n", 44 | " curr = head\n", 45 | " while curr.next is not None:\n", 46 | " curr = curr.next\n", 47 | " curr.next = newNode \n", 48 | " return head\n", 49 | "\n", 50 | "head = takeInput() #This head is different from the actual head of the LL\n", 51 | "printLL(head)" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": null, 57 | "metadata": {}, 58 | "outputs": [], 59 | "source": [] 60 | } 61 | ], 62 | "metadata": { 63 | "kernelspec": { 64 | "display_name": "Python 3", 65 | "language": "python", 66 | "name": "python3" 67 | }, 68 | "language_info": { 69 | "codemirror_mode": { 70 | "name": "ipython", 71 | "version": 3 72 | }, 73 | "file_extension": ".py", 74 | "mimetype": "text/x-python", 75 | "name": "python", 76 | "nbconvert_exporter": "python", 77 | "pygments_lexer": "ipython3", 78 | "version": "3.7.3" 79 | } 80 | }, 81 | "nbformat": 4, 82 | "nbformat_minor": 2 83 | } 84 | -------------------------------------------------------------------------------- /9 Linked List-1/9.05 length of Linked List.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "1 2 3 4 5 6\n", 13 | "[1, 2, 3, 4, 5]\n", 14 | "5\n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "class Node:\n", 20 | " def __init__(self, data):\n", 21 | " self.data = data\n", 22 | " self.next = None\n", 23 | "\n", 24 | "def length(head):\n", 25 | " curr = head\n", 26 | " cnt = 0\n", 27 | " while curr is not None:\n", 28 | " cnt += 1\n", 29 | " curr = curr.next\n", 30 | " \n", 31 | " return cnt\n", 32 | "\n", 33 | "def ll(arr):\n", 34 | " print(arr)\n", 35 | " if len(arr)==0:\n", 36 | " return None\n", 37 | " head = Node(arr[0])\n", 38 | " last = head\n", 39 | " for data in arr[1:]:\n", 40 | " last.next = Node(data)\n", 41 | " last = last.next\n", 42 | " return head\n", 43 | "\n", 44 | "# Main\n", 45 | "# Read the link list elements including -1\n", 46 | "arr=list(int(i) for i in input().strip().split(' '))\n", 47 | "# Create a Linked list after removing -1 from list\n", 48 | "l = ll(arr[:-1])\n", 49 | "len=length(l)\n", 50 | "print(len)\n" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": null, 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [] 59 | } 60 | ], 61 | "metadata": { 62 | "kernelspec": { 63 | "display_name": "Python 3", 64 | "language": "python", 65 | "name": "python3" 66 | }, 67 | "language_info": { 68 | "codemirror_mode": { 69 | "name": "ipython", 70 | "version": 3 71 | }, 72 | "file_extension": ".py", 73 | "mimetype": "text/x-python", 74 | "name": "python", 75 | "nbconvert_exporter": "python", 76 | "pygments_lexer": "ipython3", 77 | "version": "3.7.3" 78 | } 79 | }, 80 | "nbformat": 4, 81 | "nbformat_minor": 2 82 | } 83 | -------------------------------------------------------------------------------- /9 Linked List-1/9.06 Print ith node.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "1 2 3 4 5 6 7 -1\n", 13 | "2\n", 14 | "3\n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "class Node:\n", 20 | " def __init__(self, data):\n", 21 | " self.data = data\n", 22 | " self.next = None\n", 23 | "\n", 24 | "\n", 25 | "def ithNode(head, i):\n", 26 | " cnt = 0\n", 27 | " while head is not None:\n", 28 | " if cnt == i:\n", 29 | " return head\n", 30 | " head = head.next\n", 31 | " cnt = cnt + 1\n", 32 | " \n", 33 | "\n", 34 | "def ll(arr):\n", 35 | " if len(arr)==0:\n", 36 | " return None\n", 37 | " head = Node(arr[0])\n", 38 | " last = head\n", 39 | " for data in arr[1:]:\n", 40 | " last.next = Node(data)\n", 41 | " last = last.next\n", 42 | " return head\n", 43 | "\n", 44 | "# Main\n", 45 | "# Read the link list elements including -1\n", 46 | "arr=list(int(i) for i in input().strip().split(' '))\n", 47 | "# Create a Linked list after removing -1 from list\n", 48 | "l = ll(arr[:-1])\n", 49 | "i=int(input())\n", 50 | "node = ithNode(l, i)\n", 51 | "if node:\n", 52 | " print(node.data)\n" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": null, 58 | "metadata": {}, 59 | "outputs": [], 60 | "source": [] 61 | } 62 | ], 63 | "metadata": { 64 | "kernelspec": { 65 | "display_name": "Python 3", 66 | "language": "python", 67 | "name": "python3" 68 | }, 69 | "language_info": { 70 | "codemirror_mode": { 71 | "name": "ipython", 72 | "version": 3 73 | }, 74 | "file_extension": ".py", 75 | "mimetype": "text/x-python", 76 | "name": "python", 77 | "nbconvert_exporter": "python", 78 | "pygments_lexer": "ipython3", 79 | "version": "3.7.3" 80 | } 81 | }, 82 | "nbformat": 4, 83 | "nbformat_minor": 2 84 | } 85 | -------------------------------------------------------------------------------- /9 Linked List-1/9.08 Delete node.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "class Node:\n", 10 | " def __init__(self, data):\n", 11 | " self.data = data\n", 12 | " self.next = None\n", 13 | "\n", 14 | "def length(head):\n", 15 | " cnt = 0\n", 16 | " while head is not None:\n", 17 | " cnt += 1\n", 18 | " return cnt\n", 19 | " \n", 20 | "def delete(head, i):\n", 21 | " if i < 0 or i > length(head):\n", 22 | " return head\n", 23 | " if i == 0:\n", 24 | " head = head.next\n", 25 | " else:\n", 26 | " curr = head\n", 27 | " for j in range(0, i):\n", 28 | " curr = curr.next\n", 29 | " end = curr.next.next\n", 30 | " curr.next.next = None\n", 31 | " curr.next = end \n", 32 | " \n", 33 | " return head\n", 34 | " \n", 35 | "\n", 36 | "def ll(arr):\n", 37 | " if len(arr)==0:\n", 38 | " return None\n", 39 | " head = Node(arr[0])\n", 40 | " last = head\n", 41 | " for data in arr[1:]:\n", 42 | " last.next = Node(data)\n", 43 | " last = last.next\n", 44 | " return head\n", 45 | "\n", 46 | "def printll(head):\n", 47 | " while head:\n", 48 | " print(head.data, end=' ')\n", 49 | " head = head.next\n", 50 | " print()\n", 51 | "\n", 52 | "arr=list(int(i) for i in input().strip().split(' '))\n", 53 | "\n", 54 | "l = ll(arr[:-1])\n", 55 | "i=int(input())\n", 56 | "l = delete(l, i)\n", 57 | "printll(l)\n" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": null, 63 | "metadata": {}, 64 | "outputs": [], 65 | "source": [] 66 | } 67 | ], 68 | "metadata": { 69 | "kernelspec": { 70 | "display_name": "Python 3", 71 | "language": "python", 72 | "name": "python3" 73 | }, 74 | "language_info": { 75 | "codemirror_mode": { 76 | "name": "ipython", 77 | "version": 3 78 | }, 79 | "file_extension": ".py", 80 | "mimetype": "text/x-python", 81 | "name": "python", 82 | "nbconvert_exporter": "python", 83 | "pygments_lexer": "ipython3", 84 | "version": "3.7.3" 85 | } 86 | }, 87 | "nbformat": 4, 88 | "nbformat_minor": 2 89 | } 90 | -------------------------------------------------------------------------------- /9 Linked List-1/9.09 Length of a LL (Recursive).ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "1 2 3 4 5 8 -1\n", 13 | "6\n" 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "class Node:\n", 19 | " def __init__(self, data):\n", 20 | " self.data = data\n", 21 | " self.next = None\n", 22 | "\n", 23 | "def lengthRecursive(head):\n", 24 | " if head is None:\n", 25 | " return 0\n", 26 | " return 1 + lengthRecursive(head.next)\n", 27 | "\n", 28 | "def ll(arr):\n", 29 | " if len(arr)==0:\n", 30 | " return None\n", 31 | " head = Node(arr[0])\n", 32 | " last = head\n", 33 | " for data in arr[1:]:\n", 34 | " last.next = Node(data)\n", 35 | " last = last.next\n", 36 | " return head\n", 37 | "\n", 38 | "# Main\n", 39 | "from sys import setrecursionlimit\n", 40 | "setrecursionlimit(11000)\n", 41 | "# Read the link list elements including -1\n", 42 | "arr=list(int(i) for i in input().strip().split(' '))\n", 43 | "# Create a Linked list after removing -1 from list\n", 44 | "l = ll(arr[:-1])\n", 45 | "len=lengthRecursive(l)\n", 46 | "print(len)\n" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": null, 52 | "metadata": {}, 53 | "outputs": [], 54 | "source": [] 55 | } 56 | ], 57 | "metadata": { 58 | "kernelspec": { 59 | "display_name": "Python 3", 60 | "language": "python", 61 | "name": "python3" 62 | }, 63 | "language_info": { 64 | "codemirror_mode": { 65 | "name": "ipython", 66 | "version": 3 67 | }, 68 | "file_extension": ".py", 69 | "mimetype": "text/x-python", 70 | "name": "python", 71 | "nbconvert_exporter": "python", 72 | "pygments_lexer": "ipython3", 73 | "version": "3.7.3" 74 | } 75 | }, 76 | "nbformat": 4, 77 | "nbformat_minor": 2 78 | } 79 | -------------------------------------------------------------------------------- /9 Linked List-1/9.11 Deletion of a node using recursion.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 5, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "5 4 7 8 1 2 4 -1\n", 13 | "13\n", 14 | "5 4 7 8 1 2 4 \n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "class Node:\n", 20 | " def __init__(self, data):\n", 21 | " self.data = data\n", 22 | " self.next = None\n", 23 | "\n", 24 | "def deleteRec(head, i):\n", 25 | " if i < 0:\n", 26 | " return head\n", 27 | " if i == 0 :\n", 28 | " return head.next\n", 29 | " if head == None:\n", 30 | " return None\n", 31 | " \n", 32 | " dele = deleteRec(head.next, i-1)\n", 33 | " head.next = dele\n", 34 | " \n", 35 | " return head \n", 36 | " \n", 37 | "\n", 38 | "def ll(arr):\n", 39 | " if len(arr)==0:\n", 40 | " return None\n", 41 | " head = Node(arr[0])\n", 42 | " last = head\n", 43 | " for data in arr[1:]:\n", 44 | " last.next = Node(data)\n", 45 | " last = last.next\n", 46 | " return head\n", 47 | "\n", 48 | "def printll(head):\n", 49 | " while head:\n", 50 | " print(head.data, end=' ')\n", 51 | " head = head.next\n", 52 | " print()\n", 53 | "\n", 54 | "\n", 55 | "from sys import setrecursionlimit\n", 56 | "setrecursionlimit(11000)\n", 57 | "arr=list(int(i) for i in input().strip().split(' '))\n", 58 | "l = ll(arr[:-1])\n", 59 | "i=int(input())\n", 60 | "l = deleteRec(l, i)\n", 61 | "printll(l)" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": null, 67 | "metadata": {}, 68 | "outputs": [], 69 | "source": [] 70 | } 71 | ], 72 | "metadata": { 73 | "kernelspec": { 74 | "display_name": "Python 3", 75 | "language": "python", 76 | "name": "python3" 77 | }, 78 | "language_info": { 79 | "codemirror_mode": { 80 | "name": "ipython", 81 | "version": 3 82 | }, 83 | "file_extension": ".py", 84 | "mimetype": "text/x-python", 85 | "name": "python", 86 | "nbconvert_exporter": "python", 87 | "pygments_lexer": "ipython3", 88 | "version": "3.7.3" 89 | } 90 | }, 91 | "nbformat": 4, 92 | "nbformat_minor": 2 93 | } 94 | -------------------------------------------------------------------------------- /9 Linked List-1/9.12 Find a node in LL.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "1 2 5 4 3 98 54 25 -1\n", 13 | "98\n", 14 | "5\n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "class Node:\n", 20 | " def __init__(self, data):\n", 21 | " self.data = data\n", 22 | " self.next = None\n", 23 | "\n", 24 | "def linearSearch(head, n):\n", 25 | " \n", 26 | " curr = head\n", 27 | " cnt = 0\n", 28 | " while curr is not None:\n", 29 | " if curr.data == n:\n", 30 | " return cnt\n", 31 | " cnt += 1\n", 32 | " curr = curr.next\n", 33 | " \n", 34 | " return -1\n", 35 | " \n", 36 | " \n", 37 | "def ll(arr):\n", 38 | " if len(arr)==0:\n", 39 | " return None\n", 40 | " head = Node(arr[0])\n", 41 | " last = head\n", 42 | " for data in arr[1:]:\n", 43 | " last.next = Node(data)\n", 44 | " last = last.next\n", 45 | " return head\n", 46 | "\n", 47 | "# Main\n", 48 | "# Read the link list elements including -1\n", 49 | "arr=list(int(i) for i in input().strip().split(' '))\n", 50 | "# Create a Linked list after removing -1 from list\n", 51 | "l = ll(arr[:-1])\n", 52 | "data=int(input())\n", 53 | "index = linearSearch(l, data)\n", 54 | "print(index)\n" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": null, 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [] 63 | } 64 | ], 65 | "metadata": { 66 | "kernelspec": { 67 | "display_name": "Python 3", 68 | "language": "python", 69 | "name": "python3" 70 | }, 71 | "language_info": { 72 | "codemirror_mode": { 73 | "name": "ipython", 74 | "version": 3 75 | }, 76 | "file_extension": ".py", 77 | "mimetype": "text/x-python", 78 | "name": "python", 79 | "nbconvert_exporter": "python", 80 | "pygments_lexer": "ipython3", 81 | "version": "3.7.3" 82 | } 83 | }, 84 | "nbformat": 4, 85 | "nbformat_minor": 2 86 | } 87 | -------------------------------------------------------------------------------- /9 Linked List-1/9.13 Append Last N elements to First.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "1 2 3 4 5 -1\n", 13 | "3\n", 14 | "3 4 5 1 2 \n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "class Node:\n", 20 | " def __init__(self, data):\n", 21 | " self.data = data\n", 22 | " self.next = None\n", 23 | "\n", 24 | "def length(head):\n", 25 | " cnt = 0\n", 26 | " while head is not None:\n", 27 | " cnt += 1\n", 28 | " head = head.next\n", 29 | " return cnt\n", 30 | "\n", 31 | " \n", 32 | "def append_LinkedList(head,n) :\n", 33 | " if n == 0:\n", 34 | " return None\n", 35 | " \n", 36 | " pos = length(head) - n\n", 37 | " curr = head\n", 38 | " prev = head\n", 39 | " \n", 40 | " for i in range(1,pos):\n", 41 | " curr = curr.next\n", 42 | " \n", 43 | " last = curr\n", 44 | " temp = curr.next\n", 45 | " head = temp\n", 46 | " \n", 47 | " while curr.next is not None:\n", 48 | " curr = curr.next\n", 49 | " curr.next = prev\n", 50 | " last.next = None\n", 51 | " \n", 52 | " return head\n", 53 | " \n", 54 | "\n", 55 | "\n", 56 | "def ll(arr):\n", 57 | " if len(arr)==0:\n", 58 | " return None\n", 59 | " head = Node(arr[0])\n", 60 | " last = head\n", 61 | " for data in arr[1:]:\n", 62 | " last.next = Node(data)\n", 63 | " last = last.next\n", 64 | " return head\n", 65 | "\n", 66 | "def printll(head):\n", 67 | " while head:\n", 68 | " print(head.data, end=' ')\n", 69 | " head = head.next\n", 70 | " print()\n", 71 | "\n", 72 | "# Main\n", 73 | "# Read the link list elements including -1\n", 74 | "arr=list(int(i) for i in input().strip().split(' '))\n", 75 | "# Create a Linked list after removing -1 from list\n", 76 | "l = ll(arr[:-1])\n", 77 | "i=int(input())\n", 78 | "l = append_LinkedList(l, i)\n", 79 | "printll(l)\n" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": null, 85 | "metadata": {}, 86 | "outputs": [], 87 | "source": [] 88 | } 89 | ], 90 | "metadata": { 91 | "kernelspec": { 92 | "display_name": "Python 3", 93 | "language": "python", 94 | "name": "python3" 95 | }, 96 | "language_info": { 97 | "codemirror_mode": { 98 | "name": "ipython", 99 | "version": 3 100 | }, 101 | "file_extension": ".py", 102 | "mimetype": "text/x-python", 103 | "name": "python", 104 | "nbconvert_exporter": "python", 105 | "pygments_lexer": "ipython3", 106 | "version": "3.7.3" 107 | } 108 | }, 109 | "nbformat": 4, 110 | "nbformat_minor": 2 111 | } 112 | -------------------------------------------------------------------------------- /9 Linked List-1/9.14 Eliminate Duplicates from LL.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "1 2 2 3 3 3 3 4 5 6 6 6 7 7 -1\n", 13 | "1 2 3 4 5 6 7 \n" 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "class Node:\n", 19 | " def __init__(self, data):\n", 20 | " self.data = data\n", 21 | " self.next = None\n", 22 | "\n", 23 | "def eliminate_duplicate(head):\n", 24 | " \n", 25 | " if head is None or head.next is None:\n", 26 | " return head\n", 27 | " \n", 28 | " curr = head\n", 29 | " adv = curr.next\n", 30 | " \n", 31 | " while adv is not None:\n", 32 | " if curr.data == adv.data:\n", 33 | " curr.next = adv.next\n", 34 | " adv = curr.next\n", 35 | " else:\n", 36 | " curr = curr.next\n", 37 | " adv = adv.next\n", 38 | " \n", 39 | " return head\n", 40 | "\n", 41 | "def ll(arr):\n", 42 | " if len(arr)==0:\n", 43 | " return None\n", 44 | " head = Node(arr[0])\n", 45 | " last = head\n", 46 | " for data in arr[1:]:\n", 47 | " last.next = Node(data)\n", 48 | " last = last.next\n", 49 | " return head\n", 50 | "\n", 51 | "def printll(head):\n", 52 | " while head:\n", 53 | " print(head.data, end=' ')\n", 54 | " head = head.next\n", 55 | " print()\n", 56 | "\n", 57 | "# Main\n", 58 | "# Read the link list elements including -1\n", 59 | "arr=list(int(i) for i in input().strip().split(' '))\n", 60 | "# Create a Linked list after removing -1 from list\n", 61 | "l = ll(arr[:-1])\n", 62 | "l = eliminate_duplicate(l)\n", 63 | "printll(l)\n" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": null, 69 | "metadata": {}, 70 | "outputs": [], 71 | "source": [] 72 | } 73 | ], 74 | "metadata": { 75 | "kernelspec": { 76 | "display_name": "Python 3", 77 | "language": "python", 78 | "name": "python3" 79 | }, 80 | "language_info": { 81 | "codemirror_mode": { 82 | "name": "ipython", 83 | "version": 3 84 | }, 85 | "file_extension": ".py", 86 | "mimetype": "text/x-python", 87 | "name": "python", 88 | "nbconvert_exporter": "python", 89 | "pygments_lexer": "ipython3", 90 | "version": "3.7.3" 91 | } 92 | }, 93 | "nbformat": 4, 94 | "nbformat_minor": 2 95 | } 96 | -------------------------------------------------------------------------------- /9 Linked List-1/9.15 Print Reverse Linked List.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "1 2 3 4 5 -1\n", 13 | "5 4 3 2 1 " 14 | ] 15 | }, 16 | { 17 | "data": { 18 | "text/plain": [ 19 | "(((((None, None), None), None), None), None)" 20 | ] 21 | }, 22 | "execution_count": 1, 23 | "metadata": {}, 24 | "output_type": "execute_result" 25 | } 26 | ], 27 | "source": [ 28 | "class Node:\n", 29 | " def __init__(self, data):\n", 30 | " self.data = data\n", 31 | " self.next = None\n", 32 | "\n", 33 | "def print_linkedlist_spl(head):\n", 34 | "\n", 35 | " if head is None:\n", 36 | " return None\n", 37 | " \n", 38 | " return print_linkedlist_spl(head.next), print(head.data, end = \" \") \n", 39 | " \n", 40 | "\n", 41 | "def ll(arr):\n", 42 | " if len(arr)==0:\n", 43 | " return None\n", 44 | " head = Node(arr[0])\n", 45 | " last = head\n", 46 | " for data in arr[1:]:\n", 47 | " last.next = Node(data)\n", 48 | " last = last.next\n", 49 | " return head\n", 50 | "\n", 51 | "\n", 52 | "from sys import setrecursionlimit\n", 53 | "setrecursionlimit(10000)\n", 54 | "arr=list(int(i) for i in input().strip().split(' '))\n", 55 | "l = ll(arr[:-1])\n", 56 | "print_linkedlist_spl(l)\n" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": null, 62 | "metadata": {}, 63 | "outputs": [], 64 | "source": [] 65 | } 66 | ], 67 | "metadata": { 68 | "kernelspec": { 69 | "display_name": "Python 3", 70 | "language": "python", 71 | "name": "python3" 72 | }, 73 | "language_info": { 74 | "codemirror_mode": { 75 | "name": "ipython", 76 | "version": 3 77 | }, 78 | "file_extension": ".py", 79 | "mimetype": "text/x-python", 80 | "name": "python", 81 | "nbconvert_exporter": "python", 82 | "pygments_lexer": "ipython3", 83 | "version": "3.7.3" 84 | } 85 | }, 86 | "nbformat": 4, 87 | "nbformat_minor": 2 88 | } 89 | -------------------------------------------------------------------------------- /9 Linked List-1/9.16 Palindrome Linked List.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "1 2 3 4 5 4 3 2 1 -1\n", 13 | "true\n" 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "class Node:\n", 19 | " def __init__(self, data):\n", 20 | " self.data = data\n", 21 | " self.next = None\n", 22 | "\n", 23 | "def printll(head):\n", 24 | " list = []\n", 25 | " while head is not None:\n", 26 | " list.append(head.data)\n", 27 | " head = head.next\n", 28 | " return list\n", 29 | " \n", 30 | "def reverseLL(head):\n", 31 | " list = []\n", 32 | " while head is not None:\n", 33 | " list.insert(0, head.data) \n", 34 | " head = head.next\n", 35 | " return list\n", 36 | "\n", 37 | "\n", 38 | "def check_palindrome(head):\n", 39 | " \n", 40 | " if printll(head) == reverseLL(head):\n", 41 | " return True\n", 42 | " else:\n", 43 | " return False \n", 44 | " \n", 45 | " \n", 46 | " \n", 47 | "\n", 48 | "def ll(arr):\n", 49 | " if len(arr)==0:\n", 50 | " return None\n", 51 | " head = Node(arr[0])\n", 52 | " last = head\n", 53 | " for data in arr[1:]:\n", 54 | " last.next = Node(data)\n", 55 | " last = last.next\n", 56 | " return head\n", 57 | "\n", 58 | "arr=list(int(i) for i in input().strip().split(' '))\n", 59 | "l = ll(arr[:-1])\n", 60 | "ans = check_palindrome(l)\n", 61 | "if ans:\n", 62 | " print(\"true\")\n", 63 | "else:\n", 64 | " print(\"false\")\n" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": null, 70 | "metadata": {}, 71 | "outputs": [], 72 | "source": [] 73 | } 74 | ], 75 | "metadata": { 76 | "kernelspec": { 77 | "display_name": "Python 3", 78 | "language": "python", 79 | "name": "python3" 80 | }, 81 | "language_info": { 82 | "codemirror_mode": { 83 | "name": "ipython", 84 | "version": 3 85 | }, 86 | "file_extension": ".py", 87 | "mimetype": "text/x-python", 88 | "name": "python", 89 | "nbconvert_exporter": "python", 90 | "pygments_lexer": "ipython3", 91 | "version": "3.7.3" 92 | } 93 | }, 94 | "nbformat": 4, 95 | "nbformat_minor": 2 96 | } 97 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Coding-Ninjas---Data-Structures-and-Algorithms-using-Python 2 | Solved problems and assignments of DSA course taught by Coding Ninjas team 3 | 4 | This repository includes all the practice problems and assignments which I've solved during the Data Structures and Algorithms course in Python Programming taught by Coding Ninjas team. 5 | 6 | Topics discussed are: 7 | 1) Recursion - 1 8 | 2) Recursion - 2 9 | 3) OOPS - 1 10 | 4) OOPS - 2 11 | 5) OOPS - 3 12 | 6) Time Complexity Analysis 13 | 7) Space Complexity Analysis 14 | 8) Time Complexity Improvement 15 | 9) Linked List - 1 16 | 10) Linked List - 2 17 | 11) Stacks 18 | 12) Queues 19 | 13) Binary Trees - 1 20 | 14) Binary Trees - 2 21 | 15) BST - 1 22 | 16) BST - 2 23 | 17) Generic Trees 24 | 18) Dictionaries/Maps 25 | 19) Graphs - 1 26 | 20) Graphs - 2 27 | 21) Priority Queues - 1 28 | 22) Huffman Coding 29 | 23) Priority Queues - 2 30 | 24) Dynamic Programming - 1 31 | 25) Recursion - 3 32 | 26) Dynamic Programming - 2 33 | 27) Backtracking 34 | 35 | 36 | 37 | 38 | __Download Jupyter notebook from here:__ https://jupyter.org/ 39 | __Installing Jupyter notebook:__ https://jupyter.org/install 40 | If you're not comfortable with Jupyter notebooks then you can use __Repl.it (Online Editor):__ https://repl.it/languages/python3 41 | 42 | 43 | __Note:__ Refer serial wise to avoid confusion and non-understanding of topics. 44 | 45 | After this course completion, I recommend to have a look on these MIT lectures to get in depth knowledge of data structures and algorithms: https://www.youtube.com/playlist?list=PLUl4u3cNGP61Oq3tWYp6V_F-5jb5L2iHb 46 | 47 | 48 | 49 | Register the course with the following referral link to get a discount of ₹1,000/- 50 | Referral Link: https://codingninjas.in/app/invite/THMPN 51 | **Note: Don't forget to sign up with a new ID to avail discount** 52 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-architect -------------------------------------------------------------------------------- /docs/README.md: -------------------------------------------------------------------------------- 1 | This folder contains all the documents created for the course Data Structures and Algorithms taught by Coding Ninjas. 2 | -------------------------------------------------------------------------------- /docs/[Heap] Time complexity of various data structures.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/docs/[Heap] Time complexity of various data structures.docx -------------------------------------------------------------------------------- /img/1 Recursion Stack.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/img/1 Recursion Stack.png -------------------------------------------------------------------------------- /img/2 Graph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/img/2 Graph.png -------------------------------------------------------------------------------- /img/3 BFS.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/img/3 BFS.png -------------------------------------------------------------------------------- /img/Actual.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/img/Actual.JPG -------------------------------------------------------------------------------- /img/README.md: -------------------------------------------------------------------------------- 1 | This folder contains all the images used in this repo 2 | -------------------------------------------------------------------------------- /img/fibonacci_tree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeetganatra/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/95caed646cd0d64f5e02c45a26088e56afa015ee/img/fibonacci_tree.png -------------------------------------------------------------------------------- /index.md: -------------------------------------------------------------------------------- 1 | ## Welcome to GitHub Pages 2 | 3 | You can use the [editor on GitHub](https://github.com/FazeelUsmani/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/edit/master/index.md) to maintain and preview the content for your website in Markdown files. 4 | 5 | Whenever you commit to this repository, GitHub Pages will run [Jekyll](https://jekyllrb.com/) to rebuild the pages in your site, from the content in your Markdown files. 6 | 7 | ### Markdown 8 | 9 | Markdown is a lightweight and easy-to-use syntax for styling your writing. It includes conventions for 10 | 11 | ```markdown 12 | Syntax highlighted code block 13 | 14 | # Header 1 15 | ## Header 2 16 | ### Header 3 17 | 18 | - Bulleted 19 | - List 20 | 21 | 1. Numbered 22 | 2. List 23 | 24 | **Bold** and _Italic_ and `Code` text 25 | 26 | [Link](url) and ![Image](src) 27 | ``` 28 | 29 | For more details see [GitHub Flavored Markdown](https://guides.github.com/features/mastering-markdown/). 30 | 31 | ### Jekyll Themes 32 | 33 | Your Pages site will use the layout and styles from the Jekyll theme you have selected in your [repository settings](https://github.com/FazeelUsmani/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python/settings). The name of this theme is saved in the Jekyll `_config.yml` configuration file. 34 | 35 | ### Support or Contact 36 | 37 | Having trouble with Pages? Check out our [documentation](https://help.github.com/categories/github-pages-basics/) or [contact support](https://github.com/contact) and we’ll help you sort it out. 38 | --------------------------------------------------------------------------------