├── .github └── workflows │ └── greetings.yml ├── 1 Conditions and Loops ├── 1.1 Boolean ; If Else.ipynb ├── 1.2 Python Numbers.ipynb └── 1.3 Practice Problems Conditions and Loops.ipynb ├── 2 Patterns 1 ├── 2.1 Patterns 1.ipynb ├── 2.2 Patterns 1 Practice.ipynb ├── 2.3. Patterns 2.ipynb └── 2.4 Patterns 2 Practice.ipynb ├── 3 More on Loops ├── 3.1 break keyword.ipynb ├── 3.2 For Loop.ipynb └── 3.3 For Loop Practice.ipynb ├── 4 Functions ├── 4.1 Functions Demo.ipynb ├── 4.2 Scope of Variables.ipynb ├── 4.3 Default arguments in function.ipynb └── 4.4 Practice Functions.ipynb ├── 5 Arrays ├── 5.1 Lists - creation, slicing, removing an element.ipynb ├── 5.10 [Practice] Arrays & Lists.ipynb ├── 5.2 How elements are stored in List .ipynb ├── 5.3 Looping On Lists.ipynb ├── 5.4 Negative indexing and sequencing in Lists.ipynb ├── 5.5 Taking Input in List - Line separated and Space separated.ipynb ├── 5.6 Linear Search.ipynb ├── 5.7 Mutable and Immutable concepts.ipynb ├── 5.8 Passing Variables through Functions.ipynb └── 5.9 Reverse List.ipynb ├── 6 Searching and Sorting ├── 6.1 Binary Search.ipynb ├── 6.2 Selection Sort.ipynb ├── 6.3 Bubble Sort.ipynb ├── 6.4 Insertion Sort.ipynb ├── 6.5 Merge two sorted arrays.ipynb ├── 6.6 [Practice] Push Zeros to end.ipynb ├── 6.7 [Practice] Rotate array.ipynb ├── 6.8 [Practice] Second Largest element in an array.ipynb ├── 6.9 [Practice] Check array rotation.ipynb ├── 6.91 [Practice] Sort 0 1 2.ipynb └── 6.92 [Practice] Sum of two arrays.ipynb ├── 7 Strings ├── 7.01 Strings - Intro.ipynb ├── 7.02 How Strings are stored.ipynb ├── 7.03 Concatenation of Strings.ipynb ├── 7.04 Slicing on String.ipynb ├── 7.05 Iterating on String.ipynb ├── 7.06 Comparison Operators on String.ipynb ├── 7.07 Operations on Strings.ipynb ├── 7.08 Replace Character in String.ipynb ├── 7.09 Count Vowels, Consonants, Digits in String.ipynb ├── 7.10 [Practice] Check Permutation.ipynb ├── 7.11 [Practice] Remove Consecutive Duplicates.ipynb ├── 7.12 [Practice] Reverse Each Word .ipynb ├── 7.13 [Practice] Remove character.ipynb ├── 7.14 [Practice] Highest Occurring Character.ipynb └── 7.15 [Practice] Compress the String .ipynb ├── 8 Two Dimensional Lists ├── 8.01 Intro to 2-D Lists.ipynb ├── 8.02 How 2 D lists are stored.ipynb ├── 8.03 Jagged Lists.ipynb ├── 8.04 List Comprehension.ipynb ├── 8.05 Input of 2-D lists.ipynb ├── 8.06 Printing of a 2-D list.ipynb ├── 8.07 [Practice] Row Wise Sum.ipynb ├── 8.08 Largest Column Sum in Two Dimensional List.ipynb ├── 8.09 [Practice] Largest Row or Column.ipynb ├── 8.10 [Practice] Wave Print.ipynb └── 8.11 [Practice[] Spiral Print.ipynb ├── 9 Tuples, Dictionaries and Sets ├── 9.01 Tuples.ipynb ├── 9.02 Tuples Functions.ipynb ├── 9.03 Variable Length Input and Output.ipynb ├── 9.04 Dictionaries.ipynb ├── 9.05 Dictionary Functions.ipynb ├── 9.06 Accessing or deleting data in dictionaries.ipynb ├── 9.07 Print all words with freq k.ipynb ├── 9.08 Sets - Intro.ipynb ├── 9.09 Set Functions.ipynb └── 9.10 Sum of all unique numbers in list.ipynb ├── README.md └── image.png /.github/workflows/greetings.yml: -------------------------------------------------------------------------------- 1 | name: Greetings 2 | 3 | on: [pull_request, issues] 4 | 5 | jobs: 6 | greeting: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - uses: actions/first-interaction@v1 10 | with: 11 | repo-token: ${{ secrets.GITHUB_TOKEN }} 12 | issue-message: 'Great! Thanks for contributing. Will check and merge'' first issue' 13 | pr-message: 'Your expertise required'' first pr' 14 | -------------------------------------------------------------------------------- /1 Conditions and Loops/1.1 Boolean ; If Else.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 4, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "data": { 10 | "text/plain": [ 11 | "bool" 12 | ] 13 | }, 14 | "execution_count": 4, 15 | "metadata": {}, 16 | "output_type": "execute_result" 17 | } 18 | ], 19 | "source": [ 20 | "a = True\n", 21 | "b = False\n", 22 | "\n", 23 | "c = \"Ninjas\"\n", 24 | "type(b)" 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": {}, 30 | "source": [ 31 | "### Relational Operators (<, >, <=, >=, ==, !=)" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 10, 37 | "metadata": {}, 38 | "outputs": [ 39 | { 40 | "name": "stdout", 41 | "output_type": "stream", 42 | "text": [ 43 | "False\n", 44 | "True\n", 45 | "False\n", 46 | "True\n", 47 | "False\n", 48 | "True\n" 49 | ] 50 | } 51 | ], 52 | "source": [ 53 | "a = 10\n", 54 | "b =20\n", 55 | "print(a > b) # a is greater than b\n", 56 | "print(a < b) # a is less than b\n", 57 | "print(a >= b) # a is greter than or equal to b \n", 58 | "print(a <= b) # a is less than or equal to b \n", 59 | "print(a == b) # a is equal to b\n", 60 | "print(a != b) # a is not equal to b" 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": {}, 66 | "source": [ 67 | "### Logical Operators (and, or, not)" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 9, 73 | "metadata": {}, 74 | "outputs": [ 75 | { 76 | "name": "stdout", 77 | "output_type": "stream", 78 | "text": [ 79 | "False\n", 80 | "True\n", 81 | "True\n" 82 | ] 83 | } 84 | ], 85 | "source": [ 86 | "c1 = a > 10\n", 87 | "c2 = b > 10\n", 88 | "r1 = c1 and c2\n", 89 | "r2 = c1 or c2\n", 90 | "r3 = not(c1)\n", 91 | "print(r1)\n", 92 | "print(r2)\n", 93 | "print(r3)\n" 94 | ] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "### If Else" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": 16, 106 | "metadata": {}, 107 | "outputs": [ 108 | { 109 | "name": "stdout", 110 | "output_type": "stream", 111 | "text": [ 112 | "If Block\n", 113 | "Woohoo!\n" 114 | ] 115 | } 116 | ], 117 | "source": [ 118 | "a = True or False\n", 119 | "if a:\n", 120 | " print(\"If Block\")\n", 121 | " print(\"Woohoo!\")\n", 122 | "else:\n", 123 | " print(\"Else Block\")" 124 | ] 125 | }, 126 | { 127 | "cell_type": "markdown", 128 | "metadata": {}, 129 | "source": [ 130 | "### Odd or Even Number" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": 19, 136 | "metadata": {}, 137 | "outputs": [ 138 | { 139 | "name": "stdout", 140 | "output_type": "stream", 141 | "text": [ 142 | "4\n", 143 | "Even Number\n" 144 | ] 145 | } 146 | ], 147 | "source": [ 148 | "n = int(input())\n", 149 | "if( n%2 == 0):\n", 150 | " print(\"Even Number\")\n", 151 | "else:\n", 152 | " print(\"Odd Number\")\n", 153 | " " 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": 27, 159 | "metadata": {}, 160 | "outputs": [ 161 | { 162 | "name": "stdout", 163 | "output_type": "stream", 164 | "text": [ 165 | "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n", 166 | "Even\n" 167 | ] 168 | } 169 | ], 170 | "source": [ 171 | "n = int(input())\n", 172 | "if(n & 1):\n", 173 | " print(\"Odd\")\n", 174 | "else:\n", 175 | " print(\"Even\")\n" 176 | ] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "execution_count": 28, 181 | "metadata": {}, 182 | "outputs": [ 183 | { 184 | "name": "stdout", 185 | "output_type": "stream", 186 | "text": [ 187 | "medium\n" 188 | ] 189 | } 190 | ], 191 | "source": [ 192 | "n = 15\n", 193 | "#Check If the number is between 1 to 10\n", 194 | "if n>=1 and n<=10:\n", 195 | " print(\"too low\")\n", 196 | "\n", 197 | "#Check If the number is between 11 to 20\n", 198 | "elif n>=10 and n<=20:\n", 199 | " print(\"medium\")\n", 200 | "\n", 201 | "#Check If the number is between 21 to 30\n", 202 | "elif n>=20 and n<=30:\n", 203 | " print(\"large\")\n", 204 | "#Check if the number is greater than 30 \n", 205 | "else:\n", 206 | " print(\"too large\")" 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "execution_count": 30, 212 | "metadata": {}, 213 | "outputs": [ 214 | { 215 | "name": "stdout", 216 | "output_type": "stream", 217 | "text": [ 218 | "too low\n" 219 | ] 220 | } 221 | ], 222 | "source": [ 223 | "n = 10\n", 224 | "#Check If the number is between 1 to 10\n", 225 | "if n>=1 and n<=10:\n", 226 | " print(\"too low\")\n", 227 | "\n", 228 | "#Check If the number is between 11 to 20\n", 229 | "elif n>=10 and n<=20:\n", 230 | " print(\"medium\")\n", 231 | "\n", 232 | "#Check If the number is between 21 to 30\n", 233 | "elif n>=20 and n<=30:\n", 234 | " print(\"large\")\n", 235 | "#Check if the number is greater than 30 \n", 236 | "else:\n", 237 | " print(\"too large\")" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": 31, 243 | "metadata": {}, 244 | "outputs": [ 245 | { 246 | "name": "stdout", 247 | "output_type": "stream", 248 | "text": [ 249 | "Inside if\n" 250 | ] 251 | } 252 | ], 253 | "source": [ 254 | "x = 15\n", 255 | "if x <= 15:\n", 256 | " print(\"Inside if\")\n", 257 | "else:\n", 258 | " print(\"Inside else\")" 259 | ] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": 32, 264 | "metadata": {}, 265 | "outputs": [ 266 | { 267 | "name": "stdout", 268 | "output_type": "stream", 269 | "text": [ 270 | "Hello\n", 271 | "Hi\n" 272 | ] 273 | } 274 | ], 275 | "source": [ 276 | "x = 5\n", 277 | "if x < 6:\n", 278 | " print(\"Hello\")\n", 279 | "if x == 5:\n", 280 | " print(\"Hi\")\n", 281 | "else:\n", 282 | " print(\"Hey\")" 283 | ] 284 | }, 285 | { 286 | "cell_type": "code", 287 | "execution_count": null, 288 | "metadata": {}, 289 | "outputs": [], 290 | "source": [] 291 | } 292 | ], 293 | "metadata": { 294 | "celltoolbar": "Edit Metadata", 295 | "kernelspec": { 296 | "display_name": "Python 3", 297 | "language": "python", 298 | "name": "python3" 299 | }, 300 | "language_info": { 301 | "codemirror_mode": { 302 | "name": "ipython", 303 | "version": 3 304 | }, 305 | "file_extension": ".py", 306 | "mimetype": "text/x-python", 307 | "name": "python", 308 | "nbconvert_exporter": "python", 309 | "pygments_lexer": "ipython3", 310 | "version": "3.7.3" 311 | } 312 | }, 313 | "nbformat": 4, 314 | "nbformat_minor": 2 315 | } 316 | -------------------------------------------------------------------------------- /1 Conditions and Loops/1.3 Practice Problems Conditions and Loops.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Fahrenheit to Celsius" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "s = int(input())\n", 17 | "e = int(input())\n", 18 | "w = int(input())\n", 19 | "F = s\n", 20 | "while(F <= e):\n", 21 | " C = (F - 32) * (5/9)\n", 22 | " print(F,\"\\t\",int( C))\n", 23 | " F = F + w" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "### Calculator" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": null, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "while True:\n", 40 | " n = int(input()) \n", 41 | " if(n == 1):\n", 42 | " a = int(input())\n", 43 | " b = int(input())\n", 44 | " print(a+b)\n", 45 | " if(n == 2):\n", 46 | " a = int(input())\n", 47 | " b = int(input())\n", 48 | " print(a-b)\n", 49 | " if(n == 3):\n", 50 | " a = int(input())\n", 51 | " b = int(input())\n", 52 | " print(a*b)\n", 53 | " if(n == 4):\n", 54 | " a = int(input())\n", 55 | " b = int(input())\n", 56 | " print(a/b)\n", 57 | " if(n == 5):\n", 58 | " a = int(input())\n", 59 | " b = int(input())\n", 60 | " print(a%b)\n", 61 | " if(n == 6):\n", 62 | " sys.exit(0)\n", 63 | " if(n < 1 and n > 6):\n", 64 | " print(\"Invalid Operation\") " 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "metadata": {}, 70 | "source": [ 71 | "### Reverse of a number" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": null, 77 | "metadata": {}, 78 | "outputs": [], 79 | "source": [ 80 | "def reverse(n):\n", 81 | " rev = 0\n", 82 | " while(n >0):\n", 83 | " rem = n % 10\n", 84 | " rev = (rev * 10) + rem\n", 85 | " n = n//10\n", 86 | " return rev \n", 87 | "n=int(input())\n", 88 | "result = reverse(n)\n", 89 | "print(result)" 90 | ] 91 | }, 92 | { 93 | "cell_type": "markdown", 94 | "metadata": {}, 95 | "source": [ 96 | "### Palindrome Number" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 1, 102 | "metadata": {}, 103 | "outputs": [ 104 | { 105 | "name": "stdout", 106 | "output_type": "stream", 107 | "text": [ 108 | "5\n", 109 | "true\n" 110 | ] 111 | } 112 | ], 113 | "source": [ 114 | "def checkPalindrome(num):\n", 115 | " if(num == reverse(num)):\n", 116 | " return True\n", 117 | " else:\n", 118 | " return False\n", 119 | " \n", 120 | "def reverse(n):\n", 121 | " rev = 0\n", 122 | " while(n >0):\n", 123 | " rem = n % 10\n", 124 | " rev = (rev * 10) + rem\n", 125 | " n = n//10\n", 126 | " return rev \n", 127 | "\n", 128 | "num = int(input())\n", 129 | "isPalindrome = checkPalindrome(num)\n", 130 | "if(isPalindrome):\n", 131 | "\tprint('true')\n", 132 | "else:\n", 133 | "\tprint('false')\n" 134 | ] 135 | }, 136 | { 137 | "cell_type": "markdown", 138 | "metadata": {}, 139 | "source": [ 140 | "### Sum of even & odd\n", 141 | "\n", 142 | "Write a program to input an integer N and print the sum of all its even digits and sum of all its odd digits separately.\n", 143 | "Digits means numbers not the places. That is, if the given integer is \"13245\", even digits are 2 & 4 and odd digits are 1, 3 & 5.\n", 144 | "Input format :\n", 145 | " Integer N\n", 146 | "Output format :\n", 147 | "Sum_of_Even_Digits Sum_of_Odd_Digits\n", 148 | "(Print first even sum and then odd sum separated by space)\n", 149 | "Sample Input :\n", 150 | "1234\n", 151 | "Sample Output :\n", 152 | "6 4" 153 | ] 154 | }, 155 | { 156 | "cell_type": "code", 157 | "execution_count": 2, 158 | "metadata": {}, 159 | "outputs": [ 160 | { 161 | "name": "stdout", 162 | "output_type": "stream", 163 | "text": [ 164 | "548\n", 165 | "12 5\n" 166 | ] 167 | } 168 | ], 169 | "source": [ 170 | "## Note : For printing multiple values in one line, put them inside print separated by space.\n", 171 | "## You can follow this syntax for printing values of two variables val1 and val2 separaetd by space -\n", 172 | "## print(val1, \" \", val2)\n", 173 | "def iseven(no):\n", 174 | " if(no % 2 == 0):\n", 175 | " return True\n", 176 | " else:\n", 177 | " return False\n", 178 | "\n", 179 | "n = int(input())\n", 180 | "even_sum = 0\n", 181 | "odd_sum = 0\n", 182 | "while(n > 0):\n", 183 | " rem = int(n % 10)\n", 184 | " if(iseven(rem)):\n", 185 | " even_sum = even_sum + int(rem)\n", 186 | " else:\n", 187 | " odd_sum = odd_sum + int(rem)\n", 188 | " n = n/10\n", 189 | "print(even_sum,\" \",odd_sum)" 190 | ] 191 | }, 192 | { 193 | "cell_type": "markdown", 194 | "metadata": {}, 195 | "source": [ 196 | "### Nth Fibonacci Number\n", 197 | "\n", 198 | "Nth term of fibonacci series F(n) is calculated using following formula -\n", 199 | " F(n) = F(n-1) + F(n-2), \n", 200 | "Provided N you have to find out the Nth Fibonacci Number. Also F(1) = F(2) = 1.\n", 201 | "Input Format :\n", 202 | "Integer n\n", 203 | "Constraints:\n", 204 | "Time Limit: 1 second\n", 205 | "Output Format :\n", 206 | "Nth Fibonacci term i.e. F(n)\n", 207 | "Sample Input :\n", 208 | "4\n", 209 | "Sample Output :\n", 210 | "3 " 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": 1, 216 | "metadata": {}, 217 | "outputs": [ 218 | { 219 | "name": "stdout", 220 | "output_type": "stream", 221 | "text": [ 222 | "12\n", 223 | "144\n" 224 | ] 225 | } 226 | ], 227 | "source": [ 228 | "## Read input as specified in the question.\n", 229 | "## Print output as specified in the question.\n", 230 | "def Fibonacci(n): \n", 231 | " if n<0: \n", 232 | " print(\"Incorrect input\") \n", 233 | " # First Fibonacci number is 0 \n", 234 | " elif n==0: \n", 235 | " return 0\n", 236 | " # Second Fibonacci number is 1 \n", 237 | " elif n==1: \n", 238 | " return 1\n", 239 | " else: \n", 240 | " return Fibonacci(n-1)+Fibonacci(n-2) \n", 241 | " \n", 242 | "# Driver Program \n", 243 | " \n", 244 | "print(Fibonacci(int(input())))\n" 245 | ] 246 | }, 247 | { 248 | "cell_type": "code", 249 | "execution_count": null, 250 | "metadata": {}, 251 | "outputs": [], 252 | "source": [] 253 | } 254 | ], 255 | "metadata": { 256 | "kernelspec": { 257 | "display_name": "Python 3", 258 | "language": "python", 259 | "name": "python3" 260 | }, 261 | "language_info": { 262 | "codemirror_mode": { 263 | "name": "ipython", 264 | "version": 3 265 | }, 266 | "file_extension": ".py", 267 | "mimetype": "text/x-python", 268 | "name": "python", 269 | "nbconvert_exporter": "python", 270 | "pygments_lexer": "ipython3", 271 | "version": "3.7.3" 272 | } 273 | }, 274 | "nbformat": 4, 275 | "nbformat_minor": 2 276 | } 277 | -------------------------------------------------------------------------------- /2 Patterns 1/2.2 Patterns 1 Practice.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Pattern \n", 8 | "\n", 9 | "For N = 4 \n", 10 | "1\n", 11 | "11\n", 12 | "111\n", 13 | "1111" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 1, 19 | "metadata": {}, 20 | "outputs": [ 21 | { 22 | "name": "stdout", 23 | "output_type": "stream", 24 | "text": [ 25 | "5\n", 26 | "1\n", 27 | "11\n", 28 | "111\n", 29 | "1111\n", 30 | "11111\n" 31 | ] 32 | } 33 | ], 34 | "source": [ 35 | "n = int(input())\n", 36 | "i=1\n", 37 | "while i <= n:\n", 38 | " j=1\n", 39 | " while j<=i:\n", 40 | " print(1, end = '')\n", 41 | " j = j+1\n", 42 | " print()\n", 43 | " i = i+1\n", 44 | " \n", 45 | " " 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "metadata": {}, 51 | "source": [ 52 | "1\n", 53 | "11\n", 54 | "202\n", 55 | "3003\n", 56 | "40004" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 4, 62 | "metadata": {}, 63 | "outputs": [ 64 | { 65 | "name": "stdout", 66 | "output_type": "stream", 67 | "text": [ 68 | "5\n", 69 | "0\n", 70 | "00\n", 71 | "000\n", 72 | "0000\n", 73 | "00000\n" 74 | ] 75 | } 76 | ], 77 | "source": [ 78 | "n = int(input())\n", 79 | "i = 1\n", 80 | "while i <= n:\n", 81 | " j = 1\n", 82 | " while j <= i:\n", 83 | " print(0, end = '')\n", 84 | " j = j+1 \n", 85 | " print()\n", 86 | " i = i + 1" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": {}, 92 | "source": [ 93 | "1\n", 94 | "11\n", 95 | "202\n", 96 | "3003\n", 97 | "40004\n", 98 | "500005" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 19, 104 | "metadata": {}, 105 | "outputs": [ 106 | { 107 | "name": "stdout", 108 | "output_type": "stream", 109 | "text": [ 110 | "10\n", 111 | "1\n", 112 | "11\n", 113 | "202\n", 114 | "3003\n", 115 | "40004\n", 116 | "500005\n", 117 | "6000006\n", 118 | "70000007\n", 119 | "800000008\n", 120 | "9000000009\n" 121 | ] 122 | } 123 | ], 124 | "source": [ 125 | "n = int(input())\n", 126 | "i = 1\n", 127 | "while i <= n:\n", 128 | " j = 1\n", 129 | " while j <= i:\n", 130 | " if i==1 and j==i:\n", 131 | " print(1, end = '')\n", 132 | " elif j == 1:\n", 133 | " print(j+i-2, end = '')\n", 134 | " elif j == i:\n", 135 | " print(j-1, end = '')\n", 136 | " else:\n", 137 | " print(0, end = '')\n", 138 | " j = j + 1\n", 139 | " print()\n", 140 | " i = i + 1\n", 141 | " \n", 142 | " " 143 | ] 144 | }, 145 | { 146 | "cell_type": "markdown", 147 | "metadata": {}, 148 | "source": [ 149 | "1\n", 150 | "11\n", 151 | "121\n", 152 | "1221\n", 153 | "12221\n", 154 | "122221" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 23, 160 | "metadata": {}, 161 | "outputs": [ 162 | { 163 | "name": "stdout", 164 | "output_type": "stream", 165 | "text": [ 166 | "5\n", 167 | "1\n", 168 | "11\n", 169 | "121\n", 170 | "1221\n", 171 | "12221\n" 172 | ] 173 | } 174 | ], 175 | "source": [ 176 | "n = int(input())\n", 177 | "i = 1\n", 178 | "while i <= n:\n", 179 | " j = 1\n", 180 | " while j <= i:\n", 181 | " if i==1 and j==i:\n", 182 | " print(1, end = '')\n", 183 | " elif j == 1 or j == i:\n", 184 | " print(1, end = '')\n", 185 | " else:\n", 186 | " print(2, end = '')\n", 187 | " j = j + 1\n", 188 | " print()\n", 189 | " i = i + 1\n", 190 | " \n", 191 | " " 192 | ] 193 | }, 194 | { 195 | "cell_type": "markdown", 196 | "metadata": {}, 197 | "source": [ 198 | "# Inverted Triangular Pattern \n", 199 | "1234\n", 200 | "123\n", 201 | "12\n", 202 | "1" 203 | ] 204 | }, 205 | { 206 | "cell_type": "code", 207 | "execution_count": 26, 208 | "metadata": {}, 209 | "outputs": [ 210 | { 211 | "name": "stdout", 212 | "output_type": "stream", 213 | "text": [ 214 | "10\n", 215 | "12345678910\n", 216 | "123456789\n", 217 | "12345678\n", 218 | "1234567\n", 219 | "123456\n", 220 | "12345\n", 221 | "1234\n", 222 | "123\n", 223 | "12\n", 224 | "1\n", 225 | "\n" 226 | ] 227 | } 228 | ], 229 | "source": [ 230 | "n = int(input())\n", 231 | "i = 0\n", 232 | "while i <= n:\n", 233 | " j = 1\n", 234 | " temp = 1\n", 235 | " while j <= n - i:\n", 236 | " print(temp, end ='')\n", 237 | " j = j + 1\n", 238 | " temp = temp + 1\n", 239 | " print()\n", 240 | " i = i + 1" 241 | ] 242 | }, 243 | { 244 | "cell_type": "markdown", 245 | "metadata": {}, 246 | "source": [ 247 | "A\n", 248 | "BB\n", 249 | "CCC\n", 250 | "DDDD\n", 251 | "EEEEE\n", 252 | "FFFFFF\n", 253 | "GGGGGGG" 254 | ] 255 | }, 256 | { 257 | "cell_type": "code", 258 | "execution_count": 28, 259 | "metadata": {}, 260 | "outputs": [ 261 | { 262 | "name": "stdout", 263 | "output_type": "stream", 264 | "text": [ 265 | "4\n", 266 | "A\n", 267 | "BB\n", 268 | "CCC\n", 269 | "DDDD\n" 270 | ] 271 | } 272 | ], 273 | "source": [ 274 | "n = int(input())\n", 275 | "i = 1\n", 276 | "while i <= n:\n", 277 | " j = 1\n", 278 | " while j <= i:\n", 279 | " print(chr(ord('A') + i - 1), end = \"\") \n", 280 | " j = j + 1\n", 281 | " print()\n", 282 | " i = i + 1" 283 | ] 284 | }, 285 | { 286 | "cell_type": "code", 287 | "execution_count": null, 288 | "metadata": {}, 289 | "outputs": [], 290 | "source": [ 291 | "A\n", 292 | "AB\n", 293 | "ABC\n", 294 | "ABCD" 295 | ] 296 | }, 297 | { 298 | "cell_type": "code", 299 | "execution_count": null, 300 | "metadata": {}, 301 | "outputs": [], 302 | "source": [ 303 | "n = int(input())\n", 304 | "i = 1\n", 305 | "while i <= n:\n", 306 | " j = 1\n", 307 | " while j <= i:\n", 308 | " print(chr(ord('A') + j - 1), end = \"\") \n", 309 | " j = j + 1\n", 310 | " print()\n", 311 | " i = i + 1" 312 | ] 313 | }, 314 | { 315 | "cell_type": "code", 316 | "execution_count": null, 317 | "metadata": {}, 318 | "outputs": [], 319 | "source": [] 320 | } 321 | ], 322 | "metadata": { 323 | "kernelspec": { 324 | "display_name": "Python 3", 325 | "language": "python", 326 | "name": "python3" 327 | }, 328 | "language_info": { 329 | "codemirror_mode": { 330 | "name": "ipython", 331 | "version": 3 332 | }, 333 | "file_extension": ".py", 334 | "mimetype": "text/x-python", 335 | "name": "python", 336 | "nbconvert_exporter": "python", 337 | "pygments_lexer": "ipython3", 338 | "version": "3.7.3" 339 | } 340 | }, 341 | "nbformat": 4, 342 | "nbformat_minor": 2 343 | } 344 | -------------------------------------------------------------------------------- /2 Patterns 1/2.3. Patterns 2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | " *\n", 8 | " **\n", 9 | " ***\n", 10 | " ****\n", 11 | "*****" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 3, 17 | "metadata": {}, 18 | "outputs": [ 19 | { 20 | "name": "stdout", 21 | "output_type": "stream", 22 | "text": [ 23 | "5\n", 24 | " *\n", 25 | " **\n", 26 | " ***\n", 27 | " ****\n", 28 | "*****\n" 29 | ] 30 | } 31 | ], 32 | "source": [ 33 | "n = int(input())\n", 34 | "i = 1\n", 35 | "while i <= n:\n", 36 | " spaces = 1\n", 37 | " while spaces <= n-i:\n", 38 | " print(\" \", end = '')\n", 39 | " spaces = spaces + 1\n", 40 | " stars = 1\n", 41 | " while stars <= i:\n", 42 | " print('*', end = '')\n", 43 | " stars = stars + 1\n", 44 | " print()\n", 45 | " i = i + 1 " 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "metadata": {}, 51 | "source": [ 52 | "Right Angled Triangle\n", 53 | " 1\n", 54 | " 12\n", 55 | " 123\n", 56 | " 1234\n", 57 | "12345 " 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 22, 63 | "metadata": {}, 64 | "outputs": [ 65 | { 66 | "name": "stdout", 67 | "output_type": "stream", 68 | "text": [ 69 | "5\n", 70 | " 1\n", 71 | " 12\n", 72 | " 123\n", 73 | " 1234\n", 74 | "12345\n" 75 | ] 76 | } 77 | ], 78 | "source": [ 79 | "n = int(input())\n", 80 | "i = 1\n", 81 | "while i <= n:\n", 82 | " spaces = 1\n", 83 | " while spaces <= n-i:\n", 84 | " print(\" \", end = '')\n", 85 | " spaces = spaces + 1\n", 86 | " inc_num = 1\n", 87 | " while inc_num <= i:\n", 88 | " print(inc_num, end = '')\n", 89 | " inc_num = inc_num + 1\n", 90 | " print()\n", 91 | " i = i + 1\n", 92 | " \n", 93 | " " 94 | ] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "Double Triangle\n", 101 | " 1\n", 102 | " 121\n", 103 | " 12321\n", 104 | " 1234321\n", 105 | "123454321 " 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": 23, 111 | "metadata": {}, 112 | "outputs": [ 113 | { 114 | "name": "stdout", 115 | "output_type": "stream", 116 | "text": [ 117 | "5\n", 118 | " 1\n", 119 | " 121\n", 120 | " 12321\n", 121 | " 1234321\n", 122 | "123454321\n" 123 | ] 124 | } 125 | ], 126 | "source": [ 127 | "n = int(input())\n", 128 | "i = 1\n", 129 | "while i <= n:\n", 130 | " spaces = 1\n", 131 | " while spaces <= n-i:\n", 132 | " print(\" \", end = '')\n", 133 | " spaces = spaces + 1\n", 134 | " inc_num = 1\n", 135 | " while inc_num <= i:\n", 136 | " print(inc_num, end = '')\n", 137 | " inc_num = inc_num + 1\n", 138 | " dec_num = inc_num - 2\n", 139 | " while dec_num > 0:\n", 140 | " print(dec_num, end = '')\n", 141 | " dec_num = dec_num - 1\n", 142 | " print()\n", 143 | " i = i + 1\n", 144 | " \n", 145 | " " 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": null, 151 | "metadata": {}, 152 | "outputs": [], 153 | "source": [] 154 | } 155 | ], 156 | "metadata": { 157 | "kernelspec": { 158 | "display_name": "Python 3", 159 | "language": "python", 160 | "name": "python3" 161 | }, 162 | "language_info": { 163 | "codemirror_mode": { 164 | "name": "ipython", 165 | "version": 3 166 | }, 167 | "file_extension": ".py", 168 | "mimetype": "text/x-python", 169 | "name": "python", 170 | "nbconvert_exporter": "python", 171 | "pygments_lexer": "ipython3", 172 | "version": "3.7.3" 173 | } 174 | }, 175 | "nbformat": 4, 176 | "nbformat_minor": 2 177 | } 178 | -------------------------------------------------------------------------------- /2 Patterns 1/2.4 Patterns 2 Practice.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "1 1\n", 8 | "12 21\n", 9 | "123 321\n", 10 | "1234 4321\n", 11 | "1234554321" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 6, 17 | "metadata": {}, 18 | "outputs": [ 19 | { 20 | "name": "stdout", 21 | "output_type": "stream", 22 | "text": [ 23 | "5\n", 24 | "1 1\n", 25 | "12 21\n", 26 | "123 321\n", 27 | "1234 4321\n", 28 | "1234554321\n" 29 | ] 30 | } 31 | ], 32 | "source": [ 33 | "n = int(input())\n", 34 | "i = 1\n", 35 | "while i <= n:\n", 36 | " spaces = 1\n", 37 | " start = 1\n", 38 | " while start <= i:\n", 39 | " print(start, end = '')\n", 40 | " start = start + 1\n", 41 | " while spaces <= ((2*n) - (2*i)):\n", 42 | " print(' ', end = '')\n", 43 | " spaces = spaces + 1\n", 44 | " end = i\n", 45 | " while end > 0:\n", 46 | " print(end, end = '')\n", 47 | " end = end - 1\n", 48 | " \n", 49 | " print()\n", 50 | " i = i + 1" 51 | ] 52 | }, 53 | { 54 | "cell_type": "markdown", 55 | "metadata": {}, 56 | "source": [ 57 | "For n = 4\n", 58 | "\n", 59 | "*000*000*\n", 60 | "0*00*00*0\n", 61 | "00*0*0*00\n", 62 | "000***000" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 40, 68 | "metadata": {}, 69 | "outputs": [ 70 | { 71 | "name": "stdout", 72 | "output_type": "stream", 73 | "text": [ 74 | "4\n", 75 | "*000*000*\n", 76 | "0*00*00*0\n", 77 | "00*0*0*00\n", 78 | "000***000\n" 79 | ] 80 | } 81 | ], 82 | "source": [ 83 | "n = int(input())\n", 84 | "i = 1\n", 85 | "star = 1\n", 86 | "while i<=n:\n", 87 | " j = 1\n", 88 | " while j <= 2*n + 1:\n", 89 | " if star == i and star == j:\n", 90 | " print('*', end = \"\")\n", 91 | " star = star + 1\n", 92 | " elif j == n + 1 or j == 2*n+2 - i:\n", 93 | " print('*', end = \"\")\n", 94 | " else:\n", 95 | " print(0, end = \"\")\n", 96 | " j = j + 1\n", 97 | " print()\n", 98 | " i = i + 1" 99 | ] 100 | }, 101 | { 102 | "cell_type": "markdown", 103 | "metadata": {}, 104 | "source": [ 105 | " 1\n", 106 | " 212\n", 107 | " 32123\n", 108 | "4321234\n", 109 | "\n", 110 | "First try to divide the problem in parts: \n", 111 | " 1\n", 112 | " 21\n", 113 | " 321\n", 114 | "4321" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 58, 120 | "metadata": {}, 121 | "outputs": [ 122 | { 123 | "name": "stdout", 124 | "output_type": "stream", 125 | "text": [ 126 | "4\n", 127 | " 1\n", 128 | " 21\n", 129 | " 321\n", 130 | "4321\n" 131 | ] 132 | } 133 | ], 134 | "source": [ 135 | "n = int(input())\n", 136 | "i = 1\n", 137 | "while i <= n:\n", 138 | " spaces = 1\n", 139 | " while spaces <= n-i:\n", 140 | " print(\" \", end = '')\n", 141 | " spaces = spaces + 1\n", 142 | " dec_num = i\n", 143 | " while dec_num > 0:\n", 144 | " print(dec_num, end = '')\n", 145 | " dec_num = dec_num - 1 \n", 146 | " print()\n", 147 | " i = i + 1\n", 148 | " \n", 149 | " " 150 | ] 151 | }, 152 | { 153 | "cell_type": "markdown", 154 | "metadata": {}, 155 | "source": [ 156 | "Now, mirror the other part\n", 157 | "\n", 158 | " 1\n", 159 | " 212\n", 160 | " 32123\n", 161 | "4321234" 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": 60, 167 | "metadata": {}, 168 | "outputs": [ 169 | { 170 | "name": "stdout", 171 | "output_type": "stream", 172 | "text": [ 173 | "4\n", 174 | " 1\n", 175 | " 212\n", 176 | " 32123\n", 177 | "4321234\n" 178 | ] 179 | } 180 | ], 181 | "source": [ 182 | "n = int(input())\n", 183 | "i = 1\n", 184 | "while i <= n:\n", 185 | " spaces = 1\n", 186 | " while spaces <= n-i:\n", 187 | " print(\" \", end = '')\n", 188 | " spaces = spaces + 1\n", 189 | " dec_num = i\n", 190 | " while dec_num > 0:\n", 191 | " print(dec_num, end = '')\n", 192 | " dec_num = dec_num - 1\n", 193 | " inc_num = 2\n", 194 | " while inc_num <= i:\n", 195 | " print(inc_num, end = '')\n", 196 | " inc_num = inc_num + 1\n", 197 | " \n", 198 | " print()\n", 199 | " i = i + 1\n", 200 | " \n", 201 | " " 202 | ] 203 | }, 204 | { 205 | "cell_type": "markdown", 206 | "metadata": {}, 207 | "source": [ 208 | "Note: There is a space after every star [Use print('*', end = ' ')]\n", 209 | "\n", 210 | "If N = 7\n", 211 | "\n", 212 | "*\n", 213 | " * *\n", 214 | " * * *\n", 215 | " * * * *\n", 216 | " * * *\n", 217 | " * *\n", 218 | "*\n" 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": null, 224 | "metadata": {}, 225 | "outputs": [], 226 | "source": [ 227 | "n = int(input())\n", 228 | "i = 1\n", 229 | "while i <= n/2 + 1:\n", 230 | " spaces = 1\n", 231 | " while spaces < i:\n", 232 | " print(\" \", end = '')\n", 233 | " spaces = spaces + 1\n", 234 | " j = 1 \n", 235 | " while j <= i:\n", 236 | " print('*', end = ' ')\n", 237 | " j = j + 1\n", 238 | " print()\n", 239 | " i = i + 1\n", 240 | "\n", 241 | "while i > n/2 + 1 and i <= n:\n", 242 | " spaces = n - i\n", 243 | " while spaces > 0: \n", 244 | " print(\" \", end = '')\n", 245 | " spaces = spaces - 1 \n", 246 | " j = 1 \n", 247 | " while j <= n-i+1:\n", 248 | " print('*', end = ' ')\n", 249 | " j = j + 1\n", 250 | " print()\n", 251 | " i = i + 1\n", 252 | " \n" 253 | ] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": null, 258 | "metadata": {}, 259 | "outputs": [], 260 | "source": [ 261 | "2+3\n" 262 | ] 263 | } 264 | ], 265 | "metadata": { 266 | "kernelspec": { 267 | "display_name": "Python 3", 268 | "language": "python", 269 | "name": "python3" 270 | }, 271 | "language_info": { 272 | "codemirror_mode": { 273 | "name": "ipython", 274 | "version": 3 275 | }, 276 | "file_extension": ".py", 277 | "mimetype": "text/x-python", 278 | "name": "python", 279 | "nbconvert_exporter": "python", 280 | "pygments_lexer": "ipython3", 281 | "version": "3.7.3" 282 | } 283 | }, 284 | "nbformat": 4, 285 | "nbformat_minor": 2 286 | } 287 | -------------------------------------------------------------------------------- /4 Functions/4.1 Functions Demo.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### ncr (Factorial of a number)" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Syntax of a function:- def function_name(args):" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 5, 20 | "metadata": {}, 21 | "outputs": [ 22 | { 23 | "name": "stdout", 24 | "output_type": "stream", 25 | "text": [ 26 | "4\n", 27 | "2\n", 28 | "6\n" 29 | ] 30 | } 31 | ], 32 | "source": [ 33 | "n = int(input())\n", 34 | "r = int(input())\n", 35 | "\n", 36 | "n_fact = 1\n", 37 | "for i in range(1, n + 1):\n", 38 | " n_fact = n_fact * i\n", 39 | "\n", 40 | "\n", 41 | "r_fact = 1\n", 42 | "for i in range(1, r + 1):\n", 43 | " r_fact = r_fact * i\n", 44 | "\n", 45 | "\n", 46 | "n_r_fact = 1\n", 47 | "for i in range(1, n - r + 1):\n", 48 | " n_r_fact = n_r_fact * i\n", 49 | "\n", 50 | "ans = n_fact//(r_fact * n_r_fact)\n", 51 | "print(ans)" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 14, 57 | "metadata": {}, 58 | "outputs": [ 59 | { 60 | "name": "stdout", 61 | "output_type": "stream", 62 | "text": [ 63 | "12\n", 64 | "479001600\n" 65 | ] 66 | } 67 | ], 68 | "source": [ 69 | "def fact(n):\n", 70 | " n_fact = 1\n", 71 | " for i in range(1, n + 1):\n", 72 | " n_fact = n_fact * i\n", 73 | " return n_fact\n", 74 | "\n", 75 | "n = int(input())\n", 76 | "print(fact(n))\n", 77 | " \n", 78 | " " 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 17, 84 | "metadata": {}, 85 | "outputs": [ 86 | { 87 | "name": "stdout", 88 | "output_type": "stream", 89 | "text": [ 90 | "5\n", 91 | "2\n", 92 | "10\n" 93 | ] 94 | } 95 | ], 96 | "source": [ 97 | "n = int(input())\n", 98 | "r = int(input())\n", 99 | "n_fact = fact(n)\n", 100 | "r_fact = fact(r)\n", 101 | "n_r_fact = fact(n-r)\n", 102 | "\n", 103 | "ans = n_fact//(r_fact * n_r_fact)\n", 104 | "print(ans)" 105 | ] 106 | }, 107 | { 108 | "cell_type": "markdown", 109 | "metadata": {}, 110 | "source": [ 111 | "### Prime or Not" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": 6, 117 | "metadata": {}, 118 | "outputs": [ 119 | { 120 | "name": "stdout", 121 | "output_type": "stream", 122 | "text": [ 123 | "562634567\n", 124 | "562634567 is not prime\n" 125 | ] 126 | } 127 | ], 128 | "source": [ 129 | "def isPrime(n):\n", 130 | " for i in range(2,n):\n", 131 | " if(n % i == 0):\n", 132 | " break\n", 133 | " else:\n", 134 | " return True\n", 135 | " return False\n", 136 | "\n", 137 | "n = int(input())\n", 138 | "if isPrime(n):\n", 139 | " print(n, \"is prime\")\n", 140 | "else:\n", 141 | " print(n, \"is not prime\")\n" 142 | ] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "metadata": {}, 147 | "source": [ 148 | "### Print Prime Numbers from 2 to N" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": 14, 154 | "metadata": {}, 155 | "outputs": [ 156 | { 157 | "name": "stdout", 158 | "output_type": "stream", 159 | "text": [ 160 | "20\n", 161 | "2\n", 162 | "3\n", 163 | "5\n", 164 | "7\n", 165 | "11\n", 166 | "13\n", 167 | "17\n", 168 | "19\n" 169 | ] 170 | } 171 | ], 172 | "source": [ 173 | "def primeFrom2ToN(n):\n", 174 | " for k in range(2, n+1):\n", 175 | " if(isPrime(k)):\n", 176 | " print(k)\n", 177 | "def isPrime(n):\n", 178 | " for i in range(2,n):\n", 179 | " if(n % i == 0):\n", 180 | " break\n", 181 | " else:\n", 182 | " return True\n", 183 | " return False\n", 184 | "\n", 185 | "n = int(input())\n", 186 | "primeFrom2ToN(n) " 187 | ] 188 | }, 189 | { 190 | "cell_type": "markdown", 191 | "metadata": {}, 192 | "source": [ 193 | "### Function to calculate ncr" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": 17, 199 | "metadata": {}, 200 | "outputs": [], 201 | "source": [ 202 | "def ncr(n, r):\n", 203 | " n_fact = fact(n)\n", 204 | " r_fact = fact(r)\n", 205 | " n_r_fact = fact(n-r)\n", 206 | " ans = n_fact//(r_fact * n_r_fact)\n", 207 | " return ans\n", 208 | "\n", 209 | "def fact(n):\n", 210 | " n_fact = 1\n", 211 | " for i in range(1, n + 1):\n", 212 | " n_fact = n_fact * i\n", 213 | " return n_fact" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": 18, 219 | "metadata": {}, 220 | "outputs": [ 221 | { 222 | "data": { 223 | "text/plain": [ 224 | "10" 225 | ] 226 | }, 227 | "execution_count": 18, 228 | "metadata": {}, 229 | "output_type": "execute_result" 230 | } 231 | ], 232 | "source": [ 233 | "ncr(5, 2)" 234 | ] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "execution_count": 21, 239 | "metadata": {}, 240 | "outputs": [ 241 | { 242 | "name": "stdout", 243 | "output_type": "stream", 244 | "text": [ 245 | "15\n" 246 | ] 247 | } 248 | ], 249 | "source": [ 250 | "# Predict the output\n", 251 | "def func(a):\n", 252 | " a = a + 10\n", 253 | " return a\n", 254 | "a = 5\n", 255 | "func(a)\n", 256 | "print(func(a))" 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "execution_count": 22, 262 | "metadata": {}, 263 | "outputs": [ 264 | { 265 | "name": "stdout", 266 | "output_type": "stream", 267 | "text": [ 268 | "16\n" 269 | ] 270 | } 271 | ], 272 | "source": [ 273 | "# Predict the output\n", 274 | "def square(a):\n", 275 | " ans = a*a\n", 276 | " return ans\n", 277 | "\n", 278 | "a = 4\n", 279 | "a = square(a)\n", 280 | "print(a)" 281 | ] 282 | }, 283 | { 284 | "cell_type": "code", 285 | "execution_count": null, 286 | "metadata": {}, 287 | "outputs": [], 288 | "source": [] 289 | } 290 | ], 291 | "metadata": { 292 | "kernelspec": { 293 | "display_name": "Python 3", 294 | "language": "python", 295 | "name": "python3" 296 | }, 297 | "language_info": { 298 | "codemirror_mode": { 299 | "name": "ipython", 300 | "version": 3 301 | }, 302 | "file_extension": ".py", 303 | "mimetype": "text/x-python", 304 | "name": "python", 305 | "nbconvert_exporter": "python", 306 | "pygments_lexer": "ipython3", 307 | "version": "3.7.3" 308 | } 309 | }, 310 | "nbformat": 4, 311 | "nbformat_minor": 2 312 | } 313 | -------------------------------------------------------------------------------- /4 Functions/4.2 Scope of Variables.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 5, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "10\n", 13 | "12 Inside f1()" 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "a1 = 10 #global variable\n", 19 | "\n", 20 | "def f1():\n", 21 | " b1 = 12 #local variable\n", 22 | " print(b1, end = ' Inside f1()')\n", 23 | "\n", 24 | "print(a1)\n", 25 | "f1()\n", 26 | "# Cannot print a local variable outside the function print(b1)" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 8, 32 | "metadata": {}, 33 | "outputs": [ 34 | { 35 | "name": "stdout", 36 | "output_type": "stream", 37 | "text": [ 38 | "10\n", 39 | "12 Inside f1() \n", 40 | "10\n" 41 | ] 42 | } 43 | ], 44 | "source": [ 45 | "a2 = 10 #global variable\n", 46 | "\n", 47 | "def f2():\n", 48 | " b2 = 12 #local variable\n", 49 | " print(b2, end = ' Inside f1() \\n')\n", 50 | " print(a2)\n", 51 | "\n", 52 | "print(a2)\n", 53 | "f2()" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 12, 59 | "metadata": {}, 60 | "outputs": [ 61 | { 62 | "name": "stdout", 63 | "output_type": "stream", 64 | "text": [ 65 | "10\n", 66 | "12 Inside f1() \n", 67 | "10\n" 68 | ] 69 | } 70 | ], 71 | "source": [ 72 | "# You can access any global variable defined before the function call\n", 73 | "# Here c3 cannot be accessed because you've declared it after the funciton call\n", 74 | "\n", 75 | "def f3():\n", 76 | " b3 = 12 #local variable\n", 77 | " print(b3, end = ' Inside f1() \\n')\n", 78 | " print(a3)\n", 79 | " #rint(c3)\n", 80 | " \n", 81 | "a3 = 10 #global variable\n", 82 | "print(a3)\n", 83 | "f3()\n", 84 | "c3 = 9" 85 | ] 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "metadata": {}, 90 | "source": [ 91 | "### Local v/s Global variable" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 14, 97 | "metadata": {}, 98 | "outputs": [ 99 | { 100 | "name": "stdout", 101 | "output_type": "stream", 102 | "text": [ 103 | "13\n", 104 | "12\n", 105 | "Id of a4 (local) 140731172820128\n", 106 | "13\n", 107 | "Id of a4 (global) 140731172820160\n" 108 | ] 109 | } 110 | ], 111 | "source": [ 112 | "a4 = 13\n", 113 | "def f4():\n", 114 | " a4 = 12 # Python assumes you want to create a local variable \n", 115 | " print(a4) #12\n", 116 | " print(\"Id of a4 (local)\", id(a4))\n", 117 | "print(a4) #13\n", 118 | "f4()\n", 119 | "print(a4) #13 \n", 120 | "print(\"Id of a4 (global)\", id(a4))" 121 | ] 122 | }, 123 | { 124 | "cell_type": "markdown", 125 | "metadata": {}, 126 | "source": [ 127 | "### How to access global variable in a local scope (inside a function) ?\n", 128 | "### global keyword" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 18, 134 | "metadata": {}, 135 | "outputs": [ 136 | { 137 | "name": "stdout", 138 | "output_type": "stream", 139 | "text": [ 140 | "13\n", 141 | "12\n", 142 | "Id of a4 (inside the function) 140731172820128\n", 143 | "12\n", 144 | "Id of a4 (outside the function) 140731172820128\n" 145 | ] 146 | } 147 | ], 148 | "source": [ 149 | "a4 = 13\n", 150 | "def f4():\n", 151 | " global a4\n", 152 | " a4 = 12 # Python assumes you want to change the value of a global variable\n", 153 | " print(a4) #12\n", 154 | " print(\"Id of a4 (inside the function)\", id(a4))\n", 155 | "print(a4) #13\n", 156 | "f4()\n", 157 | "print(a4) #13 \n", 158 | "print(\"Id of a4 (outside the function)\", id(a4))" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": 22, 164 | "metadata": {}, 165 | "outputs": [ 166 | { 167 | "name": "stdout", 168 | "output_type": "stream", 169 | "text": [ 170 | "10\n" 171 | ] 172 | } 173 | ], 174 | "source": [ 175 | "def f():\n", 176 | " b = 10\n", 177 | " return b\n", 178 | "x = f()\n", 179 | "print(x)" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": 23, 185 | "metadata": {}, 186 | "outputs": [ 187 | { 188 | "name": "stdout", 189 | "output_type": "stream", 190 | "text": [ 191 | "14\n" 192 | ] 193 | } 194 | ], 195 | "source": [ 196 | "#predict the output\n", 197 | "a = 14\n", 198 | "def f():\n", 199 | " a=12\n", 200 | "f()\n", 201 | "print(a)" 202 | ] 203 | }, 204 | { 205 | "cell_type": "code", 206 | "execution_count": 24, 207 | "metadata": {}, 208 | "outputs": [ 209 | { 210 | "name": "stdout", 211 | "output_type": "stream", 212 | "text": [ 213 | "12\n" 214 | ] 215 | } 216 | ], 217 | "source": [ 218 | "#predict the output\n", 219 | "a=14\n", 220 | "def f():\n", 221 | " global a\n", 222 | " a=12\n", 223 | "f()\n", 224 | "print(a)" 225 | ] 226 | }, 227 | { 228 | "cell_type": "code", 229 | "execution_count": 25, 230 | "metadata": {}, 231 | "outputs": [ 232 | { 233 | "name": "stdout", 234 | "output_type": "stream", 235 | "text": [ 236 | "12\n" 237 | ] 238 | } 239 | ], 240 | "source": [ 241 | "#predict the output\n", 242 | "a = 14\n", 243 | "def f():\n", 244 | " a = 12\n", 245 | " return a\n", 246 | "a = f()\n", 247 | "print(a)" 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": null, 253 | "metadata": {}, 254 | "outputs": [], 255 | "source": [] 256 | } 257 | ], 258 | "metadata": { 259 | "kernelspec": { 260 | "display_name": "Python 3", 261 | "language": "python", 262 | "name": "python3" 263 | }, 264 | "language_info": { 265 | "codemirror_mode": { 266 | "name": "ipython", 267 | "version": 3 268 | }, 269 | "file_extension": ".py", 270 | "mimetype": "text/x-python", 271 | "name": "python", 272 | "nbconvert_exporter": "python", 273 | "pygments_lexer": "ipython3", 274 | "version": "3.7.3" 275 | } 276 | }, 277 | "nbformat": 4, 278 | "nbformat_minor": 2 279 | } 280 | -------------------------------------------------------------------------------- /4 Functions/4.3 Default arguments in function.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "range() is not a function\n", 8 | "\n", 9 | "print() is a function\n", 10 | "print(\"abc\")\n", 11 | "print(\"abc\", end = \" \")\n" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 5, 17 | "metadata": {}, 18 | "outputs": [ 19 | { 20 | "name": "stdout", 21 | "output_type": "stream", 22 | "text": [ 23 | "4\n", 24 | "9\n", 25 | "0\n", 26 | "7\n" 27 | ] 28 | } 29 | ], 30 | "source": [ 31 | "def sum(a, b, c = 0): #Python allows you to give a default value of 0 to an argument\n", 32 | " print(c)\n", 33 | " return a+b+c\n", 34 | "\n", 35 | "print(sum(2,3,4))\n", 36 | "print(sum(3,4))\n" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 7, 42 | "metadata": {}, 43 | "outputs": [ 44 | { 45 | "ename": "SyntaxError", 46 | "evalue": "non-default argument follows default argument (, line 1)", 47 | "output_type": "error", 48 | "traceback": [ 49 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m def f(a = 0, b, c): #All non-default arguments should be before arguments with default values\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m non-default argument follows default argument\n" 50 | ] 51 | } 52 | ], 53 | "source": [ 54 | "def f(a = 0, b, c): #All non-default arguments should be before arguments with default values\n", 55 | " return a+b+c\n", 56 | "f(1,2)" 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": {}, 62 | "source": [ 63 | "### Non-default arguments should be written before default arguments" 64 | ] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "metadata": {}, 69 | "source": [ 70 | "#### If you're passing the arguments with names then order doesn't matters. If you're directly passing the arguments then order should be maintained" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 14, 76 | "metadata": {}, 77 | "outputs": [ 78 | { 79 | "name": "stdout", 80 | "output_type": "stream", 81 | "text": [ 82 | "14\n", 83 | "9\n", 84 | "7\n", 85 | "10\n", 86 | "13\n" 87 | ] 88 | } 89 | ], 90 | "source": [ 91 | "def f1(a, b, c=2, d = 0):\n", 92 | " return a + b + c + d\n", 93 | "print(f1(2,3,4,5))\n", 94 | "print(f1(2,3,4))\n", 95 | "print(f1(2,3))\n", 96 | "\n", 97 | "print(f1(2, 3, d = 3)) # You can also pass a, b and d values\n", 98 | "print(f1(d = 1, b = 2, c = 6, a = 4))\n" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 16, 104 | "metadata": {}, 105 | "outputs": [ 106 | { 107 | "name": "stdout", 108 | "output_type": "stream", 109 | "text": [ 110 | "21\n" 111 | ] 112 | } 113 | ], 114 | "source": [ 115 | "#Predict the output\n", 116 | "def function(a,b,c=1):\n", 117 | " return a+b-c\n", 118 | "value = function(10,12)\n", 119 | "print(value)" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 17, 125 | "metadata": {}, 126 | "outputs": [ 127 | { 128 | "name": "stdout", 129 | "output_type": "stream", 130 | "text": [ 131 | "17\n" 132 | ] 133 | } 134 | ], 135 | "source": [ 136 | "#Predict the output\n", 137 | "def function(a,b,c=1):\n", 138 | " return a+b-c\n", 139 | "value = function(10,12,5)\n", 140 | "print(value)" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 18, 146 | "metadata": {}, 147 | "outputs": [ 148 | { 149 | "name": "stdout", 150 | "output_type": "stream", 151 | "text": [ 152 | "11\n" 153 | ] 154 | } 155 | ], 156 | "source": [ 157 | "#Predict the output\n", 158 | "def function(a,b,c=1,d=5):\n", 159 | " return a+b+c+d\n", 160 | "value = function(1,2,d=7)\n", 161 | "print(value)" 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": null, 167 | "metadata": {}, 168 | "outputs": [], 169 | "source": [] 170 | } 171 | ], 172 | "metadata": { 173 | "kernelspec": { 174 | "display_name": "Python 3", 175 | "language": "python", 176 | "name": "python3" 177 | }, 178 | "language_info": { 179 | "codemirror_mode": { 180 | "name": "ipython", 181 | "version": 3 182 | }, 183 | "file_extension": ".py", 184 | "mimetype": "text/x-python", 185 | "name": "python", 186 | "nbconvert_exporter": "python", 187 | "pygments_lexer": "ipython3", 188 | "version": "3.7.3" 189 | } 190 | }, 191 | "nbformat": 4, 192 | "nbformat_minor": 2 193 | } 194 | -------------------------------------------------------------------------------- /4 Functions/4.4 Practice Functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Faherenheit to Celsius Functions" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 3, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "name": "stdout", 17 | "output_type": "stream", 18 | "text": [ 19 | "0\n", 20 | "100\n", 21 | "20\n", 22 | "0 \t -17\n", 23 | "20 \t -6\n", 24 | "40 \t 4\n", 25 | "60 \t 15\n", 26 | "80 \t 26\n", 27 | "100 \t 37\n" 28 | ] 29 | } 30 | ], 31 | "source": [ 32 | "def printTable(start,end,step):\n", 33 | " while(start <= end):\n", 34 | " C = (start - 32) * (5/9)\n", 35 | " print(start,\"\\t\",int( C))\n", 36 | " start = start + step \n", 37 | " pass \n", 38 | "\n", 39 | "s = int(input())\n", 40 | "e = int(input())\n", 41 | "step = int(input())\n", 42 | "printTable(s,e,step)\n" 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "### Fibonacci Member Functions" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": null, 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [ 58 | "f = 0\n", 59 | "s = 1\n", 60 | "t = 1\n", 61 | "\n", 62 | "def checkMember(n):\n", 63 | " global f\n", 64 | " global s\n", 65 | " global t\n", 66 | " while t <= n:\n", 67 | " t = f + s\n", 68 | " f = s\n", 69 | " s = t\n", 70 | " if f == n:\n", 71 | " return 1\n", 72 | " pass\n", 73 | "\n", 74 | "n=int(input())\n", 75 | "if(checkMember(n)):\n", 76 | " print(\"true\")\n", 77 | "else:\n", 78 | " print(\"false\")" 79 | ] 80 | }, 81 | { 82 | "cell_type": "markdown", 83 | "metadata": {}, 84 | "source": [ 85 | "### Palindrome Number" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": null, 91 | "metadata": {}, 92 | "outputs": [], 93 | "source": [ 94 | "def reverse(n):\n", 95 | " rev = 0\n", 96 | " while(n >0):\n", 97 | " rem = n % 10\n", 98 | " rev = (rev * 10) + rem\n", 99 | " n = n//10\n", 100 | " return rev \n", 101 | "\n", 102 | "def checkPalindrome(num):\n", 103 | " if reverse(num):\n", 104 | " return 1\n", 105 | " else:\n", 106 | " return 0\n", 107 | " \n", 108 | "\tpass\n", 109 | "\t\t\n", 110 | "num = int(input())\n", 111 | "isPalindrome = checkPalindrome(num)\n", 112 | "if(isPalindrome):\n", 113 | "\tprint('true')\n", 114 | "else:\n", 115 | "\tprint('false')\n" 116 | ] 117 | }, 118 | { 119 | "cell_type": "markdown", 120 | "metadata": {}, 121 | "source": [ 122 | "### Check Palindrome number or not" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": null, 128 | "metadata": {}, 129 | "outputs": [], 130 | "source": [ 131 | "def reverse(n):\n", 132 | " rev = 0\n", 133 | " while(n >0):\n", 134 | " rem = n % 10\n", 135 | " rev = (rev * 10) + rem\n", 136 | " n = n//10\n", 137 | " return rev \n", 138 | "\n", 139 | "def checkPalindrome(num):\n", 140 | " rev = reverse(num)\n", 141 | " if rev == num:\n", 142 | " return 1\n", 143 | " else:\n", 144 | " return 0\n", 145 | " \n", 146 | "\t\t\n", 147 | "num = int(input())\n", 148 | "isPalindrome = checkPalindrome(num)\n", 149 | "if(isPalindrome):\n", 150 | "\tprint('true')\n", 151 | "else:\n", 152 | "\tprint('false')\n" 153 | ] 154 | }, 155 | { 156 | "cell_type": "markdown", 157 | "metadata": {}, 158 | "source": [ 159 | "### Armstrong number or not" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": null, 165 | "metadata": {}, 166 | "outputs": [], 167 | "source": [ 168 | "def armstrong(n):\n", 169 | " arm = 0\n", 170 | " while n > 0:\n", 171 | " rem = n % 10\n", 172 | " arm = arm + (rem * rem * rem)\n", 173 | " n = n // 10\n", 174 | " return arm\n", 175 | "\n", 176 | "n = int(input())\n", 177 | "temp = n\n", 178 | "x = armstrong(n)\n", 179 | "if temp == x:\n", 180 | " print(\"true\")\n", 181 | "else:\n", 182 | " print(\"false\")" 183 | ] 184 | }, 185 | { 186 | "cell_type": "markdown", 187 | "metadata": {}, 188 | "source": [ 189 | "### Write a Program to determine if the given number is Armstrong number or not. Print true if number is armstrong, otherwise print false.\n", 190 | "An Armstrong number is a number (with digits n) such that the sum of its digits raised to nth power is equal to the number itself.\n", 191 | "For example,\n", 192 | "371, as 3^3 + 7^3 + 1^3 = 371\n", 193 | "1634, as 1^4 + 6^4 + 3^4 + 4^4 = 1634\n", 194 | "Input Format :\n", 195 | "Integer n\n", 196 | "Output Format :\n", 197 | "true or false" 198 | ] 199 | }, 200 | { 201 | "cell_type": "code", 202 | "execution_count": null, 203 | "metadata": {}, 204 | "outputs": [], 205 | "source": [ 206 | "def armstrong(n):\n", 207 | " d = 0\n", 208 | " temp = n\n", 209 | " while temp > 0:\n", 210 | " temp = temp // 10\n", 211 | " d = d + 1\n", 212 | " arm = 0\n", 213 | " while n > 0:\n", 214 | " rem = n % 10\n", 215 | " arm = arm + rem ** d \n", 216 | " n = n // 10\n", 217 | " return arm\n", 218 | "\n", 219 | "n = int(input())\n", 220 | "temp = n\n", 221 | "x = armstrong(n)\n", 222 | "if temp == x:\n", 223 | " print(\"true\")\n", 224 | "else:\n", 225 | " print(\"false\")" 226 | ] 227 | } 228 | ], 229 | "metadata": { 230 | "kernelspec": { 231 | "display_name": "Python 3", 232 | "language": "python", 233 | "name": "python3" 234 | } 235 | }, 236 | "nbformat": 4, 237 | "nbformat_minor": 2 238 | } 239 | -------------------------------------------------------------------------------- /5 Arrays/5.10 [Practice] Arrays & Lists.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "n = int(input())\n", 10 | "li = [int(x) for x in input().split()]\n", 11 | "length = len(li)\n", 12 | "if length % 2 == 1:\n", 13 | " length = length - 1 \n", 14 | "for i in range(0,length,2):\n", 15 | " li[i], li[i+1] = li[i+1], li[i] \n", 16 | "li\n" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "### Find Unique" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 12, 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "name": "stdout", 33 | "output_type": "stream", 34 | "text": [ 35 | "7\n", 36 | "2 3 6 1 3 6 2\n", 37 | "1\n" 38 | ] 39 | } 40 | ], 41 | "source": [ 42 | "n = int(input())\n", 43 | "li = [int(x) for x in input().split()]\n", 44 | "\n", 45 | "li.sort()\n", 46 | "k = 0\n", 47 | "for i in range(0, len(li), 1):\n", 48 | " i = k\n", 49 | " if(li[i]==li[i+1]):\n", 50 | " li.remove(li[i])\n", 51 | " li.remove(li[i])\n", 52 | " \n", 53 | " if len(li) == 1:\n", 54 | " break\n", 55 | " else:\n", 56 | " k = k + 1\n", 57 | "for ele in li:\n", 58 | " print(ele)" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": {}, 64 | "source": [ 65 | "### Find Duplicate" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 13, 71 | "metadata": {}, 72 | "outputs": [ 73 | { 74 | "name": "stdout", 75 | "output_type": "stream", 76 | "text": [ 77 | "8\n", 78 | "1 2 54 87 69 54 23 6\n", 79 | "54\n" 80 | ] 81 | } 82 | ], 83 | "source": [ 84 | "n = int(input())\n", 85 | "list = [int(x) for x in input().split()[:n]]\n", 86 | "#list = [0, 7, 2, 5, 4, 7, 1, 3, 6]\n", 87 | "list.sort()\n", 88 | "for i in range(0, len(list) - 1, 1):\n", 89 | " if list[i] == list[i+1]:\n", 90 | " print(list[i])\n" 91 | ] 92 | }, 93 | { 94 | "cell_type": "markdown", 95 | "metadata": {}, 96 | "source": [ 97 | "### Intersection of Arrays" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 2, 103 | "metadata": {}, 104 | "outputs": [ 105 | { 106 | "name": "stdout", 107 | "output_type": "stream", 108 | "text": [ 109 | "5\n", 110 | "1 2 3 4 5\n", 111 | "3\n", 112 | "1 2 3\n", 113 | "1\n", 114 | "2\n", 115 | "3\n" 116 | ] 117 | } 118 | ], 119 | "source": [ 120 | "m = int(input()) \n", 121 | "li1 = [int(x) for x in input().split()[:m]] \n", 122 | "n = int(input()) \n", 123 | "li2 = [int(x) for x in input().split()[:n]] \n", 124 | "li3 = [] \n", 125 | "for i in li1:\n", 126 | " for j in li2:\n", 127 | " if i == j:\n", 128 | " li3.append(i) \n", 129 | " li2.remove(j) \n", 130 | " break \n", 131 | "for ele in li3:\n", 132 | " print(ele) " 133 | ] 134 | }, 135 | { 136 | "cell_type": "markdown", 137 | "metadata": {}, 138 | "source": [ 139 | "### Pair Sum" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": 3, 145 | "metadata": {}, 146 | "outputs": [ 147 | { 148 | "name": "stdout", 149 | "output_type": "stream", 150 | "text": [ 151 | "9\n", 152 | "1 3 6 2 5 4 3 2 4\n", 153 | "7\n", 154 | "1 6\n", 155 | "3 4\n", 156 | "3 4\n", 157 | "2 5\n", 158 | "2 5\n", 159 | "3 4\n", 160 | "3 4\n" 161 | ] 162 | } 163 | ], 164 | "source": [ 165 | "def pair_sum(a,b):\n", 166 | " if a <= b:\n", 167 | " print(a,\" \",b)\n", 168 | " else:\n", 169 | " print(b,\" \",a)\n", 170 | " \n", 171 | "n = int(input())\n", 172 | "list = [int(x) for x in input().split()[:n]]\n", 173 | "key = int(input())\n", 174 | "for i in range(0, len(list)-1):\n", 175 | " for j in range(i+1, len(list)):\n", 176 | " if(list[i] + list[j] == key):\n", 177 | " pair_sum(list[i], list[j])\n", 178 | " \n" 179 | ] 180 | }, 181 | { 182 | "cell_type": "markdown", 183 | "metadata": {}, 184 | "source": [ 185 | "### Triplet Sum" 186 | ] 187 | }, 188 | { 189 | "cell_type": "code", 190 | "execution_count": 6, 191 | "metadata": {}, 192 | "outputs": [ 193 | { 194 | "name": "stdout", 195 | "output_type": "stream", 196 | "text": [ 197 | "7\n", 198 | "1 2 3 4 5 6 7\n", 199 | "12\n", 200 | "1 4 7 \n", 201 | "1 5 6 \n", 202 | "2 3 7 \n", 203 | "2 4 6 \n", 204 | "3 4 5 \n" 205 | ] 206 | } 207 | ], 208 | "source": [ 209 | "def pair_sum(a,b,c):\n", 210 | " list3 = []\n", 211 | " list3.append(a)\n", 212 | " list3.append(b)\n", 213 | " list3.append(c)\n", 214 | " list3.sort()\n", 215 | " for ele in list3:\n", 216 | " print(ele, end = ' ')\n", 217 | " print()\n", 218 | " \n", 219 | "n = int(input())\n", 220 | "list = [int(x) for x in input().split()[:n]]\n", 221 | "key = int(input())\n", 222 | "for i in range(0, len(list)-2):\n", 223 | " for j in range(i+1, len(list)-1):\n", 224 | " for k in range(j+1, len(list)):\n", 225 | " if(list[i] + list[j] + list[k] == key):\n", 226 | " pair_sum(list[i], list[j], list[k])\n", 227 | " \n" 228 | ] 229 | }, 230 | { 231 | "cell_type": "markdown", 232 | "metadata": {}, 233 | "source": [ 234 | "### Sort 0 1" 235 | ] 236 | }, 237 | { 238 | "cell_type": "code", 239 | "execution_count": 11, 240 | "metadata": {}, 241 | "outputs": [ 242 | { 243 | "name": "stdout", 244 | "output_type": "stream", 245 | "text": [ 246 | "3\n", 247 | "0 1 0\n", 248 | "0 0 1 " 249 | ] 250 | } 251 | ], 252 | "source": [ 253 | "n = int(input())\n", 254 | "list = [int (x) for x in input().split() [:n]]\n", 255 | "count0 = 0\n", 256 | "count1 = 0\n", 257 | "for i in list:\n", 258 | " if i == 0:\n", 259 | " count0 = count0 + 1\n", 260 | " else:\n", 261 | " count1 = count1 + 1\n", 262 | "list = []\n", 263 | "for i in range(0, count0):\n", 264 | " list.append(0)\n", 265 | "for i in range(0, count1):\n", 266 | " list.append(1)\n", 267 | " \n", 268 | "for ele in list:\n", 269 | " print(ele, end = ' ')" 270 | ] 271 | }, 272 | { 273 | "cell_type": "code", 274 | "execution_count": null, 275 | "metadata": {}, 276 | "outputs": [], 277 | "source": [] 278 | } 279 | ], 280 | "metadata": { 281 | "kernelspec": { 282 | "display_name": "Python 3", 283 | "language": "python", 284 | "name": "python3" 285 | }, 286 | "language_info": { 287 | "codemirror_mode": { 288 | "name": "ipython", 289 | "version": 3 290 | }, 291 | "file_extension": ".py", 292 | "mimetype": "text/x-python", 293 | "name": "python", 294 | "nbconvert_exporter": "python", 295 | "pygments_lexer": "ipython3", 296 | "version": "3.7.3" 297 | } 298 | }, 299 | "nbformat": 4, 300 | "nbformat_minor": 2 301 | } 302 | -------------------------------------------------------------------------------- /5 Arrays/5.3 Looping On Lists.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "li = [1,2,\"Fazeel\", 9,10,11,\"Usmani\"]" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "data": { 19 | "text/plain": [ 20 | "[1, 2, 'Fazeel', 9, 10, 11, 'Usmani']" 21 | ] 22 | }, 23 | "execution_count": 2, 24 | "metadata": {}, 25 | "output_type": "execute_result" 26 | } 27 | ], 28 | "source": [ 29 | "li" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 4, 35 | "metadata": {}, 36 | "outputs": [ 37 | { 38 | "name": "stdout", 39 | "output_type": "stream", 40 | "text": [ 41 | "1\n", 42 | "2\n", 43 | "Fazeel\n", 44 | "9\n", 45 | "10\n", 46 | "11\n", 47 | "Usmani\n" 48 | ] 49 | } 50 | ], 51 | "source": [ 52 | "for i in range(len(li)):\n", 53 | " print(li[i])" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 5, 59 | "metadata": {}, 60 | "outputs": [ 61 | { 62 | "name": "stdout", 63 | "output_type": "stream", 64 | "text": [ 65 | "Fazeel\n", 66 | "9\n", 67 | "10\n", 68 | "11\n", 69 | "Usmani\n" 70 | ] 71 | } 72 | ], 73 | "source": [ 74 | "for i in range(2,len(li)): #starting from a particular index\n", 75 | " print(li[i]) " 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 7, 81 | "metadata": {}, 82 | "outputs": [ 83 | { 84 | "name": "stdout", 85 | "output_type": "stream", 86 | "text": [ 87 | "1\n", 88 | "2\n", 89 | "Fazeel\n", 90 | "9\n", 91 | "10\n", 92 | "11\n", 93 | "Usmani\n" 94 | ] 95 | } 96 | ], 97 | "source": [ 98 | "for ele in li: # To print all elements\n", 99 | " print(ele)" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 8, 105 | "metadata": {}, 106 | "outputs": [ 107 | { 108 | "name": "stdout", 109 | "output_type": "stream", 110 | "text": [ 111 | "Fazeel\n", 112 | "9\n", 113 | "10\n", 114 | "11\n", 115 | "Usmani\n" 116 | ] 117 | } 118 | ], 119 | "source": [ 120 | "for ele in li[2:]: # To start from particular index\n", 121 | " print(ele)" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": 9, 127 | "metadata": {}, 128 | "outputs": [ 129 | { 130 | "name": "stdout", 131 | "output_type": "stream", 132 | "text": [ 133 | "Fazeel\n", 134 | "9\n" 135 | ] 136 | } 137 | ], 138 | "source": [ 139 | "for ele in li[2:4]:\n", 140 | " print(ele)" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 11, 146 | "metadata": {}, 147 | "outputs": [ 148 | { 149 | "name": "stdout", 150 | "output_type": "stream", 151 | "text": [ 152 | "2 3 4 " 153 | ] 154 | } 155 | ], 156 | "source": [ 157 | "#predict the output\n", 158 | "li = [1,2,3,4,5]\n", 159 | "for i in li[1:4]:\n", 160 | " print(i,end= \" \")" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": null, 166 | "metadata": {}, 167 | "outputs": [], 168 | "source": [] 169 | } 170 | ], 171 | "metadata": { 172 | "kernelspec": { 173 | "display_name": "Python 3", 174 | "language": "python", 175 | "name": "python3" 176 | }, 177 | "language_info": { 178 | "codemirror_mode": { 179 | "name": "ipython", 180 | "version": 3 181 | }, 182 | "file_extension": ".py", 183 | "mimetype": "text/x-python", 184 | "name": "python", 185 | "nbconvert_exporter": "python", 186 | "pygments_lexer": "ipython3", 187 | "version": "3.7.3" 188 | } 189 | }, 190 | "nbformat": 4, 191 | "nbformat_minor": 2 192 | } 193 | -------------------------------------------------------------------------------- /5 Arrays/5.6 Linear Search.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "5\n" 13 | ] 14 | } 15 | ], 16 | "source": [ 17 | "n = int(input())" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 2, 23 | "metadata": {}, 24 | "outputs": [ 25 | { 26 | "name": "stdout", 27 | "output_type": "stream", 28 | "text": [ 29 | "2 4 5 7 8 7 \n" 30 | ] 31 | } 32 | ], 33 | "source": [ 34 | "li = [int(x) for x in input().split()]\n" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 3, 40 | "metadata": {}, 41 | "outputs": [ 42 | { 43 | "data": { 44 | "text/plain": [ 45 | "[2, 4, 5, 7, 8, 7]" 46 | ] 47 | }, 48 | "execution_count": 3, 49 | "metadata": {}, 50 | "output_type": "execute_result" 51 | } 52 | ], 53 | "source": [ 54 | "li" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 9, 60 | "metadata": {}, 61 | "outputs": [ 62 | { 63 | "name": "stdout", 64 | "output_type": "stream", 65 | "text": [ 66 | "11\n" 67 | ] 68 | } 69 | ], 70 | "source": [ 71 | "ele = int(input())" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 10, 77 | "metadata": { 78 | "scrolled": true 79 | }, 80 | "outputs": [ 81 | { 82 | "name": "stdout", 83 | "output_type": "stream", 84 | "text": [ 85 | "-1\n" 86 | ] 87 | } 88 | ], 89 | "source": [ 90 | "for i in range(len(li)):\n", 91 | " if li[i] == ele:\n", 92 | " print(i)\n", 93 | " break\n", 94 | "else:\n", 95 | " print(-1)" 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": {}, 101 | "source": [ 102 | "#### Linear Search Through Functions" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 15, 108 | "metadata": {}, 109 | "outputs": [ 110 | { 111 | "name": "stdout", 112 | "output_type": "stream", 113 | "text": [ 114 | "4\n" 115 | ] 116 | } 117 | ], 118 | "source": [ 119 | "def linear_search(li, ele):\n", 120 | " for i in range(len(li)):\n", 121 | " if li[i] == ele:\n", 122 | " return i\n", 123 | " return -1\n", 124 | " \n", 125 | "li = [1,2,3,4,5]\n", 126 | "index = linear_search(li,5)\n", 127 | "print(index)" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": null, 133 | "metadata": {}, 134 | "outputs": [], 135 | "source": [] 136 | } 137 | ], 138 | "metadata": { 139 | "kernelspec": { 140 | "display_name": "Python 3", 141 | "language": "python", 142 | "name": "python3" 143 | }, 144 | "language_info": { 145 | "codemirror_mode": { 146 | "name": "ipython", 147 | "version": 3 148 | }, 149 | "file_extension": ".py", 150 | "mimetype": "text/x-python", 151 | "name": "python", 152 | "nbconvert_exporter": "python", 153 | "pygments_lexer": "ipython3", 154 | "version": "3.7.3" 155 | } 156 | }, 157 | "nbformat": 4, 158 | "nbformat_minor": 2 159 | } 160 | -------------------------------------------------------------------------------- /5 Arrays/5.7 Mutable and Immutable concepts.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "#### Immutable variables" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 5, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "name": "stdout", 17 | "output_type": "stream", 18 | "text": [ 19 | "3\n" 20 | ] 21 | } 22 | ], 23 | "source": [ 24 | "x = 3\n", 25 | "a = 3\n", 26 | "print(x)" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 7, 32 | "metadata": {}, 33 | "outputs": [ 34 | { 35 | "data": { 36 | "text/plain": [ 37 | "140731172819840" 38 | ] 39 | }, 40 | "execution_count": 7, 41 | "metadata": {}, 42 | "output_type": "execute_result" 43 | } 44 | ], 45 | "source": [ 46 | "id(x)" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 8, 52 | "metadata": {}, 53 | "outputs": [ 54 | { 55 | "data": { 56 | "text/plain": [ 57 | "140731172819840" 58 | ] 59 | }, 60 | "execution_count": 8, 61 | "metadata": {}, 62 | "output_type": "execute_result" 63 | } 64 | ], 65 | "source": [ 66 | "id(a)" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 9, 72 | "metadata": {}, 73 | "outputs": [], 74 | "source": [ 75 | "a = 4" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 10, 81 | "metadata": {}, 82 | "outputs": [ 83 | { 84 | "data": { 85 | "text/plain": [ 86 | "140731172819872" 87 | ] 88 | }, 89 | "execution_count": 10, 90 | "metadata": {}, 91 | "output_type": "execute_result" 92 | } 93 | ], 94 | "source": [ 95 | "id(a)" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": 11, 101 | "metadata": {}, 102 | "outputs": [ 103 | { 104 | "data": { 105 | "text/plain": [ 106 | "3" 107 | ] 108 | }, 109 | "execution_count": 11, 110 | "metadata": {}, 111 | "output_type": "execute_result" 112 | } 113 | ], 114 | "source": [ 115 | "x" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": 12, 121 | "metadata": {}, 122 | "outputs": [ 123 | { 124 | "data": { 125 | "text/plain": [ 126 | "4" 127 | ] 128 | }, 129 | "execution_count": 12, 130 | "metadata": {}, 131 | "output_type": "execute_result" 132 | } 133 | ], 134 | "source": [ 135 | "a" 136 | ] 137 | }, 138 | { 139 | "cell_type": "markdown", 140 | "metadata": {}, 141 | "source": [ 142 | "#### Mutable Lists" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 1, 148 | "metadata": {}, 149 | "outputs": [], 150 | "source": [ 151 | "li = [1,2,3,4]\n", 152 | "li2 = li\n" 153 | ] 154 | }, 155 | { 156 | "cell_type": "code", 157 | "execution_count": 2, 158 | "metadata": {}, 159 | "outputs": [ 160 | { 161 | "data": { 162 | "text/plain": [ 163 | "[1, 2, 3, 4]" 164 | ] 165 | }, 166 | "execution_count": 2, 167 | "metadata": {}, 168 | "output_type": "execute_result" 169 | } 170 | ], 171 | "source": [ 172 | "li2" 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": 3, 178 | "metadata": {}, 179 | "outputs": [], 180 | "source": [ 181 | "li2[3] = 6" 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": 4, 187 | "metadata": {}, 188 | "outputs": [ 189 | { 190 | "data": { 191 | "text/plain": [ 192 | "[1, 2, 3, 6]" 193 | ] 194 | }, 195 | "execution_count": 4, 196 | "metadata": {}, 197 | "output_type": "execute_result" 198 | } 199 | ], 200 | "source": [ 201 | "li" 202 | ] 203 | }, 204 | { 205 | "cell_type": "code", 206 | "execution_count": 14, 207 | "metadata": {}, 208 | "outputs": [], 209 | "source": [ 210 | "li = [1,2,3,4]\n", 211 | "li2 = li\n", 212 | "li2 = [3,3,4]\n", 213 | "li2[1] = 4" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": 15, 219 | "metadata": {}, 220 | "outputs": [ 221 | { 222 | "name": "stdout", 223 | "output_type": "stream", 224 | "text": [ 225 | "[1, 2, 3, 4]\n" 226 | ] 227 | } 228 | ], 229 | "source": [ 230 | "print(li)" 231 | ] 232 | }, 233 | { 234 | "cell_type": "code", 235 | "execution_count": 16, 236 | "metadata": {}, 237 | "outputs": [ 238 | { 239 | "name": "stdout", 240 | "output_type": "stream", 241 | "text": [ 242 | "[3, 4, 4]\n" 243 | ] 244 | } 245 | ], 246 | "source": [ 247 | "print(li2)" 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": null, 253 | "metadata": {}, 254 | "outputs": [], 255 | "source": [] 256 | } 257 | ], 258 | "metadata": { 259 | "kernelspec": { 260 | "display_name": "Python 3", 261 | "language": "python", 262 | "name": "python3" 263 | }, 264 | "language_info": { 265 | "codemirror_mode": { 266 | "name": "ipython", 267 | "version": 3 268 | }, 269 | "file_extension": ".py", 270 | "mimetype": "text/x-python", 271 | "name": "python", 272 | "nbconvert_exporter": "python", 273 | "pygments_lexer": "ipython3", 274 | "version": "3.7.3" 275 | } 276 | }, 277 | "nbformat": 4, 278 | "nbformat_minor": 2 279 | } 280 | -------------------------------------------------------------------------------- /5 Arrays/5.8 Passing Variables through Functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "#### Passing variables through functions \n", 8 | "Tip: Variables are Immutable" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 8, 14 | "metadata": {}, 15 | "outputs": [ 16 | { 17 | "name": "stdout", 18 | "output_type": "stream", 19 | "text": [ 20 | "4\n" 21 | ] 22 | } 23 | ], 24 | "source": [ 25 | "def increment(a):\n", 26 | " a = a + 2\n", 27 | " return a\n", 28 | "a = 2\n", 29 | "a = increment(a)\n", 30 | "print(a)" 31 | ] 32 | }, 33 | { 34 | "cell_type": "markdown", 35 | "metadata": {}, 36 | "source": [ 37 | "#### Passing lists through functions\n", 38 | "Tip: Lists are mutable" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 11, 44 | "metadata": {}, 45 | "outputs": [ 46 | { 47 | "name": "stdout", 48 | "output_type": "stream", 49 | "text": [ 50 | "[3, 2, 3, 4]\n" 51 | ] 52 | } 53 | ], 54 | "source": [ 55 | "def increment(li):\n", 56 | " li[0] = li[0] + 2\n", 57 | " return\n", 58 | "\n", 59 | "li = [1,2,3,4]\n", 60 | "increment(li)\n", 61 | "print(li)" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 14, 67 | "metadata": {}, 68 | "outputs": [ 69 | { 70 | "name": "stdout", 71 | "output_type": "stream", 72 | "text": [ 73 | "[3, 3, 4]\n" 74 | ] 75 | } 76 | ], 77 | "source": [ 78 | "def increment(li):\n", 79 | " li = [3,3,4]\n", 80 | " return li\n", 81 | "\n", 82 | "li = [1,2,3,4]\n", 83 | "li = increment(li)\n", 84 | "print(li)" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": 15, 90 | "metadata": {}, 91 | "outputs": [ 92 | { 93 | "name": "stdout", 94 | "output_type": "stream", 95 | "text": [ 96 | "[1, 4, 3, 4, 5]\n" 97 | ] 98 | } 99 | ], 100 | "source": [ 101 | "#Predict the output\n", 102 | "def change(li):\n", 103 | " li[1] = li[1] + 2\n", 104 | "li = [1,2,3,4,5]\n", 105 | "change(li)\n", 106 | "print(li)" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 16, 112 | "metadata": {}, 113 | "outputs": [ 114 | { 115 | "name": "stdout", 116 | "output_type": "stream", 117 | "text": [ 118 | "[1, 4, 3, 4, 5]\n" 119 | ] 120 | } 121 | ], 122 | "source": [ 123 | "#Predict the output\n", 124 | "def change(li):\n", 125 | " li[1] = li[1] + 2\n", 126 | " li = [3,3,3,4,5]\n", 127 | "li = [1,2,3,4,5]\n", 128 | "change(li)\n", 129 | "print(li)" 130 | ] 131 | } 132 | 133 | ], 134 | "metadata": { 135 | "kernelspec": { 136 | "display_name": "Python 3", 137 | "language": "python", 138 | "name": "python3" 139 | }, 140 | "language_info": { 141 | "codemirror_mode": { 142 | "name": "ipython", 143 | "version": 3 144 | }, 145 | "file_extension": ".py", 146 | "mimetype": "text/x-python", 147 | "name": "python", 148 | "nbconvert_exporter": "python", 149 | "pygments_lexer": "ipython3", 150 | "version": "3.7.3" 151 | } 152 | }, 153 | "nbformat": 4, 154 | "nbformat_minor": 2 155 | } 156 | -------------------------------------------------------------------------------- /5 Arrays/5.9 Reverse List.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "[6, 5, 4, 3, 2, 1]\n" 13 | ] 14 | } 15 | ], 16 | "source": [ 17 | "def reverse_list(li):\n", 18 | " length = len(li)\n", 19 | " for i in range(length//2):\n", 20 | " li[i], li[length-i-1] = li[length-i-1], li[i] \n", 21 | "\n", 22 | "li = [1,2,3,4,5,6]\n", 23 | "reverse_list(li)\n", 24 | "print(li)\n" 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": {}, 30 | "source": [ 31 | "#### Reverse List using Negative Indexing" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 4, 37 | "metadata": {}, 38 | "outputs": [ 39 | { 40 | "name": "stdout", 41 | "output_type": "stream", 42 | "text": [ 43 | "[6, 5, 4, 3, 2, 1]\n" 44 | ] 45 | } 46 | ], 47 | "source": [ 48 | "def reverse_list(li):\n", 49 | " length = len(li)\n", 50 | " for i in range(length//2):\n", 51 | " li[i], li[-i-1] = li[-i-1], li[i] \n", 52 | "\n", 53 | "li = [1,2,3,4,5,6]\n", 54 | "reverse_list(li)\n", 55 | "print(li)\n" 56 | ] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "metadata": {}, 61 | "source": [ 62 | "#### Reverse List using Slicing" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 5, 68 | "metadata": {}, 69 | "outputs": [ 70 | { 71 | "data": { 72 | "text/plain": [ 73 | "[6, 5, 4, 3, 2, 1]" 74 | ] 75 | }, 76 | "execution_count": 5, 77 | "metadata": {}, 78 | "output_type": "execute_result" 79 | } 80 | ], 81 | "source": [ 82 | "li" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 6, 88 | "metadata": {}, 89 | "outputs": [ 90 | { 91 | "data": { 92 | "text/plain": [ 93 | "[3, 4]" 94 | ] 95 | }, 96 | "execution_count": 6, 97 | "metadata": {}, 98 | "output_type": "execute_result" 99 | } 100 | ], 101 | "source": [ 102 | "li[3:1:-1]" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 8, 108 | "metadata": {}, 109 | "outputs": [ 110 | { 111 | "data": { 112 | "text/plain": [ 113 | "[3, 4, 5, 6]" 114 | ] 115 | }, 116 | "execution_count": 8, 117 | "metadata": {}, 118 | "output_type": "execute_result" 119 | } 120 | ], 121 | "source": [ 122 | "li[3::-1]" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 9, 128 | "metadata": {}, 129 | "outputs": [ 130 | { 131 | "data": { 132 | "text/plain": [ 133 | "[6, 5, 4, 3, 2, 1]" 134 | ] 135 | }, 136 | "execution_count": 9, 137 | "metadata": {}, 138 | "output_type": "execute_result" 139 | } 140 | ], 141 | "source": [ 142 | "li[::]" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 10, 148 | "metadata": {}, 149 | "outputs": [ 150 | { 151 | "data": { 152 | "text/plain": [ 153 | "[1, 2, 3, 4, 5, 6]" 154 | ] 155 | }, 156 | "execution_count": 10, 157 | "metadata": {}, 158 | "output_type": "execute_result" 159 | } 160 | ], 161 | "source": [ 162 | "li[::-1] # Returning a new list" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": 11, 168 | "metadata": {}, 169 | "outputs": [], 170 | "source": [ 171 | "li = [1,2,3,4,5,6]" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 12, 177 | "metadata": {}, 178 | "outputs": [ 179 | { 180 | "data": { 181 | "text/plain": [ 182 | "[6, 5, 4, 3, 2, 1]" 183 | ] 184 | }, 185 | "execution_count": 12, 186 | "metadata": {}, 187 | "output_type": "execute_result" 188 | } 189 | ], 190 | "source": [ 191 | "li[::-1] # Creates a new list" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": 13, 197 | "metadata": {}, 198 | "outputs": [], 199 | "source": [ 200 | "li = li[::-1]" 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": 14, 206 | "metadata": {}, 207 | "outputs": [ 208 | { 209 | "data": { 210 | "text/plain": [ 211 | "[6, 5, 4, 3, 2, 1]" 212 | ] 213 | }, 214 | "execution_count": 14, 215 | "metadata": {}, 216 | "output_type": "execute_result" 217 | } 218 | ], 219 | "source": [ 220 | "li" 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": null, 226 | "metadata": {}, 227 | "outputs": [], 228 | "source": [] 229 | } 230 | ], 231 | "metadata": { 232 | "kernelspec": { 233 | "display_name": "Python 3", 234 | "language": "python", 235 | "name": "python3" 236 | }, 237 | "language_info": { 238 | "codemirror_mode": { 239 | "name": "ipython", 240 | "version": 3 241 | }, 242 | "file_extension": ".py", 243 | "mimetype": "text/x-python", 244 | "name": "python", 245 | "nbconvert_exporter": "python", 246 | "pygments_lexer": "ipython3", 247 | "version": "3.7.3" 248 | } 249 | }, 250 | "nbformat": 4, 251 | "nbformat_minor": 2 252 | } 253 | -------------------------------------------------------------------------------- /6 Searching and Sorting/6.1 Binary Search.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Binary Search" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 3, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "name": "stdout", 17 | "output_type": "stream", 18 | "text": [ 19 | "5\n", 20 | "12 54 23 69 85\n", 21 | "23\n", 22 | "2\n" 23 | ] 24 | } 25 | ], 26 | "source": [ 27 | "def binary(arr, low, high, key):\n", 28 | " if low <= high:\n", 29 | " mid = (low + high) // 2\n", 30 | " if key == arr[mid]:\n", 31 | " return mid\n", 32 | " elif arr[mid] > key:\n", 33 | " return binary(arr, low, mid - 1, key)\n", 34 | " elif arr[mid] < key:\n", 35 | " return binary(arr, mid + 1, high, key)\n", 36 | " else:\n", 37 | " return -1\n", 38 | " \n", 39 | "n = int(input()) \n", 40 | "arr = [int (x) for x in input().split() [:n]]\n", 41 | "key = int(input())\n", 42 | "ele = binary(arr, 0, n-1, key)\n", 43 | "print(ele)\n", 44 | " \n" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 13, 50 | "metadata": {}, 51 | "outputs": [ 52 | { 53 | "name": "stdout", 54 | "output_type": "stream", 55 | "text": [ 56 | "84\n", 57 | "4\n" 58 | ] 59 | } 60 | ], 61 | "source": [ 62 | "def binary_search(arr, element):\n", 63 | " start = 0\n", 64 | " end = len(arr) - 1\n", 65 | " while (start <= end):\n", 66 | " mid = (start + end) // 2\n", 67 | " if (arr[mid] == element):\n", 68 | " return mid\n", 69 | " elif (arr[mid] < element):\n", 70 | " start = mid + 1\n", 71 | " else:\n", 72 | " end = mid - 1\n", 73 | " return -1\n", 74 | "arr = [1,32,48,56,84,136]\n", 75 | "key = int(input())\n", 76 | "index = binary_search(arr, key)\n", 77 | "print(index)\n", 78 | " " 79 | ] 80 | } 81 | ], 82 | "metadata": { 83 | "kernelspec": { 84 | "display_name": "Python 3", 85 | "language": "python", 86 | "name": "python3" 87 | }, 88 | "language_info": { 89 | "codemirror_mode": { 90 | "name": "ipython", 91 | "version": 3 92 | }, 93 | "file_extension": ".py", 94 | "mimetype": "text/x-python", 95 | "name": "python", 96 | "nbconvert_exporter": "python", 97 | "pygments_lexer": "ipython3", 98 | "version": "3.7.3" 99 | } 100 | }, 101 | "nbformat": 4, 102 | "nbformat_minor": 2 103 | } 104 | -------------------------------------------------------------------------------- /6 Searching and Sorting/6.2 Selection Sort.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Selection Sort" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 7, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "name": "stdout", 17 | "output_type": "stream", 18 | "text": [ 19 | "How many numbers ? 5\n", 20 | "12 54 32 0 4\n", 21 | "0 4 12 32 54 " 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "n = int(input(\"How many numbers ? \"))\n", 27 | "arr = [int(x) for x in input().split()[:n]] \n", 28 | "\n", 29 | "for i in range(0, len(arr) - 1):\n", 30 | " min = arr[i]\n", 31 | " for j in range(i + 1, len(arr)):\n", 32 | " if min > arr[j]:\n", 33 | " min = arr[j]\n", 34 | " arr[i], arr[j] = arr[j], arr[i]\n", 35 | "\n", 36 | "for ele in arr:\n", 37 | " print(ele, end = ' ')\n", 38 | " " 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": null, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [] 47 | } 48 | ], 49 | "metadata": { 50 | "kernelspec": { 51 | "display_name": "Python 3", 52 | "language": "python", 53 | "name": "python3" 54 | }, 55 | "language_info": { 56 | "codemirror_mode": { 57 | "name": "ipython", 58 | "version": 3 59 | }, 60 | "file_extension": ".py", 61 | "mimetype": "text/x-python", 62 | "name": "python", 63 | "nbconvert_exporter": "python", 64 | "pygments_lexer": "ipython3", 65 | "version": "3.7.3" 66 | } 67 | }, 68 | "nbformat": 4, 69 | "nbformat_minor": 2 70 | } 71 | -------------------------------------------------------------------------------- /6 Searching and Sorting/6.3 Bubble Sort.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Bubble Sort" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 13, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "name": "stdout", 17 | "output_type": "stream", 18 | "text": [ 19 | "5\n", 20 | "102 24 02 56 40\n", 21 | "2 24 40 56 102 " 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "n = int(input())\n", 27 | "arr = [int(x) for x in input().split()[:n]]\n", 28 | "for i in range(0, n):\n", 29 | " for j in range(0, n - i - 1):\n", 30 | " if arr[j] > arr[j+1]:\n", 31 | " arr[j], arr[j+1] = arr[j+1], arr[j]\n", 32 | " \n", 33 | "for ele in arr:\n", 34 | " print(ele, end = ' ')\n", 35 | " " 36 | ] 37 | }, 38 | ], 39 | "metadata": { 40 | "kernelspec": { 41 | "display_name": "Python 3", 42 | "language": "python", 43 | "name": "python3" 44 | }, 45 | "language_info": { 46 | "codemirror_mode": { 47 | "name": "ipython", 48 | "version": 3 49 | }, 50 | "file_extension": ".py", 51 | "mimetype": "text/x-python", 52 | "name": "python", 53 | "nbconvert_exporter": "python", 54 | "pygments_lexer": "ipython3", 55 | "version": "3.7.3" 56 | } 57 | }, 58 | "nbformat": 4, 59 | "nbformat_minor": 2 60 | } 61 | -------------------------------------------------------------------------------- /6 Searching and Sorting/6.4 Insertion Sort.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Insertion Sort" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 8, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "name": "stdout", 17 | "output_type": "stream", 18 | "text": [ 19 | "5\n", 20 | "48 25 85 31 2\n", 21 | "2 25 31 48 85 " 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "n = int(input())\n", 27 | "arr = [int(x) for x in input().split()[:n]]\n", 28 | "for i in range(0, n):\n", 29 | " temp = arr[i]\n", 30 | " # shifting elements till this condition holds\n", 31 | " j = i - 1\n", 32 | " while (j >= 0 and arr[j] > temp):\n", 33 | " arr[j + 1] = arr[j]\n", 34 | " j = j - 1\n", 35 | " # j + 1 is the correct postion for ith element \n", 36 | " arr[j+1] = temp\n", 37 | "for ele in arr:\n", 38 | " print(ele, end = ' ')" 39 | ] 40 | }, 41 | ], 42 | "metadata": { 43 | "kernelspec": { 44 | "display_name": "Python 3", 45 | "language": "python", 46 | "name": "python3" 47 | }, 48 | "language_info": { 49 | "codemirror_mode": { 50 | "name": "ipython", 51 | "version": 3 52 | }, 53 | "file_extension": ".py", 54 | "mimetype": "text/x-python", 55 | "name": "python", 56 | "nbconvert_exporter": "python", 57 | "pygments_lexer": "ipython3", 58 | "version": "3.7.3" 59 | } 60 | }, 61 | "nbformat": 4, 62 | "nbformat_minor": 2 63 | } 64 | -------------------------------------------------------------------------------- /6 Searching and Sorting/6.5 Merge two sorted arrays.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Merge two sorted arrays" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "name": "stdout", 17 | "output_type": "stream", 18 | "text": [ 19 | "5\n", 20 | "2 3 54 85 96\n", 21 | "6\n", 22 | "1 4 8 52 320 855\n", 23 | "1 2 3 4 8 52 54 85 96 320 855 " 24 | ] 25 | } 26 | ], 27 | "source": [ 28 | "m = int(input())\n", 29 | "arr1 = [int(x) for x in input().split() [:m]]\n", 30 | "n = int(input())\n", 31 | "arr2 = [int(x) for x in input().split() [:n]]\n", 32 | "\n", 33 | "arr3 = []\n", 34 | "i = 0\n", 35 | "j = 0\n", 36 | "while i < len(arr1) and j < len(arr2):\n", 37 | " if arr1[i] <= arr2[j]:\n", 38 | " arr3.append(arr1[i])\n", 39 | " #print(\"i \", arr3)\n", 40 | " i = i + 1\n", 41 | " elif arr1[i] > arr2[j]:\n", 42 | " arr3.append(arr2[j])\n", 43 | " #print(\"j \",arr3)\n", 44 | " j = j + 1\n", 45 | " \n", 46 | "if i < len(arr1):\n", 47 | " for l in arr1[i : len(arr1)]:\n", 48 | " arr3.append(l)\n", 49 | "else:\n", 50 | " for k in arr2[j : len(arr2)]:\n", 51 | " arr3.append(k)\n", 52 | " \n", 53 | "for ele in arr3:\n", 54 | " print(ele, end = ' ')\n" 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.3" 75 | } 76 | }, 77 | "nbformat": 4, 78 | "nbformat_minor": 2 79 | } 80 | -------------------------------------------------------------------------------- /6 Searching and Sorting/6.6 [Practice] Push Zeros to end.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "#### Push Zeros to end\n", 8 | "\n", 9 | "Given a random integer array, push all the zeros that are present to end of the array. The respective order of other elements should remain same.\n", 10 | "Change in the input array itself. You don't need to return or print elements. Don't use extra array.\n", 11 | "Note : You need to do this in one scan of array only.\n", 12 | "Input format :\n", 13 | "Line 1 : Integer N, Array Size\n", 14 | "Line 2 : Array elements (separated by space)\n", 15 | "Output Format :\n", 16 | "Array elements (separated by space)\n", 17 | "Constraints :\n", 18 | "1 <= N <= 10^6\n", 19 | "Sample Input 1:\n", 20 | "7\n", 21 | "2 0 4 1 3 0 28\n", 22 | "Sample Output 1:\n", 23 | "2 4 1 3 28 0 0\n", 24 | "Sample Input 2:\n", 25 | "5\n", 26 | "0 3 0 2 0\n", 27 | "Sample Output 2:\n", 28 | "3 2 0 0 0" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 2, 34 | "metadata": {}, 35 | "outputs": [ 36 | { 37 | "name": "stdout", 38 | "output_type": "stream", 39 | "text": [ 40 | "6\n", 41 | "0 58 2 0 4 0\n", 42 | "58 2 4 0 0 0 " 43 | ] 44 | } 45 | ], 46 | "source": [ 47 | "n = int(input())\n", 48 | "arr = [int(x) for x in input().split()[:n]]\n", 49 | "for i in range(0, n):\n", 50 | " for j in range(0, n - i - 1):\n", 51 | " if arr[j] == 0:\n", 52 | " arr[j], arr[j+1] = arr[j+1], arr[j]\n", 53 | " \n", 54 | "for ele in arr:\n", 55 | " print(ele, end = ' ') " 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": 3, 61 | "metadata": {}, 62 | "outputs": [ 63 | { 64 | "name": "stdout", 65 | "output_type": "stream", 66 | "text": [ 67 | "65 25 36 0 0 " 68 | ] 69 | } 70 | ], 71 | "source": [ 72 | "#n = int(input())\n", 73 | "#arr = [int(x) for x in input().split()[:n]]\n", 74 | "arr = [65,0,25,0,36]\n", 75 | "zero = 0\n", 76 | "count = 0\n", 77 | "for i in range(0, len(arr)):\n", 78 | " if arr[i] == 0:\n", 79 | " count = count + 1\n", 80 | " else:\n", 81 | " print(arr[i], end = ' ')\n", 82 | "while count > 0:\n", 83 | " print(0, end = ' ')\n", 84 | " count = count - 1\n", 85 | " " 86 | ] 87 | }, 88 | ], 89 | "metadata": { 90 | "kernelspec": { 91 | "display_name": "Python 3", 92 | "language": "python", 93 | "name": "python3" 94 | }, 95 | "language_info": { 96 | "codemirror_mode": { 97 | "name": "ipython", 98 | "version": 3 99 | }, 100 | "file_extension": ".py", 101 | "mimetype": "text/x-python", 102 | "name": "python", 103 | "nbconvert_exporter": "python", 104 | "pygments_lexer": "ipython3", 105 | "version": "3.7.3" 106 | } 107 | }, 108 | "nbformat": 4, 109 | "nbformat_minor": 2 110 | } 111 | -------------------------------------------------------------------------------- /6 Searching and Sorting/6.7 [Practice] Rotate array.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "7\n", 13 | "1 2 3 4 5 6 7\n", 14 | "2\n", 15 | "3 4 5 6 7 1 2\n" 16 | ] 17 | } 18 | ], 19 | "source": [ 20 | "\n", 21 | "def Rotate(arr, d = 1):\n", 22 | " if len(arr) == 0:\n", 23 | " return 1\n", 24 | " d = d % len(arr)\n", 25 | " return arr[d:] + arr[:d]\n", 26 | "\n", 27 | "# Main\n", 28 | "n=int(input())\n", 29 | "arr=list(int(i) for i in input().strip().split(' '))\n", 30 | "d=int(input())\n", 31 | "xyz = Rotate(arr, d)\n", 32 | "print(*xyz)\n" 33 | ] 34 | }, 35 | ], 36 | "metadata": { 37 | "kernelspec": { 38 | "display_name": "Python 3", 39 | "language": "python", 40 | "name": "python3" 41 | }, 42 | "language_info": { 43 | "codemirror_mode": { 44 | "name": "ipython", 45 | "version": 3 46 | }, 47 | "file_extension": ".py", 48 | "mimetype": "text/x-python", 49 | "name": "python", 50 | "nbconvert_exporter": "python", 51 | "pygments_lexer": "ipython3", 52 | "version": "3.7.3" 53 | } 54 | }, 55 | "nbformat": 4, 56 | "nbformat_minor": 2 57 | } 58 | -------------------------------------------------------------------------------- /6 Searching and Sorting/6.8 [Practice] Second Largest element in an array.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 | "7\n", 13 | "2 5 6 8 14 0 5\n", 14 | "8\n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "\n", 20 | "n = int(input())\n", 21 | "arr = [int(x) for x in input().split()[:n]]\n", 22 | "arr.sort()\n", 23 | "count = 0\n", 24 | "max1 = 0\n", 25 | "max2 = 0\n", 26 | "for i in range(0, len(arr)-1):\n", 27 | " if arr[i] == arr[i+1]:\n", 28 | " count = count + 1\n", 29 | " elif arr[i] > max1:\n", 30 | " max2 = max2\n", 31 | " max1 = arr[i]\n", 32 | " \n", 33 | "\n", 34 | "if (count == n) or (n <= 1):\n", 35 | " print(-2147483648)\n", 36 | "else:\n", 37 | " print(max1)\n" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": null, 43 | "metadata": {}, 44 | "outputs": [], 45 | "source": [ 46 | "7\n", 47 | "2 13 4 1 3 6 28\n" 48 | ] 49 | } 50 | ], 51 | "metadata": { 52 | "kernelspec": { 53 | "display_name": "Python 3", 54 | "language": "python", 55 | "name": "python3" 56 | }, 57 | "language_info": { 58 | "codemirror_mode": { 59 | "name": "ipython", 60 | "version": 3 61 | }, 62 | "file_extension": ".py", 63 | "mimetype": "text/x-python", 64 | "name": "python", 65 | "nbconvert_exporter": "python", 66 | "pygments_lexer": "ipython3", 67 | "version": "3.7.3" 68 | } 69 | }, 70 | "nbformat": 4, 71 | "nbformat_minor": 2 72 | } 73 | -------------------------------------------------------------------------------- /6 Searching and Sorting/6.9 [Practice] Check array rotation.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "6\n", 13 | "5 6 1 2 3 4\n", 14 | "2\n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "def Rotate(arr, d = 1):\n", 20 | " if len(arr) == 0:\n", 21 | " return 1\n", 22 | " d = -d % len(arr)\n", 23 | " return arr[d:] + arr[:d]\n", 24 | "\n", 25 | "n=int(input())\n", 26 | "arr = list(int(i) for i in input().strip().split(' '))\n", 27 | "temp = []\n", 28 | "for i in arr:\n", 29 | " temp.append(i)\n", 30 | " \n", 31 | "for d in range(0, len(arr)):\n", 32 | " #temp = arr\n", 33 | " temp.sort()\n", 34 | " xyz = Rotate(temp, d)\n", 35 | " #print(temp)\n", 36 | " #print(arr)\n", 37 | " flag = True\n", 38 | " for i in range(0, len(xyz)):\n", 39 | " if xyz[i] == arr[i]:\n", 40 | " #flag = True\n", 41 | " pass\n", 42 | " else:\n", 43 | " flag = False\n", 44 | " break\n", 45 | " if flag == True:\n", 46 | " print(d)\n", 47 | " break" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": null, 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [] 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.3" 75 | } 76 | }, 77 | "nbformat": 4, 78 | "nbformat_minor": 2 79 | } 80 | -------------------------------------------------------------------------------- /6 Searching and Sorting/6.91 [Practice] Sort 0 1 2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "7\n", 13 | "0 1 2 0 2 0 1\n", 14 | "0 0 0 1 1 2 2 " 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "## Read input as specified in the question.\n", 20 | "## Print output as specified in the question.\n", 21 | "## Read input as specified in the question.\n", 22 | "## Print output as specified in the question.\n", 23 | "n = int(input())\n", 24 | "list = [int (x) for x in input().split() [:n]]\n", 25 | "count0 = 0\n", 26 | "count1 = 0\n", 27 | "count2 = 0\n", 28 | "for i in list:\n", 29 | " if i == 0:\n", 30 | " count0 = count0 + 1\n", 31 | " elif i == 1:\n", 32 | " count1 = count1 + 1\n", 33 | " else:\n", 34 | " count2 = count2 + 1\n", 35 | "list = []\n", 36 | "for i in range(0, count0):\n", 37 | " list.append(0)\n", 38 | "for i in range(0, count1):\n", 39 | " list.append(1)\n", 40 | "for i in range(0, count2):\n", 41 | " list.append(2)\n", 42 | " \n", 43 | " \n", 44 | "for ele in list:\n", 45 | " print(ele, end = ' ')" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": null, 51 | "metadata": {}, 52 | "outputs": [], 53 | "source": [] 54 | } 55 | ], 56 | "metadata": { 57 | "kernelspec": { 58 | "display_name": "Python 3", 59 | "language": "python", 60 | "name": "python3" 61 | }, 62 | "language_info": { 63 | "codemirror_mode": { 64 | "name": "ipython", 65 | "version": 3 66 | }, 67 | "file_extension": ".py", 68 | "mimetype": "text/x-python", 69 | "name": "python", 70 | "nbconvert_exporter": "python", 71 | "pygments_lexer": "ipython3", 72 | "version": "3.7.3" 73 | } 74 | }, 75 | "nbformat": 4, 76 | "nbformat_minor": 2 77 | } 78 | -------------------------------------------------------------------------------- /6 Searching and Sorting/6.92 [Practice] Sum of two arrays.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "4\n", 13 | "2 5 6 9\n", 14 | "3\n", 15 | "9 9 9\n", 16 | "0 3 5 6 8 " 17 | ] 18 | } 19 | ], 20 | "source": [ 21 | "def sumOfArrays(ar1, ar2, n, m):\n", 22 | " bFlag = False\n", 23 | " res = []\n", 24 | " if n > m:\n", 25 | " app = n - m\n", 26 | " for i in range(app):\n", 27 | " ar2.insert(i, 0)\n", 28 | " \n", 29 | " else:\n", 30 | " app = m - n\n", 31 | " for i in range(app):\n", 32 | " ar1.insert(i, 0)\n", 33 | " \n", 34 | " size = len(ar1)\n", 35 | " \n", 36 | " for i in range(size -1, -1, -1):\n", 37 | " sum = ar1[i] + ar2[i]\n", 38 | " \n", 39 | " if bFlag:\n", 40 | " sum +=1\n", 41 | " \n", 42 | " if sum >= 10:\n", 43 | " bFlag = True\n", 44 | " cur_ele = sum % 10\n", 45 | " res.append(cur_ele)\n", 46 | " \n", 47 | " else:\n", 48 | " res.append(sum)\n", 49 | " bFlag = False\n", 50 | " \n", 51 | " else:\n", 52 | " if bFlag:\n", 53 | " res.append(1)\n", 54 | " \n", 55 | " if len(res) != size + 1:\n", 56 | " res.append(0)\n", 57 | " for ele in res[::-1]:\n", 58 | " print(ele, end =' ')\n", 59 | " \n", 60 | " \n", 61 | "n = int(input())\n", 62 | "ar1 = [int(x) for x in input().split()[:n]]\n", 63 | "\n", 64 | "m = int(input())\n", 65 | "ar2 = [int(x) for x in input().split()[:m]]\n", 66 | "\n", 67 | "sumOfArrays(ar1, ar2, n, m)" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": null, 73 | "metadata": {}, 74 | "outputs": [], 75 | "source": [] 76 | } 77 | ], 78 | "metadata": { 79 | "kernelspec": { 80 | "display_name": "Python 3", 81 | "language": "python", 82 | "name": "python3" 83 | }, 84 | "language_info": { 85 | "codemirror_mode": { 86 | "name": "ipython", 87 | "version": 3 88 | }, 89 | "file_extension": ".py", 90 | "mimetype": "text/x-python", 91 | "name": "python", 92 | "nbconvert_exporter": "python", 93 | "pygments_lexer": "ipython3", 94 | "version": "3.7.3" 95 | } 96 | }, 97 | "nbformat": 4, 98 | "nbformat_minor": 2 99 | } 100 | -------------------------------------------------------------------------------- /7 Strings/7.01 Strings - Intro.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "#### Tip: Triple quote is used to print string in multiple lines" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "https://thepythonguru.com/python-strings/ - Check this out to see more on string operations and all about strings" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "s = \"Fazeel\"" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 2, 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "name": "stdout", 33 | "output_type": "stream", 34 | "text": [ 35 | "Fazeel\n" 36 | ] 37 | } 38 | ], 39 | "source": [ 40 | "print(s)" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 3, 46 | "metadata": {}, 47 | "outputs": [], 48 | "source": [ 49 | "s = 'Fazeel'" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 4, 55 | "metadata": {}, 56 | "outputs": [ 57 | { 58 | "name": "stdout", 59 | "output_type": "stream", 60 | "text": [ 61 | "Fazeel\n" 62 | ] 63 | } 64 | ], 65 | "source": [ 66 | "print(s)" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 5, 72 | "metadata": {}, 73 | "outputs": [], 74 | "source": [ 75 | "s = ''' Fazeel'''" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 6, 81 | "metadata": {}, 82 | "outputs": [ 83 | { 84 | "name": "stdout", 85 | "output_type": "stream", 86 | "text": [ 87 | " Fazeel\n" 88 | ] 89 | } 90 | ], 91 | "source": [ 92 | "print(s)" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 7, 98 | "metadata": {}, 99 | "outputs": [], 100 | "source": [ 101 | "s = ''' My Name is\n", 102 | "Fazeel Hasan\n", 103 | "Usmani'''" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 8, 109 | "metadata": {}, 110 | "outputs": [ 111 | { 112 | "name": "stdout", 113 | "output_type": "stream", 114 | "text": [ 115 | " My Name is\n", 116 | "Fazeel Hasan\n", 117 | "Usmani\n" 118 | ] 119 | } 120 | ], 121 | "source": [ 122 | "print(s)" 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": {}, 128 | "source": [ 129 | "#### String Indexing" 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": 9, 135 | "metadata": {}, 136 | "outputs": [], 137 | "source": [ 138 | "s = 'Fazeel'" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 10, 144 | "metadata": {}, 145 | "outputs": [ 146 | { 147 | "data": { 148 | "text/plain": [ 149 | "'F'" 150 | ] 151 | }, 152 | "execution_count": 10, 153 | "metadata": {}, 154 | "output_type": "execute_result" 155 | } 156 | ], 157 | "source": [ 158 | "s[0]" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": 11, 164 | "metadata": {}, 165 | "outputs": [ 166 | { 167 | "data": { 168 | "text/plain": [ 169 | "'a'" 170 | ] 171 | }, 172 | "execution_count": 11, 173 | "metadata": {}, 174 | "output_type": "execute_result" 175 | } 176 | ], 177 | "source": [ 178 | "s[1]" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": 12, 184 | "metadata": {}, 185 | "outputs": [ 186 | { 187 | "ename": "IndexError", 188 | "evalue": "string index out of range", 189 | "output_type": "error", 190 | "traceback": [ 191 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 192 | "\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", 193 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ms\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m9\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 194 | "\u001b[1;31mIndexError\u001b[0m: string index out of range" 195 | ] 196 | } 197 | ], 198 | "source": [ 199 | "s[9]" 200 | ] 201 | }, 202 | { 203 | "cell_type": "code", 204 | "execution_count": 13, 205 | "metadata": {}, 206 | "outputs": [ 207 | { 208 | "data": { 209 | "text/plain": [ 210 | "'e'" 211 | ] 212 | }, 213 | "execution_count": 13, 214 | "metadata": {}, 215 | "output_type": "execute_result" 216 | } 217 | ], 218 | "source": [ 219 | "s[-2]" 220 | ] 221 | }, 222 | { 223 | "cell_type": "code", 224 | "execution_count": 20, 225 | "metadata": {}, 226 | "outputs": [], 227 | "source": [ 228 | "s = '''My\n", 229 | "Name\n", 230 | "Is\n", 231 | "Fazeel'''" 232 | ] 233 | }, 234 | { 235 | "cell_type": "code", 236 | "execution_count": 17, 237 | "metadata": {}, 238 | "outputs": [ 239 | { 240 | "data": { 241 | "text/plain": [ 242 | "'M'" 243 | ] 244 | }, 245 | "execution_count": 17, 246 | "metadata": {}, 247 | "output_type": "execute_result" 248 | } 249 | ], 250 | "source": [ 251 | "s[0]" 252 | ] 253 | }, 254 | { 255 | "cell_type": "code", 256 | "execution_count": 18, 257 | "metadata": {}, 258 | "outputs": [ 259 | { 260 | "data": { 261 | "text/plain": [ 262 | "'y'" 263 | ] 264 | }, 265 | "execution_count": 18, 266 | "metadata": {}, 267 | "output_type": "execute_result" 268 | } 269 | ], 270 | "source": [ 271 | "s[1]" 272 | ] 273 | }, 274 | { 275 | "cell_type": "code", 276 | "execution_count": 21, 277 | "metadata": {}, 278 | "outputs": [ 279 | { 280 | "data": { 281 | "text/plain": [ 282 | "'\\n'" 283 | ] 284 | }, 285 | "execution_count": 21, 286 | "metadata": {}, 287 | "output_type": "execute_result" 288 | } 289 | ], 290 | "source": [ 291 | "s[2]" 292 | ] 293 | }, 294 | { 295 | "cell_type": "code", 296 | "execution_count": 22, 297 | "metadata": {}, 298 | "outputs": [ 299 | { 300 | "data": { 301 | "text/plain": [ 302 | "'My\\nName\\nIs\\nFazeel'" 303 | ] 304 | }, 305 | "execution_count": 22, 306 | "metadata": {}, 307 | "output_type": "execute_result" 308 | } 309 | ], 310 | "source": [ 311 | "s" 312 | ] 313 | }, 314 | { 315 | "cell_type": "code", 316 | "execution_count": 24, 317 | "metadata": {}, 318 | "outputs": [ 319 | { 320 | "name": "stdout", 321 | "output_type": "stream", 322 | "text": [ 323 | "c\n" 324 | ] 325 | } 326 | ], 327 | "source": [ 328 | "# Predict the output\n", 329 | "s = \"abcd\"\n", 330 | "print(s[-2])" 331 | ] 332 | }, 333 | ], 334 | "metadata": { 335 | "kernelspec": { 336 | "display_name": "Python 3", 337 | "language": "python", 338 | "name": "python3" 339 | }, 340 | "language_info": { 341 | "codemirror_mode": { 342 | "name": "ipython", 343 | "version": 3 344 | }, 345 | "file_extension": ".py", 346 | "mimetype": "text/x-python", 347 | "name": "python", 348 | "nbconvert_exporter": "python", 349 | "pygments_lexer": "ipython3", 350 | "version": "3.7.3" 351 | } 352 | }, 353 | "nbformat": 4, 354 | "nbformat_minor": 2 355 | } 356 | -------------------------------------------------------------------------------- /7 Strings/7.02 How Strings are stored.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Strings are immutable in Python" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 12, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "s = \"fazeel\"" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 13, 22 | "metadata": {}, 23 | "outputs": [ 24 | { 25 | "ename": "TypeError", 26 | "evalue": "'str' object does not support item assignment", 27 | "output_type": "error", 28 | "traceback": [ 29 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 30 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 31 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ms\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"Z\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 32 | "\u001b[1;31mTypeError\u001b[0m: 'str' object does not support item assignment" 33 | ] 34 | } 35 | ], 36 | "source": [ 37 | "s[0] = \"Z\"" 38 | ] 39 | }, 40 | { 41 | "cell_type": "markdown", 42 | "metadata": {}, 43 | "source": [ 44 | "#### The above example shows how strings are immutable, you cannot change the character at particular index" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 2, 50 | "metadata": {}, 51 | "outputs": [ 52 | { 53 | "data": { 54 | "text/plain": [ 55 | "2185906125936" 56 | ] 57 | }, 58 | "execution_count": 2, 59 | "metadata": {}, 60 | "output_type": "execute_result" 61 | } 62 | ], 63 | "source": [ 64 | "id(s)" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 3, 70 | "metadata": {}, 71 | "outputs": [ 72 | { 73 | "name": "stdout", 74 | "output_type": "stream", 75 | "text": [ 76 | "fazeel\n" 77 | ] 78 | } 79 | ], 80 | "source": [ 81 | "print(s)" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 4, 87 | "metadata": {}, 88 | "outputs": [], 89 | "source": [ 90 | "a = \"Usmani\"" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 5, 96 | "metadata": {}, 97 | "outputs": [ 98 | { 99 | "data": { 100 | "text/plain": [ 101 | "2185906831688" 102 | ] 103 | }, 104 | "execution_count": 5, 105 | "metadata": {}, 106 | "output_type": "execute_result" 107 | } 108 | ], 109 | "source": [ 110 | "id(a)" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 6, 116 | "metadata": {}, 117 | "outputs": [ 118 | { 119 | "name": "stdout", 120 | "output_type": "stream", 121 | "text": [ 122 | "Usmani\n" 123 | ] 124 | } 125 | ], 126 | "source": [ 127 | "print(a)" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": 7, 133 | "metadata": {}, 134 | "outputs": [], 135 | "source": [ 136 | "a = \"fazeel\"" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 8, 142 | "metadata": {}, 143 | "outputs": [ 144 | { 145 | "data": { 146 | "text/plain": [ 147 | "2185906125936" 148 | ] 149 | }, 150 | "execution_count": 8, 151 | "metadata": {}, 152 | "output_type": "execute_result" 153 | } 154 | ], 155 | "source": [ 156 | "id(a)" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 9, 162 | "metadata": {}, 163 | "outputs": [], 164 | "source": [ 165 | "s = \"Usmani\"" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 10, 171 | "metadata": {}, 172 | "outputs": [ 173 | { 174 | "data": { 175 | "text/plain": [ 176 | "2185906834600" 177 | ] 178 | }, 179 | "execution_count": 10, 180 | "metadata": {}, 181 | "output_type": "execute_result" 182 | } 183 | ], 184 | "source": [ 185 | "id(s)" 186 | ] 187 | }, 188 | { 189 | "cell_type": "code", 190 | "execution_count": 11, 191 | "metadata": {}, 192 | "outputs": [ 193 | { 194 | "data": { 195 | "text/plain": [ 196 | "2185906125936" 197 | ] 198 | }, 199 | "execution_count": 11, 200 | "metadata": {}, 201 | "output_type": "execute_result" 202 | } 203 | ], 204 | "source": [ 205 | "id(a)" 206 | ] 207 | }, 208 | { 209 | "cell_type": "code", 210 | "execution_count": 16, 211 | "metadata": {}, 212 | "outputs": [ 213 | { 214 | "ename": "TypeError", 215 | "evalue": "'str' object does not support item assignment", 216 | "output_type": "error", 217 | "traceback": [ 218 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 219 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 220 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0ms\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m\"abcd\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0ms\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'c'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 221 | "\u001b[1;31mTypeError\u001b[0m: 'str' object does not support item assignment" 222 | ] 223 | } 224 | ], 225 | "source": [ 226 | "s=\"abcd\"\n", 227 | "s[0]='c'\n", 228 | "print(s) " 229 | ] 230 | }, 231 | { 232 | "cell_type": "code", 233 | "execution_count": 19, 234 | "metadata": {}, 235 | "outputs": [ 236 | { 237 | "name": "stdout", 238 | "output_type": "stream", 239 | "text": [ 240 | "They are same\n" 241 | ] 242 | } 243 | ], 244 | "source": [ 245 | "s = \"abcd\"\n", 246 | "a = \"abcd\"\n", 247 | "if id(s) == id(a):\n", 248 | " print(\"They are same\")\n", 249 | "else:\n", 250 | " print(\"They are not same\")" 251 | ] 252 | }, 253 | ], 254 | "metadata": { 255 | "kernelspec": { 256 | "display_name": "Python 3", 257 | "language": "python", 258 | "name": "python3" 259 | }, 260 | "language_info": { 261 | "codemirror_mode": { 262 | "name": "ipython", 263 | "version": 3 264 | }, 265 | "file_extension": ".py", 266 | "mimetype": "text/x-python", 267 | "name": "python", 268 | "nbconvert_exporter": "python", 269 | "pygments_lexer": "ipython3", 270 | "version": "3.7.3" 271 | } 272 | }, 273 | "nbformat": 4, 274 | "nbformat_minor": 2 275 | } 276 | -------------------------------------------------------------------------------- /7 Strings/7.05 Iterating on String.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### How to iterate on String:" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "str = \"Hello World!\"" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 3, 22 | "metadata": {}, 23 | "outputs": [ 24 | { 25 | "name": "stdout", 26 | "output_type": "stream", 27 | "text": [ 28 | "0 1 2 3 4 5 6 7 8 9 10 11 " 29 | ] 30 | } 31 | ], 32 | "source": [ 33 | "for i in range(len(str)):\n", 34 | " print(i, end = \" \")" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 4, 40 | "metadata": {}, 41 | "outputs": [ 42 | { 43 | "name": "stdout", 44 | "output_type": "stream", 45 | "text": [ 46 | "H e l l o W o r l d ! " 47 | ] 48 | } 49 | ], 50 | "source": [ 51 | "for i in str:\n", 52 | " print(i, end = ' ')" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": {}, 58 | "source": [ 59 | "#### Frequency of a letter 'l'" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 5, 65 | "metadata": {}, 66 | "outputs": [ 67 | { 68 | "name": "stdout", 69 | "output_type": "stream", 70 | "text": [ 71 | "3\n" 72 | ] 73 | } 74 | ], 75 | "source": [ 76 | "count = 0\n", 77 | "for letter in str:\n", 78 | " if letter == 'l':\n", 79 | " count += 1\n", 80 | "print(count) " 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 6, 86 | "metadata": {}, 87 | "outputs": [ 88 | { 89 | "name": "stdout", 90 | "output_type": "stream", 91 | "text": [ 92 | "3\n" 93 | ] 94 | } 95 | ], 96 | "source": [ 97 | "count = 0\n", 98 | "for i in range(len(str)):\n", 99 | " if (str[i] == 'l'):\n", 100 | " count += 1\n", 101 | "print(count)" 102 | ] 103 | }, 104 | { 105 | "cell_type": "markdown", 106 | "metadata": {}, 107 | "source": [ 108 | "#### in and not in Operation on String\n", 109 | "substring - a continuous part of string\n", 110 | "in - to find substring is there or not\n", 111 | "notin - to check the substring is not there in the string" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": 7, 117 | "metadata": {}, 118 | "outputs": [], 119 | "source": [ 120 | "str = 'hello'" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 8, 126 | "metadata": {}, 127 | "outputs": [ 128 | { 129 | "name": "stdout", 130 | "output_type": "stream", 131 | "text": [ 132 | "Not a substring\n" 133 | ] 134 | } 135 | ], 136 | "source": [ 137 | "if ' hel' in str:\n", 138 | " print(\"Substring\")\n", 139 | "else:\n", 140 | " print(\"Not a substring\")" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 10, 146 | "metadata": {}, 147 | "outputs": [ 148 | { 149 | "name": "stdout", 150 | "output_type": "stream", 151 | "text": [ 152 | "Not a substring\n" 153 | ] 154 | } 155 | ], 156 | "source": [ 157 | "if ' hui' not in str:\n", 158 | " print(\"Not a substring\")\n", 159 | "else: \n", 160 | " print(\"Substring\")" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": 15, 166 | "metadata": {}, 167 | "outputs": [ 168 | { 169 | "data": { 170 | "text/plain": [ 171 | "98" 172 | ] 173 | }, 174 | "execution_count": 15, 175 | "metadata": {}, 176 | "output_type": "execute_result" 177 | } 178 | ], 179 | "source": [ 180 | "ord('b')" 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": 18, 186 | "metadata": {}, 187 | "outputs": [ 188 | { 189 | "data": { 190 | "text/plain": [ 191 | "102" 192 | ] 193 | }, 194 | "execution_count": 18, 195 | "metadata": {}, 196 | "output_type": "execute_result" 197 | } 198 | ], 199 | "source": [ 200 | "ord('f')" 201 | ] 202 | }, 203 | ], 204 | "metadata": { 205 | "kernelspec": { 206 | "display_name": "Python 3", 207 | "language": "python", 208 | "name": "python3" 209 | }, 210 | "language_info": { 211 | "codemirror_mode": { 212 | "name": "ipython", 213 | "version": 3 214 | }, 215 | "file_extension": ".py", 216 | "mimetype": "text/x-python", 217 | "name": "python", 218 | "nbconvert_exporter": "python", 219 | "pygments_lexer": "ipython3", 220 | "version": "3.7.3" 221 | } 222 | }, 223 | "nbformat": 4, 224 | "nbformat_minor": 2 225 | } 226 | -------------------------------------------------------------------------------- /7 Strings/7.06 Comparison Operators on String.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### = <= >= < > == !=" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "a = \"Fazeel\" == \"Fazeel\"" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 3, 22 | "metadata": {}, 23 | "outputs": [ 24 | { 25 | "data": { 26 | "text/plain": [ 27 | "True" 28 | ] 29 | }, 30 | "execution_count": 3, 31 | "metadata": {}, 32 | "output_type": "execute_result" 33 | } 34 | ], 35 | "source": [ 36 | "a" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 8, 42 | "metadata": {}, 43 | "outputs": [], 44 | "source": [ 45 | "b = \"Zazeel\" >= \"Fazeel\" # Compared on basis of ASCII Value" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 9, 51 | "metadata": {}, 52 | "outputs": [ 53 | { 54 | "data": { 55 | "text/plain": [ 56 | "True" 57 | ] 58 | }, 59 | "execution_count": 9, 60 | "metadata": {}, 61 | "output_type": "execute_result" 62 | } 63 | ], 64 | "source": [ 65 | "b" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 10, 71 | "metadata": {}, 72 | "outputs": [], 73 | "source": [ 74 | "c = \"Fbzeel\" >= \"Fazeel\"" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 11, 80 | "metadata": {}, 81 | "outputs": [ 82 | { 83 | "data": { 84 | "text/plain": [ 85 | "True" 86 | ] 87 | }, 88 | "execution_count": 11, 89 | "metadata": {}, 90 | "output_type": "execute_result" 91 | } 92 | ], 93 | "source": [ 94 | "c" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 13, 100 | "metadata": {}, 101 | "outputs": [], 102 | "source": [ 103 | "d = \"fazeel\" >= \"Fazeel\"" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 14, 109 | "metadata": {}, 110 | "outputs": [ 111 | { 112 | "data": { 113 | "text/plain": [ 114 | "True" 115 | ] 116 | }, 117 | "execution_count": 14, 118 | "metadata": {}, 119 | "output_type": "execute_result" 120 | } 121 | ], 122 | "source": [ 123 | "d" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 15, 129 | "metadata": {}, 130 | "outputs": [], 131 | "source": [ 132 | "e = \"Faz\" >= \"Fazeel\" # length also matters" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 16, 138 | "metadata": {}, 139 | "outputs": [ 140 | { 141 | "data": { 142 | "text/plain": [ 143 | "False" 144 | ] 145 | }, 146 | "execution_count": 16, 147 | "metadata": {}, 148 | "output_type": "execute_result" 149 | } 150 | ], 151 | "source": [ 152 | "e" 153 | ] 154 | }, 155 | { 156 | "cell_type": "code", 157 | "execution_count": 17, 158 | "metadata": {}, 159 | "outputs": [ 160 | { 161 | "data": { 162 | "text/plain": [ 163 | "True" 164 | ] 165 | }, 166 | "execution_count": 17, 167 | "metadata": {}, 168 | "output_type": "execute_result" 169 | } 170 | ], 171 | "source": [ 172 | "\"fazeel\" >= \"fazeel\" # they're equal" 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": 18, 178 | "metadata": {}, 179 | "outputs": [ 180 | { 181 | "data": { 182 | "text/plain": [ 183 | "False" 184 | ] 185 | }, 186 | "execution_count": 18, 187 | "metadata": {}, 188 | "output_type": "execute_result" 189 | } 190 | ], 191 | "source": [ 192 | "\"fazeel\" != \"fazeel\"" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": 19, 198 | "metadata": {}, 199 | "outputs": [ 200 | { 201 | "data": { 202 | "text/plain": [ 203 | "True" 204 | ] 205 | }, 206 | "execution_count": 19, 207 | "metadata": {}, 208 | "output_type": "execute_result" 209 | } 210 | ], 211 | "source": [ 212 | "\"faz\" != \"fazeel\"" 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": 22, 218 | "metadata": {}, 219 | "outputs": [ 220 | { 221 | "name": "stdout", 222 | "output_type": "stream", 223 | "text": [ 224 | "False\n" 225 | ] 226 | } 227 | ], 228 | "source": [ 229 | "# Predict the output\n", 230 | "a = \"abcdef\" == \"abcd\"\n", 231 | "print(a)" 232 | ] 233 | }, 234 | { 235 | "cell_type": "code", 236 | "execution_count": 23, 237 | "metadata": {}, 238 | "outputs": [ 239 | { 240 | "name": "stdout", 241 | "output_type": "stream", 242 | "text": [ 243 | "True\n" 244 | ] 245 | } 246 | ], 247 | "source": [ 248 | "# Predict the output\n", 249 | "a = \"abcdef\" >= \"abcd\"\n", 250 | "print(a)" 251 | ] 252 | }, 253 | { 254 | "cell_type": "code", 255 | "execution_count": 27, 256 | "metadata": {}, 257 | "outputs": [ 258 | { 259 | "name": "stdout", 260 | "output_type": "stream", 261 | "text": [ 262 | "True\n" 263 | ] 264 | } 265 | ], 266 | "source": [ 267 | "# Predict the output\n", 268 | "a = \"abce\" >= \"abcdef\"\n", 269 | "print(a)" 270 | ] 271 | }, 272 | { 273 | "cell_type": "code", 274 | "execution_count": null, 275 | "metadata": {}, 276 | "outputs": [], 277 | "source": [] 278 | } 279 | ], 280 | "metadata": { 281 | "kernelspec": { 282 | "display_name": "Python 3", 283 | "language": "python", 284 | "name": "python3" 285 | }, 286 | "language_info": { 287 | "codemirror_mode": { 288 | "name": "ipython", 289 | "version": 3 290 | }, 291 | "file_extension": ".py", 292 | "mimetype": "text/x-python", 293 | "name": "python", 294 | "nbconvert_exporter": "python", 295 | "pygments_lexer": "ipython3", 296 | "version": "3.7.3" 297 | } 298 | }, 299 | "nbformat": 4, 300 | "nbformat_minor": 2 301 | } 302 | -------------------------------------------------------------------------------- /7 Strings/7.07 Operations on Strings.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Operations on String - https://thepythonguru.com/python-strings/" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "### split function" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": {}, 21 | "outputs": [ 22 | { 23 | "name": "stdout", 24 | "output_type": "stream", 25 | "text": [ 26 | "['My', 'Name', 'is', 'Fazeel']\n" 27 | ] 28 | } 29 | ], 30 | "source": [ 31 | "str = \"My Name is Fazeel\"\n", 32 | "li = str.split()\n", 33 | "print(li)" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 2, 39 | "metadata": {}, 40 | "outputs": [ 41 | { 42 | "name": "stdout", 43 | "output_type": "stream", 44 | "text": [ 45 | "['My', 'Name', 'is', 'Fazeel']\n" 46 | ] 47 | } 48 | ], 49 | "source": [ 50 | "str = \"My,Name,is,Fazeel\"\n", 51 | "li = str.split(',')\n", 52 | "print(li)" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 4, 58 | "metadata": {}, 59 | "outputs": [ 60 | { 61 | "name": "stdout", 62 | "output_type": "stream", 63 | "text": [ 64 | "['My', 'Name', 'is,Fazeel']\n" 65 | ] 66 | } 67 | ], 68 | "source": [ 69 | "str = \"My,Name,is,Fazeel\"\n", 70 | "li = str.split(',',2)\n", 71 | "print(li)" 72 | ] 73 | }, 74 | { 75 | "cell_type": "markdown", 76 | "metadata": {}, 77 | "source": [ 78 | "### replace" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 7, 84 | "metadata": {}, 85 | "outputs": [ 86 | { 87 | "name": "stdout", 88 | "output_type": "stream", 89 | "text": [ 90 | "my name is Usmani\n" 91 | ] 92 | } 93 | ], 94 | "source": [ 95 | "str = \"my name is Fazeel\"\n", 96 | "str = str.replace(\"Fazeel\", \"Usmani\")\n", 97 | "print(str)" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 8, 103 | "metadata": {}, 104 | "outputs": [ 105 | { 106 | "name": "stdout", 107 | "output_type": "stream", 108 | "text": [ 109 | "my name is Usmani Usmani Usmani\n" 110 | ] 111 | } 112 | ], 113 | "source": [ 114 | "str = \"my name is Fazeel Fazeel Fazeel\"\n", 115 | "str = str.replace(\"Fazeel\", \"Usmani\")\n", 116 | "print(str)" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 9, 122 | "metadata": {}, 123 | "outputs": [ 124 | { 125 | "name": "stdout", 126 | "output_type": "stream", 127 | "text": [ 128 | "my name is Fazeel\n" 129 | ] 130 | } 131 | ], 132 | "source": [ 133 | "str = \"my name is Fazeel\"\n", 134 | "str = str.replace(\"not there\", \"Usmani\")\n", 135 | "print(str)" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": 11, 141 | "metadata": {}, 142 | "outputs": [ 143 | { 144 | "name": "stdout", 145 | "output_type": "stream", 146 | "text": [ 147 | "my name is Usmani Usmani Fazeel\n" 148 | ] 149 | } 150 | ], 151 | "source": [ 152 | "str = \"my name is Fazeel Fazeel Fazeel\"\n", 153 | "str = str.replace(\"Fazeel\", \"Usmani\", 2)\n", 154 | "print(str)" 155 | ] 156 | }, 157 | { 158 | "cell_type": "markdown", 159 | "metadata": {}, 160 | "source": [ 161 | "### find - finds a particular substring in a string\n", 162 | "--> It returns the index of substring" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": 15, 168 | "metadata": {}, 169 | "outputs": [ 170 | { 171 | "name": "stdout", 172 | "output_type": "stream", 173 | "text": [ 174 | "3\n" 175 | ] 176 | } 177 | ], 178 | "source": [ 179 | "str = \"My name is Fazeel\"\n", 180 | "index = str.find(\"na\") #returns the 1st index of substring\n", 181 | "print(index) " 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": 16, 187 | "metadata": {}, 188 | "outputs": [ 189 | { 190 | "name": "stdout", 191 | "output_type": "stream", 192 | "text": [ 193 | "11\n" 194 | ] 195 | } 196 | ], 197 | "source": [ 198 | "str = \"my name is Fazeel Fazeel Fazeel\"\n", 199 | "index = str.find(\"Fa\") #returns the 1st index of substring\n", 200 | "print(index) " 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": 17, 206 | "metadata": {}, 207 | "outputs": [ 208 | { 209 | "name": "stdout", 210 | "output_type": "stream", 211 | "text": [ 212 | "18\n" 213 | ] 214 | } 215 | ], 216 | "source": [ 217 | "str = \"my name is Fazeel Fazeel Fazeel\"\n", 218 | "index = str.find(\"Fa\", 16, 20) #returns the 1st index of substring\n", 219 | "print(index) " 220 | ] 221 | }, 222 | { 223 | "cell_type": "markdown", 224 | "metadata": {}, 225 | "source": [ 226 | "### lower and upper" 227 | ] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "execution_count": 20, 232 | "metadata": {}, 233 | "outputs": [ 234 | { 235 | "name": "stdout", 236 | "output_type": "stream", 237 | "text": [ 238 | "my name is fazeel\n", 239 | "MY NAME IS FAZEEL\n" 240 | ] 241 | } 242 | ], 243 | "source": [ 244 | "str = \"My name is Fazeel\"\n", 245 | "str = str.lower()\n", 246 | "print(str)\n", 247 | "str = str.upper()\n", 248 | "print(str)" 249 | ] 250 | }, 251 | { 252 | "cell_type": "markdown", 253 | "metadata": {}, 254 | "source": [ 255 | "### startswith and endswith" 256 | ] 257 | }, 258 | { 259 | "cell_type": "code", 260 | "execution_count": 25, 261 | "metadata": {}, 262 | "outputs": [ 263 | { 264 | "name": "stdout", 265 | "output_type": "stream", 266 | "text": [ 267 | "False\n" 268 | ] 269 | } 270 | ], 271 | "source": [ 272 | "str = \"My name is Fazeel\"\n", 273 | "ans = str.startswith(\"My is\")\n", 274 | "print(ans)" 275 | ] 276 | }, 277 | { 278 | "cell_type": "code", 279 | "execution_count": 26, 280 | "metadata": {}, 281 | "outputs": [ 282 | { 283 | "name": "stdout", 284 | "output_type": "stream", 285 | "text": [ 286 | "True\n" 287 | ] 288 | } 289 | ], 290 | "source": [ 291 | "str = \"My name is Fazeel\"\n", 292 | "ans = str.startswith(\"Faz\", 11, 20)\n", 293 | "print(ans)" 294 | ] 295 | }, 296 | { 297 | "cell_type": "code", 298 | "execution_count": 28, 299 | "metadata": {}, 300 | "outputs": [ 301 | { 302 | "name": "stdout", 303 | "output_type": "stream", 304 | "text": [ 305 | "True\n" 306 | ] 307 | } 308 | ], 309 | "source": [ 310 | "str = \"My name is Fazeel\"\n", 311 | "ans = str.endswith(\"eel\", 11, 20)\n", 312 | "print(ans)" 313 | ] 314 | } 315 | ], 316 | "metadata": { 317 | "kernelspec": { 318 | "display_name": "Python 3", 319 | "language": "python", 320 | "name": "python3" 321 | }, 322 | "language_info": { 323 | "codemirror_mode": { 324 | "name": "ipython", 325 | "version": 3 326 | }, 327 | "file_extension": ".py", 328 | "mimetype": "text/x-python", 329 | "name": "python", 330 | "nbconvert_exporter": "python", 331 | "pygments_lexer": "ipython3", 332 | "version": "3.7.3" 333 | } 334 | }, 335 | "nbformat": 4, 336 | "nbformat_minor": 2 337 | } 338 | -------------------------------------------------------------------------------- /7 Strings/7.08 Replace Character in String.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "data": { 10 | "text/plain": [ 11 | "'FazZZl'" 12 | ] 13 | }, 14 | "execution_count": 3, 15 | "metadata": {}, 16 | "output_type": "execute_result" 17 | } 18 | ], 19 | "source": [ 20 | "str = \"Fazeel\"\n", 21 | "str1 = str.replace('e', 'Z')\n", 22 | "str1" 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "metadata": {}, 28 | "source": [ 29 | "#### You cannot make changes in a string because strings are immutable. You need to make a new string." 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 6, 35 | "metadata": {}, 36 | "outputs": [ 37 | { 38 | "data": { 39 | "text/plain": [ 40 | "'Huhuhuhu'" 41 | ] 42 | }, 43 | "execution_count": 6, 44 | "metadata": {}, 45 | "output_type": "execute_result" 46 | } 47 | ], 48 | "source": [ 49 | "def replace(str, char1, char2):\n", 50 | " newStr = \"\"\n", 51 | " for char in str:\n", 52 | " if char == char1:\n", 53 | " newStr += char2\n", 54 | " else:\n", 55 | " newStr += char\n", 56 | " return newStr\n", 57 | "str = \"Hahahaha\"\n", 58 | "replace(str, 'a', 'u')" 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.3" 79 | } 80 | }, 81 | "nbformat": 4, 82 | "nbformat_minor": 2 83 | } 84 | -------------------------------------------------------------------------------- /7 Strings/7.09 Count Vowels, Consonants, Digits in String.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Count number of vowels, consonants, digits and special characters in string" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "vowels are a,e,i,o,u and A,E,I,O,U" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": {}, 21 | "outputs": [ 22 | { 23 | "name": "stdout", 24 | "output_type": "stream", 25 | "text": [ 26 | "2 14 6 10\n" 27 | ] 28 | } 29 | ], 30 | "source": [ 31 | "# Here you can also see how to return multiple values through functions\n", 32 | "def countInString(str):\n", 33 | " v,c,d,s = 0,0,0,0\n", 34 | " for char in str:\n", 35 | " \n", 36 | " if((char >= 'a' and char <= 'z') or (char >= 'A' and char <= 'Z')):\n", 37 | " char = char.lower()\n", 38 | " if(char == 'a' or char == 'e' or char == 'i' or char == 'o' or char == 'u'):\n", 39 | " v += 1\n", 40 | " else:\n", 41 | " c += 1\n", 42 | " elif char >= '0' and char <= '9':\n", 43 | " d += 1\n", 44 | " else:\n", 45 | " s += 1\n", 46 | " return v,c,d,s\n", 47 | " \n", 48 | " \n", 49 | "\n", 50 | "str = \"kladhsfklj0934u90*&%^&*^*l(dsjf)\"\n", 51 | "\n", 52 | "v,c,d,s = countInString(str)\n", 53 | "print(v,c,d,s)" 54 | ] 55 | } 56 | ], 57 | "metadata": { 58 | "kernelspec": { 59 | "display_name": "Python 3", 60 | "language": "python", 61 | "name": "python3" 62 | }, 63 | "language_info": { 64 | "codemirror_mode": { 65 | "name": "ipython", 66 | "version": 3 67 | }, 68 | "file_extension": ".py", 69 | "mimetype": "text/x-python", 70 | "name": "python", 71 | "nbconvert_exporter": "python", 72 | "pygments_lexer": "ipython3", 73 | "version": "3.7.3" 74 | } 75 | }, 76 | "nbformat": 4, 77 | "nbformat_minor": 2 78 | } 79 | -------------------------------------------------------------------------------- /7 Strings/7.10 [Practice] Check Permutation.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Check if both the strings are having same characters, order may be different" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 11, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "name": "stdout", 17 | "output_type": "stream", 18 | "text": [ 19 | "abcd\n", 20 | "dcba\n", 21 | "true\n" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "#Using count array\n", 27 | "str1 = input()\n", 28 | "str2 = input()\n", 29 | "count1 = []\n", 30 | "count2 = []\n", 31 | "\n", 32 | "num1 = 26\n", 33 | "while num1:\n", 34 | " count1.append(0)\n", 35 | " num1 -= 1\n", 36 | " \n", 37 | "num2 = 26\n", 38 | "while num2:\n", 39 | " count2.append(0)\n", 40 | " num2 -= 1\n", 41 | "\n", 42 | "def checkLength(str1,str2):\n", 43 | " if len(str1) != len(str2):\n", 44 | " return 0\n", 45 | " else:\n", 46 | " return 1\n", 47 | " \n", 48 | "def permutation1(str):\n", 49 | " for i in str:\n", 50 | " ascii = ord(i)\n", 51 | " count1[ascii % 97] += 1\n", 52 | " return count1\n", 53 | "\n", 54 | "def permutation2(str):\n", 55 | " for i in str:\n", 56 | " ascii = ord(i)\n", 57 | " count2[ascii % 97] += 1\n", 58 | " return count2\n", 59 | "\n", 60 | "if checkLength(str1, str2):\n", 61 | " list1 = permutation1(str1) \n", 62 | " list2 = permutation2(str2)\n", 63 | " \n", 64 | " flag = True\n", 65 | " for i in range(0, 26):\n", 66 | " if list1[i] != list2[i]:\n", 67 | " flag = False\n", 68 | " if flag == False:\n", 69 | " print(\"false\")\n", 70 | " else:\n", 71 | " print(\"true\")\n", 72 | " \n", 73 | "else:\n", 74 | " print(\"false\")\n", 75 | " \n", 76 | "\n", 77 | " " 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 5, 83 | "metadata": {}, 84 | "outputs": [ 85 | { 86 | "name": "stdout", 87 | "output_type": "stream", 88 | "text": [ 89 | "abc\n", 90 | "bca\n", 91 | "bca\n", 92 | "False\n" 93 | ] 94 | } 95 | ], 96 | "source": [ 97 | "str1 = input()\n", 98 | "str2 = input()\n", 99 | "str3 = str2.replace(str1, '1')\n", 100 | "print(str3)\n", 101 | "if str3 == 1:\n", 102 | " print(\"True\")\n", 103 | "else:\n", 104 | " print(\"False\")\n" 105 | ] 106 | } 107 | ], 108 | "metadata": { 109 | "kernelspec": { 110 | "display_name": "Python 3", 111 | "language": "python", 112 | "name": "python3" 113 | }, 114 | "language_info": { 115 | "codemirror_mode": { 116 | "name": "ipython", 117 | "version": 3 118 | }, 119 | "file_extension": ".py", 120 | "mimetype": "text/x-python", 121 | "name": "python", 122 | "nbconvert_exporter": "python", 123 | "pygments_lexer": "ipython3", 124 | "version": "3.7.3" 125 | } 126 | }, 127 | "nbformat": 4, 128 | "nbformat_minor": 2 129 | } 130 | -------------------------------------------------------------------------------- /7 Strings/7.11 [Practice] Remove Consecutive Duplicates.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 8, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "aavvdded\n", 13 | "avded" 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "str = input()\n", 19 | "str1 = []\n", 20 | "count = 0\n", 21 | "for i in range(0, len(str)-1):\n", 22 | " \n", 23 | " if str[i] == str[i+1]:\n", 24 | " count += 1\n", 25 | " elif str[i] != str[i+1] and count > 0:\n", 26 | " str1.append(str[i])\n", 27 | " count = 0\n", 28 | " else:\n", 29 | " str1.append(str[i])\n", 30 | "str1.append(str[i+1])\n", 31 | " \n", 32 | "for ele in str1:\n", 33 | " print(ele, end = '')" 34 | ] 35 | }, 36 | ], 37 | "metadata": { 38 | "kernelspec": { 39 | "display_name": "Python 3", 40 | "language": "python", 41 | "name": "python3" 42 | }, 43 | "language_info": { 44 | "codemirror_mode": { 45 | "name": "ipython", 46 | "version": 3 47 | }, 48 | "file_extension": ".py", 49 | "mimetype": "text/x-python", 50 | "name": "python", 51 | "nbconvert_exporter": "python", 52 | "pygments_lexer": "ipython3", 53 | "version": "3.7.3" 54 | } 55 | }, 56 | "nbformat": 4, 57 | "nbformat_minor": 2 58 | } 59 | -------------------------------------------------------------------------------- /7 Strings/7.12 [Practice] Reverse Each Word .ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 12, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "Welcome to Coding Ninjas\n", 13 | "emocleW ot gnidoC sajniN " 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "str = input().split()\n", 19 | "words = len(str)\n", 20 | "\n", 21 | "def reverse(s):\n", 22 | " print(s[::-1], end = \" \")\n", 23 | "\n", 24 | "for i in range(0, words):\n", 25 | " reverse(str[i])\n" 26 | ] 27 | }, 28 | ], 29 | "metadata": { 30 | "kernelspec": { 31 | "display_name": "Python 3", 32 | "language": "python", 33 | "name": "python3" 34 | }, 35 | "language_info": { 36 | "codemirror_mode": { 37 | "name": "ipython", 38 | "version": 3 39 | }, 40 | "file_extension": ".py", 41 | "mimetype": "text/x-python", 42 | "name": "python", 43 | "nbconvert_exporter": "python", 44 | "pygments_lexer": "ipython3", 45 | "version": "3.7.3" 46 | } 47 | }, 48 | "nbformat": 4, 49 | "nbformat_minor": 2 50 | } 51 | -------------------------------------------------------------------------------- /7 Strings/7.13 [Practice] Remove character.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Given a string and a character x. Write a function to remove all occurrences of x character from the given string.\n", 8 | "Leave the string as it is, if the given character is not present in the string.\n", 9 | "\n", 10 | "Input format :\n", 11 | "\n", 12 | "Line 1 : Input string\n", 13 | "\n", 14 | "Line 2 : Character x\n", 15 | "Sample Input :\n", 16 | "\n", 17 | "welcome to coding ninjas\n", 18 | "o\n", 19 | "\n", 20 | "Sample Output :\n", 21 | "\n", 22 | "welcme t cding ninjas\n", 23 | "\n" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": null, 29 | "metadata": {}, 30 | "outputs": [], 31 | "source": [ 32 | "str = input()\n", 33 | "x = input()\n", 34 | "list = str.split(x)\n", 35 | "for ele in list:\n", 36 | " print(ele, end = '')\n" 37 | ] 38 | }, 39 | ], 40 | "metadata": { 41 | "kernelspec": { 42 | "display_name": "Python 3", 43 | "language": "python", 44 | "name": "python3" 45 | }, 46 | "language_info": { 47 | "codemirror_mode": { 48 | "name": "ipython", 49 | "version": 3 50 | }, 51 | "file_extension": ".py", 52 | "mimetype": "text/x-python", 53 | "name": "python", 54 | "nbconvert_exporter": "python", 55 | "pygments_lexer": "ipython3", 56 | "version": "3.7.3" 57 | } 58 | }, 59 | "nbformat": 4, 60 | "nbformat_minor": 2 61 | } 62 | -------------------------------------------------------------------------------- /7 Strings/7.14 [Practice] Highest Occurring Character.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Given a string, find and return the highest occurring character present in the given string.\n", 8 | "If there are 2 characters in the input string with same frequency, return the character which comes first.\n", 9 | "Note : Assume all the characters in the given string are lowercase.\n", 10 | "Sample Input 1:\n", 11 | "\n", 12 | "abdefgbabfba\n", 13 | "\n", 14 | "Sample Output 1:\n", 15 | "\n", 16 | "b\n", 17 | "\n", 18 | "Sample Input 2:\n", 19 | "\n", 20 | "xy\n", 21 | "\n", 22 | "Sample Output 2:\n", 23 | "\n", 24 | "x\n" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 4, 30 | "metadata": {}, 31 | "outputs": [ 32 | { 33 | "name": "stdout", 34 | "output_type": "stream", 35 | "text": [ 36 | "xy\n", 37 | "x\n" 38 | ] 39 | } 40 | ], 41 | "source": [ 42 | "str = input()\n", 43 | "#Using count array\n", 44 | "count = []\n", 45 | "\n", 46 | "num = 26\n", 47 | "while num:\n", 48 | " count.append(0)\n", 49 | " num -= 1\n", 50 | "max = 0\n", 51 | "char = ''\n", 52 | "for i in str:\n", 53 | " ascii = ord(i)\n", 54 | " count[ascii % 97] += 1\n", 55 | " if count[ascii % 97] > max:\n", 56 | " max = count[ascii % 97]\n", 57 | " char = i\n", 58 | " \n", 59 | "print(char)\n" 60 | ] 61 | }, 62 | ], 63 | "metadata": { 64 | "kernelspec": { 65 | "display_name": "Python 3", 66 | "language": "python", 67 | "name": "python3" 68 | }, 69 | "language_info": { 70 | "codemirror_mode": { 71 | "name": "ipython", 72 | "version": 3 73 | }, 74 | "file_extension": ".py", 75 | "mimetype": "text/x-python", 76 | "name": "python", 77 | "nbconvert_exporter": "python", 78 | "pygments_lexer": "ipython3", 79 | "version": "3.7.3" 80 | } 81 | }, 82 | "nbformat": 4, 83 | "nbformat_minor": 2 84 | } 85 | -------------------------------------------------------------------------------- /7 Strings/7.15 [Practice] Compress the String .ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Write a program to do basic string compression. For a character which is consecutively repeated more than once, replace consecutive duplicate occurrences with the count of repetitions.\n", 8 | "For e.g. if a String has 'x' repeated 5 times, replace this \"xxxxx\" with \"x5\".\n", 9 | "Note : Consecutive count of every character in input string is less than equal to 9.\n", 10 | "Input Format :\n", 11 | "\n", 12 | "Input string S\n", 13 | "\n", 14 | "Output Format :\n", 15 | "\n", 16 | "Compressed string \n", 17 | "\n", 18 | "Sample Input:\n", 19 | "\n", 20 | "aaabbccdsa\n", 21 | "\n", 22 | "Sample Output:\n", 23 | "\n", 24 | "a3b2c2dsa\n", 25 | "\n" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 8, 31 | "metadata": {}, 32 | "outputs": [ 33 | { 34 | "name": "stdout", 35 | "output_type": "stream", 36 | "text": [ 37 | "asssdddddddfffffjkkk\n", 38 | "as3d7f5jk3" 39 | ] 40 | } 41 | ], 42 | "source": [ 43 | "str = input()\n", 44 | "str1 = []\n", 45 | "count = 0\n", 46 | "for i in range(0, len(str)-1):\n", 47 | " if str[i] == str[i+1]:\n", 48 | " count += 1\n", 49 | " elif str[i] != str[i+1] and count > 0:\n", 50 | " str1.append(str[i])\n", 51 | " str1.append(count+1)\n", 52 | " count = 0\n", 53 | " else:\n", 54 | " str1.append(str[i])\n", 55 | "str1.append(str[i+1])\n", 56 | "if count > 0:\n", 57 | " str1.append(count+1)\n", 58 | " \n", 59 | "for ele in str1:\n", 60 | " print(ele, end = '')" 61 | ] 62 | }, 63 | ], 64 | "metadata": { 65 | "kernelspec": { 66 | "display_name": "Python 3", 67 | "language": "python", 68 | "name": "python3" 69 | }, 70 | "language_info": { 71 | "codemirror_mode": { 72 | "name": "ipython", 73 | "version": 3 74 | }, 75 | "file_extension": ".py", 76 | "mimetype": "text/x-python", 77 | "name": "python", 78 | "nbconvert_exporter": "python", 79 | "pygments_lexer": "ipython3", 80 | "version": "3.7.3" 81 | } 82 | }, 83 | "nbformat": 4, 84 | "nbformat_minor": 2 85 | } 86 | -------------------------------------------------------------------------------- /8 Two Dimensional Lists/8.01 Intro to 2-D Lists.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 8, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]\n" 13 | ] 14 | } 15 | ], 16 | "source": [ 17 | "li = [[1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16]]\n", 18 | "print(li)" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 9, 24 | "metadata": {}, 25 | "outputs": [ 26 | { 27 | "data": { 28 | "text/plain": [ 29 | "11" 30 | ] 31 | }, 32 | "execution_count": 9, 33 | "metadata": {}, 34 | "output_type": "execute_result" 35 | } 36 | ], 37 | "source": [ 38 | "li[2][2]" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 10, 44 | "metadata": {}, 45 | "outputs": [ 46 | { 47 | "ename": "IndexError", 48 | "evalue": "list index out of range", 49 | "output_type": "error", 50 | "traceback": [ 51 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 52 | "\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", 53 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mli\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 54 | "\u001b[1;31mIndexError\u001b[0m: list index out of range" 55 | ] 56 | } 57 | ], 58 | "source": [ 59 | "li[2][5]" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 12, 65 | "metadata": {}, 66 | "outputs": [ 67 | { 68 | "data": { 69 | "text/plain": [ 70 | "13" 71 | ] 72 | }, 73 | "execution_count": 12, 74 | "metadata": {}, 75 | "output_type": "execute_result" 76 | } 77 | ], 78 | "source": [ 79 | "li[3][0]" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 13, 85 | "metadata": {}, 86 | "outputs": [ 87 | { 88 | "data": { 89 | "text/plain": [ 90 | "8" 91 | ] 92 | }, 93 | "execution_count": 13, 94 | "metadata": {}, 95 | "output_type": "execute_result" 96 | } 97 | ], 98 | "source": [ 99 | "li[1][3]" 100 | ] 101 | }, 102 | { 103 | "cell_type": "markdown", 104 | "metadata": {}, 105 | "source": [ 106 | "### 2-D Lists can be imagined like below table" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 18, 112 | "metadata": {}, 113 | "outputs": [ 114 | { 115 | "name": "stdout", 116 | "output_type": "stream", 117 | "text": [ 118 | "1\t2\t3\t4\t\n", 119 | "5\t6\t7\t8\t\n", 120 | "9\t10\t11\t12\t\n", 121 | "13\t14\t15\t16\t\n" 122 | ] 123 | } 124 | ], 125 | "source": [ 126 | "for i in range(0, 4):\n", 127 | " for j in range(0,4):\n", 128 | " print(li[i][j], end = \"\\t\")\n", 129 | " print()" 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": null, 135 | "metadata": {}, 136 | "outputs": [], 137 | "source": [] 138 | } 139 | ], 140 | "metadata": { 141 | "kernelspec": { 142 | "display_name": "Python 3", 143 | "language": "python", 144 | "name": "python3" 145 | }, 146 | "language_info": { 147 | "codemirror_mode": { 148 | "name": "ipython", 149 | "version": 3 150 | }, 151 | "file_extension": ".py", 152 | "mimetype": "text/x-python", 153 | "name": "python", 154 | "nbconvert_exporter": "python", 155 | "pygments_lexer": "ipython3", 156 | "version": "3.7.3" 157 | } 158 | }, 159 | "nbformat": 4, 160 | "nbformat_minor": 2 161 | } 162 | -------------------------------------------------------------------------------- /8 Two Dimensional Lists/8.03 Jagged Lists.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### If Columns sizes are not same in a 2-D lists then it is known as \"Jagged Lists\"" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "list = [[1,2,3,4], [5,6,7], [8,9], [10,11]]" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 3, 22 | "metadata": {}, 23 | "outputs": [ 24 | { 25 | "data": { 26 | "text/plain": [ 27 | "[1, 2, 3, 4]" 28 | ] 29 | }, 30 | "execution_count": 3, 31 | "metadata": {}, 32 | "output_type": "execute_result" 33 | } 34 | ], 35 | "source": [ 36 | "list[0]" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 4, 42 | "metadata": {}, 43 | "outputs": [ 44 | { 45 | "data": { 46 | "text/plain": [ 47 | "[5, 6, 7]" 48 | ] 49 | }, 50 | "execution_count": 4, 51 | "metadata": {}, 52 | "output_type": "execute_result" 53 | } 54 | ], 55 | "source": [ 56 | "list[1]" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 5, 62 | "metadata": {}, 63 | "outputs": [ 64 | { 65 | "data": { 66 | "text/plain": [ 67 | "[8, 9]" 68 | ] 69 | }, 70 | "execution_count": 5, 71 | "metadata": {}, 72 | "output_type": "execute_result" 73 | } 74 | ], 75 | "source": [ 76 | "list[2]" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 6, 82 | "metadata": {}, 83 | "outputs": [ 84 | { 85 | "data": { 86 | "text/plain": [ 87 | "[10, 11]" 88 | ] 89 | }, 90 | "execution_count": 6, 91 | "metadata": {}, 92 | "output_type": "execute_result" 93 | } 94 | ], 95 | "source": [ 96 | "list[3]" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 8, 102 | "metadata": {}, 103 | "outputs": [ 104 | { 105 | "ename": "IndexError", 106 | "evalue": "list index out of range", 107 | "output_type": "error", 108 | "traceback": [ 109 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 110 | "\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", 111 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mlist\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 112 | "\u001b[1;31mIndexError\u001b[0m: list index out of range" 113 | ] 114 | } 115 | ], 116 | "source": [ 117 | "list[2][3]" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 9, 123 | "metadata": {}, 124 | "outputs": [ 125 | { 126 | "data": { 127 | "text/plain": [ 128 | "4" 129 | ] 130 | }, 131 | "execution_count": 9, 132 | "metadata": {}, 133 | "output_type": "execute_result" 134 | } 135 | ], 136 | "source": [ 137 | "list[0][3]" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": 10, 143 | "metadata": {}, 144 | "outputs": [ 145 | { 146 | "name": "stdout", 147 | "output_type": "stream", 148 | "text": [ 149 | "[7, 8, 9]\n" 150 | ] 151 | } 152 | ], 153 | "source": [ 154 | "#Predict the Output\n", 155 | "li = [[1,2,3,4],[5,6],[7,8,9]]\n", 156 | "print(li[2])" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 11, 162 | "metadata": {}, 163 | "outputs": [ 164 | { 165 | "ename": "IndexError", 166 | "evalue": "list index out of range", 167 | "output_type": "error", 168 | "traceback": [ 169 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 170 | "\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", 171 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m#Predict the Output\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0mli\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m6\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m7\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m8\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m9\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mli\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 172 | "\u001b[1;31mIndexError\u001b[0m: list index out of range" 173 | ] 174 | } 175 | ], 176 | "source": [ 177 | "#Predict the Output\n", 178 | "li = [[1,2,3,4],[5,6],[7,8,9]]\n", 179 | "print(li[1][3])" 180 | ] 181 | }, 182 | ], 183 | "metadata": { 184 | "kernelspec": { 185 | "display_name": "Python 3", 186 | "language": "python", 187 | "name": "python3" 188 | }, 189 | "language_info": { 190 | "codemirror_mode": { 191 | "name": "ipython", 192 | "version": 3 193 | }, 194 | "file_extension": ".py", 195 | "mimetype": "text/x-python", 196 | "name": "python", 197 | "nbconvert_exporter": "python", 198 | "pygments_lexer": "ipython3", 199 | "version": "3.7.3" 200 | } 201 | }, 202 | "nbformat": 4, 203 | "nbformat_minor": 2 204 | } 205 | -------------------------------------------------------------------------------- /8 Two Dimensional Lists/8.05 Input of 2-D lists.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### To know more about 2D arrays visit https://snakify.org/en/lessons/two_dimensional_lists_arrays/" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "### To learn step by step visit https://www.w3resource.com/python-exercises/python-conditional-exercise-11.php" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": {}, 21 | "outputs": [ 22 | { 23 | "name": "stdout", 24 | "output_type": "stream", 25 | "text": [ 26 | "3 4\n", 27 | "1 2 3 4 5\n", 28 | "2 5 6 4 7\n", 29 | "2 3 6 5 4\n" 30 | ] 31 | } 32 | ], 33 | "source": [ 34 | "str = input().split()\n", 35 | "n,m = int(str[0]), int(str[1])\n", 36 | "li = [[int(j) for j in input().split()[:m]] for i in range(n)]" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 2, 42 | "metadata": {}, 43 | "outputs": [ 44 | { 45 | "data": { 46 | "text/plain": [ 47 | "[[1, 2, 3, 4], [2, 5, 6, 4], [2, 3, 6, 5]]" 48 | ] 49 | }, 50 | "execution_count": 2, 51 | "metadata": {}, 52 | "output_type": "execute_result" 53 | } 54 | ], 55 | "source": [ 56 | "li" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 4, 62 | "metadata": {}, 63 | "outputs": [ 64 | { 65 | "name": "stdout", 66 | "output_type": "stream", 67 | "text": [ 68 | "3\n", 69 | "1 2 3 4 5\n", 70 | "6 7 8 9\n", 71 | "1 2 5 \n" 72 | ] 73 | } 74 | ], 75 | "source": [ 76 | "n = int(input())\n", 77 | "li = [[int(j) for j in input().split()] for i in range(n)]" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 5, 83 | "metadata": {}, 84 | "outputs": [ 85 | { 86 | "data": { 87 | "text/plain": [ 88 | "[[1, 2, 3, 4, 5], [6, 7, 8, 9], [1, 2, 5]]" 89 | ] 90 | }, 91 | "execution_count": 5, 92 | "metadata": {}, 93 | "output_type": "execute_result" 94 | } 95 | ], 96 | "source": [ 97 | "li" 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "metadata": {}, 103 | "source": [ 104 | "### Convert a list into a 2D array by a simple formula:- (i,j) = m * i + j" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 5, 110 | "metadata": {}, 111 | "outputs": [ 112 | { 113 | "name": "stdout", 114 | "output_type": "stream", 115 | "text": [ 116 | "2 2\n", 117 | "1 2 3 4 5 6 7 8\n" 118 | ] 119 | } 120 | ], 121 | "source": [ 122 | "str = input().split()\n", 123 | "n,m = int(str[0]), int(str[1])\n", 124 | "b = input().split()\n", 125 | "arr = [ [int(b[m*i+j]) for j in range(m)] for i in range(n)]" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 6, 131 | "metadata": {}, 132 | "outputs": [ 133 | { 134 | "data": { 135 | "text/plain": [ 136 | "[[1, 2], [3, 4]]" 137 | ] 138 | }, 139 | "execution_count": 6, 140 | "metadata": {}, 141 | "output_type": "execute_result" 142 | } 143 | ], 144 | "source": [ 145 | "arr" 146 | ] 147 | }, 148 | { 149 | "cell_type": "markdown", 150 | "metadata": {}, 151 | "source": [ 152 | "#### Input in a single line" 153 | ] 154 | }, 155 | { 156 | "cell_type": "code", 157 | "execution_count": 9, 158 | "metadata": {}, 159 | "outputs": [ 160 | { 161 | "name": "stdout", 162 | "output_type": "stream", 163 | "text": [ 164 | "3 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15\n" 165 | ] 166 | } 167 | ], 168 | "source": [ 169 | "str = input().split()\n", 170 | "n,m = int(str[0]), int(str[1])\n", 171 | "b = str[2:]\n", 172 | "arr1 = [ [int(b[m*i+j]) for j in range(m)] for i in range(n)]" 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": 10, 178 | "metadata": {}, 179 | "outputs": [ 180 | { 181 | "data": { 182 | "text/plain": [ 183 | "[[1, 2], [3, 4]]" 184 | ] 185 | }, 186 | "execution_count": 10, 187 | "metadata": {}, 188 | "output_type": "execute_result" 189 | } 190 | ], 191 | "source": [ 192 | "arr" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": 11, 198 | "metadata": {}, 199 | "outputs": [ 200 | { 201 | "data": { 202 | "text/plain": [ 203 | "3" 204 | ] 205 | }, 206 | "execution_count": 11, 207 | "metadata": {}, 208 | "output_type": "execute_result" 209 | } 210 | ], 211 | "source": [ 212 | "n" 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": 12, 218 | "metadata": {}, 219 | "outputs": [ 220 | { 221 | "data": { 222 | "text/plain": [ 223 | "5" 224 | ] 225 | }, 226 | "execution_count": 12, 227 | "metadata": {}, 228 | "output_type": "execute_result" 229 | } 230 | ], 231 | "source": [ 232 | "m" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": 13, 238 | "metadata": {}, 239 | "outputs": [ 240 | { 241 | "data": { 242 | "text/plain": [ 243 | "'5'" 244 | ] 245 | }, 246 | "execution_count": 13, 247 | "metadata": {}, 248 | "output_type": "execute_result" 249 | } 250 | ], 251 | "source": [ 252 | "str[1]" 253 | ] 254 | } 255 | ], 256 | "metadata": { 257 | "kernelspec": { 258 | "display_name": "Python 3", 259 | "language": "python", 260 | "name": "python3" 261 | }, 262 | "language_info": { 263 | "codemirror_mode": { 264 | "name": "ipython", 265 | "version": 3 266 | }, 267 | "file_extension": ".py", 268 | "mimetype": "text/x-python", 269 | "name": "python", 270 | "nbconvert_exporter": "python", 271 | "pygments_lexer": "ipython3", 272 | "version": "3.7.3" 273 | } 274 | }, 275 | "nbformat": 4, 276 | "nbformat_minor": 2 277 | } 278 | -------------------------------------------------------------------------------- /8 Two Dimensional Lists/8.06 Printing of a 2-D list.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "li = [[1,2,3,4], [5,6,7,8], [9,10,11,12]]" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "n = 3\n", 19 | "m = 4" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 3, 25 | "metadata": {}, 26 | "outputs": [ 27 | { 28 | "name": "stdout", 29 | "output_type": "stream", 30 | "text": [ 31 | "1 2 3 4 \n", 32 | "5 6 7 8 \n", 33 | "9 10 11 12 \n" 34 | ] 35 | } 36 | ], 37 | "source": [ 38 | "for i in range(n):\n", 39 | " for j in range(m):\n", 40 | " print(li[i][j], end = \" \")\n", 41 | " print()" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 8, 47 | "metadata": {}, 48 | "outputs": [ 49 | { 50 | "name": "stdout", 51 | "output_type": "stream", 52 | "text": [ 53 | "1 2 3 4 \n", 54 | "5 6 \n", 55 | "9 10 11 \n" 56 | ] 57 | } 58 | ], 59 | "source": [ 60 | "list = [[1,2,3,4], [5,6], [9,10,11]]\n", 61 | "n = 3\n", 62 | "for row in list:\n", 63 | " for ele in row:\n", 64 | " print(ele, end = ' ')\n", 65 | " print() \n", 66 | " " 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 11, 72 | "metadata": {}, 73 | "outputs": [ 74 | { 75 | "data": { 76 | "text/plain": [ 77 | "'aabbabcabd'" 78 | ] 79 | }, 80 | "execution_count": 11, 81 | "metadata": {}, 82 | "output_type": "execute_result" 83 | } 84 | ], 85 | "source": [ 86 | "'ab'.join('abcd')" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 14, 92 | "metadata": {}, 93 | "outputs": [ 94 | { 95 | "data": { 96 | "text/plain": [ 97 | "'1ab2ab3'" 98 | ] 99 | }, 100 | "execution_count": 14, 101 | "metadata": {}, 102 | "output_type": "execute_result" 103 | } 104 | ], 105 | "source": [ 106 | "'ab'.join(['1', '2', '3'])" 107 | ] 108 | }, 109 | { 110 | "cell_type": "markdown", 111 | "metadata": {}, 112 | "source": [ 113 | "### Using join operation to print a 2-D list" 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": 15, 119 | "metadata": {}, 120 | "outputs": [ 121 | { 122 | "name": "stdout", 123 | "output_type": "stream", 124 | "text": [ 125 | "1 2 3 4\n", 126 | "5 6\n", 127 | "9 10 11\n" 128 | ] 129 | } 130 | ], 131 | "source": [ 132 | "list = [[1,2,3,4], [5,6], [9,10,11]]\n", 133 | "n = 3\n", 134 | "for row in list:\n", 135 | " output = ' '.join([str(ele) for ele in row])\n", 136 | " print(output) " 137 | ] 138 | }, 139 | ], 140 | "metadata": { 141 | "kernelspec": { 142 | "display_name": "Python 3", 143 | "language": "python", 144 | "name": "python3" 145 | }, 146 | "language_info": { 147 | "codemirror_mode": { 148 | "name": "ipython", 149 | "version": 3 150 | }, 151 | "file_extension": ".py", 152 | "mimetype": "text/x-python", 153 | "name": "python", 154 | "nbconvert_exporter": "python", 155 | "pygments_lexer": "ipython3", 156 | "version": "3.7.3" 157 | } 158 | }, 159 | "nbformat": 4, 160 | "nbformat_minor": 2 161 | } 162 | -------------------------------------------------------------------------------- /8 Two Dimensional Lists/8.07 [Practice] Row Wise Sum.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 4, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "3 3\n", 13 | "2 5 1 4 5 6 3 2 5\n", 14 | "8 15 10\n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "def rowWiseSum(arr):\n", 20 | " list = []\n", 21 | " for row in arr:\n", 22 | " sum = 0\n", 23 | " for ele in row:\n", 24 | " sum = sum + ele\n", 25 | " list.append(sum)\n", 26 | " return list\n", 27 | "\n", 28 | "#Main\n", 29 | "m, n=(int(i) for i in input().strip().split(' '))\n", 30 | "l=[int(i) for i in input().strip().split(' ')]\n", 31 | "arr = [ [ l[(j*n)+i] for i in range(n)] for j in range(m)]\n", 32 | "l=rowWiseSum(arr)\n", 33 | "print(*l)\n" 34 | ] 35 | }, 36 | 37 | ], 38 | "metadata": { 39 | "kernelspec": { 40 | "display_name": "Python 3", 41 | "language": "python", 42 | "name": "python3" 43 | }, 44 | "language_info": { 45 | "codemirror_mode": { 46 | "name": "ipython", 47 | "version": 3 48 | }, 49 | "file_extension": ".py", 50 | "mimetype": "text/x-python", 51 | "name": "python", 52 | "nbconvert_exporter": "python", 53 | "pygments_lexer": "ipython3", 54 | "version": "3.7.3" 55 | } 56 | }, 57 | "nbformat": 4, 58 | "nbformat_minor": 2 59 | } 60 | -------------------------------------------------------------------------------- /8 Two Dimensional Lists/8.08 Largest Column Sum in Two Dimensional List.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "#### len(li) - gives you number of rows\n", 8 | "#### len(li[0]) - gives you number of columns" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "### Here we're doing vertical iteration. It is possible in 2 ways" 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": 6, 21 | "metadata": {}, 22 | "outputs": [ 23 | { 24 | "name": "stdout", 25 | "output_type": "stream", 26 | "text": [ 27 | "21 3\n" 28 | ] 29 | } 30 | ], 31 | "source": [ 32 | "def lar_col_sum(li):\n", 33 | " n = len(li)\n", 34 | " m = len(li[0])\n", 35 | " max_sum = -1\n", 36 | " max_col_index = -1\n", 37 | " for j in range(m):\n", 38 | " sum = 0\n", 39 | " for i in range(n):\n", 40 | " sum += li[i][j]\n", 41 | " if sum > max_sum:\n", 42 | " max_sum = sum\n", 43 | " max_col_index = j\n", 44 | " return max_sum, max_col_index\n", 45 | "\n", 46 | "li = [[1,2,3,4], [8,7,6,5], [9,10,11,12]]\n", 47 | "lar_sum, lar_col_index = lar_col_sum(li)\n", 48 | "print(lar_sum, lar_col_index)" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 10, 54 | "metadata": {}, 55 | "outputs": [ 56 | { 57 | "name": "stdout", 58 | "output_type": "stream", 59 | "text": [ 60 | "24 1\n" 61 | ] 62 | } 63 | ], 64 | "source": [ 65 | "def lar_col_sum(li):\n", 66 | " n = len(li)\n", 67 | " m = len(li[0])\n", 68 | " max_sum = -1\n", 69 | " max_col_index = -1\n", 70 | " for j in range(m):\n", 71 | " sum = 0\n", 72 | " for ele in li:\n", 73 | " sum += ele[j]\n", 74 | " if sum > max_sum:\n", 75 | " max_sum = sum\n", 76 | " max_col_index = j\n", 77 | " return max_sum, max_col_index\n", 78 | "\n", 79 | "li = [[1,2,3,4], [8,7,6,5], [9,15,11,12]]\n", 80 | "lar_sum, lar_col_index = lar_col_sum(li)\n", 81 | "print(lar_sum, lar_col_index)" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 19, 87 | "metadata": {}, 88 | "outputs": [ 89 | { 90 | "name": "stdout", 91 | "output_type": "stream", 92 | "text": [ 93 | "1 5 9 \n", 94 | "2 6 10 \n", 95 | "3 7 11 \n", 96 | "4 8 12 \n" 97 | ] 98 | } 99 | ], 100 | "source": [ 101 | "#Predict the Output\n", 102 | "li=[[1,2,3,4],[5,6,7,8],[9,10,11,12]]\n", 103 | "for i in range(4):\n", 104 | " for ele in li:\n", 105 | " print(ele[i], end = \" \")\n", 106 | " print()" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 23, 112 | "metadata": {}, 113 | "outputs": [ 114 | { 115 | "name": "stdout", 116 | "output_type": "stream", 117 | "text": [ 118 | "1 2 3 4 \n", 119 | "5 6 7 8 \n", 120 | "9 10 11 12 \n" 121 | ] 122 | } 123 | ], 124 | "source": [ 125 | "li=[[1,2,3,4],[5,6,7,8],[9,10,11,12]]\n", 126 | "for i in range(3):\n", 127 | " for j in range(4):\n", 128 | " print(li[i][j], end = \" \")\n", 129 | " print()" 130 | ] 131 | }, 132 | ], 133 | "metadata": { 134 | "kernelspec": { 135 | "display_name": "Python 3", 136 | "language": "python", 137 | "name": "python3" 138 | }, 139 | "language_info": { 140 | "codemirror_mode": { 141 | "name": "ipython", 142 | "version": 3 143 | }, 144 | "file_extension": ".py", 145 | "mimetype": "text/x-python", 146 | "name": "python", 147 | "nbconvert_exporter": "python", 148 | "pygments_lexer": "ipython3", 149 | "version": "3.7.3" 150 | } 151 | }, 152 | "nbformat": 4, 153 | "nbformat_minor": 2 154 | } 155 | -------------------------------------------------------------------------------- /8 Two Dimensional Lists/8.09 [Practice] Largest Row or Column.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Largest Row or Column\n", 8 | "\n", 9 | "Given an NxM 2D array, you need to find out which row or column has largest sum (sum of its elements) overall amongst all rows and columns.\n", 10 | "Input Format :\n", 11 | "\n", 12 | " Line 1 : 2 integers N and M respectively, separated by space \n", 13 | " Line 2: Single line having N*M elements entered in row wise manner, each separated by space.\n", 14 | "\n", 15 | "Output Format :\n", 16 | "\n", 17 | " If row sum is maximum then - \"row\" row_num max_sum\n", 18 | " If column sum is maximum then - \"column\" col_num max_sum\n", 19 | "\n", 20 | "Note : If there are more than one rows/columns with maximum sum consider the row/column that comes first. And if ith row and jth column has same sum (which is largest), consider the ith row as answer.\n", 21 | "Sample Input 1 :\n", 22 | "\n", 23 | "2 2 \n", 24 | "1 1 1 1\n", 25 | "\n", 26 | "Sample Output 1 :\n", 27 | "\n", 28 | "row 0 2\n", 29 | "\n", 30 | "Sample Input 2 :\n", 31 | "\n", 32 | "3 3\n", 33 | "3 6 9 1 4 7 2 8 9\n", 34 | "\n", 35 | "Sample Output 2 :\n", 36 | "\n", 37 | "column 2 25" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 1, 43 | "metadata": {}, 44 | "outputs": [ 45 | { 46 | "name": "stdout", 47 | "output_type": "stream", 48 | "text": [ 49 | "3 3 \n", 50 | "3 6 9 1 4 7 2 8 9\n", 51 | "column 2 25\n" 52 | ] 53 | } 54 | ], 55 | "source": [ 56 | "def largestRowCol(arr):\n", 57 | " list = []\n", 58 | " n = len(arr)\n", 59 | " m = len(arr[0])\n", 60 | " max1 = -1\n", 61 | " index1 = -1\n", 62 | " max2 = -1\n", 63 | " index2 = -1\n", 64 | " for i in range(n):\n", 65 | " sum = 0\n", 66 | " for j in range(m):\n", 67 | " sum = sum + arr[i][j]\n", 68 | " if sum > max1:\n", 69 | " max1 = sum\n", 70 | " index1 = i \n", 71 | " \n", 72 | " for j in range(m):\n", 73 | " sum1 = 0 \n", 74 | " for i in range(n):\n", 75 | " sum1 = sum1 + arr[i][j]\n", 76 | " if sum1 > max2:\n", 77 | " max2 = sum1\n", 78 | " index2 = j\n", 79 | " \n", 80 | " if max1 == max2 and index1 < index2 or max1 > max2:\n", 81 | " list.append(\"row\")\n", 82 | " list.append(index1)\n", 83 | " list.append(max1) \n", 84 | " else: \n", 85 | " list.append(\"column\")\n", 86 | " list.append(index2)\n", 87 | " list.append(max2)\n", 88 | "\n", 89 | " return list\n", 90 | "\n", 91 | "#Main\n", 92 | "m, n=(int(i) for i in input().strip().split(' '))\n", 93 | "l=[int(i) for i in input().strip().split(' ')]\n", 94 | "arr = [ [ l[(j*n)+i] for i in range(n)] for j in range(m)]\n", 95 | "l=largestRowCol(arr)\n", 96 | "print(*l)\n" 97 | ] 98 | }, 99 | ], 100 | "metadata": { 101 | "kernelspec": { 102 | "display_name": "Python 3", 103 | "language": "python", 104 | "name": "python3" 105 | }, 106 | "language_info": { 107 | "codemirror_mode": { 108 | "name": "ipython", 109 | "version": 3 110 | }, 111 | "file_extension": ".py", 112 | "mimetype": "text/x-python", 113 | "name": "python", 114 | "nbconvert_exporter": "python", 115 | "pygments_lexer": "ipython3", 116 | "version": "3.7.3" 117 | } 118 | }, 119 | "nbformat": 4, 120 | "nbformat_minor": 2 121 | } 122 | -------------------------------------------------------------------------------- /8 Two Dimensional Lists/8.10 [Practice] Wave Print.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 5, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "2 3 1 2 3 4 5 6\n", 13 | "1 4 5 2 3 6 " 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "def wavePrint(arr):\n", 19 | " m = len(arr)\n", 20 | " n = len(arr[0])\n", 21 | " for j in range(n):\n", 22 | " if j % 2 == 0:\n", 23 | " for i in range(m):\n", 24 | " print(arr[i][j], end = \" \")\n", 25 | " else:\n", 26 | " for i in range(m-1, -1, -1):\n", 27 | " print(arr[i][j], end = \" \") \n", 28 | " \n", 29 | " pass\n", 30 | "\n", 31 | "#Main\n", 32 | "l=[int(i) for i in input().strip().split(' ')]\n", 33 | "m, n=l[0], l[1]\n", 34 | "arr = [ [ l[(j*n)+i+2] for i in range(n)] for j in range(m)]\n", 35 | "wavePrint(arr)\n" 36 | ] 37 | }, 38 | ], 39 | "metadata": { 40 | "kernelspec": { 41 | "display_name": "Python 3", 42 | "language": "python", 43 | "name": "python3" 44 | }, 45 | "language_info": { 46 | "codemirror_mode": { 47 | "name": "ipython", 48 | "version": 3 49 | }, 50 | "file_extension": ".py", 51 | "mimetype": "text/x-python", 52 | "name": "python", 53 | "nbconvert_exporter": "python", 54 | "pygments_lexer": "ipython3", 55 | "version": "3.7.3" 56 | } 57 | }, 58 | "nbformat": 4, 59 | "nbformat_minor": 2 60 | } 61 | -------------------------------------------------------------------------------- /8 Two Dimensional Lists/8.11 [Practice[] Spiral Print.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 6, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "4 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16\n", 13 | "1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 " 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "r=input().split()\n", 19 | "b=r[2:]\n", 20 | "l1=[[ int(b[int(r[1])*i+j]) for j in range(int(r[1])) ] for i in range(int(r[0]))]\n", 21 | "\n", 22 | "# for i in range(int(r[0])) - refers to i ---> rows (Number of rows)\n", 23 | "# for j in range(int(r[1])) - refers to j ---> cols (Number of cols)\n", 24 | "# \n", 25 | "\n", 26 | "l=[int(i) for i in input().strip().split(' ')]\n", 27 | "m, n=l[0], l[1]\n", 28 | "arr = [ [ l[(j*n)+i+2] for i in range(n)] for j in range(m)]\n", 29 | "spiralPrint(arr)\n", 30 | "\n", 31 | " \n", 32 | "rowstart=0\n", 33 | "colstart=0\n", 34 | "rowend=len(l1)-1\n", 35 | "colend=len(l1[0])-1\n", 36 | "x=1\n", 37 | " \n", 38 | " \n", 39 | "while(x<=(len(l1)*len(l1[0]))):\n", 40 | " for i in range(colstart,colend+1):\n", 41 | " print(l1[rowstart][i],end=\" \")\n", 42 | " x=x+1\n", 43 | " rowstart=rowstart+1\n", 44 | " \n", 45 | " for i in range(rowstart,rowend+1):\n", 46 | " \n", 47 | " print(l1[i][colend],end=\" \")\n", 48 | " x=x+1\n", 49 | " colend=colend-1\n", 50 | " \n", 51 | " \n", 52 | " for i in range(colend,colstart-1,-1):\n", 53 | " print(l1[rowend][i],end=\" \")\n", 54 | " x=x+1\n", 55 | " rowend=rowend-1\n", 56 | " \n", 57 | " \n", 58 | " for i in range(rowend,rowstart-1,-1):\n", 59 | " print(l1[i][colstart],end=\" \")\n", 60 | " x=x+1\n", 61 | " colstart=colstart+1" 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.3" 82 | } 83 | }, 84 | "nbformat": 4, 85 | "nbformat_minor": 2 86 | } 87 | -------------------------------------------------------------------------------- /9 Tuples, Dictionaries and Sets/9.03 Variable Length Input and Output.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 7, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "def sum(a,b, c, d, e):\n", 10 | " return a+b+c+d+e" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 8, 16 | "metadata": {}, 17 | "outputs": [ 18 | { 19 | "data": { 20 | "text/plain": [ 21 | "15" 22 | ] 23 | }, 24 | "execution_count": 8, 25 | "metadata": {}, 26 | "output_type": "execute_result" 27 | } 28 | ], 29 | "source": [ 30 | "sum(1,2,3,4,5)" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 14, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "def sum2(a,b, *more): #first two arguments go into function and more argument numbers are stored in a tuple\n", 40 | " print(a)\n", 41 | " print(b)\n", 42 | " print(type(more))\n", 43 | " ans = a + b\n", 44 | " for i in more:\n", 45 | " ans += i\n", 46 | " return ans" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 15, 52 | "metadata": {}, 53 | "outputs": [ 54 | { 55 | "name": "stdout", 56 | "output_type": "stream", 57 | "text": [ 58 | "2\n", 59 | "3\n", 60 | "\n" 61 | ] 62 | }, 63 | { 64 | "data": { 65 | "text/plain": [ 66 | "14" 67 | ] 68 | }, 69 | "execution_count": 15, 70 | "metadata": {}, 71 | "output_type": "execute_result" 72 | } 73 | ], 74 | "source": [ 75 | "sum2(2,3,4,5)" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 25, 81 | "metadata": {}, 82 | "outputs": [], 83 | "source": [ 84 | "def sum_diff(a,b): # If you're returning more than 1 value then the result is stored in the form of a tuple. Now you can access tuple using for loop or as a tuple\n", 85 | " return a+b, a-b, a*b" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 26, 91 | "metadata": {}, 92 | "outputs": [ 93 | { 94 | "data": { 95 | "text/plain": [ 96 | "(7, 3, 10)" 97 | ] 98 | }, 99 | "execution_count": 26, 100 | "metadata": {}, 101 | "output_type": "execute_result" 102 | } 103 | ], 104 | "source": [ 105 | "c = sum_diff(5,2)\n", 106 | "c" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 29, 112 | "metadata": {}, 113 | "outputs": [ 114 | { 115 | "name": "stdout", 116 | "output_type": "stream", 117 | "text": [ 118 | "(12, 2, 35)\n" 119 | ] 120 | } 121 | ], 122 | "source": [ 123 | "d = sum_diff(7,5)\n", 124 | "print(d)\n" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 30, 130 | "metadata": {}, 131 | "outputs": [ 132 | { 133 | "name": "stdout", 134 | "output_type": "stream", 135 | "text": [ 136 | "120\n" 137 | ] 138 | } 139 | ], 140 | "source": [ 141 | "#Predict the output:\n", 142 | "def multiply(a,b,c,*more):\n", 143 | " value = a*b*c\n", 144 | " for i in more:\n", 145 | " value = value * i\n", 146 | " return value\n", 147 | "V = multiply(1,2,3,4,5)\n", 148 | "print(V)" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": 31, 154 | "metadata": {}, 155 | "outputs": [ 156 | { 157 | "name": "stdout", 158 | "output_type": "stream", 159 | "text": [ 160 | "(9, 24)\n" 161 | ] 162 | } 163 | ], 164 | "source": [ 165 | "#Predict the output:\n", 166 | "def sum_multiply(a,b,*more):\n", 167 | " sum_value = a+b\n", 168 | " m_value = a*b\n", 169 | " for i in more:\n", 170 | " sum_value += i\n", 171 | " m_value*=i\n", 172 | " return sum_value,m_value\n", 173 | "s_m = sum_multiply(2,3,4)\n", 174 | "print(s_m)" 175 | ] 176 | }, 177 | ], 178 | "metadata": { 179 | "kernelspec": { 180 | "display_name": "Python 3", 181 | "language": "python", 182 | "name": "python3" 183 | }, 184 | "language_info": { 185 | "codemirror_mode": { 186 | "name": "ipython", 187 | "version": 3 188 | }, 189 | "file_extension": ".py", 190 | "mimetype": "text/x-python", 191 | "name": "python", 192 | "nbconvert_exporter": "python", 193 | "pygments_lexer": "ipython3", 194 | "version": "3.7.3" 195 | } 196 | }, 197 | "nbformat": 4, 198 | "nbformat_minor": 2 199 | } 200 | -------------------------------------------------------------------------------- /9 Tuples, Dictionaries and Sets/9.04 Dictionaries.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Dictionaries: You can use the string as a key in dictionaries unlike indexes as keys in lists\n", 8 | "#### Dictionaries are mutable and you can change keys and values" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "###### Use whatever key you want to use and store whatever value you want to store" 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": 1, 21 | "metadata": {}, 22 | "outputs": [ 23 | { 24 | "data": { 25 | "text/plain": [ 26 | "dict" 27 | ] 28 | }, 29 | "execution_count": 1, 30 | "metadata": {}, 31 | "output_type": "execute_result" 32 | } 33 | ], 34 | "source": [ 35 | "a = {}\n", 36 | "type(a)" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 4, 42 | "metadata": {}, 43 | "outputs": [ 44 | { 45 | "data": { 46 | "text/plain": [ 47 | "{'the': 1, 'a': 5, 1000: 'abc'}" 48 | ] 49 | }, 50 | "execution_count": 4, 51 | "metadata": {}, 52 | "output_type": "execute_result" 53 | } 54 | ], 55 | "source": [ 56 | "# In dictionaries you may have some keys and some values\n", 57 | "# You need to give keys and values unlike in lists the first value key is 0, second value key is 1 and so on and so forth\n", 58 | "# you can store any type of key and any type of value in dictionaries\n", 59 | "\n", 60 | "a = {\"the\":1, \"a\":5, 1000:\"abc\"}\n", 61 | "a" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 6, 67 | "metadata": {}, 68 | "outputs": [ 69 | { 70 | "data": { 71 | "text/plain": [ 72 | "3" 73 | ] 74 | }, 75 | "execution_count": 6, 76 | "metadata": {}, 77 | "output_type": "execute_result" 78 | } 79 | ], 80 | "source": [ 81 | "# If you need 10000 as a index in lists then you must have atleast 10001 indeces\n", 82 | "len(a)" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 7, 88 | "metadata": {}, 89 | "outputs": [ 90 | { 91 | "data": { 92 | "text/plain": [ 93 | "1" 94 | ] 95 | }, 96 | "execution_count": 7, 97 | "metadata": {}, 98 | "output_type": "execute_result" 99 | } 100 | ], 101 | "source": [ 102 | "a[\"the\"]" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 8, 108 | "metadata": {}, 109 | "outputs": [ 110 | { 111 | "data": { 112 | "text/plain": [ 113 | "5" 114 | ] 115 | }, 116 | "execution_count": 8, 117 | "metadata": {}, 118 | "output_type": "execute_result" 119 | } 120 | ], 121 | "source": [ 122 | "a[\"a\"]" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 10, 128 | "metadata": {}, 129 | "outputs": [], 130 | "source": [ 131 | "# Create a dictionary by copying\n", 132 | "\n", 133 | "b = a.copy()" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 11, 139 | "metadata": {}, 140 | "outputs": [ 141 | { 142 | "data": { 143 | "text/plain": [ 144 | "{'the': 1, 'a': 5, 1000: 'abc'}" 145 | ] 146 | }, 147 | "execution_count": 11, 148 | "metadata": {}, 149 | "output_type": "execute_result" 150 | } 151 | ], 152 | "source": [ 153 | "b" 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": 12, 159 | "metadata": {}, 160 | "outputs": [], 161 | "source": [ 162 | "c = dict([(\"the\", 3), (\"a\", 10), (2,3)])" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": 13, 168 | "metadata": {}, 169 | "outputs": [ 170 | { 171 | "data": { 172 | "text/plain": [ 173 | "{'the': 3, 'a': 10, 2: 3}" 174 | ] 175 | }, 176 | "execution_count": 13, 177 | "metadata": {}, 178 | "output_type": "execute_result" 179 | } 180 | ], 181 | "source": [ 182 | "c" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": 16, 188 | "metadata": {}, 189 | "outputs": [], 190 | "source": [ 191 | "d = dict.fromkeys([\"abc\", 32, 4]) # Only passed keys" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": 17, 197 | "metadata": {}, 198 | "outputs": [ 199 | { 200 | "data": { 201 | "text/plain": [ 202 | "{'abc': None, 32: None, 4: None}" 203 | ] 204 | }, 205 | "execution_count": 17, 206 | "metadata": {}, 207 | "output_type": "execute_result" 208 | } 209 | ], 210 | "source": [ 211 | "d" 212 | ] 213 | }, 214 | { 215 | "cell_type": "code", 216 | "execution_count": 18, 217 | "metadata": {}, 218 | "outputs": [], 219 | "source": [ 220 | "d = dict.fromkeys([\"abc\", 32, 4], 10) # Only passed keys and default value for all keys" 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": 19, 226 | "metadata": {}, 227 | "outputs": [ 228 | { 229 | "data": { 230 | "text/plain": [ 231 | "{'abc': 10, 32: 10, 4: 10}" 232 | ] 233 | }, 234 | "execution_count": 19, 235 | "metadata": {}, 236 | "output_type": "execute_result" 237 | } 238 | ], 239 | "source": [ 240 | "d" 241 | ] 242 | } 243 | ], 244 | "metadata": { 245 | "kernelspec": { 246 | "display_name": "Python 3", 247 | "language": "python", 248 | "name": "python3" 249 | }, 250 | "language_info": { 251 | "codemirror_mode": { 252 | "name": "ipython", 253 | "version": 3 254 | }, 255 | "file_extension": ".py", 256 | "mimetype": "text/x-python", 257 | "name": "python", 258 | "nbconvert_exporter": "python", 259 | "pygments_lexer": "ipython3", 260 | "version": "3.7.3" 261 | } 262 | }, 263 | "nbformat": 4, 264 | "nbformat_minor": 2 265 | } 266 | -------------------------------------------------------------------------------- /9 Tuples, Dictionaries and Sets/9.06 Accessing or deleting data in dictionaries.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 20, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "a = {1:2, 3:4, \"list\":[1,23], \"dict\":{32,43}}" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "a['tuple'] = (1,2,3)" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 3, 24 | "metadata": {}, 25 | "outputs": [ 26 | { 27 | "data": { 28 | "text/plain": [ 29 | "{1: 2, 3: 4, 'list': [1, 23], 'dict': {32, 43}, 'tuple': (1, 2, 3)}" 30 | ] 31 | }, 32 | "execution_count": 3, 33 | "metadata": {}, 34 | "output_type": "execute_result" 35 | } 36 | ], 37 | "source": [ 38 | "a" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 5, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "a[1] = 10" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 6, 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "data": { 57 | "text/plain": [ 58 | "{1: 10, 3: 4, 'list': [1, 23], 'dict': {32, 43}, 'tuple': (1, 2, 3)}" 59 | ] 60 | }, 61 | "execution_count": 6, 62 | "metadata": {}, 63 | "output_type": "execute_result" 64 | } 65 | ], 66 | "source": [ 67 | "a" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 7, 73 | "metadata": {}, 74 | "outputs": [], 75 | "source": [ 76 | "b = {3:5, 'the':4, 2:100}" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 8, 82 | "metadata": {}, 83 | "outputs": [], 84 | "source": [ 85 | "a.update(b)" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 9, 91 | "metadata": {}, 92 | "outputs": [ 93 | { 94 | "data": { 95 | "text/plain": [ 96 | "{1: 10,\n", 97 | " 3: 5,\n", 98 | " 'list': [1, 23],\n", 99 | " 'dict': {32, 43},\n", 100 | " 'tuple': (1, 2, 3),\n", 101 | " 'the': 4,\n", 102 | " 2: 100}" 103 | ] 104 | }, 105 | "execution_count": 9, 106 | "metadata": {}, 107 | "output_type": "execute_result" 108 | } 109 | ], 110 | "source": [ 111 | "a" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": 10, 117 | "metadata": {}, 118 | "outputs": [ 119 | { 120 | "ename": "KeyError", 121 | "evalue": "'t'", 122 | "output_type": "error", 123 | "traceback": [ 124 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 125 | "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", 126 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m't'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 127 | "\u001b[1;31mKeyError\u001b[0m: 't'" 128 | ] 129 | } 130 | ], 131 | "source": [ 132 | "a.pop('t')" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 11, 138 | "metadata": {}, 139 | "outputs": [ 140 | { 141 | "data": { 142 | "text/plain": [ 143 | "(1, 2, 3)" 144 | ] 145 | }, 146 | "execution_count": 11, 147 | "metadata": {}, 148 | "output_type": "execute_result" 149 | } 150 | ], 151 | "source": [ 152 | "a.pop('tuple')" 153 | ] 154 | }, 155 | { 156 | "cell_type": "code", 157 | "execution_count": 12, 158 | "metadata": {}, 159 | "outputs": [ 160 | { 161 | "data": { 162 | "text/plain": [ 163 | "{1: 10, 3: 5, 'list': [1, 23], 'dict': {32, 43}, 'the': 4, 2: 100}" 164 | ] 165 | }, 166 | "execution_count": 12, 167 | "metadata": {}, 168 | "output_type": "execute_result" 169 | } 170 | ], 171 | "source": [ 172 | "a" 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": 13, 178 | "metadata": {}, 179 | "outputs": [], 180 | "source": [ 181 | "del a[1]" 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": 14, 187 | "metadata": {}, 188 | "outputs": [ 189 | { 190 | "data": { 191 | "text/plain": [ 192 | "{3: 5, 'list': [1, 23], 'dict': {32, 43}, 'the': 4, 2: 100}" 193 | ] 194 | }, 195 | "execution_count": 14, 196 | "metadata": {}, 197 | "output_type": "execute_result" 198 | } 199 | ], 200 | "source": [ 201 | "a" 202 | ] 203 | }, 204 | { 205 | "cell_type": "code", 206 | "execution_count": 15, 207 | "metadata": {}, 208 | "outputs": [], 209 | "source": [ 210 | "a.clear()" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": 16, 216 | "metadata": {}, 217 | "outputs": [ 218 | { 219 | "data": { 220 | "text/plain": [ 221 | "{}" 222 | ] 223 | }, 224 | "execution_count": 16, 225 | "metadata": {}, 226 | "output_type": "execute_result" 227 | } 228 | ], 229 | "source": [ 230 | "a" 231 | ] 232 | }, 233 | { 234 | "cell_type": "code", 235 | "execution_count": 21, 236 | "metadata": {}, 237 | "outputs": [], 238 | "source": [ 239 | "del a # Delete the whole dictionary" 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "execution_count": 18, 245 | "metadata": {}, 246 | "outputs": [ 247 | { 248 | "ename": "NameError", 249 | "evalue": "name 'a' is not defined", 250 | "output_type": "error", 251 | "traceback": [ 252 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 253 | "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", 254 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 255 | "\u001b[1;31mNameError\u001b[0m: name 'a' is not defined" 256 | ] 257 | } 258 | ], 259 | "source": [ 260 | "a" 261 | ] 262 | }, 263 | { 264 | "cell_type": "code", 265 | "execution_count": null, 266 | "metadata": {}, 267 | "outputs": [], 268 | "source": [] 269 | } 270 | ], 271 | "metadata": { 272 | "kernelspec": { 273 | "display_name": "Python 3", 274 | "language": "python", 275 | "name": "python3" 276 | }, 277 | "language_info": { 278 | "codemirror_mode": { 279 | "name": "ipython", 280 | "version": 3 281 | }, 282 | "file_extension": ".py", 283 | "mimetype": "text/x-python", 284 | "name": "python", 285 | "nbconvert_exporter": "python", 286 | "pygments_lexer": "ipython3", 287 | "version": "3.7.3" 288 | } 289 | }, 290 | "nbformat": 4, 291 | "nbformat_minor": 2 292 | } 293 | -------------------------------------------------------------------------------- /9 Tuples, Dictionaries and Sets/9.07 Print all words with freq k.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "s = \"this is a word string having many many word\"\n", 10 | "k = 2" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 2, 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "words = s.split()" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 3, 25 | "metadata": {}, 26 | "outputs": [ 27 | { 28 | "data": { 29 | "text/plain": [ 30 | "['this', 'is', 'a', 'word', 'string', 'having', 'many', 'many', 'word']" 31 | ] 32 | }, 33 | "execution_count": 3, 34 | "metadata": {}, 35 | "output_type": "execute_result" 36 | } 37 | ], 38 | "source": [ 39 | "words" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 6, 45 | "metadata": {}, 46 | "outputs": [], 47 | "source": [ 48 | "dict = {}\n", 49 | "for w in words:\n", 50 | " if w in dict:\n", 51 | " dict[w] = dict[w] + 1\n", 52 | " else:\n", 53 | " dict[w] = 1" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 7, 59 | "metadata": {}, 60 | "outputs": [ 61 | { 62 | "data": { 63 | "text/plain": [ 64 | "{'this': 1, 'is': 1, 'a': 1, 'word': 2, 'string': 1, 'having': 1, 'many': 2}" 65 | ] 66 | }, 67 | "execution_count": 7, 68 | "metadata": {}, 69 | "output_type": "execute_result" 70 | } 71 | ], 72 | "source": [ 73 | "dict" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 9, 79 | "metadata": {}, 80 | "outputs": [ 81 | { 82 | "data": { 83 | "text/plain": [ 84 | "{'this': 1, 'is': 1, 'a': 1, 'word': 2, 'string': 1, 'having': 1, 'many': 2}" 85 | ] 86 | }, 87 | "execution_count": 9, 88 | "metadata": {}, 89 | "output_type": "execute_result" 90 | } 91 | ], 92 | "source": [ 93 | "dict = {}\n", 94 | "for w in words:\n", 95 | " dict[w] = dict.get(w, 0) + 1\n", 96 | "dict" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 11, 102 | "metadata": {}, 103 | "outputs": [ 104 | { 105 | "name": "stdout", 106 | "output_type": "stream", 107 | "text": [ 108 | "word\n", 109 | "many\n" 110 | ] 111 | } 112 | ], 113 | "source": [ 114 | "for w in dict:\n", 115 | " if dict[w] == k:\n", 116 | " print(w)" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 12, 122 | "metadata": {}, 123 | "outputs": [], 124 | "source": [ 125 | "def printKFreqWords(s, k):\n", 126 | " words = s.split()\n", 127 | " dict = {}\n", 128 | " for w in words:\n", 129 | " dict[w] = dict.get(w,0) + 1\n", 130 | " for w in dict:\n", 131 | " if dict[w] == k:\n", 132 | " print(w)" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 15, 138 | "metadata": {}, 139 | "outputs": [ 140 | { 141 | "name": "stdout", 142 | "output_type": "stream", 143 | "text": [ 144 | "this\n", 145 | "is\n", 146 | "a\n", 147 | "string\n", 148 | "having\n" 149 | ] 150 | } 151 | ], 152 | "source": [ 153 | "printKFreqWords(s,1)" 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": 3, 159 | "metadata": {}, 160 | "outputs": [ 161 | { 162 | "name": "stdout", 163 | "output_type": "stream", 164 | "text": [ 165 | "\n", 166 | "['My', 'name', 'is', 'Fazeel', 'Usmani']\n" 167 | ] 168 | } 169 | ], 170 | "source": [ 171 | "def printKFreqWords(s,k):\n", 172 | " words = s.split()\n", 173 | " #print(words)\n", 174 | " dict = {}\n", 175 | " for w in words:\n", 176 | " dict[w] = dict.get(w, 0) + 1\n", 177 | " for w in dict:\n", 178 | " if dict[w] == k:\n", 179 | " print(w)\n", 180 | "print(dict)\n", 181 | "s = \" My name is Fazeel Usmani\"\n", 182 | "printKFreqWords(s, 2)" 183 | ] 184 | }, 185 | ], 186 | "metadata": { 187 | "kernelspec": { 188 | "display_name": "Python 3", 189 | "language": "python", 190 | "name": "python3" 191 | }, 192 | "language_info": { 193 | "codemirror_mode": { 194 | "name": "ipython", 195 | "version": 3 196 | }, 197 | "file_extension": ".py", 198 | "mimetype": "text/x-python", 199 | "name": "python", 200 | "nbconvert_exporter": "python", 201 | "pygments_lexer": "ipython3", 202 | "version": "3.7.3" 203 | } 204 | }, 205 | "nbformat": 4, 206 | "nbformat_minor": 2 207 | } 208 | -------------------------------------------------------------------------------- /9 Tuples, Dictionaries and Sets/9.10 Sum of all unique numbers in list.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "data": { 10 | "text/plain": [ 11 | "set()" 12 | ] 13 | }, 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "output_type": "execute_result" 17 | } 18 | ], 19 | "source": [ 20 | "s =set()\n", 21 | "s" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 3, 27 | "metadata": {}, 28 | "outputs": [ 29 | { 30 | "data": { 31 | "text/plain": [ 32 | "set" 33 | ] 34 | }, 35 | "execution_count": 3, 36 | "metadata": {}, 37 | "output_type": "execute_result" 38 | } 39 | ], 40 | "source": [ 41 | "type(s)" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 4, 47 | "metadata": {}, 48 | "outputs": [ 49 | { 50 | "name": "stdout", 51 | "output_type": "stream", 52 | "text": [ 53 | "5\n", 54 | "15\n" 55 | ] 56 | } 57 | ], 58 | "source": [ 59 | "s = {1,2,1,3,3,2,4,4,4,5}\n", 60 | "sum = 0\n", 61 | "for i in s:\n", 62 | " sum += i\n", 63 | "print(len(s))\n", 64 | "print(sum) " 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 5, 70 | "metadata": {}, 71 | "outputs": [ 72 | { 73 | "data": { 74 | "text/plain": [ 75 | "dict" 76 | ] 77 | }, 78 | "execution_count": 5, 79 | "metadata": {}, 80 | "output_type": "execute_result" 81 | } 82 | ], 83 | "source": [ 84 | "dic = {}\n", 85 | "type(dic)" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 6, 91 | "metadata": {}, 92 | "outputs": [], 93 | "source": [ 94 | "def sumUnique(l):\n", 95 | " s = set()\n", 96 | " for i in l:\n", 97 | " s.add(i)\n", 98 | " sum = 0\n", 99 | " for i in s:\n", 100 | " sum = sum + i\n", 101 | " return sum" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 7, 107 | "metadata": {}, 108 | "outputs": [ 109 | { 110 | "data": { 111 | "text/plain": [ 112 | "15" 113 | ] 114 | }, 115 | "execution_count": 7, 116 | "metadata": {}, 117 | "output_type": "execute_result" 118 | } 119 | ], 120 | "source": [ 121 | "sumUnique([1,2,3,2,1,3,4,2,3,1,5,4,5,4,3,5,5,5])" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": null, 127 | "metadata": {}, 128 | "outputs": [], 129 | "source": [] 130 | } 131 | ], 132 | "metadata": { 133 | "kernelspec": { 134 | "display_name": "Python 3", 135 | "language": "python", 136 | "name": "python3" 137 | }, 138 | "language_info": { 139 | "codemirror_mode": { 140 | "name": "ipython", 141 | "version": 3 142 | }, 143 | "file_extension": ".py", 144 | "mimetype": "text/x-python", 145 | "name": "python", 146 | "nbconvert_exporter": "python", 147 | "pygments_lexer": "ipython3", 148 | "version": "3.7.3" 149 | } 150 | }, 151 | "nbformat": 4, 152 | "nbformat_minor": 2 153 | } 154 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Introduction to Python 2 | This repository includes all the practice problems and assignments which I've solved during the Intro Course of Python Programming taught by Coding Ninjas. 3 | If you're unable to view the patterns properly in Jupyter notebook then double-click the markdown cell to enlarge. 4 | File names which contain practice means the assignments of that particular topic. For an example, Patterns 1 Practice.ipynb means assignments file of first part of Patterns topic. 5 | 6 | Topics discussed are:- 7 | 1) [Conditions and Loops](https://github.com/FazeelUsmani/Coding-Ninjas-Intro-to-Python-/tree/master/1%20Conditions%20and%20Loops) 8 | 2) [Patterns](https://github.com/FazeelUsmani/Coding-Ninjas-Intro-to-Python-/tree/master/2%20Patterns%201) (This includes both basic and advanced patterns) 9 | 3) [More on Loops](https://github.com/FazeelUsmani/Coding-Ninjas-Intro-to-Python-/tree/master/3%20More%20on%20Loops) 10 | 4) [Functions](https://github.com/FazeelUsmani/Coding-Ninjas-Intro-to-Python-/tree/master/4%20Functions) 11 | 5) [Arrays/Lists](https://github.com/FazeelUsmani/Coding-Ninjas-Intro-to-Python-/tree/master/5%20Arrays) 12 | 6) [Searching & Sorting](https://github.com/FazeelUsmani/Coding-Ninjas-Intro-to-Python-/tree/master/6%20Searching%20and%20Sorting) 13 | 7) [Strings](https://github.com/FazeelUsmani/Coding-Ninjas-Intro-to-Python-/tree/master/7%20Strings) 14 | 8) [Two Dimensional Lists](https://github.com/FazeelUsmani/Coding-Ninjas-Intro-to-Python-/tree/master/8%20Two%20Dimensional%20Lists) 15 | 9) [Tuples, Dictionary and Sets](https://github.com/FazeelUsmani/Coding-Ninjas-Intro-to-Python-/tree/master/9%20Tuples%2C%20Dictionaries%20and%20Sets) 16 | 17 | __Download Jupyter notebook from here:__ https://jupyter.org/ 18 | __Installing Jupyter notebook:__ https://jupyter.org/install 19 | If you're not comfortable with Jupyter notebooks then you can use __Repl.it (Online Editor):__ https://repl.it/languages/python3 20 | 21 | 22 | **P.S**: Refer serial wise to avoid confusion and non-understanding of topics 23 | **Tip**: Change the video speed to 1.25x to save your **time** 24 | 25 | 26 | ## "Time is a huge huge money" 27 | 28 | 29 | After this course completion, I recommend to have a look on these __MIT__ lectures to get in depth knowledge of data structures and algorithms: https://www.youtube.com/playlist?list=PLUl4u3cNGP61Oq3tWYp6V_F-5jb5L2iHb 30 | 31 | 32 | Register the course with the following referral link to get a discount of ₹1,000/- 33 | Referral Link: https://codingninjas.in/app/invite/THMPN 34 | **P.S: Don't forget to sign up with a new ID to avail discount** 35 | -------------------------------------------------------------------------------- /image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FazeelUsmani/Intro-to-Python/70fdeeb4d0173a6cb5a0cf13e260950c4e7bc2b1/image.png --------------------------------------------------------------------------------