├── Problem 1- Generating Binary strings.ipynb ├── Problem 2-Time Complexity.ipynb ├── Problem 8- Anagram Problem.ipynb ├── Problem1.py └── README.md /Problem 1- Generating Binary strings.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## First Competitive Programming Problem" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "### Generate All Binary Strings\n", 15 | "- if n=2 ['00', '01', '10', '11']\n", 16 | "- if n=3 ['000', '001', '010', '011', '100', '101', '110', '111']\n", 17 | "- if n=4 ['0000', '0001', '0010', '0011', '0100', '0101', '0110', '0111', '1000', '1001', '1010', '1011', '1100', '1101', '1110', '1111']" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 34, 23 | "metadata": {}, 24 | "outputs": [ 25 | { 26 | "name": "stdout", 27 | "output_type": "stream", 28 | "text": [ 29 | "['000', '001', '010', '011', '100', '101', '110', '111']\n" 30 | ] 31 | } 32 | ], 33 | "source": [ 34 | "def append_bits(x, L):\n", 35 | " return [x + element for element in L]\n", 36 | "\n", 37 | "def generate_bit(n):\n", 38 | " if n == 0: return []\n", 39 | " if n == 1: return [\"0\", \"1\"]\n", 40 | " else:\n", 41 | " return (append_bits(\"0\", generate_bit(n - 1)) + \n", 42 | " append_bits(\"1\", generate_bit(n - 1)))\n", 43 | " \n", 44 | "print(generate_bit(3))" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": null, 50 | "metadata": {}, 51 | "outputs": [], 52 | "source": [ 53 | "generate_bits(3)\n", 54 | "append_bits(\"0\",[\"00\",\"01\"])n==2\n", 55 | "[\"000\",\"001\"]" 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.7" 76 | } 77 | }, 78 | "nbformat": 4, 79 | "nbformat_minor": 4 80 | } 81 | -------------------------------------------------------------------------------- /Problem 2-Time Complexity.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Find The Time Complexity" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 10, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "### What is the Time Complexity\n", 17 | "def Func(n):\n", 18 | " for i in range(1,n):\n", 19 | " j=i\n", 20 | " while j=97:\n", 136 | " if ord(str1[i])-32 not in dict:\n", 137 | " dict[ord(str1[i])-32] = 1\n", 138 | " else:\n", 139 | " dict[ord(str1[i])-32] = dict[ord(str1[i])-32] + 1\n", 140 | " else:\n", 141 | " if ord(str1[i]) not in dict:\n", 142 | " dict[ord(str1[i])] = 1\n", 143 | " else:\n", 144 | " dict[ord(str1[i])] = dict[ord(str1[i])] + 1\n", 145 | "\n", 146 | " for i in range(len(str2)):\n", 147 | " if ord(str2[i])<=122 and ord(str2[i])>=97:\n", 148 | " if ord(str2[i])-32 in dict:\n", 149 | " if dict[ord(str2[i])-32] != 0:\n", 150 | " dict[ord(str2[i])-32] = dict[ord(str2[i])-32] - 1\n", 151 | " else:\n", 152 | " return 0\n", 153 | " else:\n", 154 | " return 0\n", 155 | " else:\n", 156 | " if ord(str2[i]) in dict:\n", 157 | " if dict[ord(str2[i])] != 0:\n", 158 | " dict[ord(str2[i])] = dict[ord(str2[i])] - 1\n", 159 | " else:\n", 160 | " return 0\n", 161 | " else:\n", 162 | " return 0\n", 163 | "\n", 164 | " return 1\n", 165 | "\n", 166 | "\n", 167 | "\n", 168 | "str1 = input(\"Enter the first string\")\n", 169 | "str2 = input(\"Enter the second string\")\n", 170 | "if Anagram(str1,str2):\n", 171 | " print(\"Yes\")\n", 172 | "else:\n", 173 | " print(\"No\")\n", 174 | "\n", 175 | "\n", 176 | "\n" 177 | ] 178 | }, 179 | { 180 | "cell_type": "code", 181 | "execution_count": 23, 182 | "metadata": {}, 183 | "outputs": [], 184 | "source": [ 185 | "### Final Solution\n", 186 | "def anagram(str1,str2):\n", 187 | " group_letters={}\n", 188 | " for i in str1:\n", 189 | " if i in group_letters:\n", 190 | " group_letters[i]+=1\n", 191 | " else:\n", 192 | " group_letters[i]=1\n", 193 | " for i in str2:\n", 194 | " if i in group_letters:\n", 195 | " group_letters[i]-=1\n", 196 | " else:\n", 197 | " group_letters[i]=1\n", 198 | " for j in group_letters:\n", 199 | " if group_letters[j]!=0:\n", 200 | " return False\n", 201 | " return True" 202 | ] 203 | }, 204 | { 205 | "cell_type": "code", 206 | "execution_count": 22, 207 | "metadata": {}, 208 | "outputs": [ 209 | { 210 | "name": "stdout", 211 | "output_type": "stream", 212 | "text": [ 213 | "Enter the first string: Eleven plus two\n", 214 | "Enter the first string: twelve plus one\n", 215 | "The entered strings are anagram\n" 216 | ] 217 | } 218 | ], 219 | "source": [ 220 | "string1=input(\"Enter the first string: \")\n", 221 | "string1=string1.replace(\" \",\"\").lower()\n", 222 | "string2=input(\"Enter the first string: \")\n", 223 | "string2=string2.replace(\" \",\"\").lower()\n", 224 | "var=anagram(string1,string2)\n", 225 | "if(var):\n", 226 | " print(\"The entered strings are anagram\")\n", 227 | "else:\n", 228 | " print(\"The entered strings are not anagram\")" 229 | ] 230 | }, 231 | { 232 | "cell_type": "markdown", 233 | "metadata": {}, 234 | "source": [ 235 | "### Find all the anagrams from a list of words\n", 236 | "['cat', 'dog', 'tac', 'god', 'act']" 237 | ] 238 | }, 239 | { 240 | "cell_type": "code", 241 | "execution_count": null, 242 | "metadata": {}, 243 | "outputs": [], 244 | "source": [] 245 | } 246 | ], 247 | "metadata": { 248 | "kernelspec": { 249 | "display_name": "Python 3", 250 | "language": "python", 251 | "name": "python3" 252 | }, 253 | "language_info": { 254 | "codemirror_mode": { 255 | "name": "ipython", 256 | "version": 3 257 | }, 258 | "file_extension": ".py", 259 | "mimetype": "text/x-python", 260 | "name": "python", 261 | "nbconvert_exporter": "python", 262 | "pygments_lexer": "ipython3", 263 | "version": "3.7.7" 264 | } 265 | }, 266 | "nbformat": 4, 267 | "nbformat_minor": 4 268 | } 269 | -------------------------------------------------------------------------------- /Problem1.py: -------------------------------------------------------------------------------- 1 | # Python3 implementation of the approach 2 | DIGITS = 4; MIN = 1000; MAX = 9999; 3 | 4 | # Function to return the minimum element 5 | # from the range [prev, MAX] such that 6 | # it differs in at most 1 digit with cur 7 | def getBest(prev, cur) : 8 | 9 | # To start with the value 10 | # we have achieved in the last step 11 | maximum = max(MIN, prev); 12 | 13 | for i in range(maximum, MAX + 1) : 14 | cnt = 0; 15 | 16 | # Store the value with which the 17 | # current will be compared 18 | a = i; 19 | 20 | # Current value 21 | b = cur; 22 | 23 | # There are at most 4 digits 24 | for k in range(DIGITS) : 25 | 26 | # If the current digit differs 27 | # in both the numbers 28 | if (a % 10 != b % 10) : 29 | cnt += 1; 30 | 31 | # Eliminate last digits in 32 | # both the numbers 33 | a //= 10; 34 | b //= 10; 35 | 36 | # If the number of different 37 | # digits is at most 1 38 | if (cnt <= 1) : 39 | return i; 40 | 41 | # If we can't find any number for which 42 | # the number of change is less than or 43 | # equal to 1 then return -1 44 | return -1; 45 | 46 | # Function to get the non-decreasing list 47 | def getList(arr, n) : 48 | 49 | # Creating a vector for the updated list 50 | myList = []; 51 | 52 | # Let's assume that it is possible to 53 | # make the list non-decreasing 54 | possible = True; 55 | 56 | myList.append(0); 57 | 58 | for i in range(n) : 59 | 60 | # Element of the original array 61 | cur = arr[i]; 62 | 63 | myList.append(getBest(myList[-1], cur)); 64 | 65 | # Can't make the list non-decreasing 66 | if (myList[-1] == -1) : 67 | possible = False; 68 | break; 69 | 70 | # If possible then print the list 71 | if (possible) : 72 | for i in range(1, len(myList)) : 73 | print(myList[i], end = " "); 74 | else : 75 | print("-1"); 76 | 77 | # Driver code 78 | if __name__ == "__main__" : 79 | 80 | arr = [ 1095, 1094, 1095 ]; 81 | n = len(arr); 82 | 83 | getList(arr, n); 84 | 85 | # This code is contributed by AnkitRai01 86 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Competitive-Programming --------------------------------------------------------------------------------