├── Covid └── Clustering literature.ipynb ├── DP-10.ipynb ├── DP-11.ipynb ├── DP-12.ipynb ├── DP-13.ipynb ├── DP-14.ipynb ├── DP-15.ipynb ├── DP-16.ipynb ├── DP-17.ipynb ├── DP-1_DP-2.ipynb ├── DP-20.ipynb ├── DP-21.ipynb ├── DP-3.ipynb ├── DP-4.ipynb ├── DP-5.ipynb ├── DP-6.ipynb ├── DP-7.ipynb ├── DP-8.ipynb ├── DP-9.ipynb ├── Day 18.ipynb ├── Day 22.ipynb ├── Day 23.ipynb ├── Day 24.ipynb ├── Day 25.ipynb ├── Day 26.ipynb ├── Day 27.ipynb ├── Day 28.ipynb ├── Day 29.ipynb ├── Day 30.ipynb ├── Day 31.ipynb ├── Day 32.ipynb ├── Max Palindromes.ipynb ├── README.md ├── Subsequence_util.ipynb ├── Time Series Forecasting -1-AR models.ipynb └── Time Series Forecasting -1.ipynb /DP-10.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Finding probability that a person is alive after taking N Steps on an Island" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 63, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "from itertools import product as prod \n", 17 | "\n", 18 | "def calculate_prob(island_size,n_steps):\n", 19 | " # Probability of being alive is 1 initially since the person would be at a coordinate in the island\n", 20 | " old_prob = [[1. for i in range(island_size)] for j in range(island_size)]\n", 21 | " if n_steps == 0:\n", 22 | " return old_prob\n", 23 | " old_prob = calculate_prob(island_size, n_steps - 1)\n", 24 | " new_prob = [[0. for i in range(island_size)] for j in range(island_size)]\n", 25 | " directions = [(0, -1), (1, 0), (0, 1), (-1, 0)]\n", 26 | " for (i, j, direction) in prod(range(island_size), range(island_size), directions):\n", 27 | " neighbor_i = i + direction[0]\n", 28 | " neighbor_j = j + direction[1]\n", 29 | " if neighbor_i >= 0 and neighbor_i < island_size and \\\n", 30 | " neighbor_j >= 0 and neighbor_j < island_size:\n", 31 | " prob_alive_this_way = old_prob[neighbor_i][neighbor_j]\n", 32 | " else: # neighbor is outside the island \n", 33 | " prob_alive_this_way = 0.\n", 34 | " new_prob[i][j] += 0.25* prob_alive_this_way\n", 35 | " return new_prob" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 69, 41 | "metadata": {}, 42 | "outputs": [ 43 | { 44 | "name": "stdout", 45 | "output_type": "stream", 46 | "text": [ 47 | "[0.5, 0.75, 0.75, 0.75, 0.5]\n", 48 | "[0.75, 1.0, 1.0, 1.0, 0.75]\n", 49 | "[0.75, 1.0, 1.0, 1.0, 0.75]\n", 50 | "[0.75, 1.0, 1.0, 1.0, 0.75]\n", 51 | "[0.5, 0.75, 0.75, 0.75, 0.5]\n" 52 | ] 53 | } 54 | ], 55 | "source": [ 56 | "res = calculate_prob(5, 1)\n", 57 | "for r in res:\n", 58 | " print(r)" 59 | ] 60 | } 61 | ], 62 | "metadata": { 63 | "kernelspec": { 64 | "display_name": "Python 3", 65 | "language": "python", 66 | "name": "python3" 67 | }, 68 | "language_info": { 69 | "codemirror_mode": { 70 | "name": "ipython", 71 | "version": 3 72 | }, 73 | "file_extension": ".py", 74 | "mimetype": "text/x-python", 75 | "name": "python", 76 | "nbconvert_exporter": "python", 77 | "pygments_lexer": "ipython3", 78 | "version": "3.7.4" 79 | } 80 | }, 81 | "nbformat": 4, 82 | "nbformat_minor": 2 83 | } 84 | -------------------------------------------------------------------------------- /DP-11.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Rod Cutting Problem" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 2, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "# Input - A rod of length n and a list of prices for each of the lengths\n", 17 | "# Output - Maximum profit that can be obtained by optimally cutting the rod\n", 18 | "\n", 19 | "def get_max_profit_cut(prices, rod_length):\n", 20 | " \n", 21 | " # Memoization table - Something I like to call!- Not a technical term\n", 22 | " # Ex: length = 4, => sub problem lengths: 0,1,2,3,4 \n", 23 | " result = [0 for i in range(rod_length+1)]\n", 24 | " \n", 25 | " for i in range(1, rod_length+1):\n", 26 | " for j in range(1, i+1):\n", 27 | " result[i] = max(result[i] , prices[j-1] + result[i-j])\n", 28 | " \n", 29 | " return result" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 3, 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "# Test case:\n", 39 | "prices = [1,5,8,9,10,17,17,20]\n", 40 | "result = get_max_profit_cut(prices, 4)\n", 41 | "print(result)" 42 | ] 43 | } 44 | ], 45 | "metadata": { 46 | "kernelspec": { 47 | "display_name": "Python 3", 48 | "language": "python", 49 | "name": "python3" 50 | }, 51 | "language_info": { 52 | "codemirror_mode": { 53 | "name": "ipython", 54 | "version": 3 55 | }, 56 | "file_extension": ".py", 57 | "mimetype": "text/x-python", 58 | "name": "python", 59 | "nbconvert_exporter": "python", 60 | "pygments_lexer": "ipython3", 61 | "version": "3.7.4" 62 | } 63 | }, 64 | "nbformat": 4, 65 | "nbformat_minor": 2 66 | } 67 | -------------------------------------------------------------------------------- /DP-12.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 9, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "#Knapsack problem\n", 10 | "# Find optimal weight combination to yield high value\n", 11 | "def find_optimal_combination(values, weights, capacity):\n", 12 | " result = [[0 for i in range(capacity+1)] for j in range(len(values)+1)]\n", 13 | " \n", 14 | " for i in range(1,len(values)+1):\n", 15 | " for j in range(capacity+1):\n", 16 | " if weights[i-1] > j:\n", 17 | " result[i][j] = result[i-1][j]\n", 18 | " else:\n", 19 | " result[i][j] = max(result[i-1][j], result[i-1][j-weights[i-1]] + values[i-1])\n", 20 | " return result[len(values)][capacity]\n", 21 | " \n", 22 | " " 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 10, 28 | "metadata": {}, 29 | "outputs": [ 30 | { 31 | "name": "stdout", 32 | "output_type": "stream", 33 | "text": [ 34 | "60\n" 35 | ] 36 | } 37 | ], 38 | "source": [ 39 | "# test case\n", 40 | "values = [20,5,10,40,15,25]\n", 41 | "weights = [1,2,3,8,7,4]\n", 42 | "capacity = 10\n", 43 | "print(find_optimal_combination(values, weights, capacity))" 44 | ] 45 | } 46 | ], 47 | "metadata": { 48 | "kernelspec": { 49 | "display_name": "Python 3", 50 | "language": "python", 51 | "name": "python3" 52 | }, 53 | "language_info": { 54 | "codemirror_mode": { 55 | "name": "ipython", 56 | "version": 3 57 | }, 58 | "file_extension": ".py", 59 | "mimetype": "text/x-python", 60 | "name": "python", 61 | "nbconvert_exporter": "python", 62 | "pygments_lexer": "ipython3", 63 | "version": "3.7.4" 64 | } 65 | }, 66 | "nbformat": 4, 67 | "nbformat_minor": 2 68 | } 69 | -------------------------------------------------------------------------------- /DP-13.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Coin Changing Problem" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 22, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "def find_optimal_denomination(coinset, total):\n", 17 | " result = [[0 for i in range(len(coinset))] for j in range(total+1)]\n", 18 | " for i in range(len(coinset)):\n", 19 | " result[0][i] = 1\n", 20 | " \n", 21 | " for i in range(1,total+1):\n", 22 | " for j in range(len(coinset)):\n", 23 | " if i-coinset[j] >= 0:\n", 24 | " first_sum = result[i - coinset[j]][j] \n", 25 | " else:\n", 26 | " first_sum = 0\n", 27 | " if j >= 1:\n", 28 | " second_sum = result[i][j-1] \n", 29 | " else:\n", 30 | " second_sum = 0\n", 31 | " result[i][j] = first_sum + second_sum\n", 32 | " \n", 33 | " return result[total][len(coinset)-1]" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 23, 39 | "metadata": {}, 40 | "outputs": [ 41 | { 42 | "name": "stdout", 43 | "output_type": "stream", 44 | "text": [ 45 | "4\n" 46 | ] 47 | } 48 | ], 49 | "source": [ 50 | "print(find_optimal_denomination([1,2,3],4))" 51 | ] 52 | } 53 | ], 54 | "metadata": { 55 | "kernelspec": { 56 | "display_name": "Python 3", 57 | "language": "python", 58 | "name": "python3" 59 | }, 60 | "language_info": { 61 | "codemirror_mode": { 62 | "name": "ipython", 63 | "version": 3 64 | }, 65 | "file_extension": ".py", 66 | "mimetype": "text/x-python", 67 | "name": "python", 68 | "nbconvert_exporter": "python", 69 | "pygments_lexer": "ipython3", 70 | "version": "3.7.4" 71 | } 72 | }, 73 | "nbformat": 4, 74 | "nbformat_minor": 2 75 | } 76 | -------------------------------------------------------------------------------- /DP-14.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Finding Shortest Substring for which target string is a sub-sequence \n", 8 | "\n", 9 | "Problem statement- \n", 10 | "We are given a target string-For example:'challenge' and input string- For example: 'macldhaldfgenfefprer'.\n", 11 | "We need to ouput the minimum length substring which has this target string as subsequence.\n" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 116, 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "def shortest_subsequence(S,T):\n", 21 | " result = [[None for _ in range(len(S))] for _ in range(2)]\n", 22 | " for j, c in enumerate(S):\n", 23 | " if c == T[0]:\n", 24 | " result[0][j] = j\n", 25 | "\n", 26 | " for i in range(1, len(T)):\n", 27 | " prev = None\n", 28 | " result[i%2] = [None] * len(S)\n", 29 | " for j, c in enumerate(S):\n", 30 | " if prev is not None and c == T[i]:\n", 31 | " result[i%2][j] = prev\n", 32 | " if result[(i-1)%2][j] is not None:\n", 33 | " prev = result[(i-1)%2][j]\n", 34 | " start, end = 0, len(S)\n", 35 | " for j, i in enumerate(result[(len(T)-1)%2]):\n", 36 | " if (i is not None and j is not None) and i >= 0 and j-i < end-start:\n", 37 | " start, end = i, j\n", 38 | " return S[start:end+1] if end < len(S) else \"\"" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 117, 44 | "metadata": {}, 45 | "outputs": [ 46 | { 47 | "name": "stdout", 48 | "output_type": "stream", 49 | "text": [ 50 | "10\n" 51 | ] 52 | } 53 | ], 54 | "source": [ 55 | "input_s = 'abmazmnotnorapty'\n", 56 | "print(len(shortest_subsequence(input_s[0:12], 'amazon')))\n" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 103, 62 | "metadata": {}, 63 | "outputs": [ 64 | { 65 | "data": { 66 | "text/plain": [ 67 | "'lpaihlatphjaithai'" 68 | ] 69 | }, 70 | "execution_count": 103, 71 | "metadata": {}, 72 | "output_type": "execute_result" 73 | } 74 | ], 75 | "source": [ 76 | "input_s[2:19]" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 58, 82 | "metadata": {}, 83 | "outputs": [], 84 | "source": [ 85 | "dp = [[None for _ in range(len(input_s[2:19]))] for _ in range(2)]\n" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 111, 91 | "metadata": {}, 92 | "outputs": [ 93 | { 94 | "name": "stdout", 95 | "output_type": "stream", 96 | "text": [ 97 | "9223372036854775807\n" 98 | ] 99 | } 100 | ], 101 | "source": [ 102 | "import sys\n", 103 | "input_s = 'mplpaihlatphjaithaij'\n", 104 | "T = 'aphtia'\n", 105 | "S = input_s[2:19]\n", 106 | "if( S == None or len(S) == 0 or T == None or len(T) == 0):\n", 107 | " print('')\n", 108 | "result = [[None for i in range(len(T)+1)] for j in range(len(S)+1)]\n", 109 | "\n", 110 | "for i in range(1,len(T)+1):\n", 111 | " result[0][i] = -1\n", 112 | "for i in range(1,len(S)+1):\n", 113 | " result[i][0] = 1\n", 114 | "\n", 115 | "final_len = sys.maxsize\n", 116 | "start_index = -1\n", 117 | "for i in range(1, len(S)+1):\n", 118 | " for j in range(1, len(T)+1):\n", 119 | " result[i][j] = -1\n", 120 | " \n", 121 | " if S[i-1] == T[j-1]:\n", 122 | " result[i][j] = result[i-1][j-1]\n", 123 | " else:\n", 124 | " result[i][j] = result[i-1][j]\n", 125 | " if result[i][len(T)] != -1:\n", 126 | " len_val = i - result[i][len(T)]\n", 127 | " if len_val < final_len:\n", 128 | " start_index = dp[i][len(T)]\n", 129 | " final_len = len_val\n", 130 | "print(final_len)" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": 101, 136 | "metadata": {}, 137 | "outputs": [ 138 | { 139 | "name": "stdout", 140 | "output_type": "stream", 141 | "text": [ 142 | "[None, -1, -1, -1, -1, -1, -1]\n", 143 | "[1, -1, -1, -1, -1, -1, -1]\n", 144 | "[1, 1, -1, -1, -1, -1, -1]\n", 145 | "[1, 1, 1, -1, -1, -1, -1]\n", 146 | "[1, 1, 1, -1, -1, -1, -1]\n", 147 | "[1, 1, 1, -1, -1, -1, -1]\n", 148 | "[1, 1, 1, -1, -1, -1, -1]\n", 149 | "[1, 1, 1, -1, -1, -1, -1]\n", 150 | "[1, 1, 1, 1, -1, -1, -1]\n", 151 | "[1, 1, 1, 1, -1, -1, -1]\n", 152 | "[1, 1, 1, 1, 1, -1, -1]\n", 153 | "[1, 1, 1, 1, 1, -1, -1]\n", 154 | "[1, 1, 1, 1, 1, 1, -1]\n", 155 | "[1, 1, 1, 1, 1, 1, 1]\n", 156 | "[1, 1, 1, 1, 1, 1, 1]\n", 157 | "[1, 1, 1, 1, 1, 1, 1]\n", 158 | "[1, 1, 1, 1, 1, 1, 1]\n", 159 | "[1, 1, 1, 1, 1, 1, 1]\n" 160 | ] 161 | } 162 | ], 163 | "source": [ 164 | "for d in result:\n", 165 | " print(d)" 166 | ] 167 | } 168 | ], 169 | "metadata": { 170 | "kernelspec": { 171 | "display_name": "Python 3", 172 | "language": "python", 173 | "name": "python3" 174 | }, 175 | "language_info": { 176 | "codemirror_mode": { 177 | "name": "ipython", 178 | "version": 3 179 | }, 180 | "file_extension": ".py", 181 | "mimetype": "text/x-python", 182 | "name": "python", 183 | "nbconvert_exporter": "python", 184 | "pygments_lexer": "ipython3", 185 | "version": "3.7.4" 186 | } 187 | }, 188 | "nbformat": 4, 189 | "nbformat_minor": 2 190 | } 191 | -------------------------------------------------------------------------------- /DP-15.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Levenshtein Distance (Edit Distance Problem)" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Levenshtein distance is the minimum number of single character edits(insertions, deletions and substituitions) required to change one word to another. Each of the operations have unit cost.\n", 15 | "\n", 16 | "Input : X, Y\n", 17 | "For example: X = kitten; Y = sitten\n", 18 | "For converting X to Y, we can replace k with s with a cost of 1. So, Levenshtein distance here would be 1." 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 21, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "def get_levenshtein_distance(X,Y):\n", 28 | " #Building memoisation table\n", 29 | " result = [[0 for i in range(len(X)+1)] for j in range(len(Y)+1)]\n", 30 | " \n", 31 | " for i in range(len(Y)+1):\n", 32 | " result[i][0] = i\n", 33 | " for i in range(len(X)+1):\n", 34 | " result[0][i] = i\n", 35 | " \n", 36 | " # Row by row \n", 37 | " for i in range(1,len(Y)+1):\n", 38 | " # Column by column\n", 39 | " for j in range(1,len(X)+1):\n", 40 | " if Y[i-1] == X[j-1]:\n", 41 | " result[i][j] = result[i-1][j-1]\n", 42 | " else:\n", 43 | " result[i][j] = 1 + min(result[i-1][j], result[i][j-1], result[i-1][j-1])\n", 44 | " \n", 45 | " \n", 46 | " return result\n", 47 | " " 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 25, 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "name": "stdout", 57 | "output_type": "stream", 58 | "text": [ 59 | "[0, 1, 2, 3, 4, 5, 6]\n", 60 | "[1, 1, 2, 3, 4, 5, 6]\n", 61 | "[2, 2, 1, 2, 3, 4, 5]\n", 62 | "[3, 3, 2, 1, 2, 3, 4]\n", 63 | "[4, 4, 3, 2, 1, 2, 3]\n", 64 | "[5, 5, 4, 3, 2, 2, 3]\n", 65 | "[6, 6, 5, 4, 3, 3, 2]\n", 66 | "[7, 7, 6, 5, 4, 4, 3]\n", 67 | "Levenshtein distance :3\n" 68 | ] 69 | } 70 | ], 71 | "source": [ 72 | "X = 'kitten'\n", 73 | "Y = 'sitting'\n", 74 | "table = get_levenshtein_distance(X,Y)\n", 75 | "for row in table:\n", 76 | " print(row)\n", 77 | "print('Levenshtein distance :{}'.format(table[len(Y)][len(X)]))" 78 | ] 79 | } 80 | ], 81 | "metadata": { 82 | "kernelspec": { 83 | "display_name": "Python 3", 84 | "language": "python", 85 | "name": "python3" 86 | }, 87 | "language_info": { 88 | "codemirror_mode": { 89 | "name": "ipython", 90 | "version": 3 91 | }, 92 | "file_extension": ".py", 93 | "mimetype": "text/x-python", 94 | "name": "python", 95 | "nbconvert_exporter": "python", 96 | "pygments_lexer": "ipython3", 97 | "version": "3.7.4" 98 | } 99 | }, 100 | "nbformat": 4, 101 | "nbformat_minor": 2 102 | } 103 | -------------------------------------------------------------------------------- /DP-16.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Work Break Problem" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 28, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "def break_words_based_on_dict(input_s, dict_list):\n", 17 | " if input_s is None or len(input_s) == 0:\n", 18 | " return False\n", 19 | " table =[False for i in range(len(input_s)+1)]\n", 20 | " table[0] = True\n", 21 | " for i in range(1,len(input_s)+1):\n", 22 | " j = i-1\n", 23 | " while(j >= 0):\n", 24 | " if table[j] is True and (input_s[j:i] in dict_list):\n", 25 | " table[i] = True\n", 26 | " j = j-1 \n", 27 | " return table" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 31, 33 | "metadata": {}, 34 | "outputs": [ 35 | { 36 | "name": "stdout", 37 | "output_type": "stream", 38 | "text": [ 39 | "Can the String be broken? Yes\n" 40 | ] 41 | } 42 | ], 43 | "source": [ 44 | "input_s = 'Wordbreakproblem'\n", 45 | "result = break_words_based_on_dict(input_s,['this','th','b','is','famous','Word','break'\n", 46 | " 'r','e','k','a','br','bre','brea','ak','problem'])\n", 47 | "print('Can the String be broken? '+'Yes' if result[len(input_s)] else 'No')" 48 | ] 49 | } 50 | ], 51 | "metadata": { 52 | "kernelspec": { 53 | "display_name": "Python 3", 54 | "language": "python", 55 | "name": "python3" 56 | }, 57 | "language_info": { 58 | "codemirror_mode": { 59 | "name": "ipython", 60 | "version": 3 61 | }, 62 | "file_extension": ".py", 63 | "mimetype": "text/x-python", 64 | "name": "python", 65 | "nbconvert_exporter": "python", 66 | "pygments_lexer": "ipython3", 67 | "version": "3.7.4" 68 | } 69 | }, 70 | "nbformat": 4, 71 | "nbformat_minor": 2 72 | } 73 | -------------------------------------------------------------------------------- /DP-17.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Pots of gold game" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "We are given a set of coins each with a value. We need to find an optimal strategy that makes player wins knowing that opponent is playing optimally.\n", 15 | "For Example:\n", 16 | "If we have coins: [4,6,2,3]\n", 17 | "For example:\n", 18 | "first player takes coin: 3\n", 19 | "second player : 4\n", 20 | "first player: 6\n", 21 | "second player: 2\n", 22 | "\n", 23 | "Total by first player: 9\n", 24 | "Total by second player: 6\n" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 39, 30 | "metadata": {}, 31 | "outputs": [], 32 | "source": [ 33 | "def find_strategy_to_win(coins):\n", 34 | " num_coins = len(coins)\n", 35 | " if num_coins == 1:\n", 36 | " return coins[0]\n", 37 | " if num_coins == 2:\n", 38 | " return max(coins[0], coins[1])\n", 39 | " \n", 40 | " dp_table = [[0 for i in range(num_coins)] for j in range(num_coins)]\n", 41 | " for i in range(num_coins):\n", 42 | " j = 0\n", 43 | " k = i\n", 44 | " while k Problem Statement: Given two strings s1 and s2, return the length of their longest common subsequence. If there is no common Subsequence, return 0.\n", 15 | "\n", 16 | "Example:\n", 17 | "\n", 18 | "s1 = aggtab\n", 19 | "s2 = gxtxayb\n", 20 | "\n", 21 | "Ans: 4\n", 22 | "\n", 23 | "Constraints:\n", 24 | "\n", 25 | "1. Input Strings consist of lowercase letters only\n", 26 | "\n", 27 | "Some Notes:\n", 28 | "\n", 29 | "What is a Subsequence?\n", 30 | "Subsequence of a string is a NEW String generated from the ORIGINAL String with some(can be none) characters deleted without changing the relative order of the remaining characters.\n", 31 | "\n", 32 | "Reference: https://leetcode.com/problems/longest-common-subsequence/\n", 33 | "\n", 34 | "\n", 35 | "Questions I had:\n", 36 | "\n", 37 | "1. How is it a Dynamic Programming Problem? How do I even know? - May be by solving more problems :P\n", 38 | "2. So, first thing that comes to mind here is to look for the first common letter and next common letter and so on. Does my mind's pattern recognition actually work? What is the proof? What if there is some sequence that I am not able to see through my eyes? - May be I need to code.\n", 39 | "3. Ok, So someone told me to use Dynamic Programming. Now what? I need to decompose. So, what's the easiest sub problem that I can solve? - Just think I have only one character in the string.\n", 40 | "4. So now, someone again told me, just look at the last character, if it matches you have a common subsequence of length 1. Oh.. that's an easy subproblem. Wait, that's it? Just look at the last character, when it matches chop if off and look at the last character of the remaining substring? what if it doesn't match? \n", 41 | "5. Ok, now what? :-( It doesn't match. What to do- https://www.youtube.com/watch?v=L1fUKbgyAk4 gives a good explanation\n", 42 | "\n", 43 | "Good explanation at: https://www.youtube.com/watch?v=ASoaQq66foQ&t=350s\n" 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "metadata": {}, 49 | "source": [ 50 | "# Recursive Solution" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 1, 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [ 59 | "#Recursive solution\n", 60 | "\n", 61 | "# Recursive relation: if s1[i] == s2[j] then look for longest common subsequence in s1[0...i-1] and s2[0...j-1]. Remember to add 1!\n", 62 | "# if s1[i] != s2[j] then look for max(longest common subsequence in s1[0...i] and s2[0...j-1],\n", 63 | "# longest common subsequence in s1[0...i-1] and s2[0...j] )\n", 64 | "\n", 65 | "def helperFunction(s1, s2, len_s1, len_s2):\n", 66 | "\n", 67 | " #Base case\n", 68 | " if len_s1 is 0 or len_s2 is 0:\n", 69 | " return 0\n", 70 | " elif s1[len_s1-1] == s2[len_s2-1]:\n", 71 | " return 1+helperFunction(s1, s2, len_s1-1, len_s2-1)\n", 72 | " elif s1[len_s1-1] != s2[len_s2-1]:\n", 73 | " return max(helperFunction(s1, s2, len_s1, len_s2-1),\n", 74 | " helperFunction(s1, s2, len_s1-1, len_s2))\n", 75 | " \n", 76 | "\n", 77 | "def getLengthOfLongestCommonSubSequence(s1, s2):\n", 78 | " return helperFunction(s1,s2, len(s1), len(s2))\n", 79 | " \n", 80 | " " 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 2, 86 | "metadata": {}, 87 | "outputs": [ 88 | { 89 | "name": "stdout", 90 | "output_type": "stream", 91 | "text": [ 92 | "4\n", 93 | "3\n" 94 | ] 95 | } 96 | ], 97 | "source": [ 98 | "#Test case:1\n", 99 | "print(getLengthOfLongestCommonSubSequence('aggtab', 'gxtxayb'))\n", 100 | "# Test case:2\n", 101 | "print(getLengthOfLongestCommonSubSequence('ylqpejqbalahwr', 'yrkzavgdmdgtqpg'))" 102 | ] 103 | }, 104 | { 105 | "cell_type": "markdown", 106 | "metadata": {}, 107 | "source": [ 108 | "# Memoisation" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": 3, 114 | "metadata": {}, 115 | "outputs": [], 116 | "source": [ 117 | "# Memoisation\n", 118 | "def buildTable(s1, s2):\n", 119 | " len_s1 = len(s1)\n", 120 | " len_s2 = len(s2)\n", 121 | " # initialising all cells with zeros\n", 122 | " result = [[0 for i in range(len_s2+1)] for j in range(len_s1+1)]\n", 123 | " for i in range(1, len_s1+1):\n", 124 | " for j in range(1, len_s2+1):\n", 125 | " result[i][j] = (1 + result[i-1][j-1]) if s1[i-1] == s2[j-1] else max(result[i][j-1], result[i-1][j])\n", 126 | " return result" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": 4, 132 | "metadata": {}, 133 | "outputs": [], 134 | "source": [ 135 | "def getLengthOfLongestCommonSubSequence_m(s1, s2):\n", 136 | " result = buildTable(s1, s2)\n", 137 | " return result[len(s1)][len(s2)]" 138 | ] 139 | }, 140 | { 141 | "cell_type": "markdown", 142 | "metadata": {}, 143 | "source": [ 144 | "# Finding all Common Subsequences" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": 5, 150 | "metadata": {}, 151 | "outputs": [], 152 | "source": [ 153 | "#Reference and explanation: https://www.techiedelight.com/longest-common-subsequence-finding-lcs/\n", 154 | "def helperFunction(s1, s2, result, m, n):\n", 155 | " if m is 0 or n is 0:\n", 156 | " return set([''])\n", 157 | " elif s1[m-1] == s2[n-1]:\n", 158 | " all_sequences = helperFunction(s1, s2, result, m-1, n-1)\n", 159 | " all_sequences = set(a+s1[m-1] for a in all_sequences )\n", 160 | " return all_sequences\n", 161 | " else:\n", 162 | " if result[m-1][n] > result[m][n-1]:\n", 163 | " return helperFunction(s1, s2, result, m-1,n)\n", 164 | " elif result[m][n-1] > result[m-1][n]:\n", 165 | " return helperFunction(s1, s2, result, m,n-1)\n", 166 | " top_cell_sequences = helperFunction(s1, s2, result, m-1,n)\n", 167 | " left_cell_sequences = helperFunction(s1, s2, result, m,n-1)\n", 168 | " return top_cell_sequences.union(left_cell_sequences)" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": 6, 174 | "metadata": {}, 175 | "outputs": [], 176 | "source": [ 177 | "def getAllLongestCommonSubsequences(s1, s2):\n", 178 | " result = buildTable(s1, s2)\n", 179 | " sequences = helperFunction(s1, s2, result, len(s1) ,len(s2))\n", 180 | " return sequences\n" 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": 7, 186 | "metadata": {}, 187 | "outputs": [], 188 | "source": [ 189 | "def print_helper_function(s1, s2):\n", 190 | " print('Length of Longest Common Subsequence: {} and Sequences are : {}'.\n", 191 | " format(getLengthOfLongestCommonSubSequence_m(s1,s2),getAllLongestCommonSubsequences(s1, s2)))" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": 8, 197 | "metadata": {}, 198 | "outputs": [ 199 | { 200 | "name": "stdout", 201 | "output_type": "stream", 202 | "text": [ 203 | "Length of Longest Common Subsequence: 3 and Sequences are : {'ACC', 'AAC'}\n", 204 | "Length of Longest Common Subsequence: 3 and Sequences are : {'yqp'}\n" 205 | ] 206 | } 207 | ], 208 | "source": [ 209 | "#Test case:1\n", 210 | "s1 = 'AATCC'\n", 211 | "s2 = 'ACACG'\n", 212 | "print_helper_function(s1, s2)\n", 213 | "#Test case:2\n", 214 | "s1 = 'ylqpejqbalahwr'\n", 215 | "s2 = 'yrkzavgdmdgtqpg'\n", 216 | "print_helper_function(s1, s2)" 217 | ] 218 | } 219 | ], 220 | "metadata": { 221 | "kernelspec": { 222 | "display_name": "Python 3", 223 | "language": "python", 224 | "name": "python3" 225 | }, 226 | "language_info": { 227 | "codemirror_mode": { 228 | "name": "ipython", 229 | "version": 3 230 | }, 231 | "file_extension": ".py", 232 | "mimetype": "text/x-python", 233 | "name": "python", 234 | "nbconvert_exporter": "python", 235 | "pygments_lexer": "ipython3", 236 | "version": "3.7.4" 237 | } 238 | }, 239 | "nbformat": 4, 240 | "nbformat_minor": 2 241 | } 242 | -------------------------------------------------------------------------------- /DP-20.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Interleaving Strings problem" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "https://leetcode.com/articles/interleaving-strings/" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 6, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "def check_interleaving(s1, s2, s3):\n", 24 | " # For third string to be an interleaving of first and second\n", 25 | " # string,it's length must be equal to sum of lengths of s1 \n", 26 | " # s2\n", 27 | " if(len(s3) != len(s1) + len(s2)):\n", 28 | " return False\n", 29 | " \n", 30 | " table = [[False for i in range(len(s2)+1)] for j in range(len(s1)+1)]\n", 31 | " for i in range(len(s1) + 1):\n", 32 | " for j in range(len(s2)+1):\n", 33 | " # first cell\n", 34 | " if i == 0 and j == 0:\n", 35 | " table[i][j] = True\n", 36 | " # first row\n", 37 | " elif i == 0:\n", 38 | " table[i][j] = table[i][j-1] and s2[j-1] == s3[j-1]\n", 39 | " # first column\n", 40 | " elif j == 0:\n", 41 | " table[i][j] = table[i-1][j] and s1[i-1] == s3[i-1]\n", 42 | " else:\n", 43 | " table[i][j] = (table[i][j-1] and s2[j-1] == s3[i+j-1]) or (table[i-1][j] and s1[i-1] == s3[i+j-1])\n", 44 | " \n", 45 | " return table[len(s1)][len(s2)]" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 7, 51 | "metadata": {}, 52 | "outputs": [ 53 | { 54 | "data": { 55 | "text/plain": [ 56 | "True" 57 | ] 58 | }, 59 | "execution_count": 7, 60 | "metadata": {}, 61 | "output_type": "execute_result" 62 | } 63 | ], 64 | "source": [ 65 | "check_interleaving('aax','abyc','abayaxc')" 66 | ] 67 | } 68 | ], 69 | "metadata": { 70 | "kernelspec": { 71 | "display_name": "Python 3", 72 | "language": "python", 73 | "name": "python3" 74 | }, 75 | "language_info": { 76 | "codemirror_mode": { 77 | "name": "ipython", 78 | "version": 3 79 | }, 80 | "file_extension": ".py", 81 | "mimetype": "text/x-python", 82 | "name": "python", 83 | "nbconvert_exporter": "python", 84 | "pygments_lexer": "ipython3", 85 | "version": "3.7.4" 86 | } 87 | }, 88 | "nbformat": 4, 89 | "nbformat_minor": 2 90 | } 91 | -------------------------------------------------------------------------------- /DP-21.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Maximising the value of an Expression" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 7, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "def get_max_value(input_list):\n", 17 | " # Creating 4 look up tables\n", 18 | " len_l = len(input_list)\n", 19 | " lookup_1 = [float('-inf') for i in range(len_l+1)]\n", 20 | " lookup_2 = [float('-inf') for i in range(len_l)]\n", 21 | " lookup_3 = [float('-inf') for i in range(len_l-1)]\n", 22 | " lookup_4 = [float('-inf') for i in range(len_l-2)]\n", 23 | " \n", 24 | " for i in range(len_l-1, -1, -1):\n", 25 | " lookup_1[i] = max(lookup_1[i+1], input_list[i])\n", 26 | " \n", 27 | " for i in range(len_l-2, -1, -1):\n", 28 | " lookup_2[i] = max(lookup_2[i+1], lookup_1[i+1] - input_list[i]) \n", 29 | " \n", 30 | " for i in range(len_l-3, -1, -1):\n", 31 | " lookup_3[i] = max(lookup_3[i+1], lookup_2[i+1] + input_list[i])\n", 32 | " \n", 33 | " for i in range(len_l-4, -1, -1):\n", 34 | " lookup_4[i] = max(lookup_4[i+1], lookup_3[i+1] - input_list[i])\n", 35 | " \n", 36 | " return lookup_4[0]" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 8, 42 | "metadata": {}, 43 | "outputs": [ 44 | { 45 | "data": { 46 | "text/plain": [ 47 | "46" 48 | ] 49 | }, 50 | "execution_count": 8, 51 | "metadata": {}, 52 | "output_type": "execute_result" 53 | } 54 | ], 55 | "source": [ 56 | "get_max_value([3,9,10,1,30,40])" 57 | ] 58 | } 59 | ], 60 | "metadata": { 61 | "kernelspec": { 62 | "display_name": "Python 3", 63 | "language": "python", 64 | "name": "python3" 65 | }, 66 | "language_info": { 67 | "codemirror_mode": { 68 | "name": "ipython", 69 | "version": 3 70 | }, 71 | "file_extension": ".py", 72 | "mimetype": "text/x-python", 73 | "name": "python", 74 | "nbconvert_exporter": "python", 75 | "pygments_lexer": "ipython3", 76 | "version": "3.7.4" 77 | } 78 | }, 79 | "nbformat": 4, 80 | "nbformat_minor": 2 81 | } 82 | -------------------------------------------------------------------------------- /DP-3.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Longest Common Substring" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Problem Statement: Given two strings s1 and s2, return the length of their longest common substring. If there is no common string, return 0.\n", 15 | "\n", 16 | "Example:\n", 17 | "\n", 18 | "s1 = zxabcdezy\n", 19 | "s2 = yzabcdezx\n", 20 | "\n", 21 | "Ans: 6\n", 22 | "\n", 23 | "Constraints:\n", 24 | "\n", 25 | "1. Input Strings consist of lowercase letters only\n", 26 | "\n", 27 | "Some Notes:\n", 28 | "\n", 29 | "What is a Substring?\n", 30 | "A substring is a contiguous sequence of characters within a string. For instance, \"the best of\" is a substring of \"It was the best of times\". This is not to be confused with subsequence, which is a generalization of substring. For example, \"Itwastimes\" is a subsequence of \"It was the best of times\", but not a substring.\n", 31 | "\n", 32 | "Reference-Wikipedia :https://en.wikipedia.org/wiki/Substring\n", 33 | "\n", 34 | "Very Nice Explanation at : https://www.youtube.com/watch?v=L1fUKbgyAk4\n", 35 | "\n" 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": {}, 41 | "source": [ 42 | "# Recursive Solution" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 1, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [ 51 | "#Recursive solution\n", 52 | "\n", 53 | "def helperFunction(s1, s2, len_s1, len_s2, count):\n", 54 | "\n", 55 | " #Base case\n", 56 | " if len_s1 is 0 or len_s2 is 0:\n", 57 | " return count\n", 58 | " if s1[len_s1-1] == s2[len_s2-1]:\n", 59 | " count = helperFunction(s1, s2, len_s1-1, len_s2-1,count+1)\n", 60 | " count = max(count, \n", 61 | " max(helperFunction(s1, s2, len_s1, len_s2 - 1, 0), \n", 62 | " helperFunction(s1, s2, len_s1 - 1, len_s2, 0)));\n", 63 | "\n", 64 | " return count\n", 65 | "\n", 66 | "def getLengthOfLongestCommonSubString(s1, s2):\n", 67 | " return helperFunction(s1,s2, len(s1), len(s2),0)\n", 68 | " \n", 69 | " " 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 2, 75 | "metadata": {}, 76 | "outputs": [ 77 | { 78 | "name": "stdout", 79 | "output_type": "stream", 80 | "text": [ 81 | "4\n" 82 | ] 83 | } 84 | ], 85 | "source": [ 86 | "#Test case:1\n", 87 | "print(getLengthOfLongestCommonSubString('ababc', 'babca'))" 88 | ] 89 | }, 90 | { 91 | "cell_type": "markdown", 92 | "metadata": {}, 93 | "source": [ 94 | "# Memoisation" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 7, 100 | "metadata": {}, 101 | "outputs": [], 102 | "source": [ 103 | "# Memoisation\n", 104 | "def buildTable(s1, s2):\n", 105 | " len_s1 = len(s1)\n", 106 | " len_s2 = len(s2)\n", 107 | " # initialising all cells with zeros\n", 108 | " result = [[0 for i in range(len_s2+1)] for j in range(len_s1+1)]\n", 109 | " max_value = 0\n", 110 | " for i in range(1, len_s1+1):\n", 111 | " for j in range(1, len_s2+1):\n", 112 | " result[i][j] = 1 + result[i-1][j-1] if s1[i-1] == s2[j-1] else 0\n", 113 | " max_value = max(max_value, result[i][j])\n", 114 | " return result,max_value" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 12, 120 | "metadata": {}, 121 | "outputs": [], 122 | "source": [ 123 | "def getLengthOfLongestCommonSubString_m(s1, s2):\n", 124 | " result,max_value = buildTable(s1, s2)\n", 125 | " return max_value" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 17, 131 | "metadata": {}, 132 | "outputs": [ 133 | { 134 | "name": "stdout", 135 | "output_type": "stream", 136 | "text": [ 137 | "babc\n", 138 | "babc\n" 139 | ] 140 | } 141 | ], 142 | "source": [ 143 | "#Test case:1\n", 144 | "s1 = 'ababc'\n", 145 | "s2 = 'babca'\n", 146 | "max_len = getLengthOfLongestCommonSubString_m(s1,s2)\n", 147 | "# Longest Substring\n", 148 | "print(s2[0:max_len])\n", 149 | "print(s1[-max_len:])" 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.4" 170 | } 171 | }, 172 | "nbformat": 4, 173 | "nbformat_minor": 2 174 | } 175 | -------------------------------------------------------------------------------- /DP-4.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Finding Longest Palindromic Subsequence" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "For all Dynamic Programming Problems, coming up with the subproblem is kind of difficult.So, one tip here is to think of the brute force approach and see if that gives a hint to the subproblem.\n", 15 | "\n", 16 | "So, the brute force approach here is to list all the possible subsequences and then filter the palindromes and then find the one with greatest length." 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "# Brute Force Approach" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 4, 29 | "metadata": {}, 30 | "outputs": [], 31 | "source": [ 32 | "def find_subsequences(s):\n", 33 | " set_s = set()\n", 34 | " # Iterating over entire string\n", 35 | " for i in range(len(s)):\n", 36 | " for j in range(len(s), -1, -1):\n", 37 | " #Substring from i to j\n", 38 | " sub_string = s[i:j]\n", 39 | " if sub_string not in set_s:\n", 40 | " set_s.add(sub_string)\n", 41 | " \n", 42 | " for k in range(1, len(sub_string)):\n", 43 | " temp = sub_string\n", 44 | " new_substring = temp[:k] + temp[(k+1):]\n", 45 | " #print('Substring is {} and new substring after replacing {}th character is: {}'.format(temp, k, new_substring))\n", 46 | " if new_substring not in set_s:\n", 47 | " set_s.add(new_substring)\n", 48 | " \n", 49 | " return set_s\n", 50 | " " 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 3, 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [ 59 | "\n", 60 | "def brute_force_lps(s):\n", 61 | " all_subsequences = [s for s in find_subsequences('agbdba') if len(s.strip()) > 0 ]\n", 62 | " palindromic_seq = []\n", 63 | " is_palindrome = True\n", 64 | " max_len = 0\n", 65 | " for s in all_subsequences:\n", 66 | " for i in range(0, len(s)//2):\n", 67 | " if s[i] != s[len(s)-i-1]:\n", 68 | " is_palindrome = False\n", 69 | " if is_palindrome:\n", 70 | " if len(s) > max_len:\n", 71 | " max_len = len(s)\n", 72 | " palindromic_seq.append(s)\n", 73 | " return max_len, palindromic_seq\n", 74 | " \n", 75 | " " 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 9, 81 | "metadata": {}, 82 | "outputs": [], 83 | "source": [ 84 | "def longestPalindromeSubseq(s):\n", 85 | " len_s = len(s)\n", 86 | " result = [[0 for i in range(len_s)] for j in range(len_s)]\n", 87 | " # Implies starting and ending at the same index\n", 88 | " for i in range(len_s):\n", 89 | " result[i][i] = 1 \n", 90 | " \n", 91 | " for k in range(1, len_s):\n", 92 | " for i in range(0, len_s-k): \n", 93 | " j = i+k\n", 94 | " if s[i] == s[j]:\n", 95 | " result[i][j] = 2 + result[i+1][j-1]\n", 96 | " else:\n", 97 | " result[i][j] = max(result[i+1][j], result[i][j-1])\n", 98 | " \n", 99 | " return result" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 37, 105 | "metadata": {}, 106 | "outputs": [ 107 | { 108 | "name": "stdout", 109 | "output_type": "stream", 110 | "text": [ 111 | "By Brute Force: Length is 0\n", 112 | "By Dynamic Programming: Length is 5\n" 113 | ] 114 | } 115 | ], 116 | "source": [ 117 | "s = 'agbdba'\n", 118 | "\n", 119 | "# Using Brute force search\n", 120 | "max_len, p_seq = brute_force_lps(s)\n", 121 | "print('By Brute Force: Length is {}'.format(max_len))\n", 122 | "# Using Dynamic Programming\n", 123 | "result = longestPalindromeSubseq(s)\n", 124 | "max_len = result[0][len(s)-1]\n", 125 | "print('By Dynamic Programming: Length is {}'.format(max_len))" 126 | ] 127 | }, 128 | { 129 | "cell_type": "markdown", 130 | "metadata": {}, 131 | "source": [ 132 | "# Using Code from DP1_ DP2 to find all longest common palindromic subsequences" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 3, 138 | "metadata": {}, 139 | "outputs": [], 140 | "source": [ 141 | "# Memoisation\n", 142 | "def buildTable(s1, s2):\n", 143 | " len_s1 = len(s1)\n", 144 | " len_s2 = len(s2)\n", 145 | " # initialising all cells with zeros\n", 146 | " result = [[0 for i in range(len_s2+1)] for j in range(len_s1+1)]\n", 147 | " for i in range(1, len_s1+1):\n", 148 | " for j in range(1, len_s2+1):\n", 149 | " result[i][j] = (1 + result[i-1][j-1]) if s1[i-1] == s2[j-1] else max(result[i][j-1], result[i-1][j])\n", 150 | " return result\n", 151 | "\n", 152 | "#Reference and explanation: https://www.techiedelight.com/longest-common-subsequence-finding-lcs/\n", 153 | "def helperFunction(s1, s2, result, m, n):\n", 154 | " if m is 0 or n is 0:\n", 155 | " return set([''])\n", 156 | " elif s1[m-1] == s2[n-1]:\n", 157 | " all_sequences = helperFunction(s1, s2, result, m-1, n-1)\n", 158 | " all_sequences = set(a+s1[m-1] for a in all_sequences )\n", 159 | " return all_sequences\n", 160 | " else:\n", 161 | " if result[m-1][n] > result[m][n-1]:\n", 162 | " return helperFunction(s1, s2, result, m-1,n)\n", 163 | " elif result[m][n-1] > result[m-1][n]:\n", 164 | " return helperFunction(s1, s2, result, m,n-1)\n", 165 | " top_cell_sequences = helperFunction(s1, s2, result, m-1,n)\n", 166 | " left_cell_sequences = helperFunction(s1, s2, result, m,n-1)\n", 167 | " return top_cell_sequences.union(left_cell_sequences)\n", 168 | " \n", 169 | "def getAllLongestCommonSubsequences(s1, s2):\n", 170 | " result = buildTable(s1, s2)\n", 171 | " sequences = helperFunction(s1, s2, result, len(s1) ,len(s2))\n", 172 | " return sequences\n", 173 | "\n", 174 | "def print_helper_function(s1, s2):\n", 175 | " print('Length of Longest Common Subsequence: {} and Sequences are : {}'.\n", 176 | " format(getLengthOfLongestCommonSubSequence_m(s1,s2),getAllLongestCommonSubsequences(s1, s2)))" 177 | ] 178 | }, 179 | { 180 | "cell_type": "code", 181 | "execution_count": 1, 182 | "metadata": {}, 183 | "outputs": [], 184 | "source": [ 185 | "def reverse_string(s):\n", 186 | " reverse_s = s[len(s)::-1] \n", 187 | " return reverse_s" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": 4, 193 | "metadata": {}, 194 | "outputs": [ 195 | { 196 | "data": { 197 | "text/plain": [ 198 | "{'BABCBAB', 'BACBCAB'}" 199 | ] 200 | }, 201 | "execution_count": 4, 202 | "metadata": {}, 203 | "output_type": "execute_result" 204 | } 205 | ], 206 | "source": [ 207 | "# Idea from: https://www.techiedelight.com/longest-palindromic-subsequence-using-dynamic-programming/\n", 208 | "s = 'BBABCBCAB'\n", 209 | "getAllLongestCommonSubsequences(s, reverse_string(s))" 210 | ] 211 | } 212 | ], 213 | "metadata": { 214 | "kernelspec": { 215 | "display_name": "Python 3", 216 | "language": "python", 217 | "name": "python3" 218 | }, 219 | "language_info": { 220 | "codemirror_mode": { 221 | "name": "ipython", 222 | "version": 3 223 | }, 224 | "file_extension": ".py", 225 | "mimetype": "text/x-python", 226 | "name": "python", 227 | "nbconvert_exporter": "python", 228 | "pygments_lexer": "ipython3", 229 | "version": "3.7.4" 230 | } 231 | }, 232 | "nbformat": 4, 233 | "nbformat_minor": 2 234 | } 235 | -------------------------------------------------------------------------------- /DP-5.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Longest Repeated Subsequence" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 6, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "# Memoisation\n", 17 | "def buildTable(s1, s2):\n", 18 | " len_s1 = len(s1)\n", 19 | " len_s2 = len(s2)\n", 20 | " # initialising all cells with zeros\n", 21 | " result = [[0 for i in range(len_s2+1)] for j in range(len_s1+1)]\n", 22 | " for i in range(1, len_s1+1):\n", 23 | " for j in range(1, len_s2+1):\n", 24 | " result[i][j] = (1 + result[i-1][j-1]) if s1[i-1] == s2[j-1] and i != j \\\n", 25 | " else max(result[i][j-1], result[i-1][j])\n", 26 | " return result" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 13, 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [ 35 | "#Reference and explanation: https://www.techiedelight.com/longest-common-subsequence-finding-lcs/\n", 36 | "def helperFunction(s1, s2, result, m, n):\n", 37 | " if m is 0 or n is 0:\n", 38 | " return set([''])\n", 39 | " elif s1[m-1] == s2[n-1] and m != n:\n", 40 | " all_sequences = helperFunction(s1, s2, result, m-1, n-1)\n", 41 | " all_sequences = set(a+s1[m-1] for a in all_sequences )\n", 42 | " return all_sequences\n", 43 | " else:\n", 44 | " if result[m-1][n] > result[m][n-1]:\n", 45 | " return helperFunction(s1, s2, result, m-1,n)\n", 46 | " elif result[m][n-1] > result[m-1][n]:\n", 47 | " return helperFunction(s1, s2, result, m,n-1)\n", 48 | " top_cell_sequences = helperFunction(s1, s2, result, m-1,n)\n", 49 | " left_cell_sequences = helperFunction(s1, s2, result, m,n-1)\n", 50 | " return top_cell_sequences.union(left_cell_sequences)" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 10, 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [ 59 | "def getAllLongestCommonSubsequences(s1, s2):\n", 60 | " result = buildTable(s1, s2)\n", 61 | " sequences = helperFunction(s1, s2, result, len(s1) ,len(s2))\n", 62 | " return sequences" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 11, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [ 71 | "def print_helper_function(s1, s2):\n", 72 | " print('Length of Longest Common Subsequence: {} and Sequences are : {}'.\n", 73 | " format(getLengthOfLongestCommonSubSequence_m(s1,s2),getAllLongestCommonSubsequences(s1, s2)))" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 14, 79 | "metadata": {}, 80 | "outputs": [ 81 | { 82 | "name": "stdout", 83 | "output_type": "stream", 84 | "text": [ 85 | "Length of Longest Common Subsequence: 4 and Sequences are : {'ATCG'}\n" 86 | ] 87 | } 88 | ], 89 | "source": [ 90 | "s = 'ATACTCGGA'\n", 91 | "print_helper_function(s, s)" 92 | ] 93 | } 94 | ], 95 | "metadata": { 96 | "kernelspec": { 97 | "display_name": "Python 3", 98 | "language": "python", 99 | "name": "python3" 100 | }, 101 | "language_info": { 102 | "codemirror_mode": { 103 | "name": "ipython", 104 | "version": 3 105 | }, 106 | "file_extension": ".py", 107 | "mimetype": "text/x-python", 108 | "name": "python", 109 | "nbconvert_exporter": "python", 110 | "pygments_lexer": "ipython3", 111 | "version": "3.7.4" 112 | } 113 | }, 114 | "nbformat": 4, 115 | "nbformat_minor": 2 116 | } 117 | -------------------------------------------------------------------------------- /DP-6.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Shortest Common Supersequence" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 44, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "def build_lcs_super_table(s1, s2):\n", 17 | " m = len(s1)\n", 18 | " n = len(s2)\n", 19 | " \n", 20 | " result = [[0 for j in range(n+1)] for i in range(m+1)]\n", 21 | " for i in range(m+1):\n", 22 | " result[i][0] = i\n", 23 | " for j in range(n+1):\n", 24 | " result[0][j] = j\n", 25 | " for i in range(1,m+1):\n", 26 | " for j in range(1,n+1):\n", 27 | " if s1[i-1] == s2[j-1]:\n", 28 | " result[i][j] = result[i-1][j-1]+1\n", 29 | " elif s1[i-1] != s2[j-1]:\n", 30 | " result[i][j] = min(result[i][j-1]+1, result[i-1][j]+1)\n", 31 | " return result[m][n]\n", 32 | " " 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 45, 38 | "metadata": {}, 39 | "outputs": [ 40 | { 41 | "data": { 42 | "text/plain": [ 43 | "9" 44 | ] 45 | }, 46 | "execution_count": 45, 47 | "metadata": {}, 48 | "output_type": "execute_result" 49 | } 50 | ], 51 | "source": [ 52 | "build_lcs_super_table('ABCBDAB','BDCABA')" 53 | ] 54 | } 55 | ], 56 | "metadata": { 57 | "kernelspec": { 58 | "display_name": "Python 3", 59 | "language": "python", 60 | "name": "python3" 61 | }, 62 | "language_info": { 63 | "codemirror_mode": { 64 | "name": "ipython", 65 | "version": 3 66 | }, 67 | "file_extension": ".py", 68 | "mimetype": "text/x-python", 69 | "name": "python", 70 | "nbconvert_exporter": "python", 71 | "pygments_lexer": "ipython3", 72 | "version": "3.7.4" 73 | } 74 | }, 75 | "nbformat": 4, 76 | "nbformat_minor": 2 77 | } 78 | -------------------------------------------------------------------------------- /DP-7.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Matrix Chain Multiplication" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 65, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "def matrix_chain_multiplication(dimensions):\n", 17 | " \n", 18 | " # If dimensions is a list having values 10,30, 5, 50\n", 19 | " # this implies, there are matrices of dimensions 10*30, 30*5,5*50\n", 20 | " dim_len = len(dimensions)\n", 21 | " result = [[float('inf') for i in range(dim_len)] for j in range(dim_len)]\n", 22 | " for i in range(dim_len):\n", 23 | " result[i][i] = 0\n", 24 | " for length in range(1,dim_len):\n", 25 | " for i in range(1,dim_len-length):\n", 26 | " j = i+length\n", 27 | " min_val = result[i][j]\n", 28 | " for k in range(i,j):\n", 29 | " current = result[i][k]+ result[k+1][j]+dimensions[i-1]*dimensions[k]*dimensions[j]\n", 30 | " if current < min_val:\n", 31 | " min_val = current\n", 32 | " result[i][j] = min_val\n", 33 | " return result\n", 34 | " \n", 35 | " \n", 36 | " \n", 37 | " " 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 68, 43 | "metadata": {}, 44 | "outputs": [ 45 | { 46 | "name": "stdout", 47 | "output_type": "stream", 48 | "text": [ 49 | "4500\n" 50 | ] 51 | } 52 | ], 53 | "source": [ 54 | "input = [10,30,5,60]\n", 55 | "print(matrix_chain_multiplication(input)[1][len(input)-1])" 56 | ] 57 | } 58 | ], 59 | "metadata": { 60 | "kernelspec": { 61 | "display_name": "Python 3", 62 | "language": "python", 63 | "name": "python3" 64 | }, 65 | "language_info": { 66 | "codemirror_mode": { 67 | "name": "ipython", 68 | "version": 3 69 | }, 70 | "file_extension": ".py", 71 | "mimetype": "text/x-python", 72 | "name": "python", 73 | "nbconvert_exporter": "python", 74 | "pygments_lexer": "ipython3", 75 | "version": "3.7.4" 76 | } 77 | }, 78 | "nbformat": 4, 79 | "nbformat_minor": 2 80 | } 81 | -------------------------------------------------------------------------------- /DP-8.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Finding minimum cost to reach last cell of a matrix from first cell" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 33, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "def find_min_cost(list_input):\n", 17 | " result = list_input\n", 18 | " rows = len(list_input)\n", 19 | " columns = len(list_input[0])\n", 20 | " for i in range(rows):\n", 21 | " for j in range(columns):\n", 22 | " if i == 0 and j > 0:\n", 23 | " result[0][j] += list_input[0][j-1]\n", 24 | " elif j == 0 and i > 0:\n", 25 | " result[i][0] += list_input[i-1][0]\n", 26 | " elif i > 0 and j > 0:\n", 27 | " result[i][j] += min(result[i-1][j], result[i][j-1])\n", 28 | " return result\n", 29 | " \n", 30 | " " 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 39, 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "name": "stdout", 40 | "output_type": "stream", 41 | "text": [ 42 | "36\n" 43 | ] 44 | } 45 | ], 46 | "source": [ 47 | "list_input = [[4,7,8,6,4],[6,7,3,9,2],[3,8,1,2,4],[7,1,7,3,7],[2,9,8,9,3]]\n", 48 | "result = find_min_cost(list_input)\n", 49 | "rows = len(list_input)\n", 50 | "columns = len(list_input[0])\n", 51 | "print(result[rows-1][columns-1])" 52 | ] 53 | } 54 | ], 55 | "metadata": { 56 | "kernelspec": { 57 | "display_name": "Python 3", 58 | "language": "python", 59 | "name": "python3" 60 | }, 61 | "language_info": { 62 | "codemirror_mode": { 63 | "name": "ipython", 64 | "version": 3 65 | }, 66 | "file_extension": ".py", 67 | "mimetype": "text/x-python", 68 | "name": "python", 69 | "nbconvert_exporter": "python", 70 | "pygments_lexer": "ipython3", 71 | "version": "3.7.4" 72 | } 73 | }, 74 | "nbformat": 4, 75 | "nbformat_minor": 2 76 | } 77 | -------------------------------------------------------------------------------- /DP-9.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Wildcard Pattern matching" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 35, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "def check_pattern_in_string(input_s, pattern):\n", 17 | " pattern_len = len(pattern)\n", 18 | " input_len = len(input_s)\n", 19 | " result = [[False for i in range(pattern_len+1)] for j in range(input_len+1)]\n", 20 | " result[0][0] = True\n", 21 | " for j in range(pattern_len+1):\n", 22 | " if pattern[j-1] == '*':\n", 23 | " result[0][j] = result[0][j-1]\n", 24 | " for i in range(1,len(input_s)+1):\n", 25 | " for j in range(1,pattern_len+1):\n", 26 | " if input_s[i-1] == pattern[j-1]:\n", 27 | " result[i][j] = result[i-1][j-1]\n", 28 | " elif pattern[j-1] == '*':\n", 29 | " result[i][j] = result[i-1][j] or result[i][j-1]\n", 30 | " elif pattern[j-1] == '?':\n", 31 | " result[i][j] = result[i-1][j-1]\n", 32 | " return result[input_len][pattern_len]\n", 33 | " \n", 34 | " " 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 36, 40 | "metadata": {}, 41 | "outputs": [ 42 | { 43 | "data": { 44 | "text/plain": [ 45 | "False" 46 | ] 47 | }, 48 | "execution_count": 36, 49 | "metadata": {}, 50 | "output_type": "execute_result" 51 | } 52 | ], 53 | "source": [ 54 | "check_pattern_in_string('xyxzzxy','x***x')" 55 | ] 56 | } 57 | ], 58 | "metadata": { 59 | "kernelspec": { 60 | "display_name": "Python 3", 61 | "language": "python", 62 | "name": "python3" 63 | }, 64 | "language_info": { 65 | "codemirror_mode": { 66 | "name": "ipython", 67 | "version": 3 68 | }, 69 | "file_extension": ".py", 70 | "mimetype": "text/x-python", 71 | "name": "python", 72 | "nbconvert_exporter": "python", 73 | "pygments_lexer": "ipython3", 74 | "version": "3.7.4" 75 | } 76 | }, 77 | "nbformat": 4, 78 | "nbformat_minor": 2 79 | } 80 | -------------------------------------------------------------------------------- /Day 18.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Snake Sequence Problem" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 6, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "def find_max_len_snakesequence(input_grid):\n", 17 | " rows = len(input_grid)\n", 18 | " columns = len(input_grid[0])\n", 19 | " result = [[1 for i in range(columns)] for j in range(rows)]\n", 20 | " len_max = 1\n", 21 | " for i in range(rows):\n", 22 | " for j in range(columns):\n", 23 | " if i != 0 or j != 0:\n", 24 | " if i > 0 and abs(input_grid[i][j]- input_grid[i-1][j]):\n", 25 | " result[i][j] = max(result[i][j], result[i-1][j]+1)\n", 26 | " \n", 27 | " if len_max < result[i][j]:\n", 28 | " len_max = result[i][j]\n", 29 | " \n", 30 | " if j > 0 and abs(input_grid[i][j]- input_grid[i][j-1]):\n", 31 | " result[i][j] = max(result[i][j], result[i][j-1]+1)\n", 32 | " \n", 33 | " if len_max < result[i][j]:\n", 34 | " len_max = result[i][j]\n", 35 | " return len_max " 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 7, 41 | "metadata": {}, 42 | "outputs": [ 43 | { 44 | "data": { 45 | "text/plain": [ 46 | "7" 47 | ] 48 | }, 49 | "execution_count": 7, 50 | "metadata": {}, 51 | "output_type": "execute_result" 52 | } 53 | ], 54 | "source": [ 55 | "input_grid = [[1,2,1,2],[7,7,2,5],[6,4,3,4],[1,2,2,5]]\n", 56 | "find_max_len_snakesequence(input_grid)" 57 | ] 58 | } 59 | ], 60 | "metadata": { 61 | "kernelspec": { 62 | "display_name": "Python 3", 63 | "language": "python", 64 | "name": "python3" 65 | }, 66 | "language_info": { 67 | "codemirror_mode": { 68 | "name": "ipython", 69 | "version": 3 70 | }, 71 | "file_extension": ".py", 72 | "mimetype": "text/x-python", 73 | "name": "python", 74 | "nbconvert_exporter": "python", 75 | "pygments_lexer": "ipython3", 76 | "version": "3.7.4" 77 | } 78 | }, 79 | "nbformat": 4, 80 | "nbformat_minor": 2 81 | } 82 | -------------------------------------------------------------------------------- /Day 22.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "def max_product_rodcutting(length):\n", 10 | " \n", 11 | " result = [0 for i in range(length+1)]\n", 12 | " result[0] = 0\n", 13 | " result[1] = 0\n", 14 | " for i in range(1, length+1):\n", 15 | " max_length = 0\n", 16 | " for j in range(1, int((length+1)/2)):\n", 17 | " max_length = max(max_length,(i-j)*j, j*result[i-j])\n", 18 | " result[i] = max_length\n", 19 | " return result" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 9, 25 | "metadata": {}, 26 | "outputs": [ 27 | { 28 | "name": "stdout", 29 | "output_type": "stream", 30 | "text": [ 31 | "[0, 0, 1, 2, 4, 6, 9, 12, 18, 27, 36]\n", 32 | "Maximum obtainable product: 36\n" 33 | ] 34 | } 35 | ], 36 | "source": [ 37 | "length = 10\n", 38 | "table = max_product_rodcutting(length)\n", 39 | "print(table)\n", 40 | "print(\"Maximum obtainable product: \"+ str(table[length]))" 41 | ] 42 | } 43 | ], 44 | "metadata": { 45 | "kernelspec": { 46 | "display_name": "Python 3", 47 | "language": "python", 48 | "name": "python3" 49 | }, 50 | "language_info": { 51 | "codemirror_mode": { 52 | "name": "ipython", 53 | "version": 3 54 | }, 55 | "file_extension": ".py", 56 | "mimetype": "text/x-python", 57 | "name": "python", 58 | "nbconvert_exporter": "python", 59 | "pygments_lexer": "ipython3", 60 | "version": "3.7.4" 61 | } 62 | }, 63 | "nbformat": 4, 64 | "nbformat_minor": 4 65 | } 66 | -------------------------------------------------------------------------------- /Day 23.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Longest Bitonic Sequence" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 16, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "import numpy as np\n", 17 | "def longest_bitonic_seq(input_array):\n", 18 | " ar_len = len(input_array)\n", 19 | " # Initialise two arrays\n", 20 | " inc_fromleft = [1 for i in range(ar_len)]\n", 21 | " inc_fromright = [1 for j in range(ar_len)]\n", 22 | " \n", 23 | " for i in range(1,ar_len):\n", 24 | " for j in range(i):\n", 25 | " if input_array[i] > input_array[j]:\n", 26 | " inc_fromleft[i] = max(inc_fromleft[i],inc_fromleft[j]+1)\n", 27 | " \n", 28 | " for i in range(ar_len-2,-1,-1):\n", 29 | " for j in range(ar_len-1,i,-1):\n", 30 | " if input_array[i] > input_array[j]:\n", 31 | " inc_fromright[i] = max(inc_fromright[i],inc_fromright[j]+1)\n", 32 | " \n", 33 | " # Adding two arrays, subtracting 1 from them and getting the max length\n", 34 | " return (np.max(np.add(inc_fromleft, inc_fromright)-1))\n", 35 | " " 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 17, 41 | "metadata": {}, 42 | "outputs": [ 43 | { 44 | "data": { 45 | "text/plain": [ 46 | "5" 47 | ] 48 | }, 49 | "execution_count": 17, 50 | "metadata": {}, 51 | "output_type": "execute_result" 52 | } 53 | ], 54 | "source": [ 55 | "longest_bitonic_seq([2,-1,4,3,5,-1,3,2])" 56 | ] 57 | } 58 | ], 59 | "metadata": { 60 | "kernelspec": { 61 | "display_name": "Python 3", 62 | "language": "python", 63 | "name": "python3" 64 | }, 65 | "language_info": { 66 | "codemirror_mode": { 67 | "name": "ipython", 68 | "version": 3 69 | }, 70 | "file_extension": ".py", 71 | "mimetype": "text/x-python", 72 | "name": "python", 73 | "nbconvert_exporter": "python", 74 | "pygments_lexer": "ipython3", 75 | "version": "3.7.4" 76 | } 77 | }, 78 | "nbformat": 4, 79 | "nbformat_minor": 4 80 | } 81 | -------------------------------------------------------------------------------- /Day 24.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Increasing subsequence with Maximum Sum" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 22, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "def find_max_increasing_sum(in_list):\n", 17 | " result = [0 for _ in range(len(in_list))]\n", 18 | " \n", 19 | " result[0] = in_list[0]\n", 20 | " for i in range(1, len(in_list)):\n", 21 | " for j in range(0, i):\n", 22 | " if result[i] < result[j] and in_list[i] > in_list[j]:\n", 23 | " result[i] = result[j]\n", 24 | " result[i] += in_list[i]\n", 25 | " return result" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 23, 31 | "metadata": {}, 32 | "outputs": [ 33 | { 34 | "name": "stdout", 35 | "output_type": "stream", 36 | "text": [ 37 | "[8, 4, 20, 2, 18, 10, 34, 1, 19, 9, 33, 5, 30]\n", 38 | "Max increasing sum : 34\n" 39 | ] 40 | } 41 | ], 42 | "source": [ 43 | "import numpy as np\n", 44 | "result = find_max_increasing_sum([8,4,12,2,10,6,14,1,9,5,13,3,11])\n", 45 | "print(result)\n", 46 | "print('Max increasing sum : {}'.format(np.max(result) ))" 47 | ] 48 | } 49 | ], 50 | "metadata": { 51 | "kernelspec": { 52 | "display_name": "Python 3", 53 | "language": "python", 54 | "name": "python3" 55 | }, 56 | "language_info": { 57 | "codemirror_mode": { 58 | "name": "ipython", 59 | "version": 3 60 | }, 61 | "file_extension": ".py", 62 | "mimetype": "text/x-python", 63 | "name": "python", 64 | "nbconvert_exporter": "python", 65 | "pygments_lexer": "ipython3", 66 | "version": "3.7.4" 67 | } 68 | }, 69 | "nbformat": 4, 70 | "nbformat_minor": 4 71 | } 72 | -------------------------------------------------------------------------------- /Day 25.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Maximum Sum Sub Square matrix" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 24, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "import numpy as np\n", 17 | "def get_max_sum_subsquare_matrix(input_matrix):\n", 18 | " \n", 19 | " rows = len(input_matrix)\n", 20 | " columns = len(input_matrix[0])\n", 21 | " result = [[0 for _ in range(columns+1)] for _ in range(rows+1)]\n", 22 | " for i in range(1,rows+1):\n", 23 | " for j in range(1,columns+1):\n", 24 | " if input_matrix[i-1][j-1] == 1:\n", 25 | " result[i][j] = 1 + min(result[i-1][j], result[i-1][j-1], result[i][j-1])\n", 26 | " \n", 27 | " return np.max(result)" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 25, 33 | "metadata": {}, 34 | "outputs": [ 35 | { 36 | "data": { 37 | "text/plain": [ 38 | "3" 39 | ] 40 | }, 41 | "execution_count": 25, 42 | "metadata": {}, 43 | "output_type": "execute_result" 44 | } 45 | ], 46 | "source": [ 47 | "input_matrix = [[0,0,1,0,1,1],[0,1,1,1,0,0],[0,0,1,1,1,1],[1,1,0,1,1,1],[1,1,1,1,1,1],[1,1,0,1,1,1],[1,0,1,1,1,1],[1,1,1,0,1,1]]\n", 48 | "get_max_sum_subsquare_matrix(input_matrix)" 49 | ] 50 | } 51 | ], 52 | "metadata": { 53 | "kernelspec": { 54 | "display_name": "Python 3", 55 | "language": "python", 56 | "name": "python3" 57 | }, 58 | "language_info": { 59 | "codemirror_mode": { 60 | "name": "ipython", 61 | "version": 3 62 | }, 63 | "file_extension": ".py", 64 | "mimetype": "text/x-python", 65 | "name": "python", 66 | "nbconvert_exporter": "python", 67 | "pygments_lexer": "ipython3", 68 | "version": "3.7.3" 69 | } 70 | }, 71 | "nbformat": 4, 72 | "nbformat_minor": 4 73 | } 74 | -------------------------------------------------------------------------------- /Day 26.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Collect maximum points in a matrix by satisfying given constriants" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 13, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "def get_max_points(input_matrix):\n", 17 | " rows = len(input_matrix)\n", 18 | " columns = len(input_matrix[0])\n", 19 | " result = [[0 for _ in range(columns+1)] for _ in range(rows+1)]\n", 20 | " for i in range(1,rows+1):\n", 21 | " if (i & 1 == 1):\n", 22 | " for j in range(1,columns+1):\n", 23 | " if input_matrix[i-1][j-1] != -1:\n", 24 | " result[i][j] = input_matrix[i-1][j-1] + max(result[i][j-1],result[i-1][j])\n", 25 | " else:\n", 26 | " for j in range(columns-1,0,-1):\n", 27 | " if input_matrix[i-1][j-1] != -1:\n", 28 | " result[i][j] = input_matrix[i-1][j-1] + max(result[i][j+1],result[i-1][j])\n", 29 | " return result" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 16, 35 | "metadata": {}, 36 | "outputs": [ 37 | { 38 | "name": "stdout", 39 | "output_type": "stream", 40 | "text": [ 41 | "[0, 0, 0, 0, 0, 0]\n", 42 | "[0, 1, 2, 0, 1, 2]\n", 43 | "[0, 3, 2, 0, 0, 0]\n", 44 | "[0, 4, 5, 6, 7, 0]\n", 45 | "[0, 0, 0, 9, 8, 0]\n", 46 | "[0, 1, 2, 0, 0, 1]\n", 47 | "9\n" 48 | ] 49 | } 50 | ], 51 | "source": [ 52 | "import numpy as np\n", 53 | "input_matrix = [[1,1,-1,1,1],[1,0,0,-1,1],[1,1,1,1,-1],[-1,-1,1,1,1],[1,1,-1,-1,1]]\n", 54 | "result = get_max_points(input_matrix)\n", 55 | "for r in result:\n", 56 | " print(r)\n", 57 | "print(np.max(result))" 58 | ] 59 | } 60 | ], 61 | "metadata": { 62 | "kernelspec": { 63 | "display_name": "Python 3", 64 | "language": "python", 65 | "name": "python3" 66 | }, 67 | "language_info": { 68 | "codemirror_mode": { 69 | "name": "ipython", 70 | "version": 3 71 | }, 72 | "file_extension": ".py", 73 | "mimetype": "text/x-python", 74 | "name": "python", 75 | "nbconvert_exporter": "python", 76 | "pygments_lexer": "ipython3", 77 | "version": "3.7.3" 78 | } 79 | }, 80 | "nbformat": 4, 81 | "nbformat_minor": 4 82 | } 83 | -------------------------------------------------------------------------------- /Day 27.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Counting Number of times a pattern occurs in a string" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 21, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "def get_pattern_count(input_string, pattern):\n", 17 | " \n", 18 | " result = [[0 for i in range(len(pattern)+1)] for j in range(len(input_string)+1)]\n", 19 | " \n", 20 | " for i in range(len(pattern)+1):\n", 21 | " result[i][0] = 1\n", 22 | " rows = len(input_string)\n", 23 | " columns = len(pattern)\n", 24 | " for i in range(1,rows+1):\n", 25 | " for j in range(1,columns+1):\n", 26 | " if input_string[i-1] == pattern[j-1]:\n", 27 | " result[i][j] = result[i-1][j-1] + result[i-1][j]\n", 28 | " else:\n", 29 | " result[i][j] = result[i-1][j]\n", 30 | " return result" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 23, 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "name": "stdout", 40 | "output_type": "stream", 41 | "text": [ 42 | "Count: 7\n" 43 | ] 44 | } 45 | ], 46 | "source": [ 47 | "import numpy as np\n", 48 | "input_string = 'subsequence'\n", 49 | "pattern = 'sue'\n", 50 | "result = get_pattern_count(input_string, pattern)\n", 51 | "print('Count: {}'.format(np.max(result)))" 52 | ] 53 | } 54 | ], 55 | "metadata": { 56 | "kernelspec": { 57 | "display_name": "Python 3", 58 | "language": "python", 59 | "name": "python3" 60 | }, 61 | "language_info": { 62 | "codemirror_mode": { 63 | "name": "ipython", 64 | "version": 3 65 | }, 66 | "file_extension": ".py", 67 | "mimetype": "text/x-python", 68 | "name": "python", 69 | "nbconvert_exporter": "python", 70 | "pygments_lexer": "ipython3", 71 | "version": "3.7.3" 72 | } 73 | }, 74 | "nbformat": 4, 75 | "nbformat_minor": 4 76 | } 77 | -------------------------------------------------------------------------------- /Day 28.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Calculate Sum of all elements in a Sub-matrix in constant time" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "def calculate_sum(input_matrix, p, q, r, s):\n", 17 | " rows = len(input_matrix)\n", 18 | " columns = len(input_matrix[0])\n", 19 | " aux_matrix = [[0 for i in range(columns)] for j in range(rows)]\n", 20 | " \n", 21 | " #update first element - sum till the first cell of input matrix is itself\n", 22 | " aux_matrix[0][0] = input_matrix[0][0]\n", 23 | " #updating first row\n", 24 | " for j in range(1, columns):\n", 25 | " aux_matrix[0][j] = input_matrix[0][j] + aux_matrix[0][j-1]\n", 26 | " \n", 27 | " #updating first column\n", 28 | " for i in range(1, rows):\n", 29 | " aux_matrix[i][0] = input_matrix[i][0] + aux_matrix[i-1][0]\n", 30 | " \n", 31 | " # for rest of matrix\n", 32 | " for i in range(1, rows):\n", 33 | " for j in range(1, columns):\n", 34 | " aux_matrix[i][j] = input_matrix[i][j] + aux_matrix[i-1][j] + aux_matrix[i][j-1] - aux_matrix[i-1][j-1]\n", 35 | " \n", 36 | " sum_val = aux_matrix[r][s]\n", 37 | " # subtracting a part of matrix in the left\n", 38 | " if q-1 >= 0:\n", 39 | " sum_val = sum_val - aux_matrix[r][q-1]\n", 40 | " if p-1 >= 0:\n", 41 | " sum_val = sum_val - aux_matrix[p-1][s]\n", 42 | " if p-1 >= 0 and q-1 >= 0:\n", 43 | " sum_val = sum_val + aux_matrix[p-1][q-1]\n", 44 | " \n", 45 | " return sum_val\n", 46 | " \n", 47 | " \n", 48 | " " 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 2, 54 | "metadata": {}, 55 | "outputs": [ 56 | { 57 | "data": { 58 | "text/plain": [ 59 | "38" 60 | ] 61 | }, 62 | "execution_count": 2, 63 | "metadata": {}, 64 | "output_type": "execute_result" 65 | } 66 | ], 67 | "source": [ 68 | "input_matrix = [[0,2,5,4,1],[4,8,2,3,7],[6,3,4,6,2],[7,3,1,8,3],[1,5,7,9,4]]\n", 69 | "calculate_sum(input_matrix,1,1,3,3)" 70 | ] 71 | } 72 | ], 73 | "metadata": { 74 | "kernelspec": { 75 | "display_name": "Python 3", 76 | "language": "python", 77 | "name": "python3" 78 | }, 79 | "language_info": { 80 | "codemirror_mode": { 81 | "name": "ipython", 82 | "version": 3 83 | }, 84 | "file_extension": ".py", 85 | "mimetype": "text/x-python", 86 | "name": "python", 87 | "nbconvert_exporter": "python", 88 | "pygments_lexer": "ipython3", 89 | "version": "3.7.3" 90 | } 91 | }, 92 | "nbformat": 4, 93 | "nbformat_minor": 4 94 | } 95 | -------------------------------------------------------------------------------- /Day 29.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Find the size of largest plus of 1s in Binary Matrix" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 51, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "from iteration_utilities import deepflatten\n", 17 | "def find_largest_plus(input_matrix):\n", 18 | " rows = len(input_matrix)\n", 19 | " columns = len(input_matrix[0])\n", 20 | " \n", 21 | " left = [[0 for i in range(columns)] for j in range(rows)]\n", 22 | " right = [[0 for i in range(columns)] for j in range(rows)]\n", 23 | " bottom = [[0 for i in range(columns)] for j in range(rows)]\n", 24 | " top = [[0 for i in range(columns)] for j in range(rows)]\n", 25 | " #Initialisation\n", 26 | " for i in range(columns):\n", 27 | " # first row\n", 28 | " top[0][i] = input_matrix[0][i]\n", 29 | " # last row\n", 30 | " bottom[rows-1][i] = input_matrix[rows-1][i]\n", 31 | " # first column\n", 32 | " left[i][0] = input_matrix[i][0]\n", 33 | " # Last column\n", 34 | " right[i][rows-1] = input_matrix[i][rows-1]\n", 35 | " # Filling all cells of the matrix\n", 36 | " for i in range(rows):\n", 37 | " for j in range(1,columns):\n", 38 | " if input_matrix[i][j] == 1:\n", 39 | " # Look at previous column\n", 40 | " left[i][j] = left[i][j-1] + 1\n", 41 | " \n", 42 | " if input_matrix[j][i] == 1:\n", 43 | " # Look at previous row\n", 44 | " top[j][i] = top[j-1][i] + 1\n", 45 | " \n", 46 | " if input_matrix[rows-1-j][i] == 1:\n", 47 | " # Look at the previous row from bottom\n", 48 | " bottom[rows-1-j][i] = bottom[rows-j][i] + 1\n", 49 | " \n", 50 | " if input_matrix[i][columns-1-j] == 1:\n", 51 | " right[i][columns-1-j] = right[i][columns-j] + 1\n", 52 | " \n", 53 | " res = max([min(idx) for idx in zip(list(deepflatten(left)), list(deepflatten(right)) , list(deepflatten(bottom)), list(deepflatten(bottom)))])\n", 54 | " return res" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 55, 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [ 63 | "input_matrix = [[ 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 ],\n", 64 | " [ 1, 0, 1, 0, 1, 1, 1, 0, 1, 1 ],\n", 65 | " [ 1, 1, 1, 0, 1, 1, 0, 1, 0, 1 ],\n", 66 | " [ 0, 0, 0, 0, 1, 0, 0, 1, 0, 0 ],\n", 67 | " [ 1, 1, 1, 0, 1, 1, 1, 1, 1, 1 ],\n", 68 | " [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 ],\n", 69 | " [ 1, 0, 0, 0, 1, 0, 0, 1, 0, 1 ],\n", 70 | " [ 1, 0, 1, 1, 1, 1, 0, 0, 1, 1 ],\n", 71 | " [ 1, 1, 0, 0, 1, 0, 1, 0, 0, 1 ],\n", 72 | " [ 1, 0, 1, 1, 1, 1, 0, 1, 0, 0 ]]\n", 73 | "result = find_largest_plus(input_matrix)\n", 74 | "final_result = (result-1)*4+1" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 57, 80 | "metadata": {}, 81 | "outputs": [ 82 | { 83 | "name": "stdout", 84 | "output_type": "stream", 85 | "text": [ 86 | "17\n" 87 | ] 88 | } 89 | ], 90 | "source": [ 91 | "print(final_result)" 92 | ] 93 | } 94 | ], 95 | "metadata": { 96 | "kernelspec": { 97 | "display_name": "Python 3", 98 | "language": "python", 99 | "name": "python3" 100 | }, 101 | "language_info": { 102 | "codemirror_mode": { 103 | "name": "ipython", 104 | "version": 3 105 | }, 106 | "file_extension": ".py", 107 | "mimetype": "text/x-python", 108 | "name": "python", 109 | "nbconvert_exporter": "python", 110 | "pygments_lexer": "ipython3", 111 | "version": "3.7.4" 112 | } 113 | }, 114 | "nbformat": 4, 115 | "nbformat_minor": 4 116 | } 117 | -------------------------------------------------------------------------------- /Day 30.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Single Source shortest path using Bellman Ford Algorithm" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 28, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "def single_source_shortest_path(graph, start_node):\n", 17 | " distance = {}\n", 18 | " \n", 19 | " for node in graph:\n", 20 | " distance[node] = float('inf')\n", 21 | " distance[start_node] = 0\n", 22 | " for i in range(len(graph)-1):\n", 23 | " for node in graph:\n", 24 | " for child in graph[node]:\n", 25 | " distance = relax(node, child, graph, distance)\n", 26 | " return distance\n", 27 | "\n", 28 | " \n", 29 | "def relax(node, child, graph, distance):\n", 30 | " if distance[child] > distance[node] + graph[node][child]:\n", 31 | " distance[child] = distance[node] + graph[node][child]\n", 32 | " return distance" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 29, 38 | "metadata": {}, 39 | "outputs": [ 40 | { 41 | "name": "stdout", 42 | "output_type": "stream", 43 | "text": [ 44 | "{'a': 0, 'b': -1, 'c': 2, 'd': -2, 'e': 1}\n" 45 | ] 46 | } 47 | ], 48 | "source": [ 49 | "# Initialising Graph\n", 50 | "graph = {\n", 51 | " 'a':{'b':-1,'c':4},\n", 52 | " 'b':{'c':3,'d':2,'e':2},\n", 53 | " 'c':{},\n", 54 | " 'd':{'c':5,'b':1},\n", 55 | " 'e':{'d':-3}\n", 56 | "}\n", 57 | "distance = single_source_shortest_path(graph,'a')\n", 58 | "print(distance)" 59 | ] 60 | } 61 | ], 62 | "metadata": { 63 | "kernelspec": { 64 | "display_name": "Python 3", 65 | "language": "python", 66 | "name": "python3" 67 | }, 68 | "language_info": { 69 | "codemirror_mode": { 70 | "name": "ipython", 71 | "version": 3 72 | }, 73 | "file_extension": ".py", 74 | "mimetype": "text/x-python", 75 | "name": "python", 76 | "nbconvert_exporter": "python", 77 | "pygments_lexer": "ipython3", 78 | "version": "3.7.4" 79 | } 80 | }, 81 | "nbformat": 4, 82 | "nbformat_minor": 4 83 | } 84 | -------------------------------------------------------------------------------- /Day 31.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Floyd Warshall for all pairs shortest paths" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 10, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "ename": "SyntaxError", 17 | "evalue": "invalid syntax (, line 3)", 18 | "output_type": "error", 19 | "traceback": [ 20 | "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m3\u001b[0m\n\u001b[0;31m distance = [][]\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" 21 | ] 22 | } 23 | ], 24 | "source": [ 25 | "def run_floyd_warshall(graph):\n", 26 | " vertices = graph.keys()\n", 27 | " distance = [[]]\n", 28 | " for v in vertices:\n", 29 | " distance[v][v] = 0\n", 30 | " print(distance)" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 9, 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "ename": "TypeError", 40 | "evalue": "list indices must be integers or slices, not str", 41 | "output_type": "error", 42 | "traceback": [ 43 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 44 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 45 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;34m'e'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m{\u001b[0m\u001b[0;34m'd'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m }\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mdistance\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mrun_floyd_warshall\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgraph\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 46 | "\u001b[0;32m\u001b[0m in \u001b[0;36mrun_floyd_warshall\u001b[0;34m(graph)\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mdistance\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mv\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mvertices\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mdistance\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mv\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mv\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdistance\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 47 | "\u001b[0;31mTypeError\u001b[0m: list indices must be integers or slices, not str" 48 | ] 49 | } 50 | ], 51 | "source": [ 52 | "# Initialising Graph\n", 53 | "graph = {\n", 54 | " 'a':{'b':-1,'c':4},\n", 55 | " 'b':{'c':3,'d':2,'e':2},\n", 56 | " 'c':{},\n", 57 | " 'd':{'c':5,'b':1},\n", 58 | " 'e':{'d':-3}\n", 59 | "}\n", 60 | "distance = run_floyd_warshall(graph)\n" 61 | ] 62 | } 63 | ], 64 | "metadata": { 65 | "kernelspec": { 66 | "display_name": "Python 3", 67 | "language": "python", 68 | "name": "python3" 69 | }, 70 | "language_info": { 71 | "codemirror_mode": { 72 | "name": "ipython", 73 | "version": 3 74 | }, 75 | "file_extension": ".py", 76 | "mimetype": "text/x-python", 77 | "name": "python", 78 | "nbconvert_exporter": "python", 79 | "pygments_lexer": "ipython3", 80 | "version": "3.7.4" 81 | } 82 | }, 83 | "nbformat": 4, 84 | "nbformat_minor": 4 85 | } 86 | -------------------------------------------------------------------------------- /Day 32.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# All pair shortest paths\n" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 40, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "from collections import defaultdict\n", 17 | "def floyd_warshall(graph):\n", 18 | " distance = defaultdict(dict)\n", 19 | " vertices = graph.keys()\n", 20 | " for v in vertices:\n", 21 | " # Initialising distance from vertex to any other vertex as infinity\n", 22 | " for u in vertices:\n", 23 | " distance[v][u] = float('inf')\n", 24 | " # For neighbors initialising the distance as actual distance between two nodes\n", 25 | " for n in graph[v]:\n", 26 | " distance[v][n] = graph[v][n]\n", 27 | " # Initialising distance from each node to itself as 0\n", 28 | " for v in vertices:\n", 29 | " distance[v][v] = 0\n", 30 | " for k in vertices:\n", 31 | " for i in vertices:\n", 32 | " for j in vertices:\n", 33 | " if distance[i][k] + distance[k][j] < distance[i][j]:\n", 34 | " distance[i][j] = distance[i][k] + distance[k][j]\n", 35 | " return distance " 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 41, 41 | "metadata": {}, 42 | "outputs": [ 43 | { 44 | "name": "stdout", 45 | "output_type": "stream", 46 | "text": [ 47 | "0 {0: 0, 1: -1, 2: -2, 3: 0}\n", 48 | "1 {0: 4, 1: 0, 2: 2, 3: 4}\n", 49 | "2 {0: 5, 1: 1, 2: 0, 3: 2}\n", 50 | "3 {0: 3, 1: -1, 2: 1, 3: 0}\n" 51 | ] 52 | } 53 | ], 54 | "source": [ 55 | "graph = {0: {2: -2},\n", 56 | " 1: {0:4, 2:3},\n", 57 | " 2: {3:2},\n", 58 | " 3: {1: -1}}\n", 59 | "distance = floyd_warshall(graph)\n", 60 | "for d in distance:\n", 61 | " print(d, distance[d])" 62 | ] 63 | } 64 | ], 65 | "metadata": { 66 | "kernelspec": { 67 | "display_name": "Python 3", 68 | "language": "python", 69 | "name": "python3" 70 | }, 71 | "language_info": { 72 | "codemirror_mode": { 73 | "name": "ipython", 74 | "version": 3 75 | }, 76 | "file_extension": ".py", 77 | "mimetype": "text/x-python", 78 | "name": "python", 79 | "nbconvert_exporter": "python", 80 | "pygments_lexer": "ipython3", 81 | "version": "3.7.1" 82 | } 83 | }, 84 | "nbformat": 4, 85 | "nbformat_minor": 2 86 | } 87 | -------------------------------------------------------------------------------- /Max Palindromes.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 103, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "from collections import defaultdict\n", 10 | "from itertools import permutations\n", 11 | "\n", 12 | "def get_max_palindromes(s):\n", 13 | " n = len(s)\n", 14 | " z = ord('a')\n", 15 | " S = defaultdict(lambda:0)\n", 16 | " for i,v in enumerate(s,1):\n", 17 | " S[v] = S[v]+1\n", 18 | " left = ''\n", 19 | " mid_char = []\n", 20 | " for ch in S:\n", 21 | " if S[ch] % 2 == 1:\n", 22 | " mid_char.append(ch)\n", 23 | " S[ch] -= 1\n", 24 | " for ch in S:\n", 25 | " if S[ch] != 0 and S[ch]%2 == 0:\n", 26 | " count = S[ch]/2\n", 27 | " while count > 0:\n", 28 | " left = left + ch\n", 29 | " count -= 1\n", 30 | " S[ch] = S[ch]/2\n", 31 | " left_s = ''.join(sorted(left)) \n", 32 | " all_left = [''.join(p) for p in permutations(left_s)]\n", 33 | " max_len = 0\n", 34 | " final_res = []\n", 35 | " for m in mid_char:\n", 36 | " for a in set(all_left):\n", 37 | " result = a+m+a[::-1]\n", 38 | " final_res.append(result)\n", 39 | " return final_res" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 104, 45 | "metadata": {}, 46 | "outputs": [], 47 | "source": [ 48 | "result = get_max_palindromes('aaaabbbccd')" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 111, 54 | "metadata": {}, 55 | "outputs": [ 56 | { 57 | "name": "stdout", 58 | "output_type": "stream", 59 | "text": [ 60 | "9\n" 61 | ] 62 | } 63 | ], 64 | "source": [ 65 | "print(len(result[0]))" 66 | ] 67 | } 68 | ], 69 | "metadata": { 70 | "kernelspec": { 71 | "display_name": "Python 3", 72 | "language": "python", 73 | "name": "python3" 74 | }, 75 | "language_info": { 76 | "codemirror_mode": { 77 | "name": "ipython", 78 | "version": 3 79 | }, 80 | "file_extension": ".py", 81 | "mimetype": "text/x-python", 82 | "name": "python", 83 | "nbconvert_exporter": "python", 84 | "pygments_lexer": "ipython3", 85 | "version": "3.7.4" 86 | } 87 | }, 88 | "nbformat": 4, 89 | "nbformat_minor": 2 90 | } 91 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 365DaysOfCoding -------------------------------------------------------------------------------- /Subsequence_util.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "Length of Longest common Subsequence: 3 and Sequences are : {'yqp'}\n", 13 | "Length of Longest repeated Subsequence: 4 and Sequences are : {'ATCG'}\n", 14 | "Common Palindromic Sequences are: {'BABCBAB', 'BACBCAB'}\n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "class Subsequence:\n", 20 | " \n", 21 | " def __init__(self, string1, string2, problem_type):\n", 22 | " self.s1 = string1\n", 23 | " self.s2 = string2\n", 24 | " self.problem_type = problem_type\n", 25 | " \n", 26 | " # should ask if user needs common subsequence or repeated subsequence\n", 27 | " def build_dp_table(self):\n", 28 | " len_s1 = len(self.s1)\n", 29 | " len_s2 = len(self.s2)\n", 30 | " # initialising all cells with zeros\n", 31 | " result = [[0 for i in range(len_s2+1)] for j in range(len_s1+1)]\n", 32 | " for i in range(1, len_s1+1):\n", 33 | " for j in range(1, len_s2+1):\n", 34 | " if self.problem_type == 'common':\n", 35 | " result[i][j] = (1 + result[i-1][j-1]) if self.s1[i-1] == self.s2[j-1] else max(result[i][j-1], result[i-1][j])\n", 36 | " elif self.problem_type == 'repeated':\n", 37 | " result[i][j] = (1 + result[i-1][j-1]) if (self.s1[i-1] == self.s2[j-1] and i != j) else max(result[i][j-1], result[i-1][j])\n", 38 | " return result\n", 39 | " \n", 40 | " def getLengthOfLongestSubSequence(self):\n", 41 | " result = self.build_dp_table()\n", 42 | " return result[len(self.s1)][len(self.s2)]\n", 43 | " \n", 44 | " \n", 45 | " def helperFunction(self,result, m, n):\n", 46 | " s1 = self.s1\n", 47 | " s2 = self.s2\n", 48 | " \n", 49 | " if self.problem_type == 'repeated':\n", 50 | " compare_val = (m != n)\n", 51 | " elif self.problem_type == 'common':\n", 52 | " compare_val = True\n", 53 | " if m is 0 or n is 0:\n", 54 | " return set([''])\n", 55 | " elif (s1[m-1] == s2[n-1]) and compare_val:\n", 56 | " all_sequences = self.helperFunction(result, m-1, n-1)\n", 57 | " all_sequences = set(a+s1[m-1] for a in all_sequences )\n", 58 | " return all_sequences\n", 59 | " else:\n", 60 | " if result[m-1][n] > result[m][n-1]:\n", 61 | " return self.helperFunction(result, m-1,n)\n", 62 | " elif result[m][n-1] > result[m-1][n]:\n", 63 | " return self.helperFunction(result, m,n-1)\n", 64 | " top_cell_sequences = self.helperFunction(result, m-1,n)\n", 65 | " left_cell_sequences = self.helperFunction(result, m,n-1)\n", 66 | " return top_cell_sequences.union(left_cell_sequences)\n", 67 | " \n", 68 | " def get_all_subsequences_by_problem_type(self):\n", 69 | " result = self.build_dp_table()\n", 70 | " sequences = self.helperFunction(result, len(self.s1), len(self.s2))\n", 71 | " return sequences\n", 72 | " \n", 73 | " def print_results(self):\n", 74 | " print('Length of Longest {} Subsequence: {} and Sequences are : {}'.\n", 75 | " format(self.problem_type, self.getLengthOfLongestSubSequence(),self.get_all_subsequences_by_problem_type()))\n", 76 | " \n", 77 | "\n", 78 | "\n", 79 | "def reverse_string(s):\n", 80 | " reverse_s = s[len(s)::-1] \n", 81 | " return reverse_s\n", 82 | " \n", 83 | "def main():\n", 84 | " s1 = 'ylqpejqbalahwr'\n", 85 | " s2 = 'yrkzavgdmdgtqpg'\n", 86 | " common_subsequence = Subsequence(s1, s2,'common')\n", 87 | " common_subsequence.print_results()\n", 88 | " \n", 89 | " s1 = 'ATACTCGGA'\n", 90 | " s2 = 'ATACTCGGA'\n", 91 | " repeated_subsequence = Subsequence(s1, s2,'repeated')\n", 92 | " repeated_subsequence.print_results()\n", 93 | " \n", 94 | " # To get longest common palindromic subsequence\n", 95 | " s = 'BBABCBCAB'\n", 96 | " palindromic_subseq = Subsequence(s, reverse_string(s),'common')\n", 97 | " print(\"Common Palindromic Sequences are: {}\".format(palindromic_subseq.get_all_subsequences_by_problem_type()))\n", 98 | "\n", 99 | "if __name__ == \"__main__\":\n", 100 | " main() \n", 101 | " " 102 | ] 103 | }, 104 | { 105 | "cell_type": "markdown", 106 | "metadata": {}, 107 | "source": [ 108 | "# Diff Utility" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": 15, 114 | "metadata": {}, 115 | "outputs": [ 116 | { 117 | "name": "stdout", 118 | "output_type": "stream", 119 | "text": [ 120 | "XMJ-YA-U+A+TZ" 121 | ] 122 | } 123 | ], 124 | "source": [ 125 | "s1 = 'XMJYAUZ'\n", 126 | "s2 = 'XMJAATZ'\n", 127 | "common_subsequence = Subsequence(s1, s2,'common')\n", 128 | "dp_table = common_subsequence.build_dp_table()\n", 129 | "get_diff_utility(s1,s2)" 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": 14, 135 | "metadata": {}, 136 | "outputs": [], 137 | "source": [ 138 | "def get_diff_utility(s1, s2):\n", 139 | " common_subsequence = Subsequence(s1, s2,'common')\n", 140 | " dp_table = common_subsequence.build_dp_table()\n", 141 | " diff_utility_helper(s1, s2, len(s1), len(s2), dp_table)\n", 142 | " \n", 143 | "def diff_utility_helper(s1, s2, m, n, dp_table):\n", 144 | " if(m > 0 and n > 0 and s1[m-1] == s2[n-1]):\n", 145 | " diff_utility_helper(s1, s2,m-1, n-1, dp_table)\n", 146 | " print(s1[m-1], end = '')\n", 147 | " elif(n > 0 and (m == 0 or dp_table[m][n-1] >= dp_table[m-1][n])):\n", 148 | " diff_utility_helper(s1, s2,m, n-1, dp_table)\n", 149 | " print('+' + s2[n-1], end = '')\n", 150 | " elif(m > 0 and (n == 0 or dp_table[m-1][n] >= dp_table[m][n-1])):\n", 151 | " diff_utility_helper(s1, s2,m-1, n, dp_table)\n", 152 | " print('-' + s1[m-1],end = '')\n", 153 | " \n", 154 | " \n", 155 | " \n", 156 | " " 157 | ] 158 | } 159 | ], 160 | "metadata": { 161 | "kernelspec": { 162 | "display_name": "Python 3", 163 | "language": "python", 164 | "name": "python3" 165 | }, 166 | "language_info": { 167 | "codemirror_mode": { 168 | "name": "ipython", 169 | "version": 3 170 | }, 171 | "file_extension": ".py", 172 | "mimetype": "text/x-python", 173 | "name": "python", 174 | "nbconvert_exporter": "python", 175 | "pygments_lexer": "ipython3", 176 | "version": "3.7.4" 177 | } 178 | }, 179 | "nbformat": 4, 180 | "nbformat_minor": 2 181 | } 182 | --------------------------------------------------------------------------------