├── .ipynb_checkpoints ├── 1.1 Time complexity-checkpoint.ipynb ├── Array -checkpoint.ipynb └── Untitled-checkpoint.ipynb ├── Code Workspace ├── .ipynb_checkpoints │ ├── 3.1 Stack-checkpoint.ipynb │ ├── 4.1 Queue-checkpoint.ipynb │ ├── 4.2 Deque-checkpoint.ipynb │ ├── 4.3 Queue using 2 stacks-checkpoint.ipynb │ ├── 5.1 Linked List-checkpoint.ipynb │ ├── Anagram Check - SOLUTION-checkpoint.ipynb │ ├── Anagram Check -checkpoint.ipynb │ ├── Anagram Check 1 -checkpoint.ipynb │ ├── Array Pair Sum -checkpoint.ipynb │ ├── Stacks Overview-checkpoint.ipynb │ ├── Untitled-checkpoint.ipynb │ └── Untitled1-checkpoint.ipynb ├── Algorithms │ ├── .ipynb_checkpoints │ │ ├── 2.2 Binary Search-checkpoint.ipynb │ │ ├── 2.3 Hash Table-checkpoint.ipynb │ │ ├── 3.1 Bubble Sort-checkpoint.ipynb │ │ ├── 3.2 Selection sort-checkpoint.ipynb │ │ ├── 4.3 Insertion sort-checkpoint.ipynb │ │ ├── 4.4 Quick sort-checkpoint.ipynb │ │ ├── 4.5 Merge sort-checkpoint.ipynb │ │ ├── 4.6 shell sort-checkpoint.ipynb │ │ ├── 5.1 Recursion-checkpoint.ipynb │ │ ├── 5.2 Recursion-checkpoint.ipynb │ │ ├── Dictionaries-checkpoint.ipynb │ │ ├── Recursion Homework Example Problems - SOLUTIONS-checkpoint.ipynb │ │ ├── Recursion Homework Example Problems-checkpoint.ipynb │ │ ├── Recursion Problem 1 - Reverse String - SOLUTION-checkpoint.ipynb │ │ ├── Recursion Problem 2 - String Permutation- SOLUTION-checkpoint.ipynb │ │ ├── Recursion Problem 3 - Fibonacci Sequence - SOLUTION-checkpoint.ipynb │ │ └── Recursion Problem 4 - Coin Change - SOLUTION-checkpoint.ipynb │ ├── 1.1 Time complexity.ipynb │ ├── 2.1 Sequential Search.ipynb │ ├── 2.2 Binary Search.ipynb │ ├── 3.1 Hash Table.ipynb │ ├── 4.1 Bubble Sort.ipynb │ ├── 4.2 Selection sort.ipynb │ ├── 4.3 Insertion sort.ipynb │ ├── 4.4 Quick sort.ipynb │ ├── 4.5 Merge sort.ipynb │ ├── 4.6 shell sort.ipynb │ ├── 5.1 HeapSort.py │ ├── 6.1 Recursion.ipynb │ ├── 6.2 Recursion.ipynb │ ├── Implementation of Shell Sort.ipynb │ ├── RecursionQ1 String Permutation.ipynb │ └── RecursionQ2 Coin Change.ipynb └── Data structure │ ├── .ipynb_checkpoints │ ├── 2.1 Sequential Search-checkpoint.ipynb │ ├── 3.2 Stack Implementation via Linked list-checkpoint.ipynb │ ├── 4.2 Deque-checkpoint.ipynb │ ├── 4.2 Queue Implementation via Linked list-checkpoint.ipynb │ ├── 4.3 Queue using 2 stacks-checkpoint.ipynb │ ├── 4.5 Priority Queue using heap-checkpoint.ipynb │ ├── 4.6 Priority Queue using list-checkpoint.ipynb │ ├── 5.1 Linked List-checkpoint.ipynb │ ├── 5.1 Singly Linked list-checkpoint.ipynb │ ├── 5.2 Circular Linked list-checkpoint.ipynb │ ├── 5.2 Singly Linked list-checkpoint.ipynb │ ├── 5.3 Doubly Linked list-checkpoint.ipynb │ ├── 5.4 Circular Linked list-checkpoint.ipynb │ ├── 6.1 Graphs-checkpoint.ipynb │ ├── 6.2 Breadth first search in graph-checkpoint.ipynb │ ├── 6.3 Bfs method 2-checkpoint.ipynb │ ├── 6.4 Depth first search in graph -checkpoint.ipynb │ ├── 6.5 Dfs method 2-checkpoint.ipynb │ ├── 7.1 Trees-checkpoint.ipynb │ ├── 7.2 Order Traversal Binary Tree-checkpoint.ipynb │ ├── 7.3 Validate BST-checkpoint.ipynb │ ├── Balanced Parenthesis Check-checkpoint.ipynb │ └── Trim a Binary Search Tree - SOLUTION-checkpoint.ipynb │ ├── 1.1 Dictionaries.ipynb │ ├── 2.1 Array .ipynb │ ├── 3.1 Stack Implementation via List.ipynb │ ├── 3.2 Stack Implementation via Linked list.ipynb │ ├── 4.1 Queue Implementation via List.ipynb │ ├── 4.2 Queue Implementation via Linked list.ipynb │ ├── 4.3 Queue using 2 stacks.ipynb │ ├── 4.4 Dequeue Implementation via List.ipynb │ ├── 4.5 Priority Queue using heap.ipynb │ ├── 4.6 Priority Queue using list.ipynb │ ├── 5.1 Linked List.ipynb │ ├── 5.2 Singly Linked list.ipynb │ ├── 5.3 Doubly Linked list.ipynb │ ├── 5.4 Circular Linked list.ipynb │ ├── 6.1 Graphs.ipynb │ ├── 6.2 Breadth first search in graph.ipynb │ ├── 6.3 Bfs method 2.ipynb │ ├── 6.4 Depth first search in graph .ipynb │ ├── 6.5 Dfs method 2.ipynb │ ├── 7.1 Trees.ipynb │ ├── 7.2 Order Traversal Binary Tree.ipynb │ ├── 7.3 Validate BST.ipynb │ ├── 7.4 Trim a BinarySearchTree.ipynb │ └── 8.1 Heap.py ├── README.md └── Resources ├── AlgorithmsNotesForProfessionals.pdf ├── Searching └── binary search .txt ├── linkList.txt └── searching & sorting.pptx /.ipynb_checkpoints/1.1 Time complexity-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## %timeit to calculate run- time" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 6, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "def numsum(num):\n", 17 | " sum=0\n", 18 | " for i in range(num+1):\n", 19 | " sum=sum+i\n", 20 | " return sum" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 13, 26 | "metadata": {}, 27 | "outputs": [], 28 | "source": [ 29 | "def numsum2(n):\n", 30 | " return (n*(n+1))/2" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 14, 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "data": { 40 | "text/plain": [ 41 | "55" 42 | ] 43 | }, 44 | "execution_count": 14, 45 | "metadata": {}, 46 | "output_type": "execute_result" 47 | } 48 | ], 49 | "source": [ 50 | "numsum(10)" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 15, 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "data": { 60 | "text/plain": [ 61 | "55.0" 62 | ] 63 | }, 64 | "execution_count": 15, 65 | "metadata": {}, 66 | "output_type": "execute_result" 67 | } 68 | ], 69 | "source": [ 70 | "numsum2(10)" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 16, 76 | "metadata": {}, 77 | "outputs": [ 78 | { 79 | "name": "stdout", 80 | "output_type": "stream", 81 | "text": [ 82 | "21.9 µs ± 1.2 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n" 83 | ] 84 | } 85 | ], 86 | "source": [ 87 | "%timeit numsum(100)" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": 17, 93 | "metadata": {}, 94 | "outputs": [ 95 | { 96 | "name": "stdout", 97 | "output_type": "stream", 98 | "text": [ 99 | "918 ns ± 157 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n" 100 | ] 101 | } 102 | ], 103 | "source": [ 104 | "%timeit numsum2(100)" 105 | ] 106 | }, 107 | { 108 | "cell_type": "markdown", 109 | "metadata": {}, 110 | "source": [ 111 | "### time complexity - how quickly it will grow" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": 2, 117 | "metadata": {}, 118 | "outputs": [ 119 | { 120 | "name": "stdout", 121 | "output_type": "stream", 122 | "text": [ 123 | "1\n" 124 | ] 125 | } 126 | ], 127 | "source": [ 128 | "def func_constant(values):\n", 129 | " '''\n", 130 | " Prints first item in a list of values.\n", 131 | " '''\n", 132 | " print (values[0])\n", 133 | " \n", 134 | "func_constant([1,2,3])\n", 135 | "#no matter how big list is we are printing only one element. \n", 136 | "#complexity will be 1- a constant" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": null, 142 | "metadata": {}, 143 | "outputs": [], 144 | "source": [] 145 | } 146 | ], 147 | "metadata": { 148 | "kernelspec": { 149 | "display_name": "Python 3", 150 | "language": "python", 151 | "name": "python3" 152 | }, 153 | "language_info": { 154 | "codemirror_mode": { 155 | "name": "ipython", 156 | "version": 3 157 | }, 158 | "file_extension": ".py", 159 | "mimetype": "text/x-python", 160 | "name": "python", 161 | "nbconvert_exporter": "python", 162 | "pygments_lexer": "ipython3", 163 | "version": "3.7.6" 164 | } 165 | }, 166 | "nbformat": 4, 167 | "nbformat_minor": 4 168 | } 169 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Untitled-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 4 6 | } 7 | -------------------------------------------------------------------------------- /Code Workspace/.ipynb_checkpoints/4.1 Queue-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "collapsed": true 7 | }, 8 | "source": [ 9 | "____\n", 10 | "## Queue Methods and Attributes\n", 11 | "\n", 12 | "* Queue() creates a new queue that is empty. It needs no parameters and returns an empty queue.\n", 13 | "* enqueue(item) adds a new item to the rear of the queue. It needs the item and returns nothing.\n", 14 | "* dequeue() removes the front item from the queue. It needs no parameters and returns the item. The queue is modified.\n", 15 | "* isEmpty() tests to see whether the queue is empty. It needs no parameters and returns a boolean value.\n", 16 | "* size() returns the number of items in the queue. It needs no parameters and returns an integer." 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "____\n", 24 | "## Queue Implementation" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 1, 30 | "metadata": {}, 31 | "outputs": [], 32 | "source": [ 33 | "class Queue:\n", 34 | " '''implementing Queue using list'''\n", 35 | " \n", 36 | " def __init__(self):\n", 37 | " self.items = []\n", 38 | "\n", 39 | " def isEmpty(self):\n", 40 | " '''check for emplty Queue'''\n", 41 | " #self.items == [] --> emplty stack\n", 42 | " return len(self.items)==0 # --> len(stack)-zero\n", 43 | "\n", 44 | " def enqueue(self, item):\n", 45 | " '''push value in Queue'''\n", 46 | " self.items.insert(0,item)\n", 47 | "\n", 48 | " def dequeue(self):\n", 49 | " '''pops value from Queue and returns it'''\n", 50 | " #check length before popping\n", 51 | " if len(self.items) == 0:\n", 52 | " return None\n", 53 | " return self.items.pop()\n", 54 | " \n", 55 | " def peek(self):\n", 56 | " '''return top most value of Queue'''\n", 57 | " #check length before peeking\n", 58 | " if len(self.items) == 0:\n", 59 | " return None\n", 60 | " \n", 61 | " return self.items[len(self.items)-1]\n", 62 | "\n", 63 | " def size(self):\n", 64 | " '''return size of Queue'''\n", 65 | " return len(self.items)" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 2, 71 | "metadata": {}, 72 | "outputs": [], 73 | "source": [ 74 | "q = Queue()" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 3, 80 | "metadata": {}, 81 | "outputs": [ 82 | { 83 | "data": { 84 | "text/plain": [ 85 | "0" 86 | ] 87 | }, 88 | "execution_count": 3, 89 | "metadata": {}, 90 | "output_type": "execute_result" 91 | } 92 | ], 93 | "source": [ 94 | "q.size()" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 4, 100 | "metadata": {}, 101 | "outputs": [ 102 | { 103 | "data": { 104 | "text/plain": [ 105 | "True" 106 | ] 107 | }, 108 | "execution_count": 4, 109 | "metadata": {}, 110 | "output_type": "execute_result" 111 | } 112 | ], 113 | "source": [ 114 | "q.isEmpty()" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 5, 120 | "metadata": {}, 121 | "outputs": [], 122 | "source": [ 123 | "q.enqueue(1)" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 6, 129 | "metadata": {}, 130 | "outputs": [ 131 | { 132 | "data": { 133 | "text/plain": [ 134 | "1" 135 | ] 136 | }, 137 | "execution_count": 6, 138 | "metadata": {}, 139 | "output_type": "execute_result" 140 | } 141 | ], 142 | "source": [ 143 | "q.dequeue()" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": null, 149 | "metadata": {}, 150 | "outputs": [], 151 | "source": [] 152 | } 153 | ], 154 | "metadata": { 155 | "kernelspec": { 156 | "display_name": "Python 3", 157 | "language": "python", 158 | "name": "python3" 159 | }, 160 | "language_info": { 161 | "codemirror_mode": { 162 | "name": "ipython", 163 | "version": 3 164 | }, 165 | "file_extension": ".py", 166 | "mimetype": "text/x-python", 167 | "name": "python", 168 | "nbconvert_exporter": "python", 169 | "pygments_lexer": "ipython3", 170 | "version": "3.7.6" 171 | } 172 | }, 173 | "nbformat": 4, 174 | "nbformat_minor": 1 175 | } 176 | -------------------------------------------------------------------------------- /Code Workspace/.ipynb_checkpoints/4.2 Deque-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Implementation of Deque\n", 8 | "\n", 9 | "In this lecture we will implement our own Deque class!\n", 10 | "\n", 11 | "## Methods and Attributes\n", 12 | "\n", 13 | "* Deque() creates a new deque that is empty. It needs no parameters and returns an empty deque.\n", 14 | "* addFront(item) adds a new item to the front of the deque. It needs the item and returns nothing.\n", 15 | "* addRear(item) adds a new item to the rear of the deque. It needs the item and returns nothing.\n", 16 | "* removeFront() removes the front item from the deque. It needs no parameters and returns the item. The deque is modified.\n", 17 | "* removeRear() removes the rear item from the deque. It needs no parameters and returns the item. The deque is modified.\n", 18 | "* isEmpty() tests to see whether the deque is empty. It needs no parameters and returns a boolean value.\n", 19 | "* size() returns the number of items in the deque. It needs no parameters and returns an integer.\n", 20 | "\n", 21 | "## Deque Implementation" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 1, 27 | "metadata": { 28 | "collapsed": true 29 | }, 30 | "outputs": [], 31 | "source": [ 32 | "class Deque:\n", 33 | " def __init__(self):\n", 34 | " self.items = []\n", 35 | "\n", 36 | " def isEmpty(self):\n", 37 | " return self.items == []\n", 38 | "\n", 39 | " def addFront(self, item):\n", 40 | " self.items.append(item)\n", 41 | "\n", 42 | " def addRear(self, item):\n", 43 | " self.items.insert(0,item)\n", 44 | "\n", 45 | " def removeFront(self):\n", 46 | " return self.items.pop()\n", 47 | "\n", 48 | " def removeRear(self):\n", 49 | " return self.items.pop(0)\n", 50 | "\n", 51 | " def size(self):\n", 52 | " return len(self.items)" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 10, 58 | "metadata": { 59 | "collapsed": true 60 | }, 61 | "outputs": [], 62 | "source": [ 63 | "d = Deque()" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 11, 69 | "metadata": { 70 | "collapsed": true 71 | }, 72 | "outputs": [], 73 | "source": [ 74 | "d.addFront('hello')" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 12, 80 | "metadata": { 81 | "collapsed": true 82 | }, 83 | "outputs": [], 84 | "source": [ 85 | "d.addRear('world')" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 13, 91 | "metadata": {}, 92 | "outputs": [ 93 | { 94 | "data": { 95 | "text/plain": [ 96 | "2" 97 | ] 98 | }, 99 | "execution_count": 13, 100 | "metadata": {}, 101 | "output_type": "execute_result" 102 | } 103 | ], 104 | "source": [ 105 | "d.size()" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": 14, 111 | "metadata": {}, 112 | "outputs": [ 113 | { 114 | "name": "stdout", 115 | "output_type": "stream", 116 | "text": [ 117 | "hello world\n" 118 | ] 119 | } 120 | ], 121 | "source": [ 122 | "print d.removeFront() + ' ' + d.removeRear()" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 15, 128 | "metadata": {}, 129 | "outputs": [ 130 | { 131 | "data": { 132 | "text/plain": [ 133 | "0" 134 | ] 135 | }, 136 | "execution_count": 15, 137 | "metadata": {}, 138 | "output_type": "execute_result" 139 | } 140 | ], 141 | "source": [ 142 | "d.size()" 143 | ] 144 | }, 145 | { 146 | "cell_type": "markdown", 147 | "metadata": {}, 148 | "source": [ 149 | "## Good Job!" 150 | ] 151 | } 152 | ], 153 | "metadata": { 154 | "kernelspec": { 155 | "display_name": "Python 3", 156 | "language": "python", 157 | "name": "python3" 158 | }, 159 | "language_info": { 160 | "codemirror_mode": { 161 | "name": "ipython", 162 | "version": 3 163 | }, 164 | "file_extension": ".py", 165 | "mimetype": "text/x-python", 166 | "name": "python", 167 | "nbconvert_exporter": "python", 168 | "pygments_lexer": "ipython3", 169 | "version": "3.7.6" 170 | } 171 | }, 172 | "nbformat": 4, 173 | "nbformat_minor": 1 174 | } 175 | -------------------------------------------------------------------------------- /Code Workspace/.ipynb_checkpoints/4.3 Queue using 2 stacks-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 4 6 | } 7 | -------------------------------------------------------------------------------- /Code Workspace/.ipynb_checkpoints/5.1 Linked List-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 4 6 | } 7 | -------------------------------------------------------------------------------- /Code Workspace/.ipynb_checkpoints/Anagram Check -checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Anagram Check\n", 8 | "\n", 9 | "## Problem\n", 10 | "\n", 11 | "Given two strings, check to see if they are anagrams. An anagram is when the two strings can be written using the exact same letters (so you can just rearrange the letters to get a different phrase or word). \n", 12 | "\n", 13 | "For example:\n", 14 | "\n", 15 | " \"public relations\" is an anagram of \"crap built on lies.\"\n", 16 | " \n", 17 | " \"clint eastwood\" is an anagram of \"old west action\"\n", 18 | " \n", 19 | "**Note: Ignore spaces and capitalization. So \"d go\" is an anagram of \"God\" and \"dog\" and \"o d g\".**\n", 20 | "\n", 21 | "## Solution\n", 22 | "\n", 23 | "Fill out your solution below:" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 25, 29 | "metadata": {}, 30 | "outputs": [], 31 | "source": [ 32 | "def anagram(s1,s2):\n", 33 | " \n", 34 | " a1=s1.replace(' ','').lower()\n", 35 | " a2=s2.replace(' ','').lower()\n", 36 | "\n", 37 | " return sorted(a1)==sorted(a2)" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 26, 43 | "metadata": {}, 44 | "outputs": [ 45 | { 46 | "data": { 47 | "text/plain": [ 48 | "True" 49 | ] 50 | }, 51 | "execution_count": 26, 52 | "metadata": {}, 53 | "output_type": "execute_result" 54 | } 55 | ], 56 | "source": [ 57 | "anagram('dog','god')" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 27, 63 | "metadata": {}, 64 | "outputs": [ 65 | { 66 | "data": { 67 | "text/plain": [ 68 | "True" 69 | ] 70 | }, 71 | "execution_count": 27, 72 | "metadata": {}, 73 | "output_type": "execute_result" 74 | } 75 | ], 76 | "source": [ 77 | "anagram('clint eastwood','old west action')" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 28, 83 | "metadata": {}, 84 | "outputs": [ 85 | { 86 | "data": { 87 | "text/plain": [ 88 | "False" 89 | ] 90 | }, 91 | "execution_count": 28, 92 | "metadata": {}, 93 | "output_type": "execute_result" 94 | } 95 | ], 96 | "source": [ 97 | "anagram('aa','bb')" 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "metadata": {}, 103 | "source": [ 104 | "# Test Your Solution\n", 105 | "Run the cell below to test your solution" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": 30, 111 | "metadata": {}, 112 | "outputs": [ 113 | { 114 | "name": "stdout", 115 | "output_type": "stream", 116 | "text": [ 117 | "ALL TEST CASES PASSED\n" 118 | ] 119 | } 120 | ], 121 | "source": [ 122 | "\"\"\"\n", 123 | "RUN THIS CELL TO TEST YOUR SOLUTION\n", 124 | "\"\"\"\n", 125 | "from nose.tools import assert_equal\n", 126 | "\n", 127 | "class AnagramTest(object):\n", 128 | " \n", 129 | " def test(self,sol):\n", 130 | " assert_equal(sol('go go go','gggooo'),True)\n", 131 | " assert_equal(sol('abc','cba'),True)\n", 132 | " assert_equal(sol('hi man','hi man'),True)\n", 133 | " assert_equal(sol('aabbcc','aabbc'),False)\n", 134 | " assert_equal(sol('123','1 2'),False)\n", 135 | " print (\"ALL TEST CASES PASSED\")\n", 136 | "\n", 137 | "# Run Tests\n", 138 | "t = AnagramTest()\n", 139 | "t.test(anagram)" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": 32, 145 | "metadata": {}, 146 | "outputs": [ 147 | { 148 | "name": "stdout", 149 | "output_type": "stream", 150 | "text": [ 151 | "ALL TEST CASES PASSED\n" 152 | ] 153 | } 154 | ], 155 | "source": [ 156 | "t.test(anagram)" 157 | ] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "metadata": {}, 162 | "source": [ 163 | "# Good Job!" 164 | ] 165 | } 166 | ], 167 | "metadata": { 168 | "kernelspec": { 169 | "display_name": "Python 3", 170 | "language": "python", 171 | "name": "python3" 172 | }, 173 | "language_info": { 174 | "codemirror_mode": { 175 | "name": "ipython", 176 | "version": 3 177 | }, 178 | "file_extension": ".py", 179 | "mimetype": "text/x-python", 180 | "name": "python", 181 | "nbconvert_exporter": "python", 182 | "pygments_lexer": "ipython3", 183 | "version": "3.7.6" 184 | } 185 | }, 186 | "nbformat": 4, 187 | "nbformat_minor": 1 188 | } 189 | -------------------------------------------------------------------------------- /Code Workspace/.ipynb_checkpoints/Array Pair Sum -checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Array Pair Sum\n", 8 | "\n", 9 | "## Problem\n", 10 | "\n", 11 | "Given an integer array, output all the ** *unique* ** pairs that sum up to a specific value **k**.\n", 12 | "\n", 13 | "So the input:\n", 14 | " \n", 15 | " pair_sum([1,3,2,2],4)\n", 16 | "\n", 17 | "would return **2** pairs:\n", 18 | "\n", 19 | " (1,3)\n", 20 | " (2,2)\n", 21 | "\n", 22 | "**NOTE: FOR TESTING PURPOSES CHANGE YOUR FUNCTION SO IT OUTPUTS THE NUMBER OF PAIRS**\n", 23 | "\n", 24 | "## Solution\n", 25 | "\n", 26 | "Fill out your solution below:" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 11, 32 | "metadata": { 33 | "collapsed": true 34 | }, 35 | "outputs": [], 36 | "source": [ 37 | "def pair_sum(arr,k):\n", 38 | " \n", 39 | " pass" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 14, 45 | "metadata": {}, 46 | "outputs": [ 47 | { 48 | "data": { 49 | "text/plain": [ 50 | "2" 51 | ] 52 | }, 53 | "execution_count": 14, 54 | "metadata": {}, 55 | "output_type": "execute_result" 56 | } 57 | ], 58 | "source": [ 59 | "pair_sum([1,3,2,2],4)" 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": {}, 65 | "source": [ 66 | "# Test Your Solution" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 16, 72 | "metadata": {}, 73 | "outputs": [ 74 | { 75 | "name": "stdout", 76 | "output_type": "stream", 77 | "text": [ 78 | "ALL TEST CASES PASSED\n" 79 | ] 80 | } 81 | ], 82 | "source": [ 83 | "\"\"\"\n", 84 | "RUN THIS CELL TO TEST YOUR SOLUTION\n", 85 | "\"\"\"\n", 86 | "from nose.tools import assert_equal\n", 87 | "\n", 88 | "class TestPair(object):\n", 89 | " \n", 90 | " def test(self,sol):\n", 91 | " assert_equal(sol([1,9,2,8,3,7,4,6,5,5,13,14,11,13,-1],10),6)\n", 92 | " assert_equal(sol([1,2,3,1],3),1)\n", 93 | " assert_equal(sol([1,3,2,2],4),2)\n", 94 | " print 'ALL TEST CASES PASSED'\n", 95 | " \n", 96 | "#Run tests\n", 97 | "t = TestPair()\n", 98 | "t.test(pair_sum)\n", 99 | " " 100 | ] 101 | }, 102 | { 103 | "cell_type": "markdown", 104 | "metadata": {}, 105 | "source": [ 106 | "## Good Job!" 107 | ] 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.6" 127 | } 128 | }, 129 | "nbformat": 4, 130 | "nbformat_minor": 1 131 | } 132 | -------------------------------------------------------------------------------- /Code Workspace/.ipynb_checkpoints/Untitled-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 4 6 | } 7 | -------------------------------------------------------------------------------- /Code Workspace/.ipynb_checkpoints/Untitled1-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 4 6 | } 7 | -------------------------------------------------------------------------------- /Code Workspace/Algorithms/.ipynb_checkpoints/2.2 Binary Search-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "def binary_search(arr,k):\n", 10 | " first=0\n", 11 | " last=len(arr)-1\n", 12 | "\n", 13 | " while first <= last:\n", 14 | " middle=(first+last)//2\n", 15 | " if arr[middle]==k:\n", 16 | " return True\n", 17 | " elif arr[middle]>k:\n", 18 | " last=middle-1\n", 19 | " else:\n", 20 | " first=middle+1\n", 21 | " return False" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 5, 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "arr=[11,21,31,41,51,61,71,81,91,101]" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 6, 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "data": { 40 | "text/plain": [ 41 | "False" 42 | ] 43 | }, 44 | "execution_count": 6, 45 | "metadata": {}, 46 | "output_type": "execute_result" 47 | } 48 | ], 49 | "source": [ 50 | "binary_search(arr,11)" 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.6" 78 | } 79 | }, 80 | "nbformat": 4, 81 | "nbformat_minor": 4 82 | } 83 | -------------------------------------------------------------------------------- /Code Workspace/Algorithms/.ipynb_checkpoints/2.3 Hash Table-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Implementation of Hash Table" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 4, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "class HashTable():\n", 17 | " def __init__(self,size):\n", 18 | " self.size = size\n", 19 | " self.bucket = [[] for i in range(self.size)]\n", 20 | " \n", 21 | " def hash_function(self,key):\n", 22 | " return hash(key) % self.size\n", 23 | " \n", 24 | " def add(self,key):\n", 25 | " i = self.hash_function(key)\n", 26 | " if not key in self.bucket[i]:\n", 27 | " self.bucket[i].append(key)\n", 28 | " \n", 29 | " def remove(self,key):\n", 30 | " i = self.hash_function(key)\n", 31 | " if key in self.bucket[i]:\n", 32 | " self.bucket[i].remove(key)\n", 33 | " \n", 34 | " def contains(self,key):\n", 35 | " i = self.hash_function(key)\n", 36 | " if key in self.bucket[i]:\n", 37 | " return True\n", 38 | " return False" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 5, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "myht=HashTable(10)" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 6, 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [ 56 | "myht.add('hello')" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 7, 62 | "metadata": {}, 63 | "outputs": [], 64 | "source": [ 65 | "myht.add(4432)" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 8, 71 | "metadata": {}, 72 | "outputs": [], 73 | "source": [ 74 | "myht.add(44)" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 10, 80 | "metadata": {}, 81 | "outputs": [], 82 | "source": [ 83 | "myht.remove(44)" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 11, 89 | "metadata": {}, 90 | "outputs": [ 91 | { 92 | "data": { 93 | "text/plain": [ 94 | "False" 95 | ] 96 | }, 97 | "execution_count": 11, 98 | "metadata": {}, 99 | "output_type": "execute_result" 100 | } 101 | ], 102 | "source": [ 103 | "myht.contains(44)" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 12, 109 | "metadata": {}, 110 | "outputs": [ 111 | { 112 | "data": { 113 | "text/plain": [ 114 | "True" 115 | ] 116 | }, 117 | "execution_count": 12, 118 | "metadata": {}, 119 | "output_type": "execute_result" 120 | } 121 | ], 122 | "source": [ 123 | "myht.contains(4432)" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": null, 129 | "metadata": {}, 130 | "outputs": [], 131 | "source": [] 132 | } 133 | ], 134 | "metadata": { 135 | "kernelspec": { 136 | "display_name": "Python 3", 137 | "language": "python", 138 | "name": "python3" 139 | }, 140 | "language_info": { 141 | "codemirror_mode": { 142 | "name": "ipython", 143 | "version": 3 144 | }, 145 | "file_extension": ".py", 146 | "mimetype": "text/x-python", 147 | "name": "python", 148 | "nbconvert_exporter": "python", 149 | "pygments_lexer": "ipython3", 150 | "version": "3.7.6" 151 | } 152 | }, 153 | "nbformat": 4, 154 | "nbformat_minor": 4 155 | } 156 | -------------------------------------------------------------------------------- /Code Workspace/Algorithms/.ipynb_checkpoints/3.1 Bubble Sort-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Bubble sort" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 5, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "arr=[98,23,45,14,6,67,33,42]" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 22, 22 | "metadata": {}, 23 | "outputs": [], 24 | "source": [ 25 | "#method 1-\n", 26 | "#outerloop will run the times of number of elements:i\n", 27 | "#at each pass one elements bubbles up to the end\n", 28 | "#the inner loop will run for each element to bubble up to the end, will run len-i\n", 29 | "# for swapping use: a,b=b,a" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 23, 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "def bubble_sort(arr):\n", 39 | " for i in range(0,len(arr)-1):\n", 40 | " for j in range(0,len(arr)-i-1):\n", 41 | " if arr[j]>arr[j+1]:\n", 42 | " arr[j],arr[j+1]=arr[j+1],arr[j]\n", 43 | " print(arr)\n", 44 | " return arr" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 24, 50 | "metadata": {}, 51 | "outputs": [], 52 | "source": [ 53 | "arr=[98,23,45,14,6,67,33,42]" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 25, 59 | "metadata": {}, 60 | "outputs": [ 61 | { 62 | "name": "stdout", 63 | "output_type": "stream", 64 | "text": [ 65 | "[23, 98, 45, 14, 6, 67, 33, 42]\n", 66 | "[23, 45, 98, 14, 6, 67, 33, 42]\n", 67 | "[23, 45, 14, 98, 6, 67, 33, 42]\n", 68 | "[23, 45, 14, 6, 98, 67, 33, 42]\n", 69 | "[23, 45, 14, 6, 67, 98, 33, 42]\n", 70 | "[23, 45, 14, 6, 67, 33, 98, 42]\n", 71 | "[23, 45, 14, 6, 67, 33, 42, 98]\n", 72 | "[23, 14, 45, 6, 67, 33, 42, 98]\n", 73 | "[23, 14, 6, 45, 67, 33, 42, 98]\n", 74 | "[23, 14, 6, 45, 33, 67, 42, 98]\n", 75 | "[23, 14, 6, 45, 33, 42, 67, 98]\n", 76 | "[14, 23, 6, 45, 33, 42, 67, 98]\n", 77 | "[14, 6, 23, 45, 33, 42, 67, 98]\n", 78 | "[14, 6, 23, 33, 45, 42, 67, 98]\n", 79 | "[14, 6, 23, 33, 42, 45, 67, 98]\n", 80 | "[6, 14, 23, 33, 42, 45, 67, 98]\n" 81 | ] 82 | }, 83 | { 84 | "data": { 85 | "text/plain": [ 86 | "[6, 14, 23, 33, 42, 45, 67, 98]" 87 | ] 88 | }, 89 | "execution_count": 25, 90 | "metadata": {}, 91 | "output_type": "execute_result" 92 | } 93 | ], 94 | "source": [ 95 | "bubble_sort(arr)" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": 26, 101 | "metadata": {}, 102 | "outputs": [ 103 | { 104 | "name": "stdout", 105 | "output_type": "stream", 106 | "text": [ 107 | "[5, 8, 9, 12, 13, 65]\n" 108 | ] 109 | } 110 | ], 111 | "source": [ 112 | "#method-2\n", 113 | "my_list = [12, 5, 13, 8, 9, 65]\n", 114 | " \n", 115 | "def bubble_sort(arr):\n", 116 | " sorted = False\n", 117 | " length = len(arr)-1\n", 118 | " while not sorted:\n", 119 | " sorted = True\n", 120 | " for index, value in enumerate(arr[:length]):\n", 121 | " if arr[index] > arr[index+1]:\n", 122 | " sorted = False\n", 123 | " arr[index], arr[index+1] = arr[index+1], arr[index]\n", 124 | " return arr\n", 125 | " \n", 126 | "print(bubble_sort(my_list))" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": null, 132 | "metadata": {}, 133 | "outputs": [], 134 | "source": [] 135 | } 136 | ], 137 | "metadata": { 138 | "kernelspec": { 139 | "display_name": "Python 3", 140 | "language": "python", 141 | "name": "python3" 142 | }, 143 | "language_info": { 144 | "codemirror_mode": { 145 | "name": "ipython", 146 | "version": 3 147 | }, 148 | "file_extension": ".py", 149 | "mimetype": "text/x-python", 150 | "name": "python", 151 | "nbconvert_exporter": "python", 152 | "pygments_lexer": "ipython3", 153 | "version": "3.7.6" 154 | } 155 | }, 156 | "nbformat": 4, 157 | "nbformat_minor": 4 158 | } 159 | -------------------------------------------------------------------------------- /Code Workspace/Algorithms/.ipynb_checkpoints/3.2 Selection sort-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Selection Sort" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 12, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "def selection_sort(arr): \n", 17 | " for i in range(len(arr)):\n", 18 | " minpos = i\n", 19 | "\n", 20 | " for j in range(i+1,len(arr)):\n", 21 | " if arr[j] curr_val and pos>0:\n", 22 | " arr[pos] = arr[pos-1]\n", 23 | " pos = pos - 1\n", 24 | " \n", 25 | " arr[pos] = curr_val" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 9, 31 | "metadata": {}, 32 | "outputs": [], 33 | "source": [ 34 | "arr = [2,4,3,5,1]" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 11, 40 | "metadata": {}, 41 | "outputs": [], 42 | "source": [ 43 | "insertion_sort(arr)" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 12, 49 | "metadata": {}, 50 | "outputs": [ 51 | { 52 | "data": { 53 | "text/plain": [ 54 | "[1, 2, 3, 4, 5]" 55 | ] 56 | }, 57 | "execution_count": 12, 58 | "metadata": {}, 59 | "output_type": "execute_result" 60 | } 61 | ], 62 | "source": [ 63 | "arr" 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.6" 91 | } 92 | }, 93 | "nbformat": 4, 94 | "nbformat_minor": 4 95 | } 96 | -------------------------------------------------------------------------------- /Code Workspace/Algorithms/.ipynb_checkpoints/4.4 Quick sort-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Quick Sort" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 10, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "def pivot_place(list1,first,last):\n", 17 | " pivot=list1[first]\n", 18 | " left=first+1\n", 19 | " right=last\n", 20 | "\n", 21 | " while True:\n", 22 | " while left<=right and list1[left]<=pivot:\n", 23 | " left=left+1\n", 24 | " while left<=right and list1[right]>=pivot:\n", 25 | " right=right-1\n", 26 | "\n", 27 | " if right= pivot and right >= left:\n", 106 | " right -= 1\n", 107 | "\n", 108 | " if right < left:\n", 109 | " done = True\n", 110 | " else:\n", 111 | " arr[left], arr[right] = arr[right], arr[left]\n", 112 | "\n", 113 | " arr[first], arr[right] = arr[right], arr[first]\n", 114 | "\n", 115 | " return right" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": 19, 121 | "metadata": {}, 122 | "outputs": [], 123 | "source": [ 124 | "list2=[22,44,3,35,1,66,8]\n", 125 | "quick_sort(list2)" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 20, 131 | "metadata": {}, 132 | "outputs": [ 133 | { 134 | "data": { 135 | "text/plain": [ 136 | "[1, 3, 8, 22, 35, 44, 66]" 137 | ] 138 | }, 139 | "execution_count": 20, 140 | "metadata": {}, 141 | "output_type": "execute_result" 142 | } 143 | ], 144 | "source": [ 145 | "list2" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": null, 151 | "metadata": {}, 152 | "outputs": [], 153 | "source": [] 154 | } 155 | ], 156 | "metadata": { 157 | "kernelspec": { 158 | "display_name": "Python 3", 159 | "language": "python", 160 | "name": "python3" 161 | }, 162 | "language_info": { 163 | "codemirror_mode": { 164 | "name": "ipython", 165 | "version": 3 166 | }, 167 | "file_extension": ".py", 168 | "mimetype": "text/x-python", 169 | "name": "python", 170 | "nbconvert_exporter": "python", 171 | "pygments_lexer": "ipython3", 172 | "version": "3.7.6" 173 | } 174 | }, 175 | "nbformat": 4, 176 | "nbformat_minor": 4 177 | } 178 | -------------------------------------------------------------------------------- /Code Workspace/Algorithms/.ipynb_checkpoints/4.5 Merge sort-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Merge sort" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 2, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "def merge_sort(list1):\n", 17 | " if len(list1)>1:\n", 18 | " mid = len(list1)//2\n", 19 | " \n", 20 | " left_list = list1[:mid]\n", 21 | " right_list = list1[mid:]\n", 22 | " \n", 23 | " merge_sort(left_list)\n", 24 | " merge_sort(right_list)\n", 25 | " \n", 26 | " i = j = k = 0\n", 27 | " \n", 28 | " while i < len(left_list) and j < len(right_list):\n", 29 | " if left_list[i] i:\n", 39 | " list1[k] = left_list[i]\n", 40 | " i += 1\n", 41 | " k += 1\n", 42 | " while len(right_list) > j:\n", 43 | " list1[k] = right_list[j]\n", 44 | " j += 1\n", 45 | " k += 1" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 3, 51 | "metadata": {}, 52 | "outputs": [ 53 | { 54 | "name": "stdout", 55 | "output_type": "stream", 56 | "text": [ 57 | "7\n", 58 | "22\n", 59 | "33\n", 60 | "5\n", 61 | "66\n", 62 | "1\n", 63 | "77\n", 64 | "80\n" 65 | ] 66 | } 67 | ], 68 | "source": [ 69 | "n = int(input())\n", 70 | "list1 = [ int(input()) for i in range(n)]" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 4, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [ 79 | "merge_sort(list1)" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 5, 85 | "metadata": {}, 86 | "outputs": [ 87 | { 88 | "data": { 89 | "text/plain": [ 90 | "[1, 5, 22, 33, 66, 77, 80]" 91 | ] 92 | }, 93 | "execution_count": 5, 94 | "metadata": {}, 95 | "output_type": "execute_result" 96 | } 97 | ], 98 | "source": [ 99 | "list1" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 3, 105 | "metadata": {}, 106 | "outputs": [], 107 | "source": [ 108 | "#method 2" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": 2, 114 | "metadata": {}, 115 | "outputs": [ 116 | { 117 | "name": "stdout", 118 | "output_type": "stream", 119 | "text": [ 120 | "[0, 1, 1, 2, 4, 7, 9, 26, 100]\n" 121 | ] 122 | } 123 | ], 124 | "source": [ 125 | "def merge(left,right):\n", 126 | " result = []\n", 127 | " i = j = 0\n", 128 | " while i < len(left) and j < len(right):\n", 129 | " if left[i] < right[j]:\n", 130 | " result.append(left[i])\n", 131 | " i += 1\n", 132 | " else:\n", 133 | " result.append(right[j])\n", 134 | " j += 1\n", 135 | " result += left[i:]\n", 136 | " result += right[j:]\n", 137 | " return result\n", 138 | "\n", 139 | "def mergeSort(arr):\n", 140 | " if len(arr) <= 1:\n", 141 | " return arr\n", 142 | " mid = len(arr)//2\n", 143 | " left = mergeSort(arr[:mid])\n", 144 | " right = mergeSort(arr[mid:])\n", 145 | " return merge(left,right)\n", 146 | " \n", 147 | "arr = [4,2,7,1,100,26,9,1,0]\n", 148 | "print(mergeSort(arr))" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": null, 154 | "metadata": {}, 155 | "outputs": [], 156 | "source": [] 157 | } 158 | ], 159 | "metadata": { 160 | "kernelspec": { 161 | "display_name": "Python 3", 162 | "language": "python", 163 | "name": "python3" 164 | }, 165 | "language_info": { 166 | "codemirror_mode": { 167 | "name": "ipython", 168 | "version": 3 169 | }, 170 | "file_extension": ".py", 171 | "mimetype": "text/x-python", 172 | "name": "python", 173 | "nbconvert_exporter": "python", 174 | "pygments_lexer": "ipython3", 175 | "version": "3.7.6" 176 | } 177 | }, 178 | "nbformat": 4, 179 | "nbformat_minor": 4 180 | } 181 | -------------------------------------------------------------------------------- /Code Workspace/Algorithms/.ipynb_checkpoints/4.6 shell sort-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Shell sort" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 6, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "def shell_sort(alist):\n", 17 | " gap = len(alist)//2\n", 18 | " while gap > 0:\n", 19 | " for index in range(gap, len(list1)):\n", 20 | " curr_ele = alist[index]\n", 21 | " pos = index\n", 22 | " \n", 23 | " while pos >= gap and curr_ele< alist[pos-gap]:\n", 24 | " alist[pos] = alist[pos -gap]\n", 25 | " pos = pos - gap\n", 26 | " alist[pos] = curr_ele\n", 27 | " gap = gap //2" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 7, 33 | "metadata": {}, 34 | "outputs": [ 35 | { 36 | "name": "stdout", 37 | "output_type": "stream", 38 | "text": [ 39 | "5\n", 40 | "22\n", 41 | "33\n", 42 | "11\n", 43 | "44\n", 44 | "2\n" 45 | ] 46 | } 47 | ], 48 | "source": [ 49 | "n = int(input())\n", 50 | "list1 = [ int(input()) for i in range(n)]" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 8, 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [ 59 | "shell_sort(list1)" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 9, 65 | "metadata": {}, 66 | "outputs": [ 67 | { 68 | "data": { 69 | "text/plain": [ 70 | "[2, 11, 22, 33, 44]" 71 | ] 72 | }, 73 | "execution_count": 9, 74 | "metadata": {}, 75 | "output_type": "execute_result" 76 | } 77 | ], 78 | "source": [ 79 | "list1" 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.6" 107 | } 108 | }, 109 | "nbformat": 4, 110 | "nbformat_minor": 4 111 | } 112 | -------------------------------------------------------------------------------- /Code Workspace/Algorithms/.ipynb_checkpoints/5.1 Recursion-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## RECURSION" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## 1. example with factorial of a number" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 15, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "def fact(n):\n", 24 | " #base case\n", 25 | " if n==0:\n", 26 | " return 1\n", 27 | " return n*fact((n-1))" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 16, 33 | "metadata": {}, 34 | "outputs": [ 35 | { 36 | "data": { 37 | "text/plain": [ 38 | "120" 39 | ] 40 | }, 41 | "execution_count": 16, 42 | "metadata": {}, 43 | "output_type": "execute_result" 44 | } 45 | ], 46 | "source": [ 47 | "fact(5)" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": {}, 53 | "source": [ 54 | "## 2. recursive sum:" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 17, 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [ 63 | "\n", 64 | "def rec_sum(n):\n", 65 | " \n", 66 | " # Base Case\n", 67 | " if n == 0:\n", 68 | " return 0\n", 69 | " \n", 70 | " # Recursion\n", 71 | " else:\n", 72 | " return n + rec_sum(n-1)" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 18, 78 | "metadata": {}, 79 | "outputs": [ 80 | { 81 | "data": { 82 | "text/plain": [ 83 | "10" 84 | ] 85 | }, 86 | "execution_count": 18, 87 | "metadata": {}, 88 | "output_type": "execute_result" 89 | } 90 | ], 91 | "source": [ 92 | "rec_sum(4)" 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": {}, 98 | "source": [ 99 | "### 3. returns the sum of all the individual digits in that integer. For example: if n = 4321, return 4+3+2+1\n" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 19, 105 | "metadata": {}, 106 | "outputs": [], 107 | "source": [ 108 | "def sum_func(n):\n", 109 | " #base case\n", 110 | " if len(str(n))==1:\n", 111 | " return n\n", 112 | " return n%10 +sum_func(n//10)" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": 13, 118 | "metadata": {}, 119 | "outputs": [ 120 | { 121 | "data": { 122 | "text/plain": [ 123 | "10" 124 | ] 125 | }, 126 | "execution_count": 13, 127 | "metadata": {}, 128 | "output_type": "execute_result" 129 | } 130 | ], 131 | "source": [ 132 | "sum_func(4321)" 133 | ] 134 | }, 135 | { 136 | "cell_type": "markdown", 137 | "metadata": {}, 138 | "source": [ 139 | "## 4 Reverse a String" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": 2, 145 | "metadata": {}, 146 | "outputs": [], 147 | "source": [ 148 | "def reverse(s):\n", 149 | " \n", 150 | " # Base Case\n", 151 | " if len(s) <= 1:\n", 152 | " return s\n", 153 | "\n", 154 | " # Recursion\n", 155 | " return reverse(s[1:]) + s[0]" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": 3, 161 | "metadata": {}, 162 | "outputs": [ 163 | { 164 | "data": { 165 | "text/plain": [ 166 | "'dlrow olleh'" 167 | ] 168 | }, 169 | "execution_count": 3, 170 | "metadata": {}, 171 | "output_type": "execute_result" 172 | } 173 | ], 174 | "source": [ 175 | "reverse('hello world')" 176 | ] 177 | }, 178 | { 179 | "cell_type": "markdown", 180 | "metadata": {}, 181 | "source": [ 182 | "## 5. Fibonnaci Sequence" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": 4, 188 | "metadata": {}, 189 | "outputs": [], 190 | "source": [ 191 | "def fib_rec(n):\n", 192 | " \n", 193 | " # Base Case\n", 194 | " if n == 0 or n == 1:\n", 195 | " return n\n", 196 | " \n", 197 | " # Recursion\n", 198 | " else:\n", 199 | " return fib_rec(n-1) + fib_rec(n-2)\n", 200 | " " 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": 5, 206 | "metadata": {}, 207 | "outputs": [ 208 | { 209 | "data": { 210 | "text/plain": [ 211 | "55" 212 | ] 213 | }, 214 | "execution_count": 5, 215 | "metadata": {}, 216 | "output_type": "execute_result" 217 | } 218 | ], 219 | "source": [ 220 | "fib_rec(10)" 221 | ] 222 | }, 223 | { 224 | "cell_type": "markdown", 225 | "metadata": {}, 226 | "source": [ 227 | "### 6. " 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": null, 233 | "metadata": {}, 234 | "outputs": [], 235 | "source": [] 236 | } 237 | ], 238 | "metadata": { 239 | "kernelspec": { 240 | "display_name": "Python 3", 241 | "language": "python", 242 | "name": "python3" 243 | }, 244 | "language_info": { 245 | "codemirror_mode": { 246 | "name": "ipython", 247 | "version": 3 248 | }, 249 | "file_extension": ".py", 250 | "mimetype": "text/x-python", 251 | "name": "python", 252 | "nbconvert_exporter": "python", 253 | "pygments_lexer": "ipython3", 254 | "version": "3.7.6" 255 | } 256 | }, 257 | "nbformat": 4, 258 | "nbformat_minor": 4 259 | } 260 | -------------------------------------------------------------------------------- /Code Workspace/Algorithms/.ipynb_checkpoints/5.2 Recursion-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## RECURSION" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "### 6. determine if it is possible to split the string in a way in which words can be made from the list of words." 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 6, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "def word_split(phrase,list_of_words, output = None):\n", 24 | " '''\n", 25 | " Note: This is a very \"python-y\" solution.\n", 26 | " ''' \n", 27 | " \n", 28 | " # Checks to see if any output has been initiated.\n", 29 | " # If you default output=[], it would be overwritten for every recursion!\n", 30 | " if output is None:\n", 31 | " output = []\n", 32 | " \n", 33 | " # For every word in list\n", 34 | " for word in list_of_words:\n", 35 | " \n", 36 | " # If the current phrase begins with the word, we have a split point!\n", 37 | " if phrase.startswith(word):\n", 38 | " \n", 39 | " # Add the word to the output\n", 40 | " output.append(word)\n", 41 | " \n", 42 | " # Recursively call the split function on the remaining portion of the phrase--- phrase[len(word):]\n", 43 | " # Remember to pass along the output and list of words\n", 44 | " return word_split(phrase[len(word):],list_of_words,output)\n", 45 | " \n", 46 | " # Finally return output if no phrase.startswith(word) returns True\n", 47 | " return output " 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 7, 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "data": { 57 | "text/plain": [ 58 | "['the', 'man', 'ran']" 59 | ] 60 | }, 61 | "execution_count": 7, 62 | "metadata": {}, 63 | "output_type": "execute_result" 64 | } 65 | ], 66 | "source": [ 67 | "word_split('themanran',['the','ran','man'])" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 8, 73 | "metadata": {}, 74 | "outputs": [ 75 | { 76 | "data": { 77 | "text/plain": [ 78 | "['i', 'love', 'dogs', 'John']" 79 | ] 80 | }, 81 | "execution_count": 8, 82 | "metadata": {}, 83 | "output_type": "execute_result" 84 | } 85 | ], 86 | "source": [ 87 | "word_split('ilovedogsJohn',['i','am','a','dogs','lover','love','John'])" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": 9, 93 | "metadata": {}, 94 | "outputs": [ 95 | { 96 | "data": { 97 | "text/plain": [ 98 | "[]" 99 | ] 100 | }, 101 | "execution_count": 9, 102 | "metadata": {}, 103 | "output_type": "execute_result" 104 | } 105 | ], 106 | "source": [ 107 | "word_split('themanran',['clown','ran','man'])" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": null, 113 | "metadata": {}, 114 | "outputs": [], 115 | "source": [] 116 | } 117 | ], 118 | "metadata": { 119 | "kernelspec": { 120 | "display_name": "Python 3", 121 | "language": "python", 122 | "name": "python3" 123 | }, 124 | "language_info": { 125 | "codemirror_mode": { 126 | "name": "ipython", 127 | "version": 3 128 | }, 129 | "file_extension": ".py", 130 | "mimetype": "text/x-python", 131 | "name": "python", 132 | "nbconvert_exporter": "python", 133 | "pygments_lexer": "ipython3", 134 | "version": "3.7.6" 135 | } 136 | }, 137 | "nbformat": 4, 138 | "nbformat_minor": 4 139 | } 140 | -------------------------------------------------------------------------------- /Code Workspace/Algorithms/.ipynb_checkpoints/Dictionaries-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 4 6 | } 7 | -------------------------------------------------------------------------------- /Code Workspace/Algorithms/.ipynb_checkpoints/Recursion Problem 1 - Reverse String - SOLUTION-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Reverse a String\n", 8 | "\n", 9 | "This interview question requires you to reverse a string using recursion. Make sure to think of the base case here.\n", 10 | "\n", 11 | "Again, make sure you use *recursion* to accomplish this. **Do not slice (e.g. string[::-1]) or use iteration, there muse be a recursive call for the function.**\n", 12 | "\n", 13 | "____\n", 14 | "\n", 15 | "## Solution\n", 16 | "\n", 17 | "In order to reverse a string using recursion we need to consider what a base and recursive case would look like. Here we've set a base case to be when the length of the string we are passing through the function is length less than or equal to 1.\n", 18 | "\n", 19 | "During the recursive case we grab the first letter and add it on to the recursive call." 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 7, 25 | "metadata": {}, 26 | "outputs": [], 27 | "source": [ 28 | "def reverse(s):\n", 29 | " \n", 30 | " # Base Case\n", 31 | " if len(s) <= 1:\n", 32 | " return s\n", 33 | "\n", 34 | " # Recursion\n", 35 | " return reverse(s[1:]) + s[0]" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 10, 41 | "metadata": {}, 42 | "outputs": [ 43 | { 44 | "data": { 45 | "text/plain": [ 46 | "'dlrow olleh'" 47 | ] 48 | }, 49 | "execution_count": 10, 50 | "metadata": {}, 51 | "output_type": "execute_result" 52 | } 53 | ], 54 | "source": [ 55 | "reverse('hello world')" 56 | ] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "metadata": { 61 | "collapsed": true 62 | }, 63 | "source": [ 64 | "# Test Your Solution\n", 65 | "\n", 66 | "Run the cell below to test your solution against the following cases:\n", 67 | "\n", 68 | " string = 'hello'\n", 69 | " string = 'hello world'\n", 70 | " string = '123456789'" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 13, 76 | "metadata": {}, 77 | "outputs": [ 78 | { 79 | "name": "stdout", 80 | "output_type": "stream", 81 | "text": [ 82 | "PASSED ALL TEST CASES!\n" 83 | ] 84 | } 85 | ], 86 | "source": [ 87 | "'''\n", 88 | "RUN THIS CELL TO TEST YOUR FUNCTION AGAINST SOME TEST CASES\n", 89 | "'''\n", 90 | "\n", 91 | "from nose.tools import assert_equal\n", 92 | "\n", 93 | "class TestReverse(object):\n", 94 | " \n", 95 | " def test_rev(self,solution):\n", 96 | " assert_equal(solution('hello'),'olleh')\n", 97 | " assert_equal(solution('hello world'),'dlrow olleh')\n", 98 | " assert_equal(solution('123456789'),'987654321')\n", 99 | " \n", 100 | " print 'PASSED ALL TEST CASES!'\n", 101 | " \n", 102 | "# Run Tests\n", 103 | "test = TestReverse()\n", 104 | "test.test_rev(reverse)" 105 | ] 106 | }, 107 | { 108 | "cell_type": "markdown", 109 | "metadata": {}, 110 | "source": [ 111 | "## Extra Notes\n", 112 | "\n", 113 | "The \"trick\" to this question was thinking about what a base case would look like when reversing a string recursively. It takes a lot of practice to be able to begin thinking like this, so don't worry if you're struggling! However it is important to fully understand the solution!\n", 114 | "\n", 115 | "**Good Job!**" 116 | ] 117 | } 118 | ], 119 | "metadata": { 120 | "kernelspec": { 121 | "display_name": "Python 3", 122 | "language": "python", 123 | "name": "python3" 124 | }, 125 | "language_info": { 126 | "codemirror_mode": { 127 | "name": "ipython", 128 | "version": 3 129 | }, 130 | "file_extension": ".py", 131 | "mimetype": "text/x-python", 132 | "name": "python", 133 | "nbconvert_exporter": "python", 134 | "pygments_lexer": "ipython3", 135 | "version": "3.7.6" 136 | } 137 | }, 138 | "nbformat": 4, 139 | "nbformat_minor": 1 140 | } 141 | -------------------------------------------------------------------------------- /Code Workspace/Algorithms/2.1 Sequential Search.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Sequential Search" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "### Unordered lists." 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 16, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "def seq_search(arr,ele):\n", 24 | " \"\"\"\n", 25 | " Sequential Search \n", 26 | " \"\"\"\n", 27 | " for i in range(len(arr)):\n", 28 | " return arr[i]==ele\n", 29 | " return False" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 17, 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "arr = [1,9,2,8,3,4,7,5,6,10]" 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 | "True\n" 51 | ] 52 | } 53 | ], 54 | "source": [ 55 | "print(seq_search(arr,1))" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": 19, 61 | "metadata": {}, 62 | "outputs": [ 63 | { 64 | "name": "stdout", 65 | "output_type": "stream", 66 | "text": [ 67 | "False\n" 68 | ] 69 | } 70 | ], 71 | "source": [ 72 | "print(seq_search(arr,11))" 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "metadata": {}, 78 | "source": [ 79 | "### Ordered lists." 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 2, 85 | "metadata": {}, 86 | "outputs": [], 87 | "source": [ 88 | "def seq_search(arr,ele):\n", 89 | " \"\"\"\n", 90 | " Sequential Search\n", 91 | " \"\"\" \n", 92 | " for i in range(len(arr)):\n", 93 | " if arr[i]>k:\n", 94 | " return False\n", 95 | " if arr[i]==k :\n", 96 | " return True\n", 97 | " return False" 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.6" 125 | } 126 | }, 127 | "nbformat": 4, 128 | "nbformat_minor": 4 129 | } 130 | -------------------------------------------------------------------------------- /Code Workspace/Algorithms/2.2 Binary Search.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "def binary_search(arr,k):\n", 10 | " first=0\n", 11 | " last=len(arr)-1\n", 12 | "\n", 13 | " while first <= last:\n", 14 | " middle=(first+last)//2\n", 15 | " if arr[middle]==k:\n", 16 | " return True\n", 17 | " elif arr[middle]>k:\n", 18 | " last=middle-1\n", 19 | " else:\n", 20 | " first=middle+1\n", 21 | " return False" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 5, 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "arr=[11,21,31,41,51,61,71,81,91,101]" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 10, 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "data": { 40 | "text/plain": [ 41 | "False" 42 | ] 43 | }, 44 | "execution_count": 10, 45 | "metadata": {}, 46 | "output_type": "execute_result" 47 | } 48 | ], 49 | "source": [ 50 | "binary_search(arr,614)" 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.6" 78 | } 79 | }, 80 | "nbformat": 4, 81 | "nbformat_minor": 4 82 | } 83 | -------------------------------------------------------------------------------- /Code Workspace/Algorithms/3.1 Hash Table.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Implementation of Hash Table" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 4, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "class HashTable():\n", 17 | " def __init__(self,size):\n", 18 | " self.size = size\n", 19 | " self.bucket = [[] for i in range(self.size)]\n", 20 | " \n", 21 | " def hash_function(self,key):\n", 22 | " return hash(key) % self.size\n", 23 | " \n", 24 | " def add(self,key):\n", 25 | " i = self.hash_function(key)\n", 26 | " if not key in self.bucket[i]:\n", 27 | " self.bucket[i].append(key)\n", 28 | " \n", 29 | " def remove(self,key):\n", 30 | " i = self.hash_function(key)\n", 31 | " if key in self.bucket[i]:\n", 32 | " self.bucket[i].remove(key)\n", 33 | " \n", 34 | " def contains(self,key):\n", 35 | " i = self.hash_function(key)\n", 36 | " if key in self.bucket[i]:\n", 37 | " return True\n", 38 | " return False" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 5, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "myht=HashTable(10)" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 6, 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [ 56 | "myht.add('hello')" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 7, 62 | "metadata": {}, 63 | "outputs": [], 64 | "source": [ 65 | "myht.add(4432)" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 8, 71 | "metadata": {}, 72 | "outputs": [], 73 | "source": [ 74 | "myht.add(44)" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 10, 80 | "metadata": {}, 81 | "outputs": [], 82 | "source": [ 83 | "myht.remove(44)" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 11, 89 | "metadata": {}, 90 | "outputs": [ 91 | { 92 | "data": { 93 | "text/plain": [ 94 | "False" 95 | ] 96 | }, 97 | "execution_count": 11, 98 | "metadata": {}, 99 | "output_type": "execute_result" 100 | } 101 | ], 102 | "source": [ 103 | "myht.contains(44)" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 12, 109 | "metadata": {}, 110 | "outputs": [ 111 | { 112 | "data": { 113 | "text/plain": [ 114 | "True" 115 | ] 116 | }, 117 | "execution_count": 12, 118 | "metadata": {}, 119 | "output_type": "execute_result" 120 | } 121 | ], 122 | "source": [ 123 | "myht.contains(4432)" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": null, 129 | "metadata": {}, 130 | "outputs": [], 131 | "source": [] 132 | } 133 | ], 134 | "metadata": { 135 | "kernelspec": { 136 | "display_name": "Python 3", 137 | "language": "python", 138 | "name": "python3" 139 | }, 140 | "language_info": { 141 | "codemirror_mode": { 142 | "name": "ipython", 143 | "version": 3 144 | }, 145 | "file_extension": ".py", 146 | "mimetype": "text/x-python", 147 | "name": "python", 148 | "nbconvert_exporter": "python", 149 | "pygments_lexer": "ipython3", 150 | "version": "3.7.6" 151 | } 152 | }, 153 | "nbformat": 4, 154 | "nbformat_minor": 4 155 | } 156 | -------------------------------------------------------------------------------- /Code Workspace/Algorithms/4.1 Bubble Sort.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Bubble sort" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 5, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "arr=[98,23,45,14,6,67,33,42]" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 22, 22 | "metadata": {}, 23 | "outputs": [], 24 | "source": [ 25 | "#method 1-\n", 26 | "#outerloop will run the times of number of elements:i\n", 27 | "#at each pass one elements bubbles up to the end\n", 28 | "#the inner loop will run for each element to bubble up to the end, will run len-i\n", 29 | "# for swapping use: a,b=b,a" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 23, 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "def bubble_sort(arr):\n", 39 | " for i in range(0,len(arr)-1):\n", 40 | " for j in range(0,len(arr)-i-1):\n", 41 | " if arr[j]>arr[j+1]:\n", 42 | " arr[j],arr[j+1]=arr[j+1],arr[j]\n", 43 | " print(arr)\n", 44 | " return arr" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 24, 50 | "metadata": {}, 51 | "outputs": [], 52 | "source": [ 53 | "arr=[98,23,45,14,6,67,33,42]" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 25, 59 | "metadata": {}, 60 | "outputs": [ 61 | { 62 | "name": "stdout", 63 | "output_type": "stream", 64 | "text": [ 65 | "[23, 98, 45, 14, 6, 67, 33, 42]\n", 66 | "[23, 45, 98, 14, 6, 67, 33, 42]\n", 67 | "[23, 45, 14, 98, 6, 67, 33, 42]\n", 68 | "[23, 45, 14, 6, 98, 67, 33, 42]\n", 69 | "[23, 45, 14, 6, 67, 98, 33, 42]\n", 70 | "[23, 45, 14, 6, 67, 33, 98, 42]\n", 71 | "[23, 45, 14, 6, 67, 33, 42, 98]\n", 72 | "[23, 14, 45, 6, 67, 33, 42, 98]\n", 73 | "[23, 14, 6, 45, 67, 33, 42, 98]\n", 74 | "[23, 14, 6, 45, 33, 67, 42, 98]\n", 75 | "[23, 14, 6, 45, 33, 42, 67, 98]\n", 76 | "[14, 23, 6, 45, 33, 42, 67, 98]\n", 77 | "[14, 6, 23, 45, 33, 42, 67, 98]\n", 78 | "[14, 6, 23, 33, 45, 42, 67, 98]\n", 79 | "[14, 6, 23, 33, 42, 45, 67, 98]\n", 80 | "[6, 14, 23, 33, 42, 45, 67, 98]\n" 81 | ] 82 | }, 83 | { 84 | "data": { 85 | "text/plain": [ 86 | "[6, 14, 23, 33, 42, 45, 67, 98]" 87 | ] 88 | }, 89 | "execution_count": 25, 90 | "metadata": {}, 91 | "output_type": "execute_result" 92 | } 93 | ], 94 | "source": [ 95 | "bubble_sort(arr)" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": 26, 101 | "metadata": {}, 102 | "outputs": [ 103 | { 104 | "name": "stdout", 105 | "output_type": "stream", 106 | "text": [ 107 | "[5, 8, 9, 12, 13, 65]\n" 108 | ] 109 | } 110 | ], 111 | "source": [ 112 | "#method-2\n", 113 | "my_list = [12, 5, 13, 8, 9, 65]\n", 114 | " \n", 115 | "def bubble_sort(arr):\n", 116 | " sorted = False\n", 117 | " length = len(arr)-1\n", 118 | " while not sorted:\n", 119 | " sorted = True\n", 120 | " for index, value in enumerate(arr[:length]):\n", 121 | " if arr[index] > arr[index+1]:\n", 122 | " sorted = False\n", 123 | " arr[index], arr[index+1] = arr[index+1], arr[index]\n", 124 | " return arr\n", 125 | " \n", 126 | "print(bubble_sort(my_list))" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": null, 132 | "metadata": {}, 133 | "outputs": [], 134 | "source": [] 135 | } 136 | ], 137 | "metadata": { 138 | "kernelspec": { 139 | "display_name": "Python 3", 140 | "language": "python", 141 | "name": "python3" 142 | }, 143 | "language_info": { 144 | "codemirror_mode": { 145 | "name": "ipython", 146 | "version": 3 147 | }, 148 | "file_extension": ".py", 149 | "mimetype": "text/x-python", 150 | "name": "python", 151 | "nbconvert_exporter": "python", 152 | "pygments_lexer": "ipython3", 153 | "version": "3.7.6" 154 | } 155 | }, 156 | "nbformat": 4, 157 | "nbformat_minor": 4 158 | } 159 | -------------------------------------------------------------------------------- /Code Workspace/Algorithms/4.2 Selection sort.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Selection Sort" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 12, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "def selection_sort(arr): \n", 17 | " for i in range(len(arr)):\n", 18 | " minpos = i\n", 19 | "\n", 20 | " for j in range(i+1,len(arr)):\n", 21 | " if arr[j] curr_val and pos>0:\n", 22 | " arr[pos] = arr[pos-1]\n", 23 | " pos = pos - 1\n", 24 | " \n", 25 | " arr[pos] = curr_val" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 9, 31 | "metadata": {}, 32 | "outputs": [], 33 | "source": [ 34 | "arr = [2,4,3,5,1]" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 11, 40 | "metadata": {}, 41 | "outputs": [], 42 | "source": [ 43 | "insertion_sort(arr)" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 12, 49 | "metadata": {}, 50 | "outputs": [ 51 | { 52 | "data": { 53 | "text/plain": [ 54 | "[1, 2, 3, 4, 5]" 55 | ] 56 | }, 57 | "execution_count": 12, 58 | "metadata": {}, 59 | "output_type": "execute_result" 60 | } 61 | ], 62 | "source": [ 63 | "arr" 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.6" 91 | } 92 | }, 93 | "nbformat": 4, 94 | "nbformat_minor": 4 95 | } 96 | -------------------------------------------------------------------------------- /Code Workspace/Algorithms/4.4 Quick sort.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Quick Sort" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 10, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "def pivot_place(list1,first,last):\n", 17 | " pivot=list1[first]\n", 18 | " left=first+1\n", 19 | " right=last\n", 20 | "\n", 21 | " while True:\n", 22 | " while left<=right and list1[left]<=pivot:\n", 23 | " left=left+1\n", 24 | " while left<=right and list1[right]>=pivot:\n", 25 | " right=right-1\n", 26 | "\n", 27 | " if right= pivot and right >= left:\n", 106 | " right -= 1\n", 107 | "\n", 108 | " if right < left:\n", 109 | " done = True\n", 110 | " else:\n", 111 | " arr[left], arr[right] = arr[right], arr[left]\n", 112 | "\n", 113 | " arr[first], arr[right] = arr[right], arr[first]\n", 114 | "\n", 115 | " return right" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": 19, 121 | "metadata": {}, 122 | "outputs": [], 123 | "source": [ 124 | "list2=[22,44,3,35,1,66,8]\n", 125 | "quick_sort(list2)" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 20, 131 | "metadata": {}, 132 | "outputs": [ 133 | { 134 | "data": { 135 | "text/plain": [ 136 | "[1, 3, 8, 22, 35, 44, 66]" 137 | ] 138 | }, 139 | "execution_count": 20, 140 | "metadata": {}, 141 | "output_type": "execute_result" 142 | } 143 | ], 144 | "source": [ 145 | "list2" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": null, 151 | "metadata": {}, 152 | "outputs": [], 153 | "source": [] 154 | } 155 | ], 156 | "metadata": { 157 | "kernelspec": { 158 | "display_name": "Python 3", 159 | "language": "python", 160 | "name": "python3" 161 | }, 162 | "language_info": { 163 | "codemirror_mode": { 164 | "name": "ipython", 165 | "version": 3 166 | }, 167 | "file_extension": ".py", 168 | "mimetype": "text/x-python", 169 | "name": "python", 170 | "nbconvert_exporter": "python", 171 | "pygments_lexer": "ipython3", 172 | "version": "3.7.6" 173 | } 174 | }, 175 | "nbformat": 4, 176 | "nbformat_minor": 4 177 | } 178 | -------------------------------------------------------------------------------- /Code Workspace/Algorithms/4.5 Merge sort.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Merge sort" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 2, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "def merge_sort(list1):\n", 17 | " if len(list1)>1:\n", 18 | " mid = len(list1)//2\n", 19 | " \n", 20 | " left_list = list1[:mid]\n", 21 | " right_list = list1[mid:]\n", 22 | " \n", 23 | " merge_sort(left_list)\n", 24 | " merge_sort(right_list)\n", 25 | " \n", 26 | " i = j = k = 0\n", 27 | " \n", 28 | " while i < len(left_list) and j < len(right_list):\n", 29 | " if left_list[i] i:\n", 39 | " list1[k] = left_list[i]\n", 40 | " i += 1\n", 41 | " k += 1\n", 42 | " while len(right_list) > j:\n", 43 | " list1[k] = right_list[j]\n", 44 | " j += 1\n", 45 | " k += 1" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 3, 51 | "metadata": {}, 52 | "outputs": [ 53 | { 54 | "name": "stdout", 55 | "output_type": "stream", 56 | "text": [ 57 | "7\n", 58 | "22\n", 59 | "33\n", 60 | "5\n", 61 | "66\n", 62 | "1\n", 63 | "77\n", 64 | "80\n" 65 | ] 66 | } 67 | ], 68 | "source": [ 69 | "n = int(input())\n", 70 | "list1 = [ int(input()) for i in range(n)]" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 4, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [ 79 | "merge_sort(list1)" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 5, 85 | "metadata": {}, 86 | "outputs": [ 87 | { 88 | "data": { 89 | "text/plain": [ 90 | "[1, 5, 22, 33, 66, 77, 80]" 91 | ] 92 | }, 93 | "execution_count": 5, 94 | "metadata": {}, 95 | "output_type": "execute_result" 96 | } 97 | ], 98 | "source": [ 99 | "list1" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 3, 105 | "metadata": {}, 106 | "outputs": [], 107 | "source": [ 108 | "#method 2" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": 2, 114 | "metadata": {}, 115 | "outputs": [ 116 | { 117 | "name": "stdout", 118 | "output_type": "stream", 119 | "text": [ 120 | "[0, 1, 1, 2, 4, 7, 9, 26, 100]\n" 121 | ] 122 | } 123 | ], 124 | "source": [ 125 | "def merge(left,right):\n", 126 | " result = []\n", 127 | " i = j = 0\n", 128 | " while i < len(left) and j < len(right):\n", 129 | " if left[i] < right[j]:\n", 130 | " result.append(left[i])\n", 131 | " i += 1\n", 132 | " else:\n", 133 | " result.append(right[j])\n", 134 | " j += 1\n", 135 | " result += left[i:]\n", 136 | " result += right[j:]\n", 137 | " return result\n", 138 | "\n", 139 | "def mergeSort(arr):\n", 140 | " if len(arr) <= 1:\n", 141 | " return arr\n", 142 | " mid = len(arr)//2\n", 143 | " left = mergeSort(arr[:mid])\n", 144 | " right = mergeSort(arr[mid:])\n", 145 | " return merge(left,right)\n", 146 | " \n", 147 | "arr = [4,2,7,1,100,26,9,1,0]\n", 148 | "print(mergeSort(arr))" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": null, 154 | "metadata": {}, 155 | "outputs": [], 156 | "source": [] 157 | } 158 | ], 159 | "metadata": { 160 | "kernelspec": { 161 | "display_name": "Python 3", 162 | "language": "python", 163 | "name": "python3" 164 | }, 165 | "language_info": { 166 | "codemirror_mode": { 167 | "name": "ipython", 168 | "version": 3 169 | }, 170 | "file_extension": ".py", 171 | "mimetype": "text/x-python", 172 | "name": "python", 173 | "nbconvert_exporter": "python", 174 | "pygments_lexer": "ipython3", 175 | "version": "3.7.6" 176 | } 177 | }, 178 | "nbformat": 4, 179 | "nbformat_minor": 4 180 | } 181 | -------------------------------------------------------------------------------- /Code Workspace/Algorithms/4.6 shell sort.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Shell sort" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 6, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "def shell_sort(alist):\n", 17 | " gap = len(alist)//2\n", 18 | " while gap > 0:\n", 19 | " for index in range(gap, len(list1)):\n", 20 | " curr_ele = alist[index]\n", 21 | " pos = index\n", 22 | " \n", 23 | " while pos >= gap and curr_ele< alist[pos-gap]:\n", 24 | " alist[pos] = alist[pos -gap]\n", 25 | " pos = pos - gap\n", 26 | " alist[pos] = curr_ele\n", 27 | " gap = gap //2" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 7, 33 | "metadata": {}, 34 | "outputs": [ 35 | { 36 | "name": "stdout", 37 | "output_type": "stream", 38 | "text": [ 39 | "5\n", 40 | "22\n", 41 | "33\n", 42 | "11\n", 43 | "44\n", 44 | "2\n" 45 | ] 46 | } 47 | ], 48 | "source": [ 49 | "n = int(input())\n", 50 | "list1 = [ int(input()) for i in range(n)]" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 8, 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [ 59 | "shell_sort(list1)" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 9, 65 | "metadata": {}, 66 | "outputs": [ 67 | { 68 | "data": { 69 | "text/plain": [ 70 | "[2, 11, 22, 33, 44]" 71 | ] 72 | }, 73 | "execution_count": 9, 74 | "metadata": {}, 75 | "output_type": "execute_result" 76 | } 77 | ], 78 | "source": [ 79 | "list1" 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.6" 107 | } 108 | }, 109 | "nbformat": 4, 110 | "nbformat_minor": 4 111 | } 112 | -------------------------------------------------------------------------------- /Code Workspace/Algorithms/5.1 HeapSort.py: -------------------------------------------------------------------------------- 1 | class MaxHeap(): 2 | 3 | #we receive a list of items 4 | def __init__(self,items = []): 5 | self.heap = [0] 6 | # we create a heap list and store 0 at first Index 7 | # we will not use first Index 8 | for i in items: 9 | # we will store the items received in items[] in heap 10 | self.heap.append(i) 11 | #now we will float them up to thier proper position 12 | self.float_up(len(self.heap) - 1) 13 | 14 | def push(self,data): 15 | #append at last location 16 | self.heap.append(data) 17 | #heapify 18 | self.float_up(len(self.heap) - 1) 19 | 20 | def peek(self): 21 | #check topmost node - stored at index 1 22 | if self.heap[1]: 23 | return self.heap[1] 24 | return False 25 | 26 | def pop(self): 27 | #case 1: more than 2 Value 28 | #in that case swap the root to last Value 29 | if len(self.heap) > 2: 30 | #swap the max val and last node 31 | self.swap(1,len(self.heap) - 1) 32 | #pop the maximum value 33 | max=self.heap.pop() 34 | #call the helper fn 35 | self.bubble_down(1) 36 | 37 | #case 2: when one value, pop it and we have empty heap 38 | elif len(self.heap) == 2: 39 | max=self.heap.pop() 40 | 41 | #case 3: no root (pop from empty heap)then return false 42 | else: 43 | max = False 44 | 45 | return max 46 | 47 | #helper methods: 48 | def swap(self,i,j): 49 | self.heap[i], self.heap[j] = self.heap[j], self.heap[i] 50 | 51 | def float_up(self,index): 52 | #find parent 53 | parent = index//2 54 | 55 | if index <= 1: 56 | return 57 | 58 | elif self.heap[index] > self.heap[parent]: 59 | self.swap(index,parent) 60 | #recursive call 61 | self.float_up(parent) 62 | 63 | def bubble_down(self,index): #max heapify 64 | left = index * 2 65 | right = index * 2 + 1 66 | 67 | largest = index 68 | 69 | if len(self.heap) > left and self.heap[largest] < self.heap[left]: 70 | largest = left 71 | if len(self.heap) > right and self.heap[largest] < self.heap[right]: 72 | largest = right 73 | 74 | if largest != index: 75 | self.swap(index,largest) 76 | #recursive call 77 | self.bubble_down(largest) 78 | 79 | #Warning: this will delete your heap 80 | 81 | def sort_heap(self, order = 0): 82 | temp = [] 83 | while len(self.heap) > 1: 84 | temp.append(self.pop()) 85 | if order == 0: 86 | return temp 87 | return temp[::-1] 88 | 89 | 90 | m = MaxHeap([95,3,21]) 91 | m.push(10) 92 | 93 | print(str(m.heap[1:len(m.heap)])) 94 | print(str(m.pop())) 95 | print(m.sort_heap()) 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | -------------------------------------------------------------------------------- /Code Workspace/Algorithms/6.1 Recursion.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## RECURSION" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## 1. example with factorial of a number" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 15, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "def fact(n):\n", 24 | " #base case\n", 25 | " if n==0:\n", 26 | " return 1\n", 27 | " return n*fact((n-1))" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 16, 33 | "metadata": {}, 34 | "outputs": [ 35 | { 36 | "data": { 37 | "text/plain": [ 38 | "120" 39 | ] 40 | }, 41 | "execution_count": 16, 42 | "metadata": {}, 43 | "output_type": "execute_result" 44 | } 45 | ], 46 | "source": [ 47 | "fact(5)" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": {}, 53 | "source": [ 54 | "## 2. recursive sum:" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 17, 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [ 63 | "\n", 64 | "def rec_sum(n):\n", 65 | " \n", 66 | " # Base Case\n", 67 | " if n == 0:\n", 68 | " return 0\n", 69 | " \n", 70 | " # Recursion\n", 71 | " else:\n", 72 | " return n + rec_sum(n-1)" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 18, 78 | "metadata": {}, 79 | "outputs": [ 80 | { 81 | "data": { 82 | "text/plain": [ 83 | "10" 84 | ] 85 | }, 86 | "execution_count": 18, 87 | "metadata": {}, 88 | "output_type": "execute_result" 89 | } 90 | ], 91 | "source": [ 92 | "rec_sum(4)" 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": {}, 98 | "source": [ 99 | "### 3. returns the sum of all the individual digits in that integer. For example: if n = 4321, return 4+3+2+1\n" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 19, 105 | "metadata": {}, 106 | "outputs": [], 107 | "source": [ 108 | "def sum_func(n):\n", 109 | " #base case\n", 110 | " if len(str(n))==1:\n", 111 | " return n\n", 112 | " return n%10 +sum_func(n//10)" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": 13, 118 | "metadata": {}, 119 | "outputs": [ 120 | { 121 | "data": { 122 | "text/plain": [ 123 | "10" 124 | ] 125 | }, 126 | "execution_count": 13, 127 | "metadata": {}, 128 | "output_type": "execute_result" 129 | } 130 | ], 131 | "source": [ 132 | "sum_func(4321)" 133 | ] 134 | }, 135 | { 136 | "cell_type": "markdown", 137 | "metadata": {}, 138 | "source": [ 139 | "## 4 Reverse a String" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": 2, 145 | "metadata": {}, 146 | "outputs": [], 147 | "source": [ 148 | "def reverse(s):\n", 149 | " \n", 150 | " # Base Case\n", 151 | " if len(s) <= 1:\n", 152 | " return s\n", 153 | "\n", 154 | " # Recursion\n", 155 | " return reverse(s[1:]) + s[0]" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": 3, 161 | "metadata": {}, 162 | "outputs": [ 163 | { 164 | "data": { 165 | "text/plain": [ 166 | "'dlrow olleh'" 167 | ] 168 | }, 169 | "execution_count": 3, 170 | "metadata": {}, 171 | "output_type": "execute_result" 172 | } 173 | ], 174 | "source": [ 175 | "reverse('hello world')" 176 | ] 177 | }, 178 | { 179 | "cell_type": "markdown", 180 | "metadata": {}, 181 | "source": [ 182 | "## 5. Fibonnaci Sequence" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": 4, 188 | "metadata": {}, 189 | "outputs": [], 190 | "source": [ 191 | "def fib_rec(n):\n", 192 | " \n", 193 | " # Base Case\n", 194 | " if n == 0 or n == 1:\n", 195 | " return n\n", 196 | " \n", 197 | " # Recursion\n", 198 | " else:\n", 199 | " return fib_rec(n-1) + fib_rec(n-2)\n", 200 | " " 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": 5, 206 | "metadata": {}, 207 | "outputs": [ 208 | { 209 | "data": { 210 | "text/plain": [ 211 | "55" 212 | ] 213 | }, 214 | "execution_count": 5, 215 | "metadata": {}, 216 | "output_type": "execute_result" 217 | } 218 | ], 219 | "source": [ 220 | "fib_rec(10)" 221 | ] 222 | }, 223 | { 224 | "cell_type": "markdown", 225 | "metadata": {}, 226 | "source": [ 227 | "### 6. " 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": null, 233 | "metadata": {}, 234 | "outputs": [], 235 | "source": [] 236 | } 237 | ], 238 | "metadata": { 239 | "kernelspec": { 240 | "display_name": "Python 3", 241 | "language": "python", 242 | "name": "python3" 243 | }, 244 | "language_info": { 245 | "codemirror_mode": { 246 | "name": "ipython", 247 | "version": 3 248 | }, 249 | "file_extension": ".py", 250 | "mimetype": "text/x-python", 251 | "name": "python", 252 | "nbconvert_exporter": "python", 253 | "pygments_lexer": "ipython3", 254 | "version": "3.7.6" 255 | } 256 | }, 257 | "nbformat": 4, 258 | "nbformat_minor": 4 259 | } 260 | -------------------------------------------------------------------------------- /Code Workspace/Algorithms/6.2 Recursion.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## RECURSION" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "### 6. determine if it is possible to split the string in a way in which words can be made from the list of words." 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 6, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "def word_split(phrase,list_of_words, output = None):\n", 24 | " '''\n", 25 | " Note: This is a very \"python-y\" solution.\n", 26 | " ''' \n", 27 | " \n", 28 | " # Checks to see if any output has been initiated.\n", 29 | " # If you default output=[], it would be overwritten for every recursion!\n", 30 | " if output is None:\n", 31 | " output = []\n", 32 | " \n", 33 | " # For every word in list\n", 34 | " for word in list_of_words:\n", 35 | " \n", 36 | " # If the current phrase begins with the word, we have a split point!\n", 37 | " if phrase.startswith(word):\n", 38 | " \n", 39 | " # Add the word to the output\n", 40 | " output.append(word)\n", 41 | " \n", 42 | " # Recursively call the split function on the remaining portion of the phrase--- phrase[len(word):]\n", 43 | " # Remember to pass along the output and list of words\n", 44 | " return word_split(phrase[len(word):],list_of_words,output)\n", 45 | " \n", 46 | " # Finally return output if no phrase.startswith(word) returns True\n", 47 | " return output " 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 7, 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "data": { 57 | "text/plain": [ 58 | "['the', 'man', 'ran']" 59 | ] 60 | }, 61 | "execution_count": 7, 62 | "metadata": {}, 63 | "output_type": "execute_result" 64 | } 65 | ], 66 | "source": [ 67 | "word_split('themanran',['the','ran','man'])" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 8, 73 | "metadata": {}, 74 | "outputs": [ 75 | { 76 | "data": { 77 | "text/plain": [ 78 | "['i', 'love', 'dogs', 'John']" 79 | ] 80 | }, 81 | "execution_count": 8, 82 | "metadata": {}, 83 | "output_type": "execute_result" 84 | } 85 | ], 86 | "source": [ 87 | "word_split('ilovedogsJohn',['i','am','a','dogs','lover','love','John'])" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": 9, 93 | "metadata": {}, 94 | "outputs": [ 95 | { 96 | "data": { 97 | "text/plain": [ 98 | "[]" 99 | ] 100 | }, 101 | "execution_count": 9, 102 | "metadata": {}, 103 | "output_type": "execute_result" 104 | } 105 | ], 106 | "source": [ 107 | "word_split('themanran',['clown','ran','man'])" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": null, 113 | "metadata": {}, 114 | "outputs": [], 115 | "source": [] 116 | } 117 | ], 118 | "metadata": { 119 | "kernelspec": { 120 | "display_name": "Python 3", 121 | "language": "python", 122 | "name": "python3" 123 | }, 124 | "language_info": { 125 | "codemirror_mode": { 126 | "name": "ipython", 127 | "version": 3 128 | }, 129 | "file_extension": ".py", 130 | "mimetype": "text/x-python", 131 | "name": "python", 132 | "nbconvert_exporter": "python", 133 | "pygments_lexer": "ipython3", 134 | "version": "3.7.6" 135 | } 136 | }, 137 | "nbformat": 4, 138 | "nbformat_minor": 4 139 | } 140 | -------------------------------------------------------------------------------- /Code Workspace/Algorithms/Implementation of Shell Sort.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Implementation of Shell Sort\n", 8 | "\n", 9 | "The shell sort improves on the insertion sort by breaking the original list into a number of smaller sublists, each of which is sorted using an insertion sort. The unique way that these sublists are chosen is the key to the shell sort. Instead of breaking the list into sublists of contiguous items, the shell sort uses an increment i, sometimes called the gap, to create a sublist by choosing all items that are i items apart." 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Resources for Review\n", 17 | "\n", 18 | "Check out the resources below for a review of Shell sort!\n", 19 | "\n", 20 | "* [Wikipedia](https://en.wikipedia.org/wiki/Shellsort)\n", 21 | "* [Visual Algo](http://visualgo.net/sorting.html)\n", 22 | "* [Sorting Algorithms Animcation with Pseudocode](http://www.sorting-algorithms.com/shell-sort)" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 5, 28 | "metadata": { 29 | "collapsed": true 30 | }, 31 | "outputs": [], 32 | "source": [ 33 | "def shell_sort(arr):\n", 34 | " sublistcount = len(arr)/2\n", 35 | " \n", 36 | " # While we still have sub lists\n", 37 | " while sublistcount > 0:\n", 38 | " for start in range(sublistcount):\n", 39 | " # Use a gap insertion\n", 40 | " gap_insertion_sort(arr,start,sublistcount)\n", 41 | "\n", 42 | " \n", 43 | "\n", 44 | " sublistcount = sublistcount / 2\n", 45 | "\n", 46 | "def gap_insertion_sort(arr,start,gap):\n", 47 | " for i in range(start+gap,len(arr),gap):\n", 48 | "\n", 49 | " currentvalue = arr[i]\n", 50 | " position = i\n", 51 | "\n", 52 | " # Using the Gap\n", 53 | " while position>=gap and arr[position-gap]>currentvalue:\n", 54 | " arr[position]=arr[position-gap]\n", 55 | " position = position-gap\n", 56 | " \n", 57 | " # Set current value\n", 58 | " arr[position]=currentvalue" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 6, 64 | "metadata": { 65 | "collapsed": false 66 | }, 67 | "outputs": [ 68 | { 69 | "data": { 70 | "text/plain": [ 71 | "[2, 4, 6, 7, 21, 23, 24, 45, 45, 67, 90]" 72 | ] 73 | }, 74 | "execution_count": 6, 75 | "metadata": {}, 76 | "output_type": "execute_result" 77 | } 78 | ], 79 | "source": [ 80 | "arr = [45,67,23,45,21,24,7,2,6,4,90]\n", 81 | "shell_sort(arr)\n", 82 | "arr" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": {}, 88 | "source": [ 89 | "# Good Job!" 90 | ] 91 | } 92 | ], 93 | "metadata": { 94 | "kernelspec": { 95 | "display_name": "Python 2", 96 | "language": "python", 97 | "name": "python2" 98 | }, 99 | "language_info": { 100 | "codemirror_mode": { 101 | "name": "ipython", 102 | "version": 2 103 | }, 104 | "file_extension": ".py", 105 | "mimetype": "text/x-python", 106 | "name": "python", 107 | "nbconvert_exporter": "python", 108 | "pygments_lexer": "ipython2", 109 | "version": "2.7.11" 110 | } 111 | }, 112 | "nbformat": 4, 113 | "nbformat_minor": 0 114 | } 115 | -------------------------------------------------------------------------------- /Code Workspace/Data structure/.ipynb_checkpoints/2.1 Sequential Search-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Sequential Search" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 16, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "def seq_search(arr,ele):\n", 24 | " \"\"\"\n", 25 | " Sequential Search --> Unordered lists.\n", 26 | " \"\"\"\n", 27 | " for i in range(len(arr)):\n", 28 | " return arr[i]==ele\n", 29 | " return False" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 17, 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "arr = [1,9,2,8,3,4,7,5,6,10]" 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 | "True\n" 51 | ] 52 | } 53 | ], 54 | "source": [ 55 | "print(seq_search(arr,1))" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": 19, 61 | "metadata": {}, 62 | "outputs": [ 63 | { 64 | "name": "stdout", 65 | "output_type": "stream", 66 | "text": [ 67 | "False\n" 68 | ] 69 | } 70 | ], 71 | "source": [ 72 | "print(seq_search(arr,11))" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": null, 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 1, 85 | "metadata": {}, 86 | "outputs": [], 87 | "source": [ 88 | "def seq_search(arr,ele):\n", 89 | " \"\"\"\n", 90 | " Sequential Search --> ordered lists.\n", 91 | " \"\"\" \n", 92 | " for i in range(len(arr)):\n", 93 | " if arr[i]>k:\n", 94 | " return False\n", 95 | " if arr[i]==k :\n", 96 | " return True\n", 97 | " return False" 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.6" 125 | } 126 | }, 127 | "nbformat": 4, 128 | "nbformat_minor": 4 129 | } 130 | -------------------------------------------------------------------------------- /Code Workspace/Data structure/.ipynb_checkpoints/3.2 Stack Implementation via Linked list-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Stack Implementation via Linked list" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 36, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "#linked list\n", 17 | "\n", 18 | "class Node():\n", 19 | " def __init__(self,value):\n", 20 | " self.value=value\n", 21 | " self.next=None" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 100, 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "class Stack():\n", 31 | " '''implementing stack using linked list'''\n", 32 | "\n", 33 | " def __init__(self):\n", 34 | " self.head = None #not string,not integer. only placeholder\n", 35 | " self.tail = None\n", 36 | " self.length = 0\n", 37 | "\n", 38 | " def isEmpty(self):\n", 39 | " '''check for emplty stack'''\n", 40 | " #self.items == [] --> emplty stack\n", 41 | " return self.length==0 # --> len(stack)-zero\n", 42 | "\n", 43 | " def push(self,value):\n", 44 | " '''push value in stack'''\n", 45 | " #create a container node to hold value\n", 46 | " new_node = Node(value)\n", 47 | " #if its first node of list assign head and tail\n", 48 | " if self.length == 0:\n", 49 | " self.head = new_node\n", 50 | " self.tail = new_node\n", 51 | " self.length += 1\n", 52 | " return\n", 53 | "\n", 54 | " #add node to tail\n", 55 | " self.tail.next = new_node\n", 56 | " #modify tail to latest node\n", 57 | " self.tail = new_node\n", 58 | " #increment length\n", 59 | " self.length +=1\n", 60 | "\n", 61 | " def pop(self):\n", 62 | " '''pops value from stack and returns it''' \n", 63 | " temp_head = self.head\n", 64 | " for i in range(self.length-2):\n", 65 | " temp_head = temp_head.next\n", 66 | "\n", 67 | " val=temp_head.next.value\n", 68 | " self.tail=temp_head\n", 69 | " self.tail.next=None\n", 70 | " self.length -= 1\n", 71 | " return val\n", 72 | "\n", 73 | " def display(self):\n", 74 | " temp_head = self.head\n", 75 | " while(temp_head): #object at = True None = False\n", 76 | " print(temp_head.value)\n", 77 | " temp_head = temp_head.next\n", 78 | "\n", 79 | " def peek(self):\n", 80 | " return self.tail.value\n", 81 | "\n", 82 | "\n", 83 | " def size(self):\n", 84 | " '''return size of stack'''\n", 85 | " return self.len" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 101, 91 | "metadata": {}, 92 | "outputs": [], 93 | "source": [ 94 | "s = Stack()" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 102, 100 | "metadata": {}, 101 | "outputs": [ 102 | { 103 | "data": { 104 | "text/plain": [ 105 | "4" 106 | ] 107 | }, 108 | "execution_count": 102, 109 | "metadata": {}, 110 | "output_type": "execute_result" 111 | } 112 | ], 113 | "source": [ 114 | "s.push(1)\n", 115 | "s.push(2)\n", 116 | "s.push(111)\n", 117 | "s.push(4)\n", 118 | "s.push(5)\n", 119 | "s.pop()\n", 120 | "s.push(6)\n", 121 | "s.pop()\n", 122 | "s.pop()" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 104, 128 | "metadata": {}, 129 | "outputs": [ 130 | { 131 | "name": "stdout", 132 | "output_type": "stream", 133 | "text": [ 134 | "1\n", 135 | "2\n", 136 | "111\n" 137 | ] 138 | } 139 | ], 140 | "source": [ 141 | "s.display()" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 105, 147 | "metadata": {}, 148 | "outputs": [ 149 | { 150 | "data": { 151 | "text/plain": [ 152 | "111" 153 | ] 154 | }, 155 | "execution_count": 105, 156 | "metadata": {}, 157 | "output_type": "execute_result" 158 | } 159 | ], 160 | "source": [ 161 | "s.pop()" 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": 107, 167 | "metadata": {}, 168 | "outputs": [ 169 | { 170 | "name": "stdout", 171 | "output_type": "stream", 172 | "text": [ 173 | "1\n", 174 | "2\n" 175 | ] 176 | } 177 | ], 178 | "source": [ 179 | "s.display()" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": null, 185 | "metadata": {}, 186 | "outputs": [], 187 | "source": [] 188 | } 189 | ], 190 | "metadata": { 191 | "kernelspec": { 192 | "display_name": "Python 3", 193 | "language": "python", 194 | "name": "python3" 195 | }, 196 | "language_info": { 197 | "codemirror_mode": { 198 | "name": "ipython", 199 | "version": 3 200 | }, 201 | "file_extension": ".py", 202 | "mimetype": "text/x-python", 203 | "name": "python", 204 | "nbconvert_exporter": "python", 205 | "pygments_lexer": "ipython3", 206 | "version": "3.7.6" 207 | } 208 | }, 209 | "nbformat": 4, 210 | "nbformat_minor": 4 211 | } 212 | -------------------------------------------------------------------------------- /Code Workspace/Data structure/.ipynb_checkpoints/4.2 Deque-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Implementation of Deque\n", 8 | "\n", 9 | "In this lecture we will implement our own Deque class!\n", 10 | "\n", 11 | "## Methods and Attributes\n", 12 | "\n", 13 | "* Deque() creates a new deque that is empty. It needs no parameters and returns an empty deque.\n", 14 | "* addFront(item) adds a new item to the front of the deque. It needs the item and returns nothing.\n", 15 | "* addRear(item) adds a new item to the rear of the deque. It needs the item and returns nothing.\n", 16 | "* removeFront() removes the front item from the deque. It needs no parameters and returns the item. The deque is modified.\n", 17 | "* removeRear() removes the rear item from the deque. It needs no parameters and returns the item. The deque is modified.\n", 18 | "* isEmpty() tests to see whether the deque is empty. It needs no parameters and returns a boolean value.\n", 19 | "* size() returns the number of items in the deque. It needs no parameters and returns an integer.\n", 20 | "\n", 21 | "## Deque Implementation" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 1, 27 | "metadata": { 28 | "collapsed": true 29 | }, 30 | "outputs": [], 31 | "source": [ 32 | "class Deque:\n", 33 | " def __init__(self):\n", 34 | " self.items = []\n", 35 | "\n", 36 | " def isEmpty(self):\n", 37 | " return self.items == []\n", 38 | "\n", 39 | " def addFront(self, item):\n", 40 | " self.items.append(item)\n", 41 | "\n", 42 | " def addRear(self, item):\n", 43 | " self.items.insert(0,item)\n", 44 | "\n", 45 | " def removeFront(self):\n", 46 | " return self.items.pop()\n", 47 | "\n", 48 | " def removeRear(self):\n", 49 | " return self.items.pop(0)\n", 50 | "\n", 51 | " def size(self):\n", 52 | " return len(self.items)" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 10, 58 | "metadata": { 59 | "collapsed": true 60 | }, 61 | "outputs": [], 62 | "source": [ 63 | "d = Deque()" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 11, 69 | "metadata": { 70 | "collapsed": true 71 | }, 72 | "outputs": [], 73 | "source": [ 74 | "d.addFront('hello')" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 12, 80 | "metadata": { 81 | "collapsed": true 82 | }, 83 | "outputs": [], 84 | "source": [ 85 | "d.addRear('world')" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 13, 91 | "metadata": {}, 92 | "outputs": [ 93 | { 94 | "data": { 95 | "text/plain": [ 96 | "2" 97 | ] 98 | }, 99 | "execution_count": 13, 100 | "metadata": {}, 101 | "output_type": "execute_result" 102 | } 103 | ], 104 | "source": [ 105 | "d.size()" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": 14, 111 | "metadata": {}, 112 | "outputs": [ 113 | { 114 | "name": "stdout", 115 | "output_type": "stream", 116 | "text": [ 117 | "hello world\n" 118 | ] 119 | } 120 | ], 121 | "source": [ 122 | "print d.removeFront() + ' ' + d.removeRear()" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 15, 128 | "metadata": {}, 129 | "outputs": [ 130 | { 131 | "data": { 132 | "text/plain": [ 133 | "0" 134 | ] 135 | }, 136 | "execution_count": 15, 137 | "metadata": {}, 138 | "output_type": "execute_result" 139 | } 140 | ], 141 | "source": [ 142 | "d.size()" 143 | ] 144 | } 145 | ], 146 | "metadata": { 147 | "kernelspec": { 148 | "display_name": "Python 3", 149 | "language": "python", 150 | "name": "python3" 151 | }, 152 | "language_info": { 153 | "codemirror_mode": { 154 | "name": "ipython", 155 | "version": 3 156 | }, 157 | "file_extension": ".py", 158 | "mimetype": "text/x-python", 159 | "name": "python", 160 | "nbconvert_exporter": "python", 161 | "pygments_lexer": "ipython3", 162 | "version": "3.7.6" 163 | } 164 | }, 165 | "nbformat": 4, 166 | "nbformat_minor": 1 167 | } 168 | -------------------------------------------------------------------------------- /Code Workspace/Data structure/.ipynb_checkpoints/4.2 Queue Implementation via Linked list-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Queue Implementation via Linked list" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "#linked list\n", 17 | "\n", 18 | "class Node():\n", 19 | " def __init__(self,value):\n", 20 | " self.value=value\n", 21 | " self.next=None" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 3, 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "class Queue():\n", 31 | " '''implementing stack using linked list'''\n", 32 | " \n", 33 | " def __init__(self):\n", 34 | " self.head = None #not string,not integer. only placeholder\n", 35 | " self.tail = None\n", 36 | " self.length = 0\n", 37 | "\n", 38 | " def isEmpty(self):\n", 39 | " '''check for emplty stack'''\n", 40 | " #self.items == [] --> emplty stack\n", 41 | " return self.length==0 # --> len(stack)-zero\n", 42 | "\n", 43 | " def enqueue(self,value):\n", 44 | " '''push value in stack'''\n", 45 | " #create a container node to hold value\n", 46 | " new_node = Node(value)\n", 47 | " new_node.next = self.head\n", 48 | " self.head = new_node\n", 49 | " self.length += 1\n", 50 | "\n", 51 | " def dequeue(self):\n", 52 | " '''pops value from stack and returns it''' \n", 53 | " temp_head = self.head\n", 54 | " for i in range(self.length-2):\n", 55 | " temp_head = temp_head.next\n", 56 | "\n", 57 | " val=temp_head.next.value\n", 58 | " self.tail=temp_head\n", 59 | " self.tail.next=None\n", 60 | " self.length -= 1\n", 61 | " return val\n", 62 | "\n", 63 | " def display(self):\n", 64 | " temp_head = self.head\n", 65 | " while(temp_head): #object at = True None = False\n", 66 | " print(temp_head.value)\n", 67 | " temp_head = temp_head.next\n", 68 | "\n", 69 | " def peek(self):\n", 70 | " return self.tail.value\n", 71 | "\n", 72 | "\n", 73 | " def size(self):\n", 74 | " '''return size of stack'''\n", 75 | " return self.len" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": null, 81 | "metadata": {}, 82 | "outputs": [], 83 | "source": [] 84 | } 85 | ], 86 | "metadata": { 87 | "kernelspec": { 88 | "display_name": "Python 3", 89 | "language": "python", 90 | "name": "python3" 91 | }, 92 | "language_info": { 93 | "codemirror_mode": { 94 | "name": "ipython", 95 | "version": 3 96 | }, 97 | "file_extension": ".py", 98 | "mimetype": "text/x-python", 99 | "name": "python", 100 | "nbconvert_exporter": "python", 101 | "pygments_lexer": "ipython3", 102 | "version": "3.7.6" 103 | } 104 | }, 105 | "nbformat": 4, 106 | "nbformat_minor": 4 107 | } 108 | -------------------------------------------------------------------------------- /Code Workspace/Data structure/.ipynb_checkpoints/4.3 Queue using 2 stacks-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Implemantation of Queue using 2 stacks" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "class Queue2Stacks(object): \n", 17 | " def __init__(self):\n", 18 | " # Two Stacks\n", 19 | " self.instack = []\n", 20 | " self.outstack = [] \n", 21 | "\n", 22 | " def enqueue(self,element):\n", 23 | " # Add an enqueue with the \"IN\" stack\n", 24 | " self.instack.append(element)\n", 25 | "\n", 26 | " def dequeue(self):\n", 27 | " #if outstack is empty then fill it\n", 28 | " if len(self.outstack)==0:\n", 29 | " #pop from in and push in instack\n", 30 | " while len(self.instack):\n", 31 | " self.outstack.append(self.instack.pop())\n", 32 | " return self.outstack.pop()" 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.6" 60 | } 61 | }, 62 | "nbformat": 4, 63 | "nbformat_minor": 4 64 | } 65 | -------------------------------------------------------------------------------- /Code Workspace/Data structure/.ipynb_checkpoints/4.5 Priority Queue using heap-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Implementing Priority Queue using heap" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 13, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "import heapq" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 14, 22 | "metadata": {}, 23 | "outputs": [], 24 | "source": [ 25 | "q=[]" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 15, 31 | "metadata": {}, 32 | "outputs": [], 33 | "source": [ 34 | "#using heapq to push element in heap to implement priority queue\n", 35 | "#first mentioned is list we want to insert element into and then mention priority followed by data\n", 36 | "#one has highest property\n", 37 | "heapq.heappush(q, (1, 'one'))\n", 38 | "heapq.heappush(q, (2, 'two'))\n", 39 | "heapq.heappush(q, (5, 'five'))\n", 40 | "heapq.heappush(q, (4, 'four'))\n", 41 | "heapq.heappush(q, (3, 'three'))" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 16, 47 | "metadata": {}, 48 | "outputs": [ 49 | { 50 | "data": { 51 | "text/plain": [ 52 | "(1, 'one')" 53 | ] 54 | }, 55 | "execution_count": 16, 56 | "metadata": {}, 57 | "output_type": "execute_result" 58 | } 59 | ], 60 | "source": [ 61 | "#now if we pop, 'one' must pop out followed by 'two'\n", 62 | "heapq.heappop(q)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 17, 68 | "metadata": {}, 69 | "outputs": [ 70 | { 71 | "data": { 72 | "text/plain": [ 73 | "(2, 'two')" 74 | ] 75 | }, 76 | "execution_count": 17, 77 | "metadata": {}, 78 | "output_type": "execute_result" 79 | } 80 | ], 81 | "source": [ 82 | "heapq.heappop(q)" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 18, 88 | "metadata": {}, 89 | "outputs": [ 90 | { 91 | "data": { 92 | "text/plain": [ 93 | "(3, 'three')" 94 | ] 95 | }, 96 | "execution_count": 18, 97 | "metadata": {}, 98 | "output_type": "execute_result" 99 | } 100 | ], 101 | "source": [ 102 | "heapq.heappop(q)" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 19, 108 | "metadata": {}, 109 | "outputs": [ 110 | { 111 | "data": { 112 | "text/plain": [ 113 | "(4, 'four')" 114 | ] 115 | }, 116 | "execution_count": 19, 117 | "metadata": {}, 118 | "output_type": "execute_result" 119 | } 120 | ], 121 | "source": [ 122 | "heapq.heappop(q)" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 20, 128 | "metadata": {}, 129 | "outputs": [ 130 | { 131 | "data": { 132 | "text/plain": [ 133 | "(5, 'five')" 134 | ] 135 | }, 136 | "execution_count": 20, 137 | "metadata": {}, 138 | "output_type": "execute_result" 139 | } 140 | ], 141 | "source": [ 142 | "heapq.heappop(q)" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 21, 148 | "metadata": {}, 149 | "outputs": [ 150 | { 151 | "ename": "IndexError", 152 | "evalue": "index out of range", 153 | "output_type": "error", 154 | "traceback": [ 155 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 156 | "\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", 157 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m#now list is empty so if we try to pop, we should get an error\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mheapq\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mheappop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mq\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 158 | "\u001b[1;31mIndexError\u001b[0m: index out of range" 159 | ] 160 | } 161 | ], 162 | "source": [ 163 | "#now list is empty so if we try to pop, we should get an error\n", 164 | "heapq.heappop(q)" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": null, 170 | "metadata": {}, 171 | "outputs": [], 172 | "source": [] 173 | } 174 | ], 175 | "metadata": { 176 | "kernelspec": { 177 | "display_name": "Python 3", 178 | "language": "python", 179 | "name": "python3" 180 | }, 181 | "language_info": { 182 | "codemirror_mode": { 183 | "name": "ipython", 184 | "version": 3 185 | }, 186 | "file_extension": ".py", 187 | "mimetype": "text/x-python", 188 | "name": "python", 189 | "nbconvert_exporter": "python", 190 | "pygments_lexer": "ipython3", 191 | "version": "3.7.6" 192 | } 193 | }, 194 | "nbformat": 4, 195 | "nbformat_minor": 4 196 | } 197 | -------------------------------------------------------------------------------- /Code Workspace/Data structure/.ipynb_checkpoints/5.2 Circular Linked list-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Implementing Circular Linked list" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 3, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "#linked list\n", 17 | "\n", 18 | "class Node():\n", 19 | " def __init__(self,value):\n", 20 | " self.value=value\n", 21 | " self.next=None" 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.6" 49 | } 50 | }, 51 | "nbformat": 4, 52 | "nbformat_minor": 4 53 | } 54 | -------------------------------------------------------------------------------- /Code Workspace/Data structure/.ipynb_checkpoints/5.4 Circular Linked list-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Implementing Circular Linked list\n" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | " ### a) Singly Linked list" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 15, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "class Node():\n", 24 | " def __init__(self,value):\n", 25 | " self.value=value\n", 26 | " self.next=None" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 16, 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [ 35 | "node1=Node(10)\n", 36 | "node2=Node(20)\n", 37 | "node3=Node(30)\n", 38 | "\n", 39 | "node1.next=node2\n", 40 | "node2.next=node3\n", 41 | "node3.next=node1" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 17, 47 | "metadata": {}, 48 | "outputs": [ 49 | { 50 | "name": "stdout", 51 | "output_type": "stream", 52 | "text": [ 53 | "10\n", 54 | "30\n", 55 | "20\n" 56 | ] 57 | } 58 | ], 59 | "source": [ 60 | "print(node3.next.value)\n", 61 | "print(node2.next.value)\n", 62 | "print(node1.next.value)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "markdown", 67 | "metadata": {}, 68 | "source": [ 69 | "### b) Doubly Linked list\n" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 19, 75 | "metadata": {}, 76 | "outputs": [], 77 | "source": [ 78 | "class Node():\n", 79 | " def __init__(self,value):\n", 80 | " self.prev=None\n", 81 | " self.value=value\n", 82 | " self.next=None" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 20, 88 | "metadata": {}, 89 | "outputs": [], 90 | "source": [ 91 | "node1=Node(10)\n", 92 | "node2=Node(20)\n", 93 | "node3=Node(30)" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 21, 99 | "metadata": {}, 100 | "outputs": [], 101 | "source": [ 102 | "node1.next=node2\n", 103 | "node1.prev=node3\n", 104 | "\n", 105 | "node2.next=node3\n", 106 | "node2.prev=node1\n", 107 | "\n", 108 | "node3.next=node1\n", 109 | "node3.prev=node2" 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": 25, 115 | "metadata": {}, 116 | "outputs": [ 117 | { 118 | "name": "stdout", 119 | "output_type": "stream", 120 | "text": [ 121 | "10\n", 122 | "20\n", 123 | "30\n" 124 | ] 125 | } 126 | ], 127 | "source": [ 128 | "print(node3.next.value)\n", 129 | "print(node3.prev.value)\n", 130 | "print(node3.value)" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": null, 136 | "metadata": {}, 137 | "outputs": [], 138 | "source": [] 139 | } 140 | ], 141 | "metadata": { 142 | "kernelspec": { 143 | "display_name": "Python 3", 144 | "language": "python", 145 | "name": "python3" 146 | }, 147 | "language_info": { 148 | "codemirror_mode": { 149 | "name": "ipython", 150 | "version": 3 151 | }, 152 | "file_extension": ".py", 153 | "mimetype": "text/x-python", 154 | "name": "python", 155 | "nbconvert_exporter": "python", 156 | "pygments_lexer": "ipython3", 157 | "version": "3.7.6" 158 | } 159 | }, 160 | "nbformat": 4, 161 | "nbformat_minor": 4 162 | } 163 | -------------------------------------------------------------------------------- /Code Workspace/Data structure/.ipynb_checkpoints/6.2 Breadth first search in graph-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Breadth first search Implementation in graph using Adjancency List" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "### Unweighted graph" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 2, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "#method-1 --> hard-coding\n", 24 | "adj_list={\n", 25 | " 'A': ['B','D'],\n", 26 | " 'B': ['A','C'],\n", 27 | " 'C': ['B'],\n", 28 | " 'D': ['A','E','F'],\n", 29 | " 'E': ['D','F','G'],\n", 30 | " 'F': ['D','E','H'],\n", 31 | " 'G': ['E','H'],\n", 32 | " 'H': ['F','G']\n", 33 | "}" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 3, 39 | "metadata": {}, 40 | "outputs": [ 41 | { 42 | "name": "stdout", 43 | "output_type": "stream", 44 | "text": [ 45 | "['A', 'B', 'D', 'C', 'E', 'F', 'G', 'H']\n" 46 | ] 47 | } 48 | ], 49 | "source": [ 50 | "from queue import Queue\n", 51 | "\n", 52 | "#bfs code\n", 53 | "visited = {}\n", 54 | "level = {} #distance dict\n", 55 | "parent = {}\n", 56 | "bfs_traversal_output=[]\n", 57 | "queue = Queue()\n", 58 | "\n", 59 | "for node in adj_list.keys():\n", 60 | " #initialize all \n", 61 | " visited[node] = False\n", 62 | " level[node] = -1 #inf\n", 63 | " parent[node] = None\n", 64 | " \n", 65 | "#for root node\n", 66 | "s = 'A'\n", 67 | "visited['A'] = True\n", 68 | "level['A'] = 0\n", 69 | "#A has no parent\n", 70 | "queue.put(s)\n", 71 | "\n", 72 | "while not queue.empty():\n", 73 | " #push-put pop-get; pop from queue and store 1st element to bfs_traversal_output list\n", 74 | " u = queue.get()\n", 75 | " bfs_traversal_output.append(u)\n", 76 | " \n", 77 | " #now we will check if adj vertex of u is visted or not\n", 78 | " for v in adj_list[u]:\n", 79 | " #if visited do nothing\n", 80 | " if not visited[v]:\n", 81 | " visited[v] = True\n", 82 | " level[v] = level[u] + 1\n", 83 | " parent[v] = u\n", 84 | " queue.put(v)\n", 85 | " \n", 86 | "print(bfs_traversal_output)" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": {}, 92 | "source": [ 93 | "### Shortest distance of all nodes from source node" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 5, 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "name": "stdout", 103 | "output_type": "stream", 104 | "text": [ 105 | "{'A': 0, 'B': 1, 'C': 2, 'D': 1, 'E': 2, 'F': 2, 'G': 3, 'H': 3}\n", 106 | "3\n" 107 | ] 108 | } 109 | ], 110 | "source": [ 111 | "print(level)\n", 112 | "print(level['G'])" 113 | ] 114 | }, 115 | { 116 | "cell_type": "markdown", 117 | "metadata": {}, 118 | "source": [ 119 | "### Shortest path of any nodes from source node" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 6, 125 | "metadata": {}, 126 | "outputs": [ 127 | { 128 | "name": "stdout", 129 | "output_type": "stream", 130 | "text": [ 131 | "['A', 'D', 'E', 'G']\n" 132 | ] 133 | } 134 | ], 135 | "source": [ 136 | "#finding path A-G\n", 137 | "v='G'\n", 138 | "path=[]\n", 139 | "\n", 140 | "while v is not None:\n", 141 | " path.append(v)\n", 142 | " v = parent[v]\n", 143 | " \n", 144 | "path.reverse()\n", 145 | "print(path)" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": null, 151 | "metadata": {}, 152 | "outputs": [], 153 | "source": [] 154 | } 155 | ], 156 | "metadata": { 157 | "kernelspec": { 158 | "display_name": "Python 3", 159 | "language": "python", 160 | "name": "python3" 161 | }, 162 | "language_info": { 163 | "codemirror_mode": { 164 | "name": "ipython", 165 | "version": 3 166 | }, 167 | "file_extension": ".py", 168 | "mimetype": "text/x-python", 169 | "name": "python", 170 | "nbconvert_exporter": "python", 171 | "pygments_lexer": "ipython3", 172 | "version": "3.7.6" 173 | } 174 | }, 175 | "nbformat": 4, 176 | "nbformat_minor": 4 177 | } 178 | -------------------------------------------------------------------------------- /Code Workspace/Data structure/.ipynb_checkpoints/6.3 Bfs method 2-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "def bfs(graph, root):\n", 10 | " visited, queue = set(), [root]\n", 11 | " visited.add(root)\n", 12 | " while queue: \n", 13 | " vertex = queue.pop(0)\n", 14 | " print(vertex)\n", 15 | " for neighbour in graph[vertex]: \n", 16 | " if neighbour not in visited: \n", 17 | " visited.add(neighbour) \n", 18 | " queue.append(neighbour) " 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 7, 24 | "metadata": {}, 25 | "outputs": [ 26 | { 27 | "name": "stdout", 28 | "output_type": "stream", 29 | "text": [ 30 | "2\n", 31 | "3\n", 32 | "1\n", 33 | "0\n" 34 | ] 35 | } 36 | ], 37 | "source": [ 38 | "graph = {\n", 39 | " 0: [2], \n", 40 | " 1: [0,2], \n", 41 | " 2: [3], \n", 42 | " 3: [1,2]\n", 43 | " } \n", 44 | "bfs(graph, 2) # return 2 3 1 0" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 4, 50 | "metadata": {}, 51 | "outputs": [], 52 | "source": [ 53 | "#method-1 --> hard-coding\n", 54 | "adj_list={\n", 55 | " 'A': ['B','D'],\n", 56 | " 'B': ['A','C'],\n", 57 | " 'C': ['B'],\n", 58 | " 'D': ['A','E','F'],\n", 59 | " 'E': ['D','F','G'],\n", 60 | " 'F': ['D','E','H'],\n", 61 | " 'G': ['E','H'],\n", 62 | " 'H': ['F','G']\n", 63 | "}" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 5, 69 | "metadata": {}, 70 | "outputs": [ 71 | { 72 | "name": "stdout", 73 | "output_type": "stream", 74 | "text": [ 75 | "A\n", 76 | "B\n", 77 | "D\n", 78 | "C\n", 79 | "E\n", 80 | "F\n", 81 | "G\n", 82 | "H\n" 83 | ] 84 | } 85 | ], 86 | "source": [ 87 | "bfs(adj_list, 'A')" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": null, 93 | "metadata": {}, 94 | "outputs": [], 95 | "source": [] 96 | } 97 | ], 98 | "metadata": { 99 | "kernelspec": { 100 | "display_name": "Python 3", 101 | "language": "python", 102 | "name": "python3" 103 | }, 104 | "language_info": { 105 | "codemirror_mode": { 106 | "name": "ipython", 107 | "version": 3 108 | }, 109 | "file_extension": ".py", 110 | "mimetype": "text/x-python", 111 | "name": "python", 112 | "nbconvert_exporter": "python", 113 | "pygments_lexer": "ipython3", 114 | "version": "3.7.6" 115 | } 116 | }, 117 | "nbformat": 4, 118 | "nbformat_minor": 4 119 | } 120 | -------------------------------------------------------------------------------- /Code Workspace/Data structure/.ipynb_checkpoints/6.4 Depth first search in graph -checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Breadth first search Implementation in graph using Adjancency List" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "### Unweighted graph" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 4, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "def dfs(graph, start):\n", 24 | " visited, stack = set(), [start]\n", 25 | " while stack:\n", 26 | " vertex = stack.pop()\n", 27 | " if vertex not in visited:\n", 28 | " visited.add(vertex)\n", 29 | " stack.extend(graph[vertex] - visited)\n", 30 | " return visited" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 8, 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "data": { 40 | "text/plain": [ 41 | "{0, 1, 2, 3}" 42 | ] 43 | }, 44 | "execution_count": 8, 45 | "metadata": {}, 46 | "output_type": "execute_result" 47 | } 48 | ], 49 | "source": [ 50 | "graph = {\n", 51 | " 0: set([2]), \n", 52 | " 1: set([0,2]), \n", 53 | " 2: set([3]), \n", 54 | " 3: set([1,2])\n", 55 | " } \n", 56 | "dfs(graph, 1) " 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 10, 62 | "metadata": {}, 63 | "outputs": [ 64 | { 65 | "data": { 66 | "text/plain": [ 67 | "{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'}" 68 | ] 69 | }, 70 | "execution_count": 10, 71 | "metadata": {}, 72 | "output_type": "execute_result" 73 | } 74 | ], 75 | "source": [ 76 | "adj_list={\n", 77 | " 'A': set(['B','D']),\n", 78 | " 'B': set(['A','C']),\n", 79 | " 'C': set(['B']),\n", 80 | " 'D': set(['A','E','F']),\n", 81 | " 'E': set(['D','F','G']),\n", 82 | " 'F': set(['D','E','H']),\n", 83 | " 'G': set(['E','H']),\n", 84 | " 'H': set(['F','G'])\n", 85 | "}\n", 86 | "dfs(adj_list, 'A') " 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": null, 92 | "metadata": {}, 93 | "outputs": [], 94 | "source": [] 95 | } 96 | ], 97 | "metadata": { 98 | "kernelspec": { 99 | "display_name": "Python 3", 100 | "language": "python", 101 | "name": "python3" 102 | }, 103 | "language_info": { 104 | "codemirror_mode": { 105 | "name": "ipython", 106 | "version": 3 107 | }, 108 | "file_extension": ".py", 109 | "mimetype": "text/x-python", 110 | "name": "python", 111 | "nbconvert_exporter": "python", 112 | "pygments_lexer": "ipython3", 113 | "version": "3.7.6" 114 | } 115 | }, 116 | "nbformat": 4, 117 | "nbformat_minor": 4 118 | } 119 | -------------------------------------------------------------------------------- /Code Workspace/Data structure/.ipynb_checkpoints/6.5 Dfs method 2-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 8, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "def dfs(graph, start, visited=None):\n", 10 | " if visited is None:\n", 11 | " visited = set()\n", 12 | " visited.add(start)\n", 13 | " for i in graph[start]:\n", 14 | " if i not in visited:\n", 15 | " dfs(graph, i, visited)\n", 16 | " return visited" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 9, 22 | "metadata": {}, 23 | "outputs": [ 24 | { 25 | "data": { 26 | "text/plain": [ 27 | "{0, 1, 2, 3}" 28 | ] 29 | }, 30 | "execution_count": 9, 31 | "metadata": {}, 32 | "output_type": "execute_result" 33 | } 34 | ], 35 | "source": [ 36 | "graph = {\n", 37 | " 0: [2], \n", 38 | " 1: [0,2], \n", 39 | " 2: [3], \n", 40 | " 3: [1,2]\n", 41 | " } \n", 42 | "dfs(graph, 1) # return 2 3 1 0" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 12, 48 | "metadata": {}, 49 | "outputs": [ 50 | { 51 | "data": { 52 | "text/plain": [ 53 | "{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'}" 54 | ] 55 | }, 56 | "execution_count": 12, 57 | "metadata": {}, 58 | "output_type": "execute_result" 59 | } 60 | ], 61 | "source": [ 62 | "adj_list={\n", 63 | " 'A': ['B','D'],\n", 64 | " 'B': ['A','C'],\n", 65 | " 'C': ['B'],\n", 66 | " 'D': ['A','E','F'],\n", 67 | " 'E': ['D','F','G'],\n", 68 | " 'F': ['D','E','H'],\n", 69 | " 'G': ['E','H'],\n", 70 | " 'H': ['F','G']\n", 71 | "}\n", 72 | "dfs(adj_list, 'A')" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": null, 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [] 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.6" 107 | } 108 | }, 109 | "nbformat": 4, 110 | "nbformat_minor": 4 111 | } 112 | -------------------------------------------------------------------------------- /Code Workspace/Data structure/.ipynb_checkpoints/7.1 Trees-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Implementation of Binary Search Tree" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 69, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "class Node():\n", 17 | " def __init__(self,value):\n", 18 | " self.left = None #left edge\n", 19 | " self.value = value\n", 20 | " self.right = None #right edge" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 70, 26 | "metadata": {}, 27 | "outputs": [], 28 | "source": [ 29 | "class BinarySearchTree():\n", 30 | " def __init__(self):\n", 31 | " self.root = None\n", 32 | " \n", 33 | " def add(self,value):\n", 34 | " if self.root == None:\n", 35 | " self.root = Node(value)\n", 36 | " else:\n", 37 | " self.add_node(self.root, value)\n", 38 | " \n", 39 | " def add_node(self, node, value):\n", 40 | " if node.value < value:\n", 41 | " self.add_right(node, value)\n", 42 | " else:\n", 43 | " self.add_right(node, value)\n", 44 | " \n", 45 | " def add_right(self, node, value):\n", 46 | " if node.right == None:\n", 47 | " node.right = Node(value)\n", 48 | " else:\n", 49 | " self.add_node(node.right, value)\n", 50 | " \n", 51 | " def add_left(self, node, value):\n", 52 | " if node.left == None:\n", 53 | " node.left = Node(value)\n", 54 | " else:\n", 55 | " self.add_node(node.left, value)\n", 56 | " \n", 57 | " def lookup(self, value):\n", 58 | " #travel tree to find given element and return it if found else return None\n", 59 | " currentNode = self.root\n", 60 | " while True:\n", 61 | " if currentNode == None:\n", 62 | " return None\n", 63 | " elif value == currentNode.value:\n", 64 | " return currentNode.value\n", 65 | " elif value > currentNode.value:\n", 66 | " #move right\n", 67 | " currentNode = currentNode.right\n", 68 | " elif value < currentNode.value:\n", 69 | " #move left\n", 70 | " currentNode = currentNode.left" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 71, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [ 79 | "mytree = BinarySearchTree()" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 72, 85 | "metadata": {}, 86 | "outputs": [], 87 | "source": [ 88 | "mytree.add(3)\n", 89 | "mytree.add(4)\n", 90 | "mytree.add(0)\n", 91 | "mytree.add(8)\n", 92 | "mytree.add(2)" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 73, 98 | "metadata": {}, 99 | "outputs": [ 100 | { 101 | "data": { 102 | "text/plain": [ 103 | "4" 104 | ] 105 | }, 106 | "execution_count": 73, 107 | "metadata": {}, 108 | "output_type": "execute_result" 109 | } 110 | ], 111 | "source": [ 112 | "mytree.lookup(4)" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": 1, 118 | "metadata": {}, 119 | "outputs": [ 120 | { 121 | "ename": "NameError", 122 | "evalue": "name 'mytree' is not defined", 123 | "output_type": "error", 124 | "traceback": [ 125 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 126 | "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", 127 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mmytree\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 128 | "\u001b[1;31mNameError\u001b[0m: name 'mytree' is not defined" 129 | ] 130 | } 131 | ], 132 | "source": [ 133 | "mytree" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": null, 139 | "metadata": {}, 140 | "outputs": [], 141 | "source": [] 142 | } 143 | ], 144 | "metadata": { 145 | "kernelspec": { 146 | "display_name": "Python 3", 147 | "language": "python", 148 | "name": "python3" 149 | }, 150 | "language_info": { 151 | "codemirror_mode": { 152 | "name": "ipython", 153 | "version": 3 154 | }, 155 | "file_extension": ".py", 156 | "mimetype": "text/x-python", 157 | "name": "python", 158 | "nbconvert_exporter": "python", 159 | "pygments_lexer": "ipython3", 160 | "version": "3.7.6" 161 | } 162 | }, 163 | "nbformat": 4, 164 | "nbformat_minor": 4 165 | } 166 | -------------------------------------------------------------------------------- /Code Workspace/Data structure/.ipynb_checkpoints/7.2 Order Traversal Binary Tree-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Pre-Order Traversal (NLR)" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 4, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "def preorder_traversal(self,root):\n", 17 | " if root is None:\n", 18 | " return []\n", 19 | " \n", 20 | " return [root.value] + self.preorder_traversal(root.left) + self.preorder_traversal(root.right)" 21 | ] 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "metadata": {}, 26 | "source": [ 27 | "## In-Order Traversal (LNR)" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 5, 33 | "metadata": {}, 34 | "outputs": [], 35 | "source": [ 36 | "def inorder_traversal(self,root):\n", 37 | " if root is None:\n", 38 | " return []\n", 39 | " \n", 40 | " return self.inorder_traversal(root.left) + [root.value] + self.inorder_traversal(root.right)" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "## Post-Order Traversal (LRN)" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 6, 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [ 56 | "def postorder_traversal(self,root):\n", 57 | " if root is None:\n", 58 | " return []\n", 59 | " \n", 60 | " return self.postorder_traversal(root.left) + self.postorder_traversal(root.right) + [root.value] " 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": null, 66 | "metadata": {}, 67 | "outputs": [], 68 | "source": [] 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.6" 88 | } 89 | }, 90 | "nbformat": 4, 91 | "nbformat_minor": 4 92 | } 93 | -------------------------------------------------------------------------------- /Code Workspace/Data structure/.ipynb_checkpoints/7.3 Validate BST-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Validate BST" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 14, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "class Node():\n", 17 | " def __init__(self,data):\n", 18 | " self.left = None #left edge\n", 19 | " self.data = data\n", 20 | " self.right = None #right edge" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 15, 26 | "metadata": {}, 27 | "outputs": [ 28 | { 29 | "name": "stdout", 30 | "output_type": "stream", 31 | "text": [ 32 | "Is BST\n" 33 | ] 34 | } 35 | ], 36 | "source": [ 37 | "# Python program to check if a binary tree is bst or not \n", 38 | "INT_MAX = 4294967296\n", 39 | "INT_MIN = -4294967296\n", 40 | "\n", 41 | "\n", 42 | "# Returns true if the given tree is a binary search tree \n", 43 | "def isBST(node): \n", 44 | " return (isBSTUtil(node, INT_MIN, INT_MAX)) \n", 45 | "\n", 46 | "# Retusn true if the given tree is a BST and its values >= min and <= max \n", 47 | "def isBSTUtil(node, mini, maxi): \n", 48 | "\n", 49 | " # An empty tree is BST \n", 50 | " if node is None: \n", 51 | " return True\n", 52 | "\n", 53 | " # False if this node violates min/max constraint \n", 54 | " if node.data < mini or node.data > maxi: \n", 55 | " return False\n", 56 | "\n", 57 | " # Otherwise check the subtrees recursively \n", 58 | " # tightening the min or max constraint \n", 59 | " return ( isBSTUtil(node.left, mini, node.data -1) and isBSTUtil(node.right, node.data+1, maxi)) \n", 60 | "\n", 61 | "# Driver program to test above function \n", 62 | "root = Node(4) \n", 63 | "root.left = Node(2) \n", 64 | "root.right = Node(5) \n", 65 | "root.left.left = Node(1) \n", 66 | "root.left.right = Node(3) \n", 67 | "\n", 68 | "if (isBST(root)): \n", 69 | " print(\"Is BST\")\n", 70 | "else: \n", 71 | " print(\"Not a BST\")" 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.6" 99 | } 100 | }, 101 | "nbformat": 4, 102 | "nbformat_minor": 4 103 | } 104 | -------------------------------------------------------------------------------- /Code Workspace/Data structure/.ipynb_checkpoints/Balanced Parenthesis Check-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 4 6 | } 7 | -------------------------------------------------------------------------------- /Code Workspace/Data structure/.ipynb_checkpoints/Trim a Binary Search Tree - SOLUTION-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Trim a Binary Search Tree \n", 8 | "\n", 9 | "\n", 10 | "Given the root of a binary search tree and 2 numbers min and max, trim the tree such that all the numbers in the new tree are between min and max (inclusive). The resulting tree should still be a valid binary search tree.\n", 11 | "___\n", 12 | "and we’re given **min value as 5** and **max value as 13**: \n", 13 | "___\n", 14 | "We should remove all the nodes whose value is not between min and max. \n" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "def trimBST(tree, minVal, maxVal): \n", 24 | " \n", 25 | " if not tree: \n", 26 | " return \n", 27 | " \n", 28 | " tree.left=trimBST(tree.left, minVal, maxVal) \n", 29 | " tree.right=trimBST(tree.right, minVal, maxVal) \n", 30 | " \n", 31 | " if minVal<=tree.val<=maxVal: \n", 32 | " return tree \n", 33 | " \n", 34 | " if tree.valmaxVal: \n", 38 | " return tree.left " 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "The complexity of this algorithm is O(N), where N is the number of nodes in the tree. Because we basically perform a post-order traversal of the tree, visiting each and every node one. " 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": null, 51 | "metadata": {}, 52 | "outputs": [], 53 | "source": [] 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.6" 73 | } 74 | }, 75 | "nbformat": 4, 76 | "nbformat_minor": 1 77 | } 78 | -------------------------------------------------------------------------------- /Code Workspace/Data structure/3.2 Stack Implementation via Linked list.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Stack Implementation via Linked list" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 36, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "#linked list\n", 17 | "\n", 18 | "class Node():\n", 19 | " def __init__(self,value):\n", 20 | " self.value=value\n", 21 | " self.next=None" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 100, 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "class Stack():\n", 31 | " '''implementing stack using linked list'''\n", 32 | "\n", 33 | " def __init__(self):\n", 34 | " self.head = None #not string,not integer. only placeholder\n", 35 | " self.tail = None\n", 36 | " self.length = 0\n", 37 | "\n", 38 | " def isEmpty(self):\n", 39 | " '''check for emplty stack'''\n", 40 | " #self.items == [] --> emplty stack\n", 41 | " return self.length==0 # --> len(stack)-zero\n", 42 | "\n", 43 | " def push(self,value):\n", 44 | " '''push value in stack'''\n", 45 | " #create a container node to hold value\n", 46 | " new_node = Node(value)\n", 47 | " #if its first node of list assign head and tail\n", 48 | " if self.length == 0:\n", 49 | " self.head = new_node\n", 50 | " self.tail = new_node\n", 51 | " self.length += 1\n", 52 | " return\n", 53 | "\n", 54 | " #add node to tail\n", 55 | " self.tail.next = new_node\n", 56 | " #modify tail to latest node\n", 57 | " self.tail = new_node\n", 58 | " #increment length\n", 59 | " self.length +=1\n", 60 | "\n", 61 | " def pop(self):\n", 62 | " '''pops value from stack and returns it''' \n", 63 | " temp_head = self.head\n", 64 | " for i in range(self.length-2):\n", 65 | " temp_head = temp_head.next\n", 66 | "\n", 67 | " val=temp_head.next.value\n", 68 | " self.tail=temp_head\n", 69 | " self.tail.next=None\n", 70 | " self.length -= 1\n", 71 | " return val\n", 72 | "\n", 73 | " def display(self):\n", 74 | " temp_head = self.head\n", 75 | " while(temp_head): #object at = True None = False\n", 76 | " print(temp_head.value)\n", 77 | " temp_head = temp_head.next\n", 78 | "\n", 79 | " def peek(self):\n", 80 | " return self.tail.value\n", 81 | "\n", 82 | "\n", 83 | " def size(self):\n", 84 | " '''return size of stack'''\n", 85 | " return self.len" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 101, 91 | "metadata": {}, 92 | "outputs": [], 93 | "source": [ 94 | "s = Stack()" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 102, 100 | "metadata": {}, 101 | "outputs": [ 102 | { 103 | "data": { 104 | "text/plain": [ 105 | "4" 106 | ] 107 | }, 108 | "execution_count": 102, 109 | "metadata": {}, 110 | "output_type": "execute_result" 111 | } 112 | ], 113 | "source": [ 114 | "s.push(1)\n", 115 | "s.push(2)\n", 116 | "s.push(111)\n", 117 | "s.push(4)\n", 118 | "s.push(5)\n", 119 | "s.pop()\n", 120 | "s.push(6)\n", 121 | "s.pop()\n", 122 | "s.pop()" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 104, 128 | "metadata": {}, 129 | "outputs": [ 130 | { 131 | "name": "stdout", 132 | "output_type": "stream", 133 | "text": [ 134 | "1\n", 135 | "2\n", 136 | "111\n" 137 | ] 138 | } 139 | ], 140 | "source": [ 141 | "s.display()" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 105, 147 | "metadata": {}, 148 | "outputs": [ 149 | { 150 | "data": { 151 | "text/plain": [ 152 | "111" 153 | ] 154 | }, 155 | "execution_count": 105, 156 | "metadata": {}, 157 | "output_type": "execute_result" 158 | } 159 | ], 160 | "source": [ 161 | "s.pop()" 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": 107, 167 | "metadata": {}, 168 | "outputs": [ 169 | { 170 | "name": "stdout", 171 | "output_type": "stream", 172 | "text": [ 173 | "1\n", 174 | "2\n" 175 | ] 176 | } 177 | ], 178 | "source": [ 179 | "s.display()" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": null, 185 | "metadata": {}, 186 | "outputs": [], 187 | "source": [] 188 | } 189 | ], 190 | "metadata": { 191 | "kernelspec": { 192 | "display_name": "Python 3", 193 | "language": "python", 194 | "name": "python3" 195 | }, 196 | "language_info": { 197 | "codemirror_mode": { 198 | "name": "ipython", 199 | "version": 3 200 | }, 201 | "file_extension": ".py", 202 | "mimetype": "text/x-python", 203 | "name": "python", 204 | "nbconvert_exporter": "python", 205 | "pygments_lexer": "ipython3", 206 | "version": "3.7.6" 207 | } 208 | }, 209 | "nbformat": 4, 210 | "nbformat_minor": 4 211 | } 212 | -------------------------------------------------------------------------------- /Code Workspace/Data structure/4.1 Queue Implementation via List.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "collapsed": true 7 | }, 8 | "source": [ 9 | "____\n", 10 | "## Queue Methods and Attributes\n", 11 | "\n", 12 | "* Queue() creates a new queue that is empty. It needs no parameters and returns an empty queue.\n", 13 | "* enqueue(item) adds a new item to the rear of the queue. It needs the item and returns nothing.\n", 14 | "* dequeue() removes the front item from the queue. It needs no parameters and returns the item. The queue is modified.\n", 15 | "* isEmpty() tests to see whether the queue is empty. It needs no parameters and returns a boolean value.\n", 16 | "* size() returns the number of items in the queue. It needs no parameters and returns an integer." 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "____\n", 24 | "## Queue Implementation" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 1, 30 | "metadata": {}, 31 | "outputs": [], 32 | "source": [ 33 | "class Queue:\n", 34 | " '''implementing Queue using list'''\n", 35 | " \n", 36 | " def __init__(self):\n", 37 | " self.items = []\n", 38 | "\n", 39 | " def isEmpty(self):\n", 40 | " '''check for emplty Queue'''\n", 41 | " #self.items == [] --> emplty stack\n", 42 | " return len(self.items)==0 # --> len(stack)-zero\n", 43 | "\n", 44 | " def enqueue(self, item):\n", 45 | " '''push value in Queue'''\n", 46 | " self.items.insert(0,item)\n", 47 | "\n", 48 | " def dequeue(self):\n", 49 | " '''pops value from Queue and returns it'''\n", 50 | " #check length before popping\n", 51 | " if len(self.items) == 0:\n", 52 | " return None\n", 53 | " return self.items.pop()\n", 54 | " \n", 55 | " def peek(self):\n", 56 | " '''return top most value of Queue'''\n", 57 | " #check length before peeking\n", 58 | " if len(self.items) == 0:\n", 59 | " return None\n", 60 | " \n", 61 | " return self.items[len(self.items)-1]\n", 62 | "\n", 63 | " def size(self):\n", 64 | " '''return size of Queue'''\n", 65 | " return len(self.items)" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 2, 71 | "metadata": {}, 72 | "outputs": [], 73 | "source": [ 74 | "q = Queue()" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 3, 80 | "metadata": {}, 81 | "outputs": [ 82 | { 83 | "data": { 84 | "text/plain": [ 85 | "0" 86 | ] 87 | }, 88 | "execution_count": 3, 89 | "metadata": {}, 90 | "output_type": "execute_result" 91 | } 92 | ], 93 | "source": [ 94 | "q.size()" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 4, 100 | "metadata": {}, 101 | "outputs": [ 102 | { 103 | "data": { 104 | "text/plain": [ 105 | "True" 106 | ] 107 | }, 108 | "execution_count": 4, 109 | "metadata": {}, 110 | "output_type": "execute_result" 111 | } 112 | ], 113 | "source": [ 114 | "q.isEmpty()" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 5, 120 | "metadata": {}, 121 | "outputs": [], 122 | "source": [ 123 | "q.enqueue(1)" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 6, 129 | "metadata": {}, 130 | "outputs": [ 131 | { 132 | "data": { 133 | "text/plain": [ 134 | "1" 135 | ] 136 | }, 137 | "execution_count": 6, 138 | "metadata": {}, 139 | "output_type": "execute_result" 140 | } 141 | ], 142 | "source": [ 143 | "q.dequeue()" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": null, 149 | "metadata": {}, 150 | "outputs": [], 151 | "source": [] 152 | } 153 | ], 154 | "metadata": { 155 | "kernelspec": { 156 | "display_name": "Python 3", 157 | "language": "python", 158 | "name": "python3" 159 | }, 160 | "language_info": { 161 | "codemirror_mode": { 162 | "name": "ipython", 163 | "version": 3 164 | }, 165 | "file_extension": ".py", 166 | "mimetype": "text/x-python", 167 | "name": "python", 168 | "nbconvert_exporter": "python", 169 | "pygments_lexer": "ipython3", 170 | "version": "3.7.6" 171 | } 172 | }, 173 | "nbformat": 4, 174 | "nbformat_minor": 1 175 | } 176 | -------------------------------------------------------------------------------- /Code Workspace/Data structure/4.2 Queue Implementation via Linked list.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Queue Implementation via Linked list" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "#linked list\n", 17 | "\n", 18 | "class Node():\n", 19 | " def __init__(self,value):\n", 20 | " self.value=value\n", 21 | " self.next=None" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 3, 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "class Queue():\n", 31 | " '''implementing stack using linked list'''\n", 32 | " \n", 33 | " def __init__(self):\n", 34 | " self.head = None #not string,not integer. only placeholder\n", 35 | " self.tail = None\n", 36 | " self.length = 0\n", 37 | "\n", 38 | " def isEmpty(self):\n", 39 | " '''check for emplty stack'''\n", 40 | " #self.items == [] --> emplty stack\n", 41 | " return self.length==0 # --> len(stack)-zero\n", 42 | "\n", 43 | " def enqueue(self,value):\n", 44 | " '''push value in stack'''\n", 45 | " #create a container node to hold value\n", 46 | " new_node = Node(value)\n", 47 | " new_node.next = self.head\n", 48 | " self.head = new_node\n", 49 | " self.length += 1\n", 50 | "\n", 51 | " def dequeue(self):\n", 52 | " '''pops value from stack and returns it''' \n", 53 | " temp_head = self.head\n", 54 | " for i in range(self.length-2):\n", 55 | " temp_head = temp_head.next\n", 56 | "\n", 57 | " val=temp_head.next.value\n", 58 | " self.tail=temp_head\n", 59 | " self.tail.next=None\n", 60 | " self.length -= 1\n", 61 | " return val\n", 62 | "\n", 63 | " def display(self):\n", 64 | " temp_head = self.head\n", 65 | " while(temp_head): #object at = True None = False\n", 66 | " print(temp_head.value)\n", 67 | " temp_head = temp_head.next\n", 68 | "\n", 69 | " def peek(self):\n", 70 | " return self.tail.value\n", 71 | "\n", 72 | "\n", 73 | " def size(self):\n", 74 | " '''return size of stack'''\n", 75 | " return self.len" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": null, 81 | "metadata": {}, 82 | "outputs": [], 83 | "source": [] 84 | } 85 | ], 86 | "metadata": { 87 | "kernelspec": { 88 | "display_name": "Python 3", 89 | "language": "python", 90 | "name": "python3" 91 | }, 92 | "language_info": { 93 | "codemirror_mode": { 94 | "name": "ipython", 95 | "version": 3 96 | }, 97 | "file_extension": ".py", 98 | "mimetype": "text/x-python", 99 | "name": "python", 100 | "nbconvert_exporter": "python", 101 | "pygments_lexer": "ipython3", 102 | "version": "3.7.6" 103 | } 104 | }, 105 | "nbformat": 4, 106 | "nbformat_minor": 4 107 | } 108 | -------------------------------------------------------------------------------- /Code Workspace/Data structure/4.3 Queue using 2 stacks.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Implemantation of Queue using 2 stacks" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "class Queue2Stacks(object): \n", 17 | " def __init__(self):\n", 18 | " # Two Stacks\n", 19 | " self.instack = []\n", 20 | " self.outstack = [] \n", 21 | "\n", 22 | " def enqueue(self,element):\n", 23 | " # Add an enqueue with the \"IN\" stack\n", 24 | " self.instack.append(element)\n", 25 | "\n", 26 | " def dequeue(self):\n", 27 | " #if outstack is empty then fill it\n", 28 | " if len(self.outstack)==0:\n", 29 | " #pop from in and push in instack\n", 30 | " while len(self.instack):\n", 31 | " self.outstack.append(self.instack.pop())\n", 32 | " return self.outstack.pop()" 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.6" 60 | } 61 | }, 62 | "nbformat": 4, 63 | "nbformat_minor": 4 64 | } 65 | -------------------------------------------------------------------------------- /Code Workspace/Data structure/4.4 Dequeue Implementation via List.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Implementation of Deque\n", 8 | "\n", 9 | "## Methods and Attributes\n", 10 | "\n", 11 | "* Deque() creates a new deque that is empty. It needs no parameters and returns an empty deque.\n", 12 | "* addFront(item) adds a new item to the front of the deque. It needs the item and returns nothing.\n", 13 | "* addRear(item) adds a new item to the rear of the deque. It needs the item and returns nothing.\n", 14 | "* removeFront() removes the front item from the deque. It needs no parameters and returns the item. The deque is modified.\n", 15 | "* removeRear() removes the rear item from the deque. It needs no parameters and returns the item. The deque is modified.\n", 16 | "* isEmpty() tests to see whether the deque is empty. It needs no parameters and returns a boolean value.\n", 17 | "* size() returns the number of items in the deque. It needs no parameters and returns an integer.\n", 18 | "\n", 19 | "## Deque Implementation" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 1, 25 | "metadata": { 26 | "collapsed": true 27 | }, 28 | "outputs": [], 29 | "source": [ 30 | "class Deque:\n", 31 | " def __init__(self):\n", 32 | " self.items = []\n", 33 | "\n", 34 | " def isEmpty(self):\n", 35 | " return self.items == []\n", 36 | "\n", 37 | " def addFront(self, item):\n", 38 | " self.items.append(item)\n", 39 | "\n", 40 | " def addRear(self, item):\n", 41 | " self.items.insert(0,item)\n", 42 | "\n", 43 | " def removeFront(self):\n", 44 | " return self.items.pop()\n", 45 | "\n", 46 | " def removeRear(self):\n", 47 | " return self.items.pop(0)\n", 48 | "\n", 49 | " def size(self):\n", 50 | " return len(self.items)" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 10, 56 | "metadata": { 57 | "collapsed": true 58 | }, 59 | "outputs": [], 60 | "source": [ 61 | "d = Deque()" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 11, 67 | "metadata": { 68 | "collapsed": true 69 | }, 70 | "outputs": [], 71 | "source": [ 72 | "d.addFront('hello')" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 12, 78 | "metadata": { 79 | "collapsed": true 80 | }, 81 | "outputs": [], 82 | "source": [ 83 | "d.addRear('world')" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 13, 89 | "metadata": {}, 90 | "outputs": [ 91 | { 92 | "data": { 93 | "text/plain": [ 94 | "2" 95 | ] 96 | }, 97 | "execution_count": 13, 98 | "metadata": {}, 99 | "output_type": "execute_result" 100 | } 101 | ], 102 | "source": [ 103 | "d.size()" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 14, 109 | "metadata": {}, 110 | "outputs": [ 111 | { 112 | "name": "stdout", 113 | "output_type": "stream", 114 | "text": [ 115 | "hello world\n" 116 | ] 117 | } 118 | ], 119 | "source": [ 120 | "print d.removeFront() + ' ' + d.removeRear()" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 15, 126 | "metadata": {}, 127 | "outputs": [ 128 | { 129 | "data": { 130 | "text/plain": [ 131 | "0" 132 | ] 133 | }, 134 | "execution_count": 15, 135 | "metadata": {}, 136 | "output_type": "execute_result" 137 | } 138 | ], 139 | "source": [ 140 | "d.size()" 141 | ] 142 | } 143 | ], 144 | "metadata": { 145 | "kernelspec": { 146 | "display_name": "Python 3", 147 | "language": "python", 148 | "name": "python3" 149 | }, 150 | "language_info": { 151 | "codemirror_mode": { 152 | "name": "ipython", 153 | "version": 3 154 | }, 155 | "file_extension": ".py", 156 | "mimetype": "text/x-python", 157 | "name": "python", 158 | "nbconvert_exporter": "python", 159 | "pygments_lexer": "ipython3", 160 | "version": "3.7.6" 161 | } 162 | }, 163 | "nbformat": 4, 164 | "nbformat_minor": 1 165 | } 166 | -------------------------------------------------------------------------------- /Code Workspace/Data structure/4.5 Priority Queue using heap.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Implementing Priority Queue using heap" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 13, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "import heapq" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 14, 22 | "metadata": {}, 23 | "outputs": [], 24 | "source": [ 25 | "q=[]" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 15, 31 | "metadata": {}, 32 | "outputs": [], 33 | "source": [ 34 | "#using heapq to push element in heap to implement priority queue\n", 35 | "#first mentioned is list we want to insert element into and then mention priority followed by data\n", 36 | "#one has highest property\n", 37 | "heapq.heappush(q, (1, 'one'))\n", 38 | "heapq.heappush(q, (2, 'two'))\n", 39 | "heapq.heappush(q, (5, 'five'))\n", 40 | "heapq.heappush(q, (4, 'four'))\n", 41 | "heapq.heappush(q, (3, 'three'))" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 16, 47 | "metadata": {}, 48 | "outputs": [ 49 | { 50 | "data": { 51 | "text/plain": [ 52 | "(1, 'one')" 53 | ] 54 | }, 55 | "execution_count": 16, 56 | "metadata": {}, 57 | "output_type": "execute_result" 58 | } 59 | ], 60 | "source": [ 61 | "#now if we pop, 'one' must pop out followed by 'two'\n", 62 | "heapq.heappop(q)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 17, 68 | "metadata": {}, 69 | "outputs": [ 70 | { 71 | "data": { 72 | "text/plain": [ 73 | "(2, 'two')" 74 | ] 75 | }, 76 | "execution_count": 17, 77 | "metadata": {}, 78 | "output_type": "execute_result" 79 | } 80 | ], 81 | "source": [ 82 | "heapq.heappop(q)" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 18, 88 | "metadata": {}, 89 | "outputs": [ 90 | { 91 | "data": { 92 | "text/plain": [ 93 | "(3, 'three')" 94 | ] 95 | }, 96 | "execution_count": 18, 97 | "metadata": {}, 98 | "output_type": "execute_result" 99 | } 100 | ], 101 | "source": [ 102 | "heapq.heappop(q)" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 19, 108 | "metadata": {}, 109 | "outputs": [ 110 | { 111 | "data": { 112 | "text/plain": [ 113 | "(4, 'four')" 114 | ] 115 | }, 116 | "execution_count": 19, 117 | "metadata": {}, 118 | "output_type": "execute_result" 119 | } 120 | ], 121 | "source": [ 122 | "heapq.heappop(q)" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 20, 128 | "metadata": {}, 129 | "outputs": [ 130 | { 131 | "data": { 132 | "text/plain": [ 133 | "(5, 'five')" 134 | ] 135 | }, 136 | "execution_count": 20, 137 | "metadata": {}, 138 | "output_type": "execute_result" 139 | } 140 | ], 141 | "source": [ 142 | "heapq.heappop(q)" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 21, 148 | "metadata": {}, 149 | "outputs": [ 150 | { 151 | "ename": "IndexError", 152 | "evalue": "index out of range", 153 | "output_type": "error", 154 | "traceback": [ 155 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 156 | "\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", 157 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m#now list is empty so if we try to pop, we should get an error\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mheapq\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mheappop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mq\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 158 | "\u001b[1;31mIndexError\u001b[0m: index out of range" 159 | ] 160 | } 161 | ], 162 | "source": [ 163 | "#now list is empty so if we try to pop, we should get an error\n", 164 | "heapq.heappop(q)" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": null, 170 | "metadata": {}, 171 | "outputs": [], 172 | "source": [] 173 | } 174 | ], 175 | "metadata": { 176 | "kernelspec": { 177 | "display_name": "Python 3", 178 | "language": "python", 179 | "name": "python3" 180 | }, 181 | "language_info": { 182 | "codemirror_mode": { 183 | "name": "ipython", 184 | "version": 3 185 | }, 186 | "file_extension": ".py", 187 | "mimetype": "text/x-python", 188 | "name": "python", 189 | "nbconvert_exporter": "python", 190 | "pygments_lexer": "ipython3", 191 | "version": "3.7.6" 192 | } 193 | }, 194 | "nbformat": 4, 195 | "nbformat_minor": 4 196 | } 197 | -------------------------------------------------------------------------------- /Code Workspace/Data structure/5.4 Circular Linked list.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Implementing Circular Linked list\n" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | " ### a) Singly Linked list" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 15, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "class Node():\n", 24 | " def __init__(self,value):\n", 25 | " self.value=value\n", 26 | " self.next=None" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 16, 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [ 35 | "node1=Node(10)\n", 36 | "node2=Node(20)\n", 37 | "node3=Node(30)\n", 38 | "\n", 39 | "node1.next=node2\n", 40 | "node2.next=node3\n", 41 | "node3.next=node1" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 17, 47 | "metadata": {}, 48 | "outputs": [ 49 | { 50 | "name": "stdout", 51 | "output_type": "stream", 52 | "text": [ 53 | "10\n", 54 | "30\n", 55 | "20\n" 56 | ] 57 | } 58 | ], 59 | "source": [ 60 | "print(node3.next.value)\n", 61 | "print(node2.next.value)\n", 62 | "print(node1.next.value)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "markdown", 67 | "metadata": {}, 68 | "source": [ 69 | "### b) Doubly Linked list\n" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 19, 75 | "metadata": {}, 76 | "outputs": [], 77 | "source": [ 78 | "class Node():\n", 79 | " def __init__(self,value):\n", 80 | " self.prev=None\n", 81 | " self.value=value\n", 82 | " self.next=None" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 20, 88 | "metadata": {}, 89 | "outputs": [], 90 | "source": [ 91 | "node1=Node(10)\n", 92 | "node2=Node(20)\n", 93 | "node3=Node(30)" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 21, 99 | "metadata": {}, 100 | "outputs": [], 101 | "source": [ 102 | "node1.next=node2\n", 103 | "node1.prev=node3\n", 104 | "\n", 105 | "node2.next=node3\n", 106 | "node2.prev=node1\n", 107 | "\n", 108 | "node3.next=node1\n", 109 | "node3.prev=node2" 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": 25, 115 | "metadata": {}, 116 | "outputs": [ 117 | { 118 | "name": "stdout", 119 | "output_type": "stream", 120 | "text": [ 121 | "10\n", 122 | "20\n", 123 | "30\n" 124 | ] 125 | } 126 | ], 127 | "source": [ 128 | "print(node3.next.value)\n", 129 | "print(node3.prev.value)\n", 130 | "print(node3.value)" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": null, 136 | "metadata": {}, 137 | "outputs": [], 138 | "source": [] 139 | } 140 | ], 141 | "metadata": { 142 | "kernelspec": { 143 | "display_name": "Python 3", 144 | "language": "python", 145 | "name": "python3" 146 | }, 147 | "language_info": { 148 | "codemirror_mode": { 149 | "name": "ipython", 150 | "version": 3 151 | }, 152 | "file_extension": ".py", 153 | "mimetype": "text/x-python", 154 | "name": "python", 155 | "nbconvert_exporter": "python", 156 | "pygments_lexer": "ipython3", 157 | "version": "3.7.6" 158 | } 159 | }, 160 | "nbformat": 4, 161 | "nbformat_minor": 4 162 | } 163 | -------------------------------------------------------------------------------- /Code Workspace/Data structure/6.2 Breadth first search in graph.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Breadth first search Implementation in graph using Adjancency List" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "### Unweighted graph" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 2, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "#method-1 --> hard-coding\n", 24 | "adj_list={\n", 25 | " 'A': ['B','D'],\n", 26 | " 'B': ['A','C'],\n", 27 | " 'C': ['B'],\n", 28 | " 'D': ['A','E','F'],\n", 29 | " 'E': ['D','F','G'],\n", 30 | " 'F': ['D','E','H'],\n", 31 | " 'G': ['E','H'],\n", 32 | " 'H': ['F','G']\n", 33 | "}" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 3, 39 | "metadata": {}, 40 | "outputs": [ 41 | { 42 | "name": "stdout", 43 | "output_type": "stream", 44 | "text": [ 45 | "['A', 'B', 'D', 'C', 'E', 'F', 'G', 'H']\n" 46 | ] 47 | } 48 | ], 49 | "source": [ 50 | "from queue import Queue\n", 51 | "\n", 52 | "#bfs code\n", 53 | "visited = {}\n", 54 | "level = {} #distance dict\n", 55 | "parent = {}\n", 56 | "bfs_traversal_output=[]\n", 57 | "queue = Queue()\n", 58 | "\n", 59 | "for node in adj_list.keys():\n", 60 | " #initialize all \n", 61 | " visited[node] = False\n", 62 | " level[node] = -1 #inf\n", 63 | " parent[node] = None\n", 64 | " \n", 65 | "#for root node\n", 66 | "s = 'A'\n", 67 | "visited['A'] = True\n", 68 | "level['A'] = 0\n", 69 | "#A has no parent\n", 70 | "queue.put(s)\n", 71 | "\n", 72 | "while not queue.empty():\n", 73 | " #push-put pop-get; pop from queue and store 1st element to bfs_traversal_output list\n", 74 | " u = queue.get()\n", 75 | " bfs_traversal_output.append(u)\n", 76 | " \n", 77 | " #now we will check if adj vertex of u is visted or not\n", 78 | " for v in adj_list[u]:\n", 79 | " #if visited do nothing\n", 80 | " if not visited[v]:\n", 81 | " visited[v] = True\n", 82 | " level[v] = level[u] + 1\n", 83 | " parent[v] = u\n", 84 | " queue.put(v)\n", 85 | " \n", 86 | "print(bfs_traversal_output)" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": {}, 92 | "source": [ 93 | "### Shortest distance of all nodes from source node" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 5, 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "name": "stdout", 103 | "output_type": "stream", 104 | "text": [ 105 | "{'A': 0, 'B': 1, 'C': 2, 'D': 1, 'E': 2, 'F': 2, 'G': 3, 'H': 3}\n", 106 | "3\n" 107 | ] 108 | } 109 | ], 110 | "source": [ 111 | "print(level)\n", 112 | "print(level['G'])" 113 | ] 114 | }, 115 | { 116 | "cell_type": "markdown", 117 | "metadata": {}, 118 | "source": [ 119 | "### Shortest path of any nodes from source node" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 6, 125 | "metadata": {}, 126 | "outputs": [ 127 | { 128 | "name": "stdout", 129 | "output_type": "stream", 130 | "text": [ 131 | "['A', 'D', 'E', 'G']\n" 132 | ] 133 | } 134 | ], 135 | "source": [ 136 | "#finding path A-G\n", 137 | "v='G'\n", 138 | "path=[]\n", 139 | "\n", 140 | "while v is not None:\n", 141 | " path.append(v)\n", 142 | " v = parent[v]\n", 143 | " \n", 144 | "path.reverse()\n", 145 | "print(path)" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": null, 151 | "metadata": {}, 152 | "outputs": [], 153 | "source": [] 154 | } 155 | ], 156 | "metadata": { 157 | "kernelspec": { 158 | "display_name": "Python 3", 159 | "language": "python", 160 | "name": "python3" 161 | }, 162 | "language_info": { 163 | "codemirror_mode": { 164 | "name": "ipython", 165 | "version": 3 166 | }, 167 | "file_extension": ".py", 168 | "mimetype": "text/x-python", 169 | "name": "python", 170 | "nbconvert_exporter": "python", 171 | "pygments_lexer": "ipython3", 172 | "version": "3.7.6" 173 | } 174 | }, 175 | "nbformat": 4, 176 | "nbformat_minor": 4 177 | } 178 | -------------------------------------------------------------------------------- /Code Workspace/Data structure/6.3 Bfs method 2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "def bfs(graph, root):\n", 10 | " visited, queue = set(), [root]\n", 11 | " visited.add(root)\n", 12 | " while queue: \n", 13 | " vertex = queue.pop(0)\n", 14 | " print(vertex)\n", 15 | " for neighbour in graph[vertex]: \n", 16 | " if neighbour not in visited: \n", 17 | " visited.add(neighbour) \n", 18 | " queue.append(neighbour) " 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 7, 24 | "metadata": {}, 25 | "outputs": [ 26 | { 27 | "name": "stdout", 28 | "output_type": "stream", 29 | "text": [ 30 | "2\n", 31 | "3\n", 32 | "1\n", 33 | "0\n" 34 | ] 35 | } 36 | ], 37 | "source": [ 38 | "graph = {\n", 39 | " 0: [2], \n", 40 | " 1: [0,2], \n", 41 | " 2: [3], \n", 42 | " 3: [1,2]\n", 43 | " } \n", 44 | "bfs(graph, 2) # return 2 3 1 0" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 4, 50 | "metadata": {}, 51 | "outputs": [], 52 | "source": [ 53 | "#method-1 --> hard-coding\n", 54 | "adj_list={\n", 55 | " 'A': ['B','D'],\n", 56 | " 'B': ['A','C'],\n", 57 | " 'C': ['B'],\n", 58 | " 'D': ['A','E','F'],\n", 59 | " 'E': ['D','F','G'],\n", 60 | " 'F': ['D','E','H'],\n", 61 | " 'G': ['E','H'],\n", 62 | " 'H': ['F','G']\n", 63 | "}" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 5, 69 | "metadata": {}, 70 | "outputs": [ 71 | { 72 | "name": "stdout", 73 | "output_type": "stream", 74 | "text": [ 75 | "A\n", 76 | "B\n", 77 | "D\n", 78 | "C\n", 79 | "E\n", 80 | "F\n", 81 | "G\n", 82 | "H\n" 83 | ] 84 | } 85 | ], 86 | "source": [ 87 | "bfs(adj_list, 'A')" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": null, 93 | "metadata": {}, 94 | "outputs": [], 95 | "source": [] 96 | } 97 | ], 98 | "metadata": { 99 | "kernelspec": { 100 | "display_name": "Python 3", 101 | "language": "python", 102 | "name": "python3" 103 | }, 104 | "language_info": { 105 | "codemirror_mode": { 106 | "name": "ipython", 107 | "version": 3 108 | }, 109 | "file_extension": ".py", 110 | "mimetype": "text/x-python", 111 | "name": "python", 112 | "nbconvert_exporter": "python", 113 | "pygments_lexer": "ipython3", 114 | "version": "3.7.6" 115 | } 116 | }, 117 | "nbformat": 4, 118 | "nbformat_minor": 4 119 | } 120 | -------------------------------------------------------------------------------- /Code Workspace/Data structure/6.4 Depth first search in graph .ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Breadth first search Implementation in graph using Adjancency List" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "### Unweighted graph" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 4, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "def dfs(graph, start):\n", 24 | " visited, stack = set(), [start]\n", 25 | " while stack:\n", 26 | " vertex = stack.pop()\n", 27 | " if vertex not in visited:\n", 28 | " visited.add(vertex)\n", 29 | " stack.extend(graph[vertex] - visited)\n", 30 | " return visited" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 8, 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "data": { 40 | "text/plain": [ 41 | "{0, 1, 2, 3}" 42 | ] 43 | }, 44 | "execution_count": 8, 45 | "metadata": {}, 46 | "output_type": "execute_result" 47 | } 48 | ], 49 | "source": [ 50 | "graph = {\n", 51 | " 0: set([2]), \n", 52 | " 1: set([0,2]), \n", 53 | " 2: set([3]), \n", 54 | " 3: set([1,2])\n", 55 | " } \n", 56 | "dfs(graph, 1) " 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 10, 62 | "metadata": {}, 63 | "outputs": [ 64 | { 65 | "data": { 66 | "text/plain": [ 67 | "{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'}" 68 | ] 69 | }, 70 | "execution_count": 10, 71 | "metadata": {}, 72 | "output_type": "execute_result" 73 | } 74 | ], 75 | "source": [ 76 | "adj_list={\n", 77 | " 'A': set(['B','D']),\n", 78 | " 'B': set(['A','C']),\n", 79 | " 'C': set(['B']),\n", 80 | " 'D': set(['A','E','F']),\n", 81 | " 'E': set(['D','F','G']),\n", 82 | " 'F': set(['D','E','H']),\n", 83 | " 'G': set(['E','H']),\n", 84 | " 'H': set(['F','G'])\n", 85 | "}\n", 86 | "dfs(adj_list, 'A') " 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": null, 92 | "metadata": {}, 93 | "outputs": [], 94 | "source": [] 95 | } 96 | ], 97 | "metadata": { 98 | "kernelspec": { 99 | "display_name": "Python 3", 100 | "language": "python", 101 | "name": "python3" 102 | }, 103 | "language_info": { 104 | "codemirror_mode": { 105 | "name": "ipython", 106 | "version": 3 107 | }, 108 | "file_extension": ".py", 109 | "mimetype": "text/x-python", 110 | "name": "python", 111 | "nbconvert_exporter": "python", 112 | "pygments_lexer": "ipython3", 113 | "version": "3.7.6" 114 | } 115 | }, 116 | "nbformat": 4, 117 | "nbformat_minor": 4 118 | } 119 | -------------------------------------------------------------------------------- /Code Workspace/Data structure/6.5 Dfs method 2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 8, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "def dfs(graph, start, visited=None):\n", 10 | " if visited is None:\n", 11 | " visited = set()\n", 12 | " visited.add(start)\n", 13 | " for i in graph[start]:\n", 14 | " if i not in visited:\n", 15 | " dfs(graph, i, visited)\n", 16 | " return visited" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 9, 22 | "metadata": {}, 23 | "outputs": [ 24 | { 25 | "data": { 26 | "text/plain": [ 27 | "{0, 1, 2, 3}" 28 | ] 29 | }, 30 | "execution_count": 9, 31 | "metadata": {}, 32 | "output_type": "execute_result" 33 | } 34 | ], 35 | "source": [ 36 | "graph = {\n", 37 | " 0: [2], \n", 38 | " 1: [0,2], \n", 39 | " 2: [3], \n", 40 | " 3: [1,2]\n", 41 | " } \n", 42 | "dfs(graph, 1) # return 2 3 1 0" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 12, 48 | "metadata": {}, 49 | "outputs": [ 50 | { 51 | "data": { 52 | "text/plain": [ 53 | "{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'}" 54 | ] 55 | }, 56 | "execution_count": 12, 57 | "metadata": {}, 58 | "output_type": "execute_result" 59 | } 60 | ], 61 | "source": [ 62 | "adj_list={\n", 63 | " 'A': ['B','D'],\n", 64 | " 'B': ['A','C'],\n", 65 | " 'C': ['B'],\n", 66 | " 'D': ['A','E','F'],\n", 67 | " 'E': ['D','F','G'],\n", 68 | " 'F': ['D','E','H'],\n", 69 | " 'G': ['E','H'],\n", 70 | " 'H': ['F','G']\n", 71 | "}\n", 72 | "dfs(adj_list, 'A')" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": null, 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [] 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.6" 107 | } 108 | }, 109 | "nbformat": 4, 110 | "nbformat_minor": 4 111 | } 112 | -------------------------------------------------------------------------------- /Code Workspace/Data structure/7.1 Trees.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Implementation of Binary Search Tree" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 69, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "class Node():\n", 17 | " def __init__(self,value):\n", 18 | " self.left = None #left edge\n", 19 | " self.value = value\n", 20 | " self.right = None #right edge" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 70, 26 | "metadata": {}, 27 | "outputs": [], 28 | "source": [ 29 | "class BinarySearchTree():\n", 30 | " def __init__(self):\n", 31 | " self.root = None\n", 32 | " \n", 33 | " def add(self,value):\n", 34 | " if self.root == None:\n", 35 | " self.root = Node(value)\n", 36 | " else:\n", 37 | " self.add_node(self.root, value)\n", 38 | " \n", 39 | " def add_node(self, node, value):\n", 40 | " if node.value < value:\n", 41 | " self.add_right(node, value)\n", 42 | " else:\n", 43 | " self.add_right(node, value)\n", 44 | " \n", 45 | " def add_right(self, node, value):\n", 46 | " if node.right == None:\n", 47 | " node.right = Node(value)\n", 48 | " else:\n", 49 | " self.add_node(node.right, value)\n", 50 | " \n", 51 | " def add_left(self, node, value):\n", 52 | " if node.left == None:\n", 53 | " node.left = Node(value)\n", 54 | " else:\n", 55 | " self.add_node(node.left, value)\n", 56 | " \n", 57 | " def lookup(self, value):\n", 58 | " #travel tree to find given element and return it if found else return None\n", 59 | " currentNode = self.root\n", 60 | " while True:\n", 61 | " if currentNode == None:\n", 62 | " return None\n", 63 | " elif value == currentNode.value:\n", 64 | " return currentNode.value\n", 65 | " elif value > currentNode.value:\n", 66 | " #move right\n", 67 | " currentNode = currentNode.right\n", 68 | " elif value < currentNode.value:\n", 69 | " #move left\n", 70 | " currentNode = currentNode.left" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 71, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [ 79 | "mytree = BinarySearchTree()" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 72, 85 | "metadata": {}, 86 | "outputs": [], 87 | "source": [ 88 | "mytree.add(3)\n", 89 | "mytree.add(4)\n", 90 | "mytree.add(0)\n", 91 | "mytree.add(8)\n", 92 | "mytree.add(2)" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 73, 98 | "metadata": {}, 99 | "outputs": [ 100 | { 101 | "data": { 102 | "text/plain": [ 103 | "4" 104 | ] 105 | }, 106 | "execution_count": 73, 107 | "metadata": {}, 108 | "output_type": "execute_result" 109 | } 110 | ], 111 | "source": [ 112 | "mytree.lookup(4)" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": 1, 118 | "metadata": {}, 119 | "outputs": [ 120 | { 121 | "ename": "NameError", 122 | "evalue": "name 'mytree' is not defined", 123 | "output_type": "error", 124 | "traceback": [ 125 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 126 | "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", 127 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mmytree\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 128 | "\u001b[1;31mNameError\u001b[0m: name 'mytree' is not defined" 129 | ] 130 | } 131 | ], 132 | "source": [ 133 | "mytree" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": null, 139 | "metadata": {}, 140 | "outputs": [], 141 | "source": [] 142 | } 143 | ], 144 | "metadata": { 145 | "kernelspec": { 146 | "display_name": "Python 3", 147 | "language": "python", 148 | "name": "python3" 149 | }, 150 | "language_info": { 151 | "codemirror_mode": { 152 | "name": "ipython", 153 | "version": 3 154 | }, 155 | "file_extension": ".py", 156 | "mimetype": "text/x-python", 157 | "name": "python", 158 | "nbconvert_exporter": "python", 159 | "pygments_lexer": "ipython3", 160 | "version": "3.7.6" 161 | } 162 | }, 163 | "nbformat": 4, 164 | "nbformat_minor": 4 165 | } 166 | -------------------------------------------------------------------------------- /Code Workspace/Data structure/7.2 Order Traversal Binary Tree.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Pre-Order Traversal (NLR)" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 4, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "def preorder_traversal(self,root):\n", 17 | " if root is None:\n", 18 | " return []\n", 19 | " \n", 20 | " return [root.value] + self.preorder_traversal(root.left) + self.preorder_traversal(root.right)" 21 | ] 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "metadata": {}, 26 | "source": [ 27 | "## In-Order Traversal (LNR)" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 5, 33 | "metadata": {}, 34 | "outputs": [], 35 | "source": [ 36 | "def inorder_traversal(self,root):\n", 37 | " if root is None:\n", 38 | " return []\n", 39 | " \n", 40 | " return self.inorder_traversal(root.left) + [root.value] + self.inorder_traversal(root.right)" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "## Post-Order Traversal (LRN)" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 6, 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [ 56 | "def postorder_traversal(self,root):\n", 57 | " if root is None:\n", 58 | " return []\n", 59 | " \n", 60 | " return self.postorder_traversal(root.left) + self.postorder_traversal(root.right) + [root.value] " 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": null, 66 | "metadata": {}, 67 | "outputs": [], 68 | "source": [] 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.6" 88 | } 89 | }, 90 | "nbformat": 4, 91 | "nbformat_minor": 4 92 | } 93 | -------------------------------------------------------------------------------- /Code Workspace/Data structure/7.3 Validate BST.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Validate BST" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 14, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "class Node():\n", 17 | " def __init__(self,data):\n", 18 | " self.left = None #left edge\n", 19 | " self.data = data\n", 20 | " self.right = None #right edge" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 15, 26 | "metadata": {}, 27 | "outputs": [ 28 | { 29 | "name": "stdout", 30 | "output_type": "stream", 31 | "text": [ 32 | "Is BST\n" 33 | ] 34 | } 35 | ], 36 | "source": [ 37 | "# Python program to check if a binary tree is bst or not \n", 38 | "INT_MAX = 4294967296\n", 39 | "INT_MIN = -4294967296\n", 40 | "\n", 41 | "\n", 42 | "# Returns true if the given tree is a binary search tree \n", 43 | "def isBST(node): \n", 44 | " return (isBSTUtil(node, INT_MIN, INT_MAX)) \n", 45 | "\n", 46 | "# Retusn true if the given tree is a BST and its values >= min and <= max \n", 47 | "def isBSTUtil(node, mini, maxi): \n", 48 | "\n", 49 | " # An empty tree is BST \n", 50 | " if node is None: \n", 51 | " return True\n", 52 | "\n", 53 | " # False if this node violates min/max constraint \n", 54 | " if node.data < mini or node.data > maxi: \n", 55 | " return False\n", 56 | "\n", 57 | " # Otherwise check the subtrees recursively \n", 58 | " # tightening the min or max constraint \n", 59 | " return ( isBSTUtil(node.left, mini, node.data -1) and isBSTUtil(node.right, node.data+1, maxi)) \n", 60 | "\n", 61 | "# Driver program to test above function \n", 62 | "root = Node(4) \n", 63 | "root.left = Node(2) \n", 64 | "root.right = Node(5) \n", 65 | "root.left.left = Node(1) \n", 66 | "root.left.right = Node(3) \n", 67 | "\n", 68 | "if (isBST(root)): \n", 69 | " print(\"Is BST\")\n", 70 | "else: \n", 71 | " print(\"Not a BST\")" 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.6" 99 | } 100 | }, 101 | "nbformat": 4, 102 | "nbformat_minor": 4 103 | } 104 | -------------------------------------------------------------------------------- /Code Workspace/Data structure/7.4 Trim a BinarySearchTree.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Trim a Binary Search Tree \n", 8 | "\n", 9 | "\n", 10 | "Given the root of a binary search tree and 2 numbers min and max, trim the tree such that all the numbers in the new tree are between min and max (inclusive). The resulting tree should still be a valid binary search tree.\n", 11 | "___\n", 12 | "and we’re given **min value as 5** and **max value as 13**: \n", 13 | "___\n", 14 | "We should remove all the nodes whose value is not between min and max. \n" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "def trimBST(tree, minVal, maxVal): \n", 24 | " \n", 25 | " if not tree: \n", 26 | " return \n", 27 | " \n", 28 | " tree.left=trimBST(tree.left, minVal, maxVal) \n", 29 | " tree.right=trimBST(tree.right, minVal, maxVal) \n", 30 | " \n", 31 | " if minVal<=tree.val<=maxVal: \n", 32 | " return tree \n", 33 | " \n", 34 | " if tree.valmaxVal: \n", 38 | " return tree.left " 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "The complexity of this algorithm is O(N), where N is the number of nodes in the tree. Because we basically perform a post-order traversal of the tree, visiting each and every node one. " 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": null, 51 | "metadata": {}, 52 | "outputs": [], 53 | "source": [] 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.6" 73 | } 74 | }, 75 | "nbformat": 4, 76 | "nbformat_minor": 1 77 | } 78 | -------------------------------------------------------------------------------- /Code Workspace/Data structure/8.1 Heap.py: -------------------------------------------------------------------------------- 1 | class MaxHeap(): 2 | 3 | #we receive a list of items 4 | def __init__(self,items = []): 5 | self.heap = [0] 6 | # we create a heap list and store 0 at first Index 7 | # we will not use first Index 8 | for i in items: 9 | # we will store the items received in items[] in heap 10 | self.heap.append(i) 11 | #now we will float them up to thier proper position 12 | self.float_up(len(self.heap) - 1) 13 | 14 | def push(self,data): 15 | #append at last location 16 | self.heap.append(data) 17 | #heapify 18 | self.float_up(len(self.heap) - 1) 19 | 20 | def peek(self): 21 | #check topmost node - stored at index 1 22 | if self.heap[1]: 23 | return self.heap[1] 24 | return False 25 | 26 | def pop(self): 27 | #case 1: more than 2 Value 28 | #in that case swap the root to last Value 29 | if len(self.heap) > 2: 30 | #swap the max val and last node 31 | self.swap(1,len(self.heap) - 1) 32 | #pop the maximum value 33 | max=self.heap.pop() 34 | #call the helper fn 35 | self.bubble_down(1) 36 | 37 | #case 2: when one value, pop it and we have empty heap 38 | elif len(self.heap) == 2: 39 | max=self.heap.pop() 40 | 41 | #case 3: no root (pop from empty heap)then return false 42 | else: 43 | max = False 44 | 45 | return max 46 | 47 | #helper methods: 48 | def swap(self,i,j): 49 | self.heap[i], self.heap[j] = self.heap[j], self.heap[i] 50 | 51 | def float_up(self,index): 52 | #find parent 53 | parent = index//2 54 | 55 | if index <= 1: 56 | return 57 | 58 | elif self.heap[index] > self.heap[parent]: 59 | self.swap(index,parent) 60 | #recursive call 61 | self.float_up(parent) 62 | 63 | def bubble_down(self,index): #max heapify 64 | left = index * 2 65 | right = index * 2 + 1 66 | 67 | largest = index 68 | 69 | if len(self.heap) > left and self.heap[largest] < self.heap[left]: 70 | largest = left 71 | if len(self.heap) > right and self.heap[largest] < self.heap[right]: 72 | largest = right 73 | 74 | if largest != index: 75 | self.swap(index,largest) 76 | #recursive call 77 | self.bubble_down(largest) 78 | 79 | m = MaxHeap([95,3,21]) 80 | m.push(10) 81 | 82 | print(str(m.heap[1:len(m.heap)])) 83 | print(str(m.pop())) 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Data-Structure-and-Algorithm 2 | Implementing data structure and algorithm in python 3 | -------------------------------------------------------------------------------- /Resources/AlgorithmsNotesForProfessionals.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ayushi7rawat/Data-Structure-and-Algorithm/fca7d5262998b19476d3300fe46843ed94a8a291/Resources/AlgorithmsNotesForProfessionals.pdf -------------------------------------------------------------------------------- /Resources/Searching/binary search .txt: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main(){ 8 | cout<<"hello"<>key; 18 | 19 | start=0; 20 | end=sizeof(arr)/sizeof(int); 21 | do{ 22 | mid=(start+end)/2; 23 | if(arr[mid]==key){ 24 | flag=true; 25 | break; 26 | } 27 | else if(arr[mid]>key){ 28 | end=end-1; 29 | } 30 | else 31 | start=start+1; 32 | }while(end-start>1); 33 | 34 | if(flag==true) 35 | cout<