├── 1 Flow-Chart ├── 10 Strings ├── Check Palindrome.ipynb ├── Check Permutation.ipynb ├── Compress the String.ipynb ├── Highest Occurring Character.ipynb ├── Remove Character.ipynb ├── Remove Consecutive Duplicates.ipynb └── Reverse Each Word.ipynb ├── 11 Two Dimensional Lists ├── Largest Row or Column.ipynb ├── Row Wise Sum.ipynb ├── Sprial Print.ipynb ├── Wave Print.cpp └── Wave Print.ipynb ├── 12 Tupples, Dictionary And Sets ├── Accessing or deleting data in dictionaries.ipynb ├── Dictionaries.ipynb ├── Dictionary Functions.ipynb ├── Print all words with freq k.ipynb ├── Set Functions.ipynb ├── Sets - Intro.ipynb ├── Sum of all unique numbers in list.ipynb ├── Tuples Functions.ipynb ├── Tuples.ipynb └── Variable Length Input and Output.ipynb ├── 2 Introduction to Python ├── Find_Average_Marks.ipynb ├── Find_X_to_The_Power_of_N.ipynb └── Rectangular_Area.ipynb ├── 3 Conditionals and Loops ├── Calculator.cpp ├── Calculator.ipynb ├── Check number.ipynb ├── Fahrenheit to Celsius.ipynb ├── Nth Fibonacci Number.ipynb ├── Palindrome Number.ipynb ├── Reverse of a Number.ipynb ├── Sum of Even & Odd.ipynb ├── Sum of Even Numbers.ipynb └── Sum of n Number.ipynb ├── 4 Patterns 1 ├── Alpha Pattern.ipynb ├── Character Pattern.ipynb ├── Interesting Alphabets.ipynb ├── Number Pattern 1.ipynb ├── Number Pattern 2.ipynb ├── Number Pattern 3.ipynb ├── Number Pattern.ipynb ├── Reverse Number Pattern.ipynb ├── Square Pattern.ipynb ├── Triangle Number Pattern.ipynb └── Triangular Star Pattern.ipynb ├── 5 Patterns 2 ├── Arrow Pattern.ipynb ├── Diamond of Star.ipynb ├── Inverted Number Pattern.ipynb ├── Mirror Number Pattern.ipynb ├── Number Pattern.ipynb ├── Pyramid Number Pattern.ipynb ├── Star Pattern.ipynb ├── Triangle of Number.ipynb └── Zeros and Star Pattern.ipynb ├── 6 More on Loops ├── Binary Pattern.ipynb ├── Diamond of Stars.ipynb ├── Print Number Pyramid.ipynb ├── Print the Pattern.ipynb └── Rectangular Number.ipynb ├── 7 Functions ├── Check Amstrong.ipynb ├── Check Armstrong function.ipynb ├── Fahrenheit to Celsius Function.ipynb ├── Fibonacci Member alt.ipynb ├── Fibonacci Member.ipynb └── Palindrome Number.ipynb ├── 8 Arrays and Lists ├── Array Sum.ipynb ├── Array intersection.py ├── Find Duplicate.cpp ├── Find Duplicate.ipynb ├── Find Unique.cpp ├── Find Unique.ipynb ├── Pair Sum.ipynb ├── Sort 0 1.ipynb └── Triplet Sum.ipynb ├── 9 Searching & Sorting ├── Binary Search.ipynb ├── Bubble Sort.ipynb ├── Check Array Rotation.ipynb ├── Insertion Sort.ipynb ├── Merge Sort.ipynb ├── Push Zeros to End.ipynb ├── Rotate Array.ipynb ├── Second Largest in Array.ipynb ├── Sort 0 1 2.ipynb └── Sum of Two Arrays.ipynb ├── README.md └── _config.yml /1 Flow-Chart: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /10 Strings/Check Palindrome.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "s=input()\n", 10 | "p=s[::-1]\n", 11 | "if(s==p):\n", 12 | " print(\"true\")\n", 13 | "else:\n", 14 | " print(\"false\")" 15 | ] 16 | } 17 | ], 18 | "metadata": { 19 | "kernelspec": { 20 | "display_name": "Python 3", 21 | "language": "python", 22 | "name": "python3" 23 | }, 24 | "language_info": { 25 | "codemirror_mode": { 26 | "name": "ipython", 27 | "version": 3 28 | }, 29 | "file_extension": ".py", 30 | "mimetype": "text/x-python", 31 | "name": "python", 32 | "nbconvert_exporter": "python", 33 | "pygments_lexer": "ipython3", 34 | "version": "3.7.6" 35 | } 36 | }, 37 | "nbformat": 4, 38 | "nbformat_minor": 4 39 | } 40 | -------------------------------------------------------------------------------- /10 Strings/Check Permutation.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "Check Permutation\n", 10 | "--------------------\n", 11 | "Given two strings, S and T, check if they are permutations of each other. Return true or false.\n", 12 | "Permutation means - length of both the strings should same and should contain same set of characters. Order of characters doesn't matter.\n", 13 | "Note : Input strings contain only lowercase english alphabets.\n", 14 | "\n", 15 | "\n", 16 | "Input format :\n", 17 | "Line 1 : String 1\n", 18 | "Line 2 : String 2\n", 19 | "Output format :\n", 20 | "'true' or 'false'\n", 21 | "Constraints :\n", 22 | "0 <= |S| <= 10^7\n", 23 | "0 <= |T| <= 10^7\n", 24 | "where |S| represents the length of string, S.\n", 25 | "Sample Input 1 :\n", 26 | "abcde\n", 27 | "baedc\n", 28 | "Sample Output 1 :\n", 29 | "true" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 1, 35 | "metadata": {}, 36 | "outputs": [ 37 | { 38 | "name": "stdout", 39 | "output_type": "stream", 40 | "text": [ 41 | "abcde\n", 42 | "baedc\n", 43 | "true\n" 44 | ] 45 | } 46 | ], 47 | "source": [ 48 | "def arePermutation(str1, str2):\n", 49 | " \n", 50 | " n1 = len(str1) \n", 51 | " n2 = len(str2)\n", 52 | " if (n1 != n2):\n", 53 | " return False\n", 54 | " \n", 55 | " # Sort both strings \n", 56 | " a = sorted(str1) \n", 57 | " str1 = \"\".join(a) \n", 58 | " b = sorted(str2) \n", 59 | " str2 = \"\".join(b) \n", 60 | " \n", 61 | " # Compare sorted strings \n", 62 | " for i in range(0, n1, 1): \n", 63 | " if (str1[i] != str2[i]): \n", 64 | " return False\n", 65 | " \n", 66 | " return True\n", 67 | " \n", 68 | "str1 = input()\n", 69 | "str2 = input()\n", 70 | "if (arePermutation(str1, str2)):\n", 71 | " print(\"true\") \n", 72 | "else: \n", 73 | " print(\"false\") " 74 | ] 75 | } 76 | ], 77 | "metadata": { 78 | "kernelspec": { 79 | "display_name": "Python 3", 80 | "language": "python", 81 | "name": "python3" 82 | }, 83 | "language_info": { 84 | "codemirror_mode": { 85 | "name": "ipython", 86 | "version": 3 87 | }, 88 | "file_extension": ".py", 89 | "mimetype": "text/x-python", 90 | "name": "python", 91 | "nbconvert_exporter": "python", 92 | "pygments_lexer": "ipython3", 93 | "version": "3.7.6" 94 | } 95 | }, 96 | "nbformat": 4, 97 | "nbformat_minor": 4 98 | } 99 | -------------------------------------------------------------------------------- /10 Strings/Compress the String.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 | "aaabbccdsa\n", 13 | "a3b2c2dsa\n" 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "def ab(a):\n", 19 | " i=0\n", 20 | " x=''\n", 21 | " while(i l[2]:\n", 57 | " l[2] = sumRow[i] \n", 58 | " l[1] = i\n", 59 | " for j in range(cols):\n", 60 | " if sumCol[j] > l[2]:\n", 61 | " l[2] = sumCol[j] \n", 62 | " l[1] = j \n", 63 | " l[0] = 'column' \n", 64 | " return l\n", 65 | "\n", 66 | "m, n=(int(i) for i in input().strip().split(' ')) \n", 67 | "l=[int(i) for i in input().strip().split(' ')]\n", 68 | "arr = [ [ l[(j*n)+i] for i in range(n)] for j in range(m)] \n", 69 | "l=largestRowCol(arr) \n", 70 | "print(*l)" 71 | ] 72 | } 73 | ], 74 | "metadata": { 75 | "kernelspec": { 76 | "display_name": "Python 3", 77 | "language": "python", 78 | "name": "python3" 79 | }, 80 | "language_info": { 81 | "codemirror_mode": { 82 | "name": "ipython", 83 | "version": 3 84 | }, 85 | "file_extension": ".py", 86 | "mimetype": "text/x-python", 87 | "name": "python", 88 | "nbconvert_exporter": "python", 89 | "pygments_lexer": "ipython3", 90 | "version": "3.7.6" 91 | } 92 | }, 93 | "nbformat": 4, 94 | "nbformat_minor": 4 95 | } 96 | -------------------------------------------------------------------------------- /11 Two Dimensional Lists/Row Wise Sum.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 2\n", 13 | "1 2 3 4 5 6 7 8\n", 14 | "3 7 11 15\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 | "m,n=(int(i) for i in input().strip().split())\n", 29 | "l=[int(i) for i in input().strip().split()]\n", 30 | "arr = [ [ l[(j*n)+i] for i in range(n)] for j in range(m)]\n", 31 | "l=rowWiseSum(arr)\n", 32 | "print(*l)\n", 33 | " " 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.6" 54 | } 55 | }, 56 | "nbformat": 4, 57 | "nbformat_minor": 4 58 | } 59 | -------------------------------------------------------------------------------- /11 Two Dimensional Lists/Sprial Print.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "Spiral Print\n", 10 | "------------------------\n", 11 | "Given an N*M 2D array, print it in spiral form. That is, first you need to print the 1st row, then last column, then last row and then first column and so on.\n", 12 | "Print every element only once.\n", 13 | "Input format :\n", 14 | "Line 1 : N and M, No. of rows & No. of columns (separated by space) followed by N*M elements in row wise fashion.\n", 15 | "Output format :\n", 16 | "Elements of matrix in spiral form in a single line and space separated\n", 17 | "Constraints :\n", 18 | "0 <= N <= 10^4\n", 19 | "0 <= M <= 10^4\n", 20 | "Sample Input 1:\n", 21 | " 4 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16\n", 22 | "Sample Output 1:\n", 23 | "1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 " 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 3, 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "name": "stdout", 33 | "output_type": "stream", 34 | "text": [ 35 | " 4 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16\n", 36 | "1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 " 37 | ] 38 | } 39 | ], 40 | "source": [ 41 | "def spiralPrint(arr): \n", 42 | " rows = len(arr) \n", 43 | " cols = len(arr[0]) \n", 44 | " startRow, startCol, endRow, endCol = 0, 0, rows-1, cols-1\n", 45 | " while startRow<=endRow and startCol<=endCol:\n", 46 | " # Print startRow \n", 47 | " for j in range(startCol, endCol+1): \n", 48 | " print(arr[startRow][j], end=' ')\n", 49 | " startRow += 1\n", 50 | " if startRow>endRow or startCol>endCol: \n", 51 | " break \n", 52 | " # Print endCol \n", 53 | " for i in range(startRow, endRow+1): \n", 54 | " print(arr[i][endCol], end=' ') \n", 55 | " endCol -= 1 \n", 56 | " if startRow>endRow or startCol>endCol: \n", 57 | " break \n", 58 | " for j in range(endCol, startCol-1, -1):\n", 59 | " print(arr[endRow][j], end=' ')\n", 60 | " endRow -= 1 \n", 61 | " if startRow>endRow or startCol>endCol:\n", 62 | " break \n", 63 | " # Print startCol \n", 64 | " for i in range(endRow, startRow-1, -1): \n", 65 | " print(arr[i][startCol], end=' ') \n", 66 | " startCol += 1 \n", 67 | "l=[int(i) for i in input().strip().split(' ')] \n", 68 | "m, n=l[0], l[1]\n", 69 | "arr = [ [ l[(j*n)+i+2] for i in range(n)] for j in range(m)] \n", 70 | "spiralPrint(arr)" 71 | ] 72 | } 73 | ], 74 | "metadata": { 75 | "kernelspec": { 76 | "display_name": "Python 3", 77 | "language": "python", 78 | "name": "python3" 79 | }, 80 | "language_info": { 81 | "codemirror_mode": { 82 | "name": "ipython", 83 | "version": 3 84 | }, 85 | "file_extension": ".py", 86 | "mimetype": "text/x-python", 87 | "name": "python", 88 | "nbconvert_exporter": "python", 89 | "pygments_lexer": "ipython3", 90 | "version": "3.7.6" 91 | } 92 | }, 93 | "nbformat": 4, 94 | "nbformat_minor": 4 95 | } 96 | -------------------------------------------------------------------------------- /11 Two Dimensional Lists/Wave Print.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void wavePrint(int **input, int row, int col) 4 | { 5 | if (row == 0) 6 | { 7 | return; 8 | } 9 | int numRows = row; 10 | int numCols = col; 11 | for (int j = 0; j < numCols; j++) 12 | { 13 | if (j % 2 == 0) 14 | { 15 | for (int i = 0; i < numRows; i++) 16 | { 17 | cout << input[i][j] << " "; 18 | } 19 | 20 | } 21 | else 22 | { 23 | for (int i = row - 1; i >= 0; i--) 24 | { 25 | cout << input[i][j] << " "; 26 | } 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /11 Two Dimensional Lists/Wave Print.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [] 9 | } 10 | ], 11 | "metadata": { 12 | "kernelspec": { 13 | "display_name": "Python 3", 14 | "language": "python", 15 | "name": "python3" 16 | }, 17 | "language_info": { 18 | "codemirror_mode": { 19 | "name": "ipython", 20 | "version": 3 21 | }, 22 | "file_extension": ".py", 23 | "mimetype": "text/x-python", 24 | "name": "python", 25 | "nbconvert_exporter": "python", 26 | "pygments_lexer": "ipython3", 27 | "version": "3.7.6" 28 | } 29 | }, 30 | "nbformat": 4, 31 | "nbformat_minor": 4 32 | } 33 | -------------------------------------------------------------------------------- /12 Tupples, Dictionary And Sets/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 | -------------------------------------------------------------------------------- /12 Tupples, Dictionary And Sets/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 | -------------------------------------------------------------------------------- /12 Tupples, Dictionary And Sets/Dictionary Functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 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": 3, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "data": { 19 | "text/plain": [ 20 | "{1: 2, 3: 4, 'list': [1, 23], 'dict': {32, 43}}" 21 | ] 22 | }, 23 | "execution_count": 3, 24 | "metadata": {}, 25 | "output_type": "execute_result" 26 | } 27 | ], 28 | "source": [ 29 | "a" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 4, 35 | "metadata": {}, 36 | "outputs": [ 37 | { 38 | "data": { 39 | "text/plain": [ 40 | "{32, 43}" 41 | ] 42 | }, 43 | "execution_count": 4, 44 | "metadata": {}, 45 | "output_type": "execute_result" 46 | } 47 | ], 48 | "source": [ 49 | "a['dict']" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 5, 55 | "metadata": {}, 56 | "outputs": [ 57 | { 58 | "ename": "KeyError", 59 | "evalue": "0", 60 | "output_type": "error", 61 | "traceback": [ 62 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 63 | "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", 64 | "\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;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;31m# THere is no key as 0\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 65 | "\u001b[1;31mKeyError\u001b[0m: 0" 66 | ] 67 | } 68 | ], 69 | "source": [ 70 | "a[0] # THere is no key as 0 " 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 6, 76 | "metadata": {}, 77 | "outputs": [ 78 | { 79 | "data": { 80 | "text/plain": [ 81 | "2" 82 | ] 83 | }, 84 | "execution_count": 6, 85 | "metadata": {}, 86 | "output_type": "execute_result" 87 | } 88 | ], 89 | "source": [ 90 | "a[1] # Because 1 is one of the keys" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 7, 96 | "metadata": {}, 97 | "outputs": [ 98 | { 99 | "data": { 100 | "text/plain": [ 101 | "[1, 23]" 102 | ] 103 | }, 104 | "execution_count": 7, 105 | "metadata": {}, 106 | "output_type": "execute_result" 107 | } 108 | ], 109 | "source": [ 110 | "a['list']" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 8, 116 | "metadata": {}, 117 | "outputs": [ 118 | { 119 | "ename": "KeyError", 120 | "evalue": "'li'", 121 | "output_type": "error", 122 | "traceback": [ 123 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 124 | "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", 125 | "\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;34m'li'\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;31m# No key as a 'li' in the dictionary\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 126 | "\u001b[1;31mKeyError\u001b[0m: 'li'" 127 | ] 128 | } 129 | ], 130 | "source": [ 131 | "a['li'] # No key as a 'li' in the dictionary" 132 | ] 133 | }, 134 | { 135 | "cell_type": "markdown", 136 | "metadata": {}, 137 | "source": [ 138 | "#### Accessing data in dictionary using get() function:" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 9, 144 | "metadata": {}, 145 | "outputs": [ 146 | { 147 | "data": { 148 | "text/plain": [ 149 | "[1, 23]" 150 | ] 151 | }, 152 | "execution_count": 9, 153 | "metadata": {}, 154 | "output_type": "execute_result" 155 | } 156 | ], 157 | "source": [ 158 | "a.get('list')" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": 10, 164 | "metadata": {}, 165 | "outputs": [ 166 | { 167 | "name": "stdout", 168 | "output_type": "stream", 169 | "text": [ 170 | "None\n" 171 | ] 172 | } 173 | ], 174 | "source": [ 175 | "print(a.get('li'))" 176 | ] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "execution_count": 12, 181 | "metadata": {}, 182 | "outputs": [ 183 | { 184 | "name": "stdout", 185 | "output_type": "stream", 186 | "text": [ 187 | "0\n" 188 | ] 189 | } 190 | ], 191 | "source": [ 192 | "print(a.get('li', 0))" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": 11, 198 | "metadata": {}, 199 | "outputs": [ 200 | { 201 | "ename": "AttributeError", 202 | "evalue": "'dict' object has no attribute 'add'", 203 | "output_type": "error", 204 | "traceback": [ 205 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 206 | "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", 207 | "\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;33m{\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0ms\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0madd\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[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[0ms\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0madd\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[0m\n\u001b[0;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlen\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[1;33m\u001b[0m\u001b[0m\n", 208 | "\u001b[1;31mAttributeError\u001b[0m: 'dict' object has no attribute 'add'" 209 | ] 210 | } 211 | ], 212 | "source": [ 213 | "s = {}\n", 214 | "s.add(4)\n", 215 | "s.add(4)\n", 216 | "print(len(s))" 217 | ] 218 | }, 219 | { 220 | "cell_type": "code", 221 | "execution_count": 14, 222 | "metadata": {}, 223 | "outputs": [ 224 | { 225 | "data": { 226 | "text/plain": [ 227 | "dict_keys([1, 3, 'list', 'dict'])" 228 | ] 229 | }, 230 | "execution_count": 14, 231 | "metadata": {}, 232 | "output_type": "execute_result" 233 | } 234 | ], 235 | "source": [ 236 | "a.keys() # To get all the keys of this dictionary" 237 | ] 238 | }, 239 | { 240 | "cell_type": "code", 241 | "execution_count": 15, 242 | "metadata": {}, 243 | "outputs": [ 244 | { 245 | "data": { 246 | "text/plain": [ 247 | "dict_values([2, 4, [1, 23], {32, 43}])" 248 | ] 249 | }, 250 | "execution_count": 15, 251 | "metadata": {}, 252 | "output_type": "execute_result" 253 | } 254 | ], 255 | "source": [ 256 | "a.values() # To get all the values" 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "execution_count": 16, 262 | "metadata": {}, 263 | "outputs": [ 264 | { 265 | "data": { 266 | "text/plain": [ 267 | "dict_items([(1, 2), (3, 4), ('list', [1, 23]), ('dict', {32, 43})])" 268 | ] 269 | }, 270 | "execution_count": 16, 271 | "metadata": {}, 272 | "output_type": "execute_result" 273 | } 274 | ], 275 | "source": [ 276 | "a.items() # To return pairs" 277 | ] 278 | }, 279 | { 280 | "cell_type": "code", 281 | "execution_count": 18, 282 | "metadata": {}, 283 | "outputs": [ 284 | { 285 | "name": "stdout", 286 | "output_type": "stream", 287 | "text": [ 288 | "1\n", 289 | "3\n", 290 | "list\n", 291 | "dict\n" 292 | ] 293 | } 294 | ], 295 | "source": [ 296 | "for i in a: #Keys\n", 297 | " print(i)" 298 | ] 299 | }, 300 | { 301 | "cell_type": "code", 302 | "execution_count": 20, 303 | "metadata": {}, 304 | "outputs": [ 305 | { 306 | "name": "stdout", 307 | "output_type": "stream", 308 | "text": [ 309 | "1 2\n", 310 | "3 4\n", 311 | "list [1, 23]\n", 312 | "dict {32, 43}\n" 313 | ] 314 | } 315 | ], 316 | "source": [ 317 | "for i in a: # To get key and corresponding value\n", 318 | " print(i,\" \", a[i]) " 319 | ] 320 | }, 321 | { 322 | "cell_type": "code", 323 | "execution_count": 21, 324 | "metadata": {}, 325 | "outputs": [ 326 | { 327 | "name": "stdout", 328 | "output_type": "stream", 329 | "text": [ 330 | "2\n", 331 | "4\n", 332 | "[1, 23]\n", 333 | "{32, 43}\n" 334 | ] 335 | } 336 | ], 337 | "source": [ 338 | "for i in a.values():\n", 339 | " print(i)" 340 | ] 341 | }, 342 | { 343 | "cell_type": "code", 344 | "execution_count": 23, 345 | "metadata": {}, 346 | "outputs": [ 347 | { 348 | "data": { 349 | "text/plain": [ 350 | "True" 351 | ] 352 | }, 353 | "execution_count": 23, 354 | "metadata": {}, 355 | "output_type": "execute_result" 356 | } 357 | ], 358 | "source": [ 359 | "\"list\" in a # Is list a key in a ?" 360 | ] 361 | }, 362 | { 363 | "cell_type": "code", 364 | "execution_count": 24, 365 | "metadata": {}, 366 | "outputs": [ 367 | { 368 | "data": { 369 | "text/plain": [ 370 | "False" 371 | ] 372 | }, 373 | "execution_count": 24, 374 | "metadata": {}, 375 | "output_type": "execute_result" 376 | } 377 | ], 378 | "source": [ 379 | "\"li\" in a " 380 | ] 381 | }, 382 | { 383 | "cell_type": "code", 384 | "execution_count": 26, 385 | "metadata": {}, 386 | "outputs": [ 387 | { 388 | "data": { 389 | "text/plain": [ 390 | "False" 391 | ] 392 | }, 393 | "execution_count": 26, 394 | "metadata": {}, 395 | "output_type": "execute_result" 396 | } 397 | ], 398 | "source": [ 399 | "2 in a # Because 2 is a value, not a key" 400 | ] 401 | }, 402 | ], 403 | "metadata": { 404 | "kernelspec": { 405 | "display_name": "Python 3", 406 | "language": "python", 407 | "name": "python3" 408 | }, 409 | "language_info": { 410 | "codemirror_mode": { 411 | "name": "ipython", 412 | "version": 3 413 | }, 414 | "file_extension": ".py", 415 | "mimetype": "text/x-python", 416 | "name": "python", 417 | "nbconvert_exporter": "python", 418 | "pygments_lexer": "ipython3", 419 | "version": "3.7.3" 420 | } 421 | }, 422 | "nbformat": 4, 423 | "nbformat_minor": 2 424 | } 425 | -------------------------------------------------------------------------------- /12 Tupples, Dictionary And Sets/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 | -------------------------------------------------------------------------------- /12 Tupples, Dictionary And Sets/Set Functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Operations on sets: Union, Intersection and difference" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 24, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "a = {1,2,3,4}\n", 17 | "b = {2,3,4,5,6}" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 3, 23 | "metadata": {}, 24 | "outputs": [ 25 | { 26 | "data": { 27 | "text/plain": [ 28 | "{2, 3, 4}" 29 | ] 30 | }, 31 | "execution_count": 3, 32 | "metadata": {}, 33 | "output_type": "execute_result" 34 | } 35 | ], 36 | "source": [ 37 | "a.intersection(b)" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 4, 43 | "metadata": {}, 44 | "outputs": [ 45 | { 46 | "data": { 47 | "text/plain": [ 48 | "{1, 2, 3, 4, 5, 6}" 49 | ] 50 | }, 51 | "execution_count": 4, 52 | "metadata": {}, 53 | "output_type": "execute_result" 54 | } 55 | ], 56 | "source": [ 57 | "a.union(b)" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 5, 63 | "metadata": {}, 64 | "outputs": [ 65 | { 66 | "data": { 67 | "text/plain": [ 68 | "{1, 2, 3, 4, 5, 6}" 69 | ] 70 | }, 71 | "execution_count": 5, 72 | "metadata": {}, 73 | "output_type": "execute_result" 74 | } 75 | ], 76 | "source": [ 77 | "b.union(a)" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 6, 83 | "metadata": {}, 84 | "outputs": [ 85 | { 86 | "data": { 87 | "text/plain": [ 88 | "{1}" 89 | ] 90 | }, 91 | "execution_count": 6, 92 | "metadata": {}, 93 | "output_type": "execute_result" 94 | } 95 | ], 96 | "source": [ 97 | "a.difference(b)" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 7, 103 | "metadata": {}, 104 | "outputs": [ 105 | { 106 | "data": { 107 | "text/plain": [ 108 | "{5, 6}" 109 | ] 110 | }, 111 | "execution_count": 7, 112 | "metadata": {}, 113 | "output_type": "execute_result" 114 | } 115 | ], 116 | "source": [ 117 | "b.difference(a)" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 9, 123 | "metadata": {}, 124 | "outputs": [ 125 | { 126 | "data": { 127 | "text/plain": [ 128 | "{1, 5, 6}" 129 | ] 130 | }, 131 | "execution_count": 9, 132 | "metadata": {}, 133 | "output_type": "execute_result" 134 | } 135 | ], 136 | "source": [ 137 | "a.symmetric_difference(b) # Union - Intersection" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": 11, 143 | "metadata": {}, 144 | "outputs": [], 145 | "source": [ 146 | "a.intersection_update(b)" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": 12, 152 | "metadata": {}, 153 | "outputs": [ 154 | { 155 | "data": { 156 | "text/plain": [ 157 | "{2, 3, 4}" 158 | ] 159 | }, 160 | "execution_count": 12, 161 | "metadata": {}, 162 | "output_type": "execute_result" 163 | } 164 | ], 165 | "source": [ 166 | "a" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": 15, 172 | "metadata": {}, 173 | "outputs": [], 174 | "source": [ 175 | "a.difference_update(b)" 176 | ] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "execution_count": 16, 181 | "metadata": {}, 182 | "outputs": [ 183 | { 184 | "data": { 185 | "text/plain": [ 186 | "{1}" 187 | ] 188 | }, 189 | "execution_count": 16, 190 | "metadata": {}, 191 | "output_type": "execute_result" 192 | } 193 | ], 194 | "source": [ 195 | "a" 196 | ] 197 | }, 198 | { 199 | "cell_type": "code", 200 | "execution_count": 18, 201 | "metadata": {}, 202 | "outputs": [], 203 | "source": [ 204 | "a.symmetric_difference_update(b)" 205 | ] 206 | }, 207 | { 208 | "cell_type": "code", 209 | "execution_count": 19, 210 | "metadata": {}, 211 | "outputs": [ 212 | { 213 | "data": { 214 | "text/plain": [ 215 | "{1, 5, 6}" 216 | ] 217 | }, 218 | "execution_count": 19, 219 | "metadata": {}, 220 | "output_type": "execute_result" 221 | } 222 | ], 223 | "source": [ 224 | "a" 225 | ] 226 | }, 227 | { 228 | "cell_type": "code", 229 | "execution_count": 23, 230 | "metadata": {}, 231 | "outputs": [ 232 | { 233 | "ename": "AttributeError", 234 | "evalue": "'set' object has no attribute 'union_update'", 235 | "output_type": "error", 236 | "traceback": [ 237 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 238 | "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", 239 | "\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[0munion_update\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# No union update\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 240 | "\u001b[1;31mAttributeError\u001b[0m: 'set' object has no attribute 'union_update'" 241 | ] 242 | } 243 | ], 244 | "source": [ 245 | "a.union_update(b) # No union update" 246 | ] 247 | }, 248 | { 249 | "cell_type": "code", 250 | "execution_count": 25, 251 | "metadata": {}, 252 | "outputs": [ 253 | { 254 | "data": { 255 | "text/plain": [ 256 | "{1, 2, 3, 4}" 257 | ] 258 | }, 259 | "execution_count": 25, 260 | "metadata": {}, 261 | "output_type": "execute_result" 262 | } 263 | ], 264 | "source": [ 265 | "a" 266 | ] 267 | }, 268 | { 269 | "cell_type": "code", 270 | "execution_count": 26, 271 | "metadata": {}, 272 | "outputs": [ 273 | { 274 | "data": { 275 | "text/plain": [ 276 | "{2, 3, 4, 5, 6}" 277 | ] 278 | }, 279 | "execution_count": 26, 280 | "metadata": {}, 281 | "output_type": "execute_result" 282 | } 283 | ], 284 | "source": [ 285 | "b" 286 | ] 287 | }, 288 | { 289 | "cell_type": "code", 290 | "execution_count": 27, 291 | "metadata": {}, 292 | "outputs": [ 293 | { 294 | "data": { 295 | "text/plain": [ 296 | "False" 297 | ] 298 | }, 299 | "execution_count": 27, 300 | "metadata": {}, 301 | "output_type": "execute_result" 302 | } 303 | ], 304 | "source": [ 305 | "a.issubset(b)" 306 | ] 307 | }, 308 | { 309 | "cell_type": "code", 310 | "execution_count": 28, 311 | "metadata": {}, 312 | "outputs": [], 313 | "source": [ 314 | "c = {3,4}" 315 | ] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "execution_count": 29, 320 | "metadata": {}, 321 | "outputs": [ 322 | { 323 | "data": { 324 | "text/plain": [ 325 | "False" 326 | ] 327 | }, 328 | "execution_count": 29, 329 | "metadata": {}, 330 | "output_type": "execute_result" 331 | } 332 | ], 333 | "source": [ 334 | "a.issubset(c)" 335 | ] 336 | }, 337 | { 338 | "cell_type": "code", 339 | "execution_count": 30, 340 | "metadata": {}, 341 | "outputs": [ 342 | { 343 | "data": { 344 | "text/plain": [ 345 | "True" 346 | ] 347 | }, 348 | "execution_count": 30, 349 | "metadata": {}, 350 | "output_type": "execute_result" 351 | } 352 | ], 353 | "source": [ 354 | "c.issubset(a)" 355 | ] 356 | }, 357 | { 358 | "cell_type": "code", 359 | "execution_count": 32, 360 | "metadata": {}, 361 | "outputs": [ 362 | { 363 | "data": { 364 | "text/plain": [ 365 | "True" 366 | ] 367 | }, 368 | "execution_count": 32, 369 | "metadata": {}, 370 | "output_type": "execute_result" 371 | } 372 | ], 373 | "source": [ 374 | "a.issuperset(c)" 375 | ] 376 | }, 377 | { 378 | "cell_type": "code", 379 | "execution_count": 33, 380 | "metadata": {}, 381 | "outputs": [ 382 | { 383 | "data": { 384 | "text/plain": [ 385 | "False" 386 | ] 387 | }, 388 | "execution_count": 33, 389 | "metadata": {}, 390 | "output_type": "execute_result" 391 | } 392 | ], 393 | "source": [ 394 | "a.isdisjoint(b)" 395 | ] 396 | }, 397 | { 398 | "cell_type": "code", 399 | "execution_count": 34, 400 | "metadata": {}, 401 | "outputs": [], 402 | "source": [ 403 | "d = {434,23}" 404 | ] 405 | }, 406 | { 407 | "cell_type": "code", 408 | "execution_count": 35, 409 | "metadata": {}, 410 | "outputs": [ 411 | { 412 | "data": { 413 | "text/plain": [ 414 | "True" 415 | ] 416 | }, 417 | "execution_count": 35, 418 | "metadata": {}, 419 | "output_type": "execute_result" 420 | } 421 | ], 422 | "source": [ 423 | "a.isdisjoint(d)" 424 | ] 425 | } 426 | ], 427 | "metadata": { 428 | "kernelspec": { 429 | "display_name": "Python 3", 430 | "language": "python", 431 | "name": "python3" 432 | }, 433 | "language_info": { 434 | "codemirror_mode": { 435 | "name": "ipython", 436 | "version": 3 437 | }, 438 | "file_extension": ".py", 439 | "mimetype": "text/x-python", 440 | "name": "python", 441 | "nbconvert_exporter": "python", 442 | "pygments_lexer": "ipython3", 443 | "version": "3.7.3" 444 | } 445 | }, 446 | "nbformat": 4, 447 | "nbformat_minor": 2 448 | } 449 | -------------------------------------------------------------------------------- /12 Tupples, Dictionary And Sets/Sets - Intro.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "##### Sets are unique collection of data but there is no particular order of data" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "data": { 17 | "text/plain": [ 18 | "{'abc', 'apple', 'banana'}" 19 | ] 20 | }, 21 | "execution_count": 1, 22 | "metadata": {}, 23 | "output_type": "execute_result" 24 | } 25 | ], 26 | "source": [ 27 | "a = {\"apple\", \"banana\", \"abc\" } #Just keys not the values\n", 28 | "a" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 2, 34 | "metadata": {}, 35 | "outputs": [ 36 | { 37 | "data": { 38 | "text/plain": [ 39 | "set" 40 | ] 41 | }, 42 | "execution_count": 2, 43 | "metadata": {}, 44 | "output_type": "execute_result" 45 | } 46 | ], 47 | "source": [ 48 | "type(a)" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 9, 54 | "metadata": {}, 55 | "outputs": [ 56 | { 57 | "data": { 58 | "text/plain": [ 59 | "dict" 60 | ] 61 | }, 62 | "execution_count": 9, 63 | "metadata": {}, 64 | "output_type": "execute_result" 65 | } 66 | ], 67 | "source": [ 68 | "dict = {\"abc\": 3, \"str\" : \"Helllo\"} # Has key and value\n", 69 | "type(dict)" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 8, 75 | "metadata": {}, 76 | "outputs": [ 77 | { 78 | "data": { 79 | "text/plain": [ 80 | "tuple" 81 | ] 82 | }, 83 | "execution_count": 8, 84 | "metadata": {}, 85 | "output_type": "execute_result" 86 | } 87 | ], 88 | "source": [ 89 | "a = (1,2,3)\n", 90 | "type(a)" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 10, 96 | "metadata": {}, 97 | "outputs": [ 98 | { 99 | "data": { 100 | "text/plain": [ 101 | "list" 102 | ] 103 | }, 104 | "execution_count": 10, 105 | "metadata": {}, 106 | "output_type": "execute_result" 107 | } 108 | ], 109 | "source": [ 110 | "a = [1,2,3,4]\n", 111 | "type(a)" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": 11, 117 | "metadata": {}, 118 | "outputs": [ 119 | { 120 | "data": { 121 | "text/plain": [ 122 | "{'abc', 'apple', 'banana'}" 123 | ] 124 | }, 125 | "execution_count": 11, 126 | "metadata": {}, 127 | "output_type": "execute_result" 128 | } 129 | ], 130 | "source": [ 131 | "a = {\"apple\", \"banana\", \"abc\" } #Just keys not the values\n", 132 | "a" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 12, 138 | "metadata": {}, 139 | "outputs": [ 140 | { 141 | "data": { 142 | "text/plain": [ 143 | "set" 144 | ] 145 | }, 146 | "execution_count": 12, 147 | "metadata": {}, 148 | "output_type": "execute_result" 149 | } 150 | ], 151 | "source": [ 152 | "type(a)" 153 | ] 154 | }, 155 | { 156 | "cell_type": "code", 157 | "execution_count": 16, 158 | "metadata": {}, 159 | "outputs": [ 160 | { 161 | "data": { 162 | "text/plain": [ 163 | "{'abc', 'apple', 'banana'}" 164 | ] 165 | }, 166 | "execution_count": 16, 167 | "metadata": {}, 168 | "output_type": "execute_result" 169 | } 170 | ], 171 | "source": [ 172 | "a" 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": 20, 178 | "metadata": {}, 179 | "outputs": [ 180 | { 181 | "data": { 182 | "text/plain": [ 183 | "True" 184 | ] 185 | }, 186 | "execution_count": 20, 187 | "metadata": {}, 188 | "output_type": "execute_result" 189 | } 190 | ], 191 | "source": [ 192 | "'abc' in a # To check your set contains a value or not" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": 21, 198 | "metadata": {}, 199 | "outputs": [ 200 | { 201 | "data": { 202 | "text/plain": [ 203 | "False" 204 | ] 205 | }, 206 | "execution_count": 21, 207 | "metadata": {}, 208 | "output_type": "execute_result" 209 | } 210 | ], 211 | "source": [ 212 | "21 in a" 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 | "abc\n", 225 | "apple\n", 226 | "banana\n" 227 | ] 228 | } 229 | ], 230 | "source": [ 231 | "for v in a:\n", 232 | " print(v)" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": 23, 238 | "metadata": {}, 239 | "outputs": [ 240 | { 241 | "data": { 242 | "text/plain": [ 243 | "3" 244 | ] 245 | }, 246 | "execution_count": 23, 247 | "metadata": {}, 248 | "output_type": "execute_result" 249 | } 250 | ], 251 | "source": [ 252 | "len(a)" 253 | ] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": 24, 258 | "metadata": {}, 259 | "outputs": [], 260 | "source": [ 261 | "a.add(\"temp\")" 262 | ] 263 | }, 264 | { 265 | "cell_type": "code", 266 | "execution_count": 25, 267 | "metadata": {}, 268 | "outputs": [ 269 | { 270 | "data": { 271 | "text/plain": [ 272 | "{'abc', 'apple', 'banana', 'temp'}" 273 | ] 274 | }, 275 | "execution_count": 25, 276 | "metadata": {}, 277 | "output_type": "execute_result" 278 | } 279 | ], 280 | "source": [ 281 | "a" 282 | ] 283 | }, 284 | { 285 | "cell_type": "code", 286 | "execution_count": 26, 287 | "metadata": {}, 288 | "outputs": [], 289 | "source": [ 290 | "b = {\"abc\", 'Hello'}" 291 | ] 292 | }, 293 | { 294 | "cell_type": "code", 295 | "execution_count": 27, 296 | "metadata": {}, 297 | "outputs": [], 298 | "source": [ 299 | "a.update(b)" 300 | ] 301 | }, 302 | { 303 | "cell_type": "code", 304 | "execution_count": 28, 305 | "metadata": {}, 306 | "outputs": [ 307 | { 308 | "data": { 309 | "text/plain": [ 310 | "{'Hello', 'abc', 'apple', 'banana', 'temp'}" 311 | ] 312 | }, 313 | "execution_count": 28, 314 | "metadata": {}, 315 | "output_type": "execute_result" 316 | } 317 | ], 318 | "source": [ 319 | "a" 320 | ] 321 | }, 322 | { 323 | "cell_type": "code", 324 | "execution_count": 29, 325 | "metadata": {}, 326 | "outputs": [], 327 | "source": [ 328 | "num = {1,3,2,4,5,4,2}" 329 | ] 330 | }, 331 | { 332 | "cell_type": "code", 333 | "execution_count": 30, 334 | "metadata": {}, 335 | "outputs": [ 336 | { 337 | "data": { 338 | "text/plain": [ 339 | "{1, 2, 3, 4, 5}" 340 | ] 341 | }, 342 | "execution_count": 30, 343 | "metadata": {}, 344 | "output_type": "execute_result" 345 | } 346 | ], 347 | "source": [ 348 | "num" 349 | ] 350 | }, 351 | { 352 | "cell_type": "code", 353 | "execution_count": 31, 354 | "metadata": {}, 355 | "outputs": [], 356 | "source": [ 357 | "a.remove('temp')" 358 | ] 359 | }, 360 | { 361 | "cell_type": "code", 362 | "execution_count": 32, 363 | "metadata": {}, 364 | "outputs": [ 365 | { 366 | "data": { 367 | "text/plain": [ 368 | "{'Hello', 'abc', 'apple', 'banana'}" 369 | ] 370 | }, 371 | "execution_count": 32, 372 | "metadata": {}, 373 | "output_type": "execute_result" 374 | } 375 | ], 376 | "source": [ 377 | "a" 378 | ] 379 | }, 380 | { 381 | "cell_type": "code", 382 | "execution_count": 33, 383 | "metadata": {}, 384 | "outputs": [ 385 | { 386 | "ename": "KeyError", 387 | "evalue": "'Hell'", 388 | "output_type": "error", 389 | "traceback": [ 390 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 391 | "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", 392 | "\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[0mremove\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Hell'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 393 | "\u001b[1;31mKeyError\u001b[0m: 'Hell'" 394 | ] 395 | } 396 | ], 397 | "source": [ 398 | "a.remove('Hell')" 399 | ] 400 | }, 401 | { 402 | "cell_type": "code", 403 | "execution_count": 34, 404 | "metadata": {}, 405 | "outputs": [ 406 | { 407 | "data": { 408 | "text/plain": [ 409 | "{'Hello', 'abc', 'apple', 'banana'}" 410 | ] 411 | }, 412 | "execution_count": 34, 413 | "metadata": {}, 414 | "output_type": "execute_result" 415 | } 416 | ], 417 | "source": [ 418 | "a" 419 | ] 420 | }, 421 | { 422 | "cell_type": "code", 423 | "execution_count": 35, 424 | "metadata": {}, 425 | "outputs": [], 426 | "source": [ 427 | "a.discard(23)" 428 | ] 429 | }, 430 | { 431 | "cell_type": "code", 432 | "execution_count": 36, 433 | "metadata": {}, 434 | "outputs": [ 435 | { 436 | "name": "stdout", 437 | "output_type": "stream", 438 | "text": [ 439 | "None\n" 440 | ] 441 | } 442 | ], 443 | "source": [ 444 | "print(a.discard(23))" 445 | ] 446 | }, 447 | { 448 | "cell_type": "code", 449 | "execution_count": 38, 450 | "metadata": {}, 451 | "outputs": [ 452 | { 453 | "data": { 454 | "text/plain": [ 455 | "'abc'" 456 | ] 457 | }, 458 | "execution_count": 38, 459 | "metadata": {}, 460 | "output_type": "execute_result" 461 | } 462 | ], 463 | "source": [ 464 | "a.pop()" 465 | ] 466 | }, 467 | { 468 | "cell_type": "code", 469 | "execution_count": 39, 470 | "metadata": {}, 471 | "outputs": [ 472 | { 473 | "data": { 474 | "text/plain": [ 475 | "{'Hello', 'apple', 'banana'}" 476 | ] 477 | }, 478 | "execution_count": 39, 479 | "metadata": {}, 480 | "output_type": "execute_result" 481 | } 482 | ], 483 | "source": [ 484 | "a" 485 | ] 486 | }, 487 | { 488 | "cell_type": "code", 489 | "execution_count": 40, 490 | "metadata": {}, 491 | "outputs": [ 492 | { 493 | "data": { 494 | "text/plain": [ 495 | "'banana'" 496 | ] 497 | }, 498 | "execution_count": 40, 499 | "metadata": {}, 500 | "output_type": "execute_result" 501 | } 502 | ], 503 | "source": [ 504 | "a.pop()" 505 | ] 506 | }, 507 | { 508 | "cell_type": "code", 509 | "execution_count": 41, 510 | "metadata": {}, 511 | "outputs": [], 512 | "source": [ 513 | "del a" 514 | ] 515 | }, 516 | { 517 | "cell_type": "code", 518 | "execution_count": 42, 519 | "metadata": {}, 520 | "outputs": [ 521 | { 522 | "ename": "NameError", 523 | "evalue": "name 'a' is not defined", 524 | "output_type": "error", 525 | "traceback": [ 526 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 527 | "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", 528 | "\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", 529 | "\u001b[1;31mNameError\u001b[0m: name 'a' is not defined" 530 | ] 531 | } 532 | ], 533 | "source": [ 534 | "a" 535 | ] 536 | }, 537 | { 538 | "cell_type": "code", 539 | "execution_count": null, 540 | "metadata": {}, 541 | "outputs": [], 542 | "source": [] 543 | } 544 | ], 545 | "metadata": { 546 | "kernelspec": { 547 | "display_name": "Python 3", 548 | "language": "python", 549 | "name": "python3" 550 | }, 551 | "language_info": { 552 | "codemirror_mode": { 553 | "name": "ipython", 554 | "version": 3 555 | }, 556 | "file_extension": ".py", 557 | "mimetype": "text/x-python", 558 | "name": "python", 559 | "nbconvert_exporter": "python", 560 | "pygments_lexer": "ipython3", 561 | "version": "3.7.3" 562 | } 563 | }, 564 | "nbformat": 4, 565 | "nbformat_minor": 2 566 | } 567 | -------------------------------------------------------------------------------- /12 Tupples, Dictionary And Sets/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 | -------------------------------------------------------------------------------- /12 Tupples, Dictionary And Sets/Tuples Functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "a = (1,2,3)\n", 10 | "b = 4,5" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 4, 16 | "metadata": {}, 17 | "outputs": [ 18 | { 19 | "name": "stdout", 20 | "output_type": "stream", 21 | "text": [ 22 | "1\n", 23 | "2\n", 24 | "3\n" 25 | ] 26 | } 27 | ], 28 | "source": [ 29 | "#for loops\n", 30 | "for i in a:\n", 31 | " print(i)" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 5, 37 | "metadata": {}, 38 | "outputs": [ 39 | { 40 | "name": "stdout", 41 | "output_type": "stream", 42 | "text": [ 43 | "4\n", 44 | "5\n" 45 | ] 46 | } 47 | ], 48 | "source": [ 49 | "for i in b :\n", 50 | " print(i)" 51 | ] 52 | }, 53 | { 54 | "cell_type": "markdown", 55 | "metadata": {}, 56 | "source": [ 57 | "#### Membership function" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 6, 63 | "metadata": {}, 64 | "outputs": [ 65 | { 66 | "data": { 67 | "text/plain": [ 68 | "True" 69 | ] 70 | }, 71 | "execution_count": 6, 72 | "metadata": {}, 73 | "output_type": "execute_result" 74 | } 75 | ], 76 | "source": [ 77 | "#checking for membership, 1 is part of a or not\n", 78 | "1 in a" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 7, 84 | "metadata": {}, 85 | "outputs": [ 86 | { 87 | "data": { 88 | "text/plain": [ 89 | "False" 90 | ] 91 | }, 92 | "execution_count": 7, 93 | "metadata": {}, 94 | "output_type": "execute_result" 95 | } 96 | ], 97 | "source": [ 98 | "1 in b" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 11, 104 | "metadata": {}, 105 | "outputs": [ 106 | { 107 | "name": "stdout", 108 | "output_type": "stream", 109 | "text": [ 110 | "(1, 2, 3)\n" 111 | ] 112 | } 113 | ], 114 | "source": [ 115 | "if 1 in a:\n", 116 | " print(a)" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 12, 122 | "metadata": {}, 123 | "outputs": [ 124 | { 125 | "data": { 126 | "text/plain": [ 127 | "3" 128 | ] 129 | }, 130 | "execution_count": 12, 131 | "metadata": {}, 132 | "output_type": "execute_result" 133 | } 134 | ], 135 | "source": [ 136 | "len(a)" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 17, 142 | "metadata": {}, 143 | "outputs": [], 144 | "source": [ 145 | "c = a + b #appends all the elements in a single tuple" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 18, 151 | "metadata": {}, 152 | "outputs": [], 153 | "source": [ 154 | "d = (a,b) ## creates another tuple with the two tuples" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 19, 160 | "metadata": {}, 161 | "outputs": [ 162 | { 163 | "data": { 164 | "text/plain": [ 165 | "(1, 2, 3, 4, 5)" 166 | ] 167 | }, 168 | "execution_count": 19, 169 | "metadata": {}, 170 | "output_type": "execute_result" 171 | } 172 | ], 173 | "source": [ 174 | "c " 175 | ] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": 20, 180 | "metadata": {}, 181 | "outputs": [ 182 | { 183 | "data": { 184 | "text/plain": [ 185 | "((1, 2, 3), (4, 5))" 186 | ] 187 | }, 188 | "execution_count": 20, 189 | "metadata": {}, 190 | "output_type": "execute_result" 191 | } 192 | ], 193 | "source": [ 194 | "d" 195 | ] 196 | }, 197 | { 198 | "cell_type": "code", 199 | "execution_count": 24, 200 | "metadata": {}, 201 | "outputs": [ 202 | { 203 | "data": { 204 | "text/plain": [ 205 | "(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3)" 206 | ] 207 | }, 208 | "execution_count": 24, 209 | "metadata": {}, 210 | "output_type": "execute_result" 211 | } 212 | ], 213 | "source": [ 214 | "#repetition\n", 215 | "e = a*4\n", 216 | "e" 217 | ] 218 | }, 219 | { 220 | "cell_type": "code", 221 | "execution_count": 25, 222 | "metadata": {}, 223 | "outputs": [], 224 | "source": [ 225 | "del e" 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "execution_count": 26, 231 | "metadata": {}, 232 | "outputs": [ 233 | { 234 | "data": { 235 | "text/plain": [ 236 | "1" 237 | ] 238 | }, 239 | "execution_count": 26, 240 | "metadata": {}, 241 | "output_type": "execute_result" 242 | } 243 | ], 244 | "source": [ 245 | "min(a)" 246 | ] 247 | }, 248 | { 249 | "cell_type": "markdown", 250 | "metadata": {}, 251 | "source": [ 252 | "### Min, Max operations" 253 | ] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": 27, 258 | "metadata": {}, 259 | "outputs": [ 260 | { 261 | "data": { 262 | "text/plain": [ 263 | "3" 264 | ] 265 | }, 266 | "execution_count": 27, 267 | "metadata": {}, 268 | "output_type": "execute_result" 269 | } 270 | ], 271 | "source": [ 272 | "max(a)" 273 | ] 274 | }, 275 | { 276 | "cell_type": "code", 277 | "execution_count": 28, 278 | "metadata": {}, 279 | "outputs": [], 280 | "source": [ 281 | "a2 = (1,2,\"s\", (1,2))" 282 | ] 283 | }, 284 | { 285 | "cell_type": "code", 286 | "execution_count": 29, 287 | "metadata": {}, 288 | "outputs": [ 289 | { 290 | "ename": "TypeError", 291 | "evalue": "'<' not supported between instances of 'str' and 'int'", 292 | "output_type": "error", 293 | "traceback": [ 294 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 295 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 296 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mmin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 297 | "\u001b[1;31mTypeError\u001b[0m: '<' not supported between instances of 'str' and 'int'" 298 | ] 299 | } 300 | ], 301 | "source": [ 302 | "min(a2)" 303 | ] 304 | }, 305 | { 306 | "cell_type": "code", 307 | "execution_count": 30, 308 | "metadata": {}, 309 | "outputs": [], 310 | "source": [ 311 | "a2 = (1,2,(1,2))" 312 | ] 313 | }, 314 | { 315 | "cell_type": "code", 316 | "execution_count": 31, 317 | "metadata": {}, 318 | "outputs": [ 319 | { 320 | "ename": "TypeError", 321 | "evalue": "'<' not supported between instances of 'tuple' and 'int'", 322 | "output_type": "error", 323 | "traceback": [ 324 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 325 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 326 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mmin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 327 | "\u001b[1;31mTypeError\u001b[0m: '<' not supported between instances of 'tuple' and 'int'" 328 | ] 329 | } 330 | ], 331 | "source": [ 332 | "min(a2)" 333 | ] 334 | }, 335 | { 336 | "cell_type": "code", 337 | "execution_count": 34, 338 | "metadata": {}, 339 | "outputs": [], 340 | "source": [ 341 | "a2 = (1,2,5,7.5) # They should be comparable to each other" 342 | ] 343 | }, 344 | { 345 | "cell_type": "code", 346 | "execution_count": 35, 347 | "metadata": {}, 348 | "outputs": [ 349 | { 350 | "data": { 351 | "text/plain": [ 352 | "1" 353 | ] 354 | }, 355 | "execution_count": 35, 356 | "metadata": {}, 357 | "output_type": "execute_result" 358 | } 359 | ], 360 | "source": [ 361 | "min(a2)" 362 | ] 363 | }, 364 | { 365 | "cell_type": "code", 366 | "execution_count": 37, 367 | "metadata": {}, 368 | "outputs": [ 369 | { 370 | "data": { 371 | "text/plain": [ 372 | "(1, 2, 3, 4)" 373 | ] 374 | }, 375 | "execution_count": 37, 376 | "metadata": {}, 377 | "output_type": "execute_result" 378 | } 379 | ], 380 | "source": [ 381 | "list = [1,2,3,4]\n", 382 | "tuple(list)" 383 | ] 384 | }, 385 | { 386 | "cell_type": "code", 387 | "execution_count": 38, 388 | "metadata": {}, 389 | "outputs": [ 390 | { 391 | "name": "stdout", 392 | "output_type": "stream", 393 | "text": [ 394 | "(1, 2)\n" 395 | ] 396 | } 397 | ], 398 | "source": [ 399 | "#Predict the output\n", 400 | "a = 1,2\n", 401 | "b = (4,5)\n", 402 | "d = (a,b)\n", 403 | "print(d[0])" 404 | ] 405 | }, 406 | { 407 | "cell_type": "code", 408 | "execution_count": 39, 409 | "metadata": {}, 410 | "outputs": [ 411 | { 412 | "name": "stdout", 413 | "output_type": "stream", 414 | "text": [ 415 | "4\n" 416 | ] 417 | } 418 | ], 419 | "source": [ 420 | "#Predict the output\n", 421 | "a = 1,2\n", 422 | "b = (4,5)\n", 423 | "d = a+b\n", 424 | "print(d[2])" 425 | ] 426 | }, 427 | { 428 | "cell_type": "code", 429 | "execution_count": 41, 430 | "metadata": {}, 431 | "outputs": [ 432 | { 433 | "name": "stdout", 434 | "output_type": "stream", 435 | "text": [ 436 | "ab\n" 437 | ] 438 | } 439 | ], 440 | "source": [ 441 | "#Predict the output\n", 442 | "a = (\"ab\",\"abc\",\"def\")\n", 443 | "print(min(a))" 444 | ] 445 | }, 446 | ], 447 | "metadata": { 448 | "kernelspec": { 449 | "display_name": "Python 3", 450 | "language": "python", 451 | "name": "python3" 452 | }, 453 | "language_info": { 454 | "codemirror_mode": { 455 | "name": "ipython", 456 | "version": 3 457 | }, 458 | "file_extension": ".py", 459 | "mimetype": "text/x-python", 460 | "name": "python", 461 | "nbconvert_exporter": "python", 462 | "pygments_lexer": "ipython3", 463 | "version": "3.7.3" 464 | } 465 | }, 466 | "nbformat": 4, 467 | "nbformat_minor": 2 468 | } 469 | -------------------------------------------------------------------------------- /12 Tupples, Dictionary And Sets/Tuples.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "#### Tuples are like lists but different from lists" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "a = (1,2)" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 2, 22 | "metadata": {}, 23 | "outputs": [ 24 | { 25 | "data": { 26 | "text/plain": [ 27 | "(1, 2)" 28 | ] 29 | }, 30 | "execution_count": 2, 31 | "metadata": {}, 32 | "output_type": "execute_result" 33 | } 34 | ], 35 | "source": [ 36 | "a" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 3, 42 | "metadata": {}, 43 | "outputs": [ 44 | { 45 | "name": "stdout", 46 | "output_type": "stream", 47 | "text": [ 48 | "\n" 49 | ] 50 | } 51 | ], 52 | "source": [ 53 | "print(type(a))" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 4, 59 | "metadata": {}, 60 | "outputs": [ 61 | { 62 | "name": "stdout", 63 | "output_type": "stream", 64 | "text": [ 65 | "3\n", 66 | "4\n", 67 | "(5, 6)\n", 68 | "\n" 69 | ] 70 | } 71 | ], 72 | "source": [ 73 | "b, c = 3,4\n", 74 | "\n", 75 | "print(b)\n", 76 | "print(c)\n", 77 | "\n", 78 | "e = 5,6\n", 79 | "print(e)\n", 80 | "print(type(e))" 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "metadata": {}, 86 | "source": [ 87 | "#### Accessing from tuples works exactly like lists (Indexing and Slicing works same like lists because it is ordered)" 88 | ] 89 | }, 90 | { 91 | "cell_type": "markdown", 92 | "metadata": {}, 93 | "source": [ 94 | "In case of strings, tuples and lists idexing works same because all of them have a sequence" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 5, 100 | "metadata": {}, 101 | "outputs": [ 102 | { 103 | "data": { 104 | "text/plain": [ 105 | "5" 106 | ] 107 | }, 108 | "execution_count": 5, 109 | "metadata": {}, 110 | "output_type": "execute_result" 111 | } 112 | ], 113 | "source": [ 114 | "e[0]" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 6, 120 | "metadata": {}, 121 | "outputs": [ 122 | { 123 | "data": { 124 | "text/plain": [ 125 | "6" 126 | ] 127 | }, 128 | "execution_count": 6, 129 | "metadata": {}, 130 | "output_type": "execute_result" 131 | } 132 | ], 133 | "source": [ 134 | "e[1]" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": 7, 140 | "metadata": {}, 141 | "outputs": [ 142 | { 143 | "data": { 144 | "text/plain": [ 145 | "6" 146 | ] 147 | }, 148 | "execution_count": 7, 149 | "metadata": {}, 150 | "output_type": "execute_result" 151 | } 152 | ], 153 | "source": [ 154 | "e[-1]" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 10, 160 | "metadata": {}, 161 | "outputs": [ 162 | { 163 | "data": { 164 | "text/plain": [ 165 | "(1, 2, 3)" 166 | ] 167 | }, 168 | "execution_count": 10, 169 | "metadata": {}, 170 | "output_type": "execute_result" 171 | } 172 | ], 173 | "source": [ 174 | "f = (1,2,3,4,5,6)\n", 175 | "f[0:3]" 176 | ] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "execution_count": 11, 181 | "metadata": {}, 182 | "outputs": [ 183 | { 184 | "data": { 185 | "text/plain": [ 186 | "(4, 5, 6)" 187 | ] 188 | }, 189 | "execution_count": 11, 190 | "metadata": {}, 191 | "output_type": "execute_result" 192 | } 193 | ], 194 | "source": [ 195 | "f[3:]" 196 | ] 197 | }, 198 | { 199 | "cell_type": "markdown", 200 | "metadata": {}, 201 | "source": [ 202 | "#### Difference between tuples and lists:- \n", 203 | "Tuples are immutable but lists are mutable. \n", 204 | "You can update, add and remove in lists but you can't do any of these operations in tuple.\n", 205 | "You cannot change or add entries in tuple, if you want you can delete the entire tuple by using del keyword.\n", 206 | "##### Syntax:- del tuple_name" 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "execution_count": 12, 212 | "metadata": {}, 213 | "outputs": [], 214 | "source": [ 215 | "del f" 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": 13, 221 | "metadata": {}, 222 | "outputs": [ 223 | { 224 | "ename": "NameError", 225 | "evalue": "name 'f' is not defined", 226 | "output_type": "error", 227 | "traceback": [ 228 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 229 | "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", 230 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mf\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 231 | "\u001b[1;31mNameError\u001b[0m: name 'f' is not defined" 232 | ] 233 | } 234 | ], 235 | "source": [ 236 | "f" 237 | ] 238 | }, 239 | { 240 | "cell_type": "code", 241 | "execution_count": 14, 242 | "metadata": {}, 243 | "outputs": [ 244 | { 245 | "name": "stdout", 246 | "output_type": "stream", 247 | "text": [ 248 | "(6, 7)\n" 249 | ] 250 | } 251 | ], 252 | "source": [ 253 | "#Predict the output\n", 254 | "a = 5,6,7\n", 255 | "print(a[1:])" 256 | ] 257 | }, 258 | { 259 | "cell_type": "code", 260 | "execution_count": 15, 261 | "metadata": {}, 262 | "outputs": [ 263 | { 264 | "ename": "TypeError", 265 | "evalue": "'tuple' object does not support item assignment", 266 | "output_type": "error", 267 | "traceback": [ 268 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 269 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 270 | "\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[0ma\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;36m7\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m9\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 271 | "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" 272 | ] 273 | } 274 | ], 275 | "source": [ 276 | "#Predict the output\n", 277 | "a = 5,6,7\n", 278 | "a[2] = 9 \n", 279 | "print(a)" 280 | ] 281 | }, 282 | ], 283 | "metadata": { 284 | "kernelspec": { 285 | "display_name": "Python 3", 286 | "language": "python", 287 | "name": "python3" 288 | }, 289 | "language_info": { 290 | "codemirror_mode": { 291 | "name": "ipython", 292 | "version": 3 293 | }, 294 | "file_extension": ".py", 295 | "mimetype": "text/x-python", 296 | "name": "python", 297 | "nbconvert_exporter": "python", 298 | "pygments_lexer": "ipython3", 299 | "version": "3.7.3" 300 | } 301 | }, 302 | "nbformat": 4, 303 | "nbformat_minor": 2 304 | } 305 | -------------------------------------------------------------------------------- /12 Tupples, Dictionary And Sets/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 | -------------------------------------------------------------------------------- /2 Introduction to Python/Find_Average_Marks.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "#Find average Marks\n", 10 | "\n", 11 | "#Write a program to input marks of three tests of a student (all integers). Then calculate and print the average of all test marks." 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 1, 17 | "metadata": {}, 18 | "outputs": [ 19 | { 20 | "name": "stdout", 21 | "output_type": "stream", 22 | "text": [ 23 | "1\n", 24 | "7\n", 25 | "9\n", 26 | "5.666666666666667\n" 27 | ] 28 | } 29 | ], 30 | "source": [ 31 | "# Read input as sepcified in the question\n", 32 | "# Print output as specified in the question\n", 33 | "test1=int(input())\n", 34 | "test2=int(input())\n", 35 | "test3=int(input())\n", 36 | "average=(test1+test2+test3)/3\n", 37 | "print(average)" 38 | ] 39 | } 40 | ], 41 | "metadata": { 42 | "kernelspec": { 43 | "display_name": "Python 3", 44 | "language": "python", 45 | "name": "python3" 46 | }, 47 | "language_info": { 48 | "codemirror_mode": { 49 | "name": "ipython", 50 | "version": 3 51 | }, 52 | "file_extension": ".py", 53 | "mimetype": "text/x-python", 54 | "name": "python", 55 | "nbconvert_exporter": "python", 56 | "pygments_lexer": "ipython3", 57 | "version": "3.7.6" 58 | } 59 | }, 60 | "nbformat": 4, 61 | "nbformat_minor": 4 62 | } 63 | -------------------------------------------------------------------------------- /2 Introduction to Python/Find_X_to_The_Power_of_N.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 | "5\n", 13 | "8\n", 14 | "390625\n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "#Find X raised to power N\n", 20 | "\n", 21 | "\n", 22 | "x=int(input())\n", 23 | "n=int(input())\n", 24 | "res=pow(x,n)\n", 25 | "print(res)" 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.6" 46 | } 47 | }, 48 | "nbformat": 4, 49 | "nbformat_minor": 4 50 | } 51 | -------------------------------------------------------------------------------- /2 Introduction to Python/Rectangular_Area.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 4 6 | } 7 | -------------------------------------------------------------------------------- /3 Conditionals and Loops/Calculator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int n,a,b; 6 | cin>>n; 7 | while(n!=6) 8 | { 9 | switch(n){ 10 | case 1: cin>>a>>b; 11 | cout<>a>>b; 14 | cout<>a>>b; 17 | cout<>a>>b; 20 | cout<>a>>b; 23 | cout<>n; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /3 Conditionals and Loops/Calculator.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "3\n", 13 | "1\n", 14 | "2\n", 15 | "2\n", 16 | "4\n", 17 | "4\n", 18 | "2\n", 19 | "2\n", 20 | "1\n", 21 | "3\n", 22 | "2\n", 23 | "5\n", 24 | "7\n", 25 | "Invalid Operation\n" 26 | ] 27 | } 28 | ], 29 | "source": [ 30 | "n=int(input())\n", 31 | "while n!=6:\n", 32 | " if n <= 5 and n >= 1:\n", 33 | " a=int(input())\n", 34 | " b=int(input())\n", 35 | " if n==1:\n", 36 | " print(a+b)\n", 37 | " if n==2:\n", 38 | " print(a-b)\n", 39 | " if n==3:\n", 40 | " print(a*2)\n", 41 | " if n==4:\n", 42 | " print(a//b)\n", 43 | " if n==5:\n", 44 | " print(a%b)\n", 45 | " elif n < 1 or n > 6:\n", 46 | " print(\"Invalid Operation\")\n", 47 | " n=int(input())" 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.6" 75 | } 76 | }, 77 | "nbformat": 4, 78 | "nbformat_minor": 4 79 | } 80 | -------------------------------------------------------------------------------- /3 Conditionals and Loops/Check number.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Given an integer n, find if n is positive, negative or 0.\n", 8 | "\n", 9 | "If n is positive, print \"Positive\"\n", 10 | "If n is negative, print \"Negative\"\n", 11 | "And if n is equal to 0, print \"Zero\".\n", 12 | "\n", 13 | "Input Format :\n", 14 | "\n", 15 | "Integer n\n", 16 | "\n", 17 | "Output Format :\n", 18 | "\n", 19 | "\"Positive\" or \"Negative\" or \"Zero\" (without double quotes)\n", 20 | "\n", 21 | "Constraints :\n", 22 | "\n", 23 | "1 <= n <= 100\n", 24 | "\n", 25 | "Sample Input 1 :\n", 26 | "\n", 27 | "10\n", 28 | "\n", 29 | "Sample Output 1 :\n", 30 | "\n", 31 | "Positive\n", 32 | "\n", 33 | "Sample Input 2 :\n", 34 | "\n", 35 | "-10\n", 36 | "\n", 37 | "Sample Output 2 :\n", 38 | "\n", 39 | "Negative\n" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": null, 45 | "metadata": {}, 46 | "outputs": [], 47 | "source": [ 48 | "n=int(input())\n", 49 | "if n>0:\n", 50 | " print(\"Positive\")\n", 51 | "elif n<0" 52 | ] 53 | } 54 | ], 55 | "metadata": { 56 | "kernelspec": { 57 | "display_name": "Python 3", 58 | "language": "python", 59 | "name": "python3" 60 | }, 61 | "language_info": { 62 | "codemirror_mode": { 63 | "name": "ipython", 64 | "version": 3 65 | }, 66 | "file_extension": ".py", 67 | "mimetype": "text/x-python", 68 | "name": "python", 69 | "nbconvert_exporter": "python", 70 | "pygments_lexer": "ipython3", 71 | "version": "3.7.6" 72 | } 73 | }, 74 | "nbformat": 4, 75 | "nbformat_minor": 4 76 | } 77 | -------------------------------------------------------------------------------- /3 Conditionals and Loops/Fahrenheit to Celsius.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "start = int(input())\n", 10 | "end = int(input())\n", 11 | "step = int(input())\n", 12 | "\n", 13 | "curr_temp = start\n", 14 | "\n", 15 | "while curr_temp <= end:\n", 16 | " c = 5/9 * (curr_temp-32)\n", 17 | " print(curr_temp, \" \", int(c))\n", 18 | " curr_temp = curr_temp+step" 19 | ] 20 | } 21 | ], 22 | "metadata": { 23 | "kernelspec": { 24 | "display_name": "Python 3", 25 | "language": "python", 26 | "name": "python3" 27 | }, 28 | "language_info": { 29 | "codemirror_mode": { 30 | "name": "ipython", 31 | "version": 3 32 | }, 33 | "file_extension": ".py", 34 | "mimetype": "text/x-python", 35 | "name": "python", 36 | "nbconvert_exporter": "python", 37 | "pygments_lexer": "ipython3", 38 | "version": "3.7.6" 39 | } 40 | }, 41 | "nbformat": 4, 42 | "nbformat_minor": 4 43 | } 44 | -------------------------------------------------------------------------------- /3 Conditionals and Loops/Nth Fibonacci Number.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "def Fibonacci(n): \n", 10 | " if n<0: \n", 11 | " print(\"Incorrect input\") \n", 12 | " elif n==0: \n", 13 | " return 0\n", 14 | " elif n==1: \n", 15 | " return 1\n", 16 | " else: \n", 17 | " return Fibonacci(n-1)+Fibonacci(n-2) \n", 18 | " \n", 19 | "n=int(input())\n", 20 | "print(Fibonacci(n)) " 21 | ] 22 | } 23 | ], 24 | "metadata": { 25 | "kernelspec": { 26 | "display_name": "Python 3", 27 | "language": "python", 28 | "name": "python3" 29 | }, 30 | "language_info": { 31 | "codemirror_mode": { 32 | "name": "ipython", 33 | "version": 3 34 | }, 35 | "file_extension": ".py", 36 | "mimetype": "text/x-python", 37 | "name": "python", 38 | "nbconvert_exporter": "python", 39 | "pygments_lexer": "ipython3", 40 | "version": "3.7.6" 41 | } 42 | }, 43 | "nbformat": 4, 44 | "nbformat_minor": 4 45 | } 46 | -------------------------------------------------------------------------------- /3 Conditionals and Loops/Palindrome Number.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "number = int(input())\n", 10 | "\n", 11 | "reverse = 0\n", 12 | "temp = number\n", 13 | "\n", 14 | "while(temp > 0):\n", 15 | " Reminder = temp % 10\n", 16 | " reverse = (reverse * 10) + Reminder\n", 17 | " temp = temp //10\n", 18 | " \n", 19 | "\n", 20 | "if(number == reverse):\n", 21 | " print(\"true\")\n", 22 | "else:\n", 23 | " print(\"false\")" 24 | ] 25 | } 26 | ], 27 | "metadata": { 28 | "kernelspec": { 29 | "display_name": "Python 3", 30 | "language": "python", 31 | "name": "python3" 32 | }, 33 | "language_info": { 34 | "codemirror_mode": { 35 | "name": "ipython", 36 | "version": 3 37 | }, 38 | "file_extension": ".py", 39 | "mimetype": "text/x-python", 40 | "name": "python", 41 | "nbconvert_exporter": "python", 42 | "pygments_lexer": "ipython3", 43 | "version": "3.7.6" 44 | } 45 | }, 46 | "nbformat": 4, 47 | "nbformat_minor": 4 48 | } 49 | -------------------------------------------------------------------------------- /3 Conditionals and Loops/Reverse of a Number.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 | "rev=0\n", 11 | "while(n>0):\n", 12 | " dig=n%10\n", 13 | " rev=rev*10+dig\n", 14 | " n=n//10\n", 15 | "print(rev)" 16 | ] 17 | } 18 | ], 19 | "metadata": { 20 | "kernelspec": { 21 | "display_name": "Python 3", 22 | "language": "python", 23 | "name": "python3" 24 | }, 25 | "language_info": { 26 | "codemirror_mode": { 27 | "name": "ipython", 28 | "version": 3 29 | }, 30 | "file_extension": ".py", 31 | "mimetype": "text/x-python", 32 | "name": "python", 33 | "nbconvert_exporter": "python", 34 | "pygments_lexer": "ipython3", 35 | "version": "3.7.6" 36 | } 37 | }, 38 | "nbformat": 4, 39 | "nbformat_minor": 4 40 | } 41 | -------------------------------------------------------------------------------- /3 Conditionals and Loops/Sum of Even & Odd.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "Number=int(input())\n", 10 | "even=0\n", 11 | "odd=0\n", 12 | "while(Number>0):\n", 13 | " Reminder = Number %10\n", 14 | " if(Reminder % 2 == 0):\n", 15 | " even=even + Reminder\n", 16 | " else:\n", 17 | " odd= odd + Reminder\n", 18 | " Number = Number //10\n", 19 | "print(even,\"\",odd)\n" 20 | ] 21 | } 22 | ], 23 | "metadata": { 24 | "kernelspec": { 25 | "display_name": "Python 3", 26 | "language": "python", 27 | "name": "python3" 28 | }, 29 | "language_info": { 30 | "codemirror_mode": { 31 | "name": "ipython", 32 | "version": 3 33 | }, 34 | "file_extension": ".py", 35 | "mimetype": "text/x-python", 36 | "name": "python", 37 | "nbconvert_exporter": "python", 38 | "pygments_lexer": "ipython3", 39 | "version": "3.7.6" 40 | } 41 | }, 42 | "nbformat": 4, 43 | "nbformat_minor": 4 44 | } 45 | -------------------------------------------------------------------------------- /3 Conditionals and Loops/Sum of Even Numbers.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "## Read input as specified in the question.\n", 10 | "## Print output as specified in the question.\n", 11 | "n=int(input())\n", 12 | "sum=0\n", 13 | "for i in range(n,0,-1):\n", 14 | " if(i%2==0):\n", 15 | " sum=sum+i\n", 16 | "print(sum)" 17 | ] 18 | } 19 | ], 20 | "metadata": { 21 | "kernelspec": { 22 | "display_name": "Python 3", 23 | "language": "python", 24 | "name": "python3" 25 | }, 26 | "language_info": { 27 | "codemirror_mode": { 28 | "name": "ipython", 29 | "version": 3 30 | }, 31 | "file_extension": ".py", 32 | "mimetype": "text/x-python", 33 | "name": "python", 34 | "nbconvert_exporter": "python", 35 | "pygments_lexer": "ipython3", 36 | "version": "3.7.6" 37 | } 38 | }, 39 | "nbformat": 4, 40 | "nbformat_minor": 4 41 | } 42 | -------------------------------------------------------------------------------- /3 Conditionals and Loops/Sum of n Number.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 | "sum=0\n", 11 | "for i in range(n,0,-1):\n", 12 | " sum=sum+i\n", 13 | "print(sum)\n" 14 | ] 15 | } 16 | ], 17 | "metadata": { 18 | "kernelspec": { 19 | "display_name": "Python 3", 20 | "language": "python", 21 | "name": "python3" 22 | }, 23 | "language_info": { 24 | "codemirror_mode": { 25 | "name": "ipython", 26 | "version": 3 27 | }, 28 | "file_extension": ".py", 29 | "mimetype": "text/x-python", 30 | "name": "python", 31 | "nbconvert_exporter": "python", 32 | "pygments_lexer": "ipython3", 33 | "version": "3.7.6" 34 | } 35 | }, 36 | "nbformat": 4, 37 | "nbformat_minor": 4 38 | } 39 | -------------------------------------------------------------------------------- /4 Patterns 1/Alpha Pattern.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 | "5\n", 13 | "A\n", 14 | "BB\n", 15 | "CCC\n", 16 | "DDDD\n", 17 | "EEEEE\n" 18 | ] 19 | } 20 | ], 21 | "source": [ 22 | "n=int(input())\n", 23 | "for i in range(65,65 + n):\n", 24 | " for j in range(65,i+1):\n", 25 | " print(chr(i),end='')\n", 26 | " print(\"\")" 27 | ] 28 | } 29 | ], 30 | "metadata": { 31 | "kernelspec": { 32 | "display_name": "Python 3", 33 | "language": "python", 34 | "name": "python3" 35 | }, 36 | "language_info": { 37 | "codemirror_mode": { 38 | "name": "ipython", 39 | "version": 3 40 | }, 41 | "file_extension": ".py", 42 | "mimetype": "text/x-python", 43 | "name": "python", 44 | "nbconvert_exporter": "python", 45 | "pygments_lexer": "ipython3", 46 | "version": "3.7.6" 47 | } 48 | }, 49 | "nbformat": 4, 50 | "nbformat_minor": 4 51 | } 52 | -------------------------------------------------------------------------------- /4 Patterns 1/Character Pattern.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 | "A\n", 14 | "BC\n", 15 | "CDE\n", 16 | "DEFG\n", 17 | "EFGHI\n" 18 | ] 19 | } 20 | ], 21 | "source": [ 22 | "n=int(input())\n", 23 | "currRow=1\n", 24 | "while currRow <=n:\n", 25 | " currCol=1\n", 26 | " ch=ord('A') + currRow-1\n", 27 | " while currCol <= currRow:\n", 28 | " print(chr(ch + currCol - 1),end=\"\")\n", 29 | " currCol +=1\n", 30 | " print()\n", 31 | " currRow+=1" 32 | ] 33 | } 34 | ], 35 | "metadata": { 36 | "kernelspec": { 37 | "display_name": "Python 3", 38 | "language": "python", 39 | "name": "python3" 40 | }, 41 | "language_info": { 42 | "codemirror_mode": { 43 | "name": "ipython", 44 | "version": 3 45 | }, 46 | "file_extension": ".py", 47 | "mimetype": "text/x-python", 48 | "name": "python", 49 | "nbconvert_exporter": "python", 50 | "pygments_lexer": "ipython3", 51 | "version": "3.7.6" 52 | } 53 | }, 54 | "nbformat": 4, 55 | "nbformat_minor": 4 56 | } 57 | -------------------------------------------------------------------------------- /4 Patterns 1/Interesting Alphabets.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 | "E\n", 14 | "DE\n", 15 | "CDE\n", 16 | "BCDE\n", 17 | "ABCDE\n" 18 | ] 19 | } 20 | ], 21 | "source": [ 22 | "n=int(input())\n", 23 | "for i in range(n,0,-1):\n", 24 | " for j in range(i,n+1):\n", 25 | " print(chr(j+64),end=\"\")\n", 26 | " print(\"\")" 27 | ] 28 | } 29 | ], 30 | "metadata": { 31 | "kernelspec": { 32 | "display_name": "Python 3", 33 | "language": "python", 34 | "name": "python3" 35 | }, 36 | "language_info": { 37 | "codemirror_mode": { 38 | "name": "ipython", 39 | "version": 3 40 | }, 41 | "file_extension": ".py", 42 | "mimetype": "text/x-python", 43 | "name": "python", 44 | "nbconvert_exporter": "python", 45 | "pygments_lexer": "ipython3", 46 | "version": "3.7.6" 47 | } 48 | }, 49 | "nbformat": 4, 50 | "nbformat_minor": 4 51 | } 52 | -------------------------------------------------------------------------------- /4 Patterns 1/Number Pattern 1.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 | "for i in range(0,n):\n", 11 | " for j in range(0,i+1):\n", 12 | " print(1,end=\"\")\n", 13 | " print(\"\")" 14 | ] 15 | } 16 | ], 17 | "metadata": { 18 | "kernelspec": { 19 | "display_name": "Python 3", 20 | "language": "python", 21 | "name": "python3" 22 | }, 23 | "language_info": { 24 | "codemirror_mode": { 25 | "name": "ipython", 26 | "version": 3 27 | }, 28 | "file_extension": ".py", 29 | "mimetype": "text/x-python", 30 | "name": "python", 31 | "nbconvert_exporter": "python", 32 | "pygments_lexer": "ipython3", 33 | "version": "3.7.6" 34 | } 35 | }, 36 | "nbformat": 4, 37 | "nbformat_minor": 4 38 | } 39 | -------------------------------------------------------------------------------- /4 Patterns 1/Number Pattern 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 | "5\n", 13 | "1\n", 14 | "11\n", 15 | "202\n", 16 | "3003\n", 17 | "40004\n" 18 | ] 19 | } 20 | ], 21 | "source": [ 22 | "n=int(input())\n", 23 | "print(1)\n", 24 | "i=1\n", 25 | "while(i= 1 :\n", 42 | " spaces = 1\n", 43 | " while spaces <= (secondHalf - currRow + 1) :\n", 44 | " print(\" \", end = \"\") \n", 45 | " spaces += 1\n", 46 | " currCol = 1 \n", 47 | " while currCol <= (2 * currRow) - 1 :\n", 48 | " print(\"*\", end = \"\") \n", 49 | " currCol += 1\n", 50 | " \n", 51 | " print()\n", 52 | " currRow -= 1" 53 | ] 54 | } 55 | ], 56 | "metadata": { 57 | "kernelspec": { 58 | "display_name": "Python 3", 59 | "language": "python", 60 | "name": "python3" 61 | }, 62 | "language_info": { 63 | "codemirror_mode": { 64 | "name": "ipython", 65 | "version": 3 66 | }, 67 | "file_extension": ".py", 68 | "mimetype": "text/x-python", 69 | "name": "python", 70 | "nbconvert_exporter": "python", 71 | "pygments_lexer": "ipython3", 72 | "version": "3.7.6" 73 | } 74 | }, 75 | "nbformat": 4, 76 | "nbformat_minor": 4 77 | } 78 | -------------------------------------------------------------------------------- /5 Patterns 2/Inverted Number Pattern.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 | "55555\n", 14 | "4444\n", 15 | "333\n", 16 | "22\n", 17 | "1\n" 18 | ] 19 | } 20 | ], 21 | "source": [ 22 | "n=int(input())\n", 23 | "for i in range(n,0,-1):\n", 24 | " for j in range(0,i):\n", 25 | " print(i,end='')\n", 26 | " print(\"\")" 27 | ] 28 | } 29 | ], 30 | "metadata": { 31 | "kernelspec": { 32 | "display_name": "Python 3", 33 | "language": "python", 34 | "name": "python3" 35 | }, 36 | "language_info": { 37 | "codemirror_mode": { 38 | "name": "ipython", 39 | "version": 3 40 | }, 41 | "file_extension": ".py", 42 | "mimetype": "text/x-python", 43 | "name": "python", 44 | "nbconvert_exporter": "python", 45 | "pygments_lexer": "ipython3", 46 | "version": "3.7.6" 47 | } 48 | }, 49 | "nbformat": 4, 50 | "nbformat_minor": 4 51 | } 52 | -------------------------------------------------------------------------------- /5 Patterns 2/Mirror Number Pattern.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "5\n", 13 | " 1\n", 14 | " 12\n", 15 | " 123\n", 16 | " 1234\n", 17 | "12345\n" 18 | ] 19 | } 20 | ], 21 | "source": [ 22 | "n=int(input())\n", 23 | "for x in range(1,n+1):\n", 24 | " for y in range(n,x,-1):\n", 25 | " print(\" \",end=\"\")\n", 26 | " for z in range(1,x+1):\n", 27 | " print(z,end=\"\")\n", 28 | " print(\"\")\n", 29 | " " 30 | ] 31 | } 32 | ], 33 | "metadata": { 34 | "kernelspec": { 35 | "display_name": "Python 3", 36 | "language": "python", 37 | "name": "python3" 38 | }, 39 | "language_info": { 40 | "codemirror_mode": { 41 | "name": "ipython", 42 | "version": 3 43 | }, 44 | "file_extension": ".py", 45 | "mimetype": "text/x-python", 46 | "name": "python", 47 | "nbconvert_exporter": "python", 48 | "pygments_lexer": "ipython3", 49 | "version": "3.7.6" 50 | } 51 | }, 52 | "nbformat": 4, 53 | "nbformat_minor": 4 54 | } 55 | -------------------------------------------------------------------------------- /5 Patterns 2/Number Pattern.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 | "5\n", 13 | "1 1\n", 14 | "12 21\n", 15 | "123 321\n", 16 | "1234 4321\n", 17 | "1234554321\n" 18 | ] 19 | } 20 | ], 21 | "source": [ 22 | "a=int(input())\n", 23 | "\n", 24 | "for k in range(1,a+1):\n", 25 | " b='1'\n", 26 | " for i in range(2,a+1):\n", 27 | " c=str(i)\n", 28 | " if k >= i:\n", 29 | " b=b+c\n", 30 | " else:\n", 31 | " b=b+' '\n", 32 | " for j in range(a,0,-1):\n", 33 | " d=str(j)\n", 34 | " if k >= j:\n", 35 | " b=b+d\n", 36 | " else:\n", 37 | " b=b+' '\n", 38 | " print(b)" 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.6" 59 | } 60 | }, 61 | "nbformat": 4, 62 | "nbformat_minor": 4 63 | } 64 | -------------------------------------------------------------------------------- /5 Patterns 2/Pyramid Number Pattern.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "5\n", 13 | " 1\n", 14 | " 212\n", 15 | " 32123\n", 16 | " 4321234\n", 17 | "543212345\n" 18 | ] 19 | } 20 | ], 21 | "source": [ 22 | "num=int(input())\n", 23 | "for i in range(1,num+1):\n", 24 | " for j in range(1,num-i+1):\n", 25 | " print(end=\" \")\n", 26 | " for j in range(i,0,-1):\n", 27 | " print(j,end=\"\")\n", 28 | " for j in range(2,i+1):\n", 29 | " print(j,end=\"\")\n", 30 | " print() \n" 31 | ] 32 | } 33 | ], 34 | "metadata": { 35 | "kernelspec": { 36 | "display_name": "Python 3", 37 | "language": "python", 38 | "name": "python3" 39 | }, 40 | "language_info": { 41 | "codemirror_mode": { 42 | "name": "ipython", 43 | "version": 3 44 | }, 45 | "file_extension": ".py", 46 | "mimetype": "text/x-python", 47 | "name": "python", 48 | "nbconvert_exporter": "python", 49 | "pygments_lexer": "ipython3", 50 | "version": "3.7.6" 51 | } 52 | }, 53 | "nbformat": 4, 54 | "nbformat_minor": 4 55 | } 56 | -------------------------------------------------------------------------------- /5 Patterns 2/Star Pattern.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 | " * \n", 14 | " *** \n", 15 | " ***** \n", 16 | " ******* \n", 17 | "*********\n" 18 | ] 19 | } 20 | ], 21 | "source": [ 22 | "side=int(input())\n", 23 | "c='*'\n", 24 | "for i in range(side):\n", 25 | " print((c*i).rjust(side-1)+c+(c*i).ljust(side-1))" 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.6" 46 | } 47 | }, 48 | "nbformat": 4, 49 | "nbformat_minor": 4 50 | } 51 | -------------------------------------------------------------------------------- /5 Patterns 2/Triangle of Number.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "5\n", 13 | " 1\n", 14 | " 232\n", 15 | " 34543\n", 16 | " 4567654\n", 17 | "567898765\n" 18 | ] 19 | } 20 | ], 21 | "source": [ 22 | "\n", 23 | "n = int(input())\n", 24 | "currRow = 1\n", 25 | "while currRow <= n:\n", 26 | " spaces = 1\n", 27 | " while spaces <= (n - currRow) :\n", 28 | " print(\" \", end = \"\")\n", 29 | " spaces += 1\n", 30 | " currCol = 1\n", 31 | " valToPrint = currRow\n", 32 | " while currCol <= currRow :\n", 33 | " print(valToPrint, end = \"\")\n", 34 | " valToPrint += 1\n", 35 | " currCol += 1\n", 36 | " currCol = 1\n", 37 | " valToPrint = 2 * currRow - 2 \n", 38 | " while currCol <= currRow - 1 :\n", 39 | " print(valToPrint, end = \"\")\n", 40 | " valToPrint -= 1\n", 41 | " currCol += 1 \n", 42 | " print()\n", 43 | " currRow += 1\n" 44 | ] 45 | } 46 | ], 47 | "metadata": { 48 | "kernelspec": { 49 | "display_name": "Python 3", 50 | "language": "python", 51 | "name": "python3" 52 | }, 53 | "language_info": { 54 | "codemirror_mode": { 55 | "name": "ipython", 56 | "version": 3 57 | }, 58 | "file_extension": ".py", 59 | "mimetype": "text/x-python", 60 | "name": "python", 61 | "nbconvert_exporter": "python", 62 | "pygments_lexer": "ipython3", 63 | "version": "3.7.6" 64 | } 65 | }, 66 | "nbformat": 4, 67 | "nbformat_minor": 4 68 | } 69 | -------------------------------------------------------------------------------- /5 Patterns 2/Zeros and Star Pattern.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 | "*0000*0000*\n", 14 | "0*000*000*0\n", 15 | "00*00*00*00\n", 16 | "000*0*0*000\n", 17 | "0000***0000\n" 18 | ] 19 | } 20 | ], 21 | "source": [ 22 | "lines=int(input()) \n", 23 | "i=1 \n", 24 | "j=1 \n", 25 | "while i<=lines: \n", 26 | " j=1 \n", 27 | " while j<=lines: \n", 28 | " if i==j: \n", 29 | " print(\"*\", end='', flush=True) \n", 30 | " else : \n", 31 | " print(\"0\", end='', flush=True) \n", 32 | " j=j+1 \n", 33 | " j=j-1; \n", 34 | " print(\"*\", end='', flush=True) \n", 35 | " while j>=1: \n", 36 | " if i==j: \n", 37 | " print(\"*\", end='', flush=True) \n", 38 | " else : \n", 39 | " print(\"0\", end='', flush=True) \n", 40 | " j=j-1 \n", 41 | " print(\"\"); \n", 42 | " i=i+1 " 43 | ] 44 | } 45 | ], 46 | "metadata": { 47 | "kernelspec": { 48 | "display_name": "Python 3", 49 | "language": "python", 50 | "name": "python3" 51 | }, 52 | "language_info": { 53 | "codemirror_mode": { 54 | "name": "ipython", 55 | "version": 3 56 | }, 57 | "file_extension": ".py", 58 | "mimetype": "text/x-python", 59 | "name": "python", 60 | "nbconvert_exporter": "python", 61 | "pygments_lexer": "ipython3", 62 | "version": "3.7.6" 63 | } 64 | }, 65 | "nbformat": 4, 66 | "nbformat_minor": 4 67 | } 68 | -------------------------------------------------------------------------------- /6 More on Loops/Binary Pattern.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 | "11111\n", 14 | "0000\n", 15 | "111\n", 16 | "00\n", 17 | "1\n" 18 | ] 19 | } 20 | ], 21 | "source": [ 22 | "n=int(input())\n", 23 | "for i in range(0,n):\n", 24 | " for j in range(n-i,0,-1):\n", 25 | " if(i%2==0):\n", 26 | " print(1,end=\"\")\n", 27 | " else:\n", 28 | " print(0,end=\"\")\n", 29 | " print()" 30 | ] 31 | } 32 | ], 33 | "metadata": { 34 | "kernelspec": { 35 | "display_name": "Python 3", 36 | "language": "python", 37 | "name": "python3" 38 | }, 39 | "language_info": { 40 | "codemirror_mode": { 41 | "name": "ipython", 42 | "version": 3 43 | }, 44 | "file_extension": ".py", 45 | "mimetype": "text/x-python", 46 | "name": "python", 47 | "nbconvert_exporter": "python", 48 | "pygments_lexer": "ipython3", 49 | "version": "3.7.6" 50 | } 51 | }, 52 | "nbformat": 4, 53 | "nbformat_minor": 4 54 | } 55 | -------------------------------------------------------------------------------- /6 More on Loops/Diamond of Stars.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 | "n1=(n//2)+1\n", 11 | "for i in range(1,n1+1):\n", 12 | " for j in range(1,n1-i+1):\n", 13 | " print(\" \",end=\"\")\n", 14 | " for j in range(1,2*i):\n", 15 | " print(\"*\",end=\"\")\n", 16 | " print()\n", 17 | "n2=n-n1\n", 18 | "for i in range(n2,0,-1):\n", 19 | " for j in range(1,n1-i+1):\n", 20 | " print(\" \",end=\"\")\n", 21 | " for j in range(1,2*i):\n", 22 | " print(\"*\",end=\"\")\n", 23 | " print()" 24 | ] 25 | } 26 | ], 27 | "metadata": { 28 | "kernelspec": { 29 | "display_name": "Python 3", 30 | "language": "python", 31 | "name": "python3" 32 | }, 33 | "language_info": { 34 | "codemirror_mode": { 35 | "name": "ipython", 36 | "version": 3 37 | }, 38 | "file_extension": ".py", 39 | "mimetype": "text/x-python", 40 | "name": "python", 41 | "nbconvert_exporter": "python", 42 | "pygments_lexer": "ipython3", 43 | "version": "3.7.6" 44 | } 45 | }, 46 | "nbformat": 4, 47 | "nbformat_minor": 4 48 | } 49 | -------------------------------------------------------------------------------- /6 More on Loops/Print Number Pyramid.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 | "5\n", 13 | "12345\n", 14 | " 2345\n", 15 | " 345\n", 16 | " 45\n", 17 | " 5\n", 18 | " 45\n", 19 | " 345\n", 20 | " 2345\n", 21 | "12345\n" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "n = int(input())\n", 27 | "for i in range(1,n+1):\n", 28 | " count = 1\n", 29 | " for j in range(1,i):\n", 30 | " print(\" \",end=\"\")\n", 31 | " count = count + 1\n", 32 | " num = i \n", 33 | " for j in range(count,n+1):\n", 34 | " print(num,end=\"\") \n", 35 | " num = num + 1\n", 36 | " print()\n", 37 | "for i in range(n-1,0,-1):\n", 38 | " count = 1\n", 39 | " for j in range(1,i):\n", 40 | " print(\" \",end=\"\") \n", 41 | " count = count + 1\n", 42 | " num = i \n", 43 | " for j in range(count,n+1): \n", 44 | " print(num,end=\"\")\n", 45 | " num = num + 1 \n", 46 | " print()" 47 | ] 48 | } 49 | ], 50 | "metadata": { 51 | "kernelspec": { 52 | "display_name": "Python 3", 53 | "language": "python", 54 | "name": "python3" 55 | }, 56 | "language_info": { 57 | "codemirror_mode": { 58 | "name": "ipython", 59 | "version": 3 60 | }, 61 | "file_extension": ".py", 62 | "mimetype": "text/x-python", 63 | "name": "python", 64 | "nbconvert_exporter": "python", 65 | "pygments_lexer": "ipython3", 66 | "version": "3.7.6" 67 | } 68 | }, 69 | "nbformat": 4, 70 | "nbformat_minor": 4 71 | } 72 | -------------------------------------------------------------------------------- /6 More on Loops/Print the Pattern.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 | "5\n", 13 | "1 2 3 4 5 \n", 14 | "11 12 13 14 15 \n", 15 | "21 22 23 24 25 \n", 16 | "16 17 18 19 20 \n", 17 | "6 7 8 9 10 \n" 18 | ] 19 | } 20 | ], 21 | "source": [ 22 | "n = int(input()) \n", 23 | "startValue = 1\n", 24 | "for i in range(1,n+1): \n", 25 | " for j in range(startValue,startValue + n):\n", 26 | " print(j,end=\" \")\n", 27 | " print()\n", 28 | " if(i==((n+1)//2)):\n", 29 | " if((n%2)!=0):\n", 30 | " startValue = n*(n-2)+1\n", 31 | " else:\n", 32 | " startValue = n*(n-1) + 1\n", 33 | " elif((i>(n+1)//2)):\n", 34 | " startValue = startValue - 2*n\n", 35 | " else:\n", 36 | " startValue = startValue + 2*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.6" 57 | } 58 | }, 59 | "nbformat": 4, 60 | "nbformat_minor": 4 61 | } 62 | -------------------------------------------------------------------------------- /6 More on Loops/Rectangular Number.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 | "5\n", 13 | "555555555\n", 14 | "544444445\n", 15 | "543333345\n", 16 | "543222345\n", 17 | "543212345\n", 18 | "543222345\n", 19 | "543333345\n", 20 | "544444445\n", 21 | "555555555\n" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "\n", 27 | "n = int(input())\n", 28 | "for i in range(1,n+1):\n", 29 | " temp = n\n", 30 | " for j in range(1,i):\n", 31 | " print(temp,end=\"\")\n", 32 | " temp = temp -1\n", 33 | " for j in range(1,(2*n) - (2*i) + 2):\n", 34 | " print(n-i+1,end=\"\")\n", 35 | " for j in range(1,i):\n", 36 | " temp = temp+1\n", 37 | " print(temp,end=\"\")\n", 38 | " print()\n", 39 | "for i in range(n-1,0,-1):\n", 40 | " temp = n\n", 41 | " for j in range(1,i):\n", 42 | " print(temp,end=\"\")\n", 43 | " temp = temp - 1 \n", 44 | " for j in range(1,(2*n) - (2*i) + 2):\n", 45 | " print(n-i+1,end=\"\")\n", 46 | " for j in range(1,i):\n", 47 | " temp = temp+1\n", 48 | " print(temp,end=\"\")\n", 49 | " print()\n", 50 | "\n" 51 | ] 52 | } 53 | ], 54 | "metadata": { 55 | "kernelspec": { 56 | "display_name": "Python 3", 57 | "language": "python", 58 | "name": "python3" 59 | }, 60 | "language_info": { 61 | "codemirror_mode": { 62 | "name": "ipython", 63 | "version": 3 64 | }, 65 | "file_extension": ".py", 66 | "mimetype": "text/x-python", 67 | "name": "python", 68 | "nbconvert_exporter": "python", 69 | "pygments_lexer": "ipython3", 70 | "version": "3.7.6" 71 | } 72 | }, 73 | "nbformat": 4, 74 | "nbformat_minor": 4 75 | } 76 | -------------------------------------------------------------------------------- /7 Functions/Check Amstrong.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 | "103\n", 13 | "false\n" 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "num = int(input())\n", 19 | 20 | "copy = num\n", 21 | "n=0\n", 22 | "while(copy>0):\n", 23 | " n=n+1 # counting number of digits\n", 24 | " copy=copy//10\n", 25 | "\n", 26 | 27 | "sum = 0\n", 28 | "temp = num\n", 29 | "while temp > 0:\n", 30 | " digit = temp % 10\n", 31 | " sum += digit ** n #number of digits in power\n", 32 | " temp //= 10\n", 33 | "if num == sum:\n", 34 | " print(\"true\")\n", 35 | "else:\n", 36 | " print(\"false\")" 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.6" 57 | } 58 | }, 59 | "nbformat": 4, 60 | "nbformat_minor": 4 61 | } 62 | -------------------------------------------------------------------------------- /7 Functions/Check Armstrong function.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 9, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "True\n" 13 | ] 14 | } 15 | ], 16 | "source": [ 17 | "def checkArmstrong(n):\n", 18 | " arr = [int(x) for x in str(n)]\n", 19 | " p = len(arr)\n", 20 | " sum = 0\n", 21 | " for digit in arr:\n", 22 | " sum += digit**p\n", 23 | " return sum\n", 24 | "\n", 25 | "n = int(input())\n", 26 | "print(n==checkArmstrong(n))" 27 | ] 28 | } 29 | ], 30 | "metadata": { 31 | "kernelspec": { 32 | "display_name": "Python 3.10.4 64-bit", 33 | "language": "python", 34 | "name": "python3" 35 | }, 36 | "language_info": { 37 | "codemirror_mode": { 38 | "name": "ipython", 39 | "version": 3 40 | }, 41 | "file_extension": ".py", 42 | "mimetype": "text/x-python", 43 | "name": "python", 44 | "nbconvert_exporter": "python", 45 | "pygments_lexer": "ipython3", 46 | "version": "3.10.4" 47 | }, 48 | "orig_nbformat": 4, 49 | "vscode": { 50 | "interpreter": { 51 | "hash": "369f2c481f4da34e4445cda3fffd2e751bd1c4d706f27375911949ba6bb62e1c" 52 | } 53 | } 54 | }, 55 | "nbformat": 4, 56 | "nbformat_minor": 2 57 | } 58 | -------------------------------------------------------------------------------- /7 Functions/Fahrenheit to Celsius Function.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 | "0 -17\n", 13 | "20 -6\n", 14 | "40 4\n", 15 | "60 15\n", 16 | "80 26\n", 17 | "100 37\n" 18 | ] 19 | } 20 | ], 21 | "source": [ 22 | "def printTable(start,end,step):\n", 23 | " for curr_temp in range(start, end+1, step):\n", 24 | " c = 5/9 * (curr_temp-32)\n", 25 | " print(curr_temp, \" \", int(c))\n", 26 | " \n", 27 | "s = int(input())\n", 28 | "e = int(input())\n", 29 | "step = int(input())\n", 30 | "printTable(s,e,step)" 31 | ] 32 | } 33 | ], 34 | "metadata": { 35 | "kernelspec": { 36 | "display_name": "Python 3.10.4 64-bit", 37 | "language": "python", 38 | "name": "python3" 39 | }, 40 | "language_info": { 41 | "codemirror_mode": { 42 | "name": "ipython", 43 | "version": 3 44 | }, 45 | "file_extension": ".py", 46 | "mimetype": "text/x-python", 47 | "name": "python", 48 | "nbconvert_exporter": "python", 49 | "pygments_lexer": "ipython3", 50 | "version": "3.10.4" 51 | }, 52 | "vscode": { 53 | "interpreter": { 54 | "hash": "369f2c481f4da34e4445cda3fffd2e751bd1c4d706f27375911949ba6bb62e1c" 55 | } 56 | } 57 | }, 58 | "nbformat": 4, 59 | "nbformat_minor": 4 60 | } 61 | -------------------------------------------------------------------------------- /7 Functions/Fibonacci Member alt.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 18, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "true\n" 13 | ] 14 | } 15 | ], 16 | "source": [ 17 | "def checkFib(n):\n", 18 | " if n==1 or n==0:\n", 19 | " return True\n", 20 | " a = 1\n", 21 | " b = 1\n", 22 | " while a 0 : 20 | arr1, n = takeInput() 21 | arr2, m = takeInput() 22 | intersections(arr1, n, arr2, m) 23 | print() 24 | t -= 1 25 | -------------------------------------------------------------------------------- /8 Arrays and Lists/Find Duplicate.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int duplicateNumber(int *arr, int size) 3 | { 4 | for (int i = 0; i < size - 1; ++i) 5 | { 6 | for (int j = i + 1; j < size; ++j) 7 | { 8 | if (arr[i] == arr[j]) 9 | { 10 | return arr[i]; 11 | } 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /8 Arrays and Lists/Find Duplicate.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 4, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "1\n", 13 | "9\n", 14 | "0 7 2 5 4 7 1 3 6\n", 15 | "7\n" 16 | ] 17 | } 18 | ], 19 | "source": [ 20 | "import sys\n", 21 | "def duplicateNumber(arr, size) :\n", 22 | " for i in range(size - 1) :\n", 23 | " for j in range((i + 1), size) :\n", 24 | " if arr[i] == arr[j] :\n", 25 | " return arr[i] \n", 26 | "def takeInput() :\n", 27 | " n = int(input()) \n", 28 | " if n == 0 :\n", 29 | " return list(), 0\n", 30 | " arr = list(map(int, input().strip().split())) \n", 31 | " return arr, n\n", 32 | "t = int(input()) \n", 33 | "while t > 0 :\n", 34 | " arr, n = takeInput()\n", 35 | " print(duplicateNumber(arr, n))\n", 36 | " \n", 37 | " t -= 1" 38 | ] 39 | } 40 | ], 41 | "metadata": { 42 | "kernelspec": { 43 | "display_name": "Python 3", 44 | "language": "python", 45 | "name": "python3" 46 | }, 47 | "language_info": { 48 | "codemirror_mode": { 49 | "name": "ipython", 50 | "version": 3 51 | }, 52 | "file_extension": ".py", 53 | "mimetype": "text/x-python", 54 | "name": "python", 55 | "nbconvert_exporter": "python", 56 | "pygments_lexer": "ipython3", 57 | "version": "3.7.6" 58 | } 59 | }, 60 | "nbformat": 4, 61 | "nbformat_minor": 4 62 | } 63 | -------------------------------------------------------------------------------- /8 Arrays and Lists/Find Unique.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int findUnique(int *arr, int size) 3 | { for (int i = 0; i < size; ++i) 4 | { 5 | int j = 0; 6 | for (; j < size; j++) 7 | { 8 | if (i != j && arr[i] == arr[j]) 9 | { 10 | break; 11 | } 12 | } 13 | if (j == size) 14 | { 15 | return arr[i]; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /8 Arrays and Lists/Find Unique.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 7, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "1\n", 13 | "7\n", 14 | "2 3 1 6 3 6 2\n", 15 | "1\n" 16 | ] 17 | } 18 | ], 19 | "source": [ 20 | "import sys\n", 21 | "def findUnique(arr, n) :\n", 22 | " for i in range(n) :\n", 23 | " j = 0\n", 24 | " while j < n :\n", 25 | " if i != j :\n", 26 | " if arr[i] == arr[j] :\n", 27 | " break\n", 28 | " j += 1\n", 29 | " if j == n : \n", 30 | " return arr[i]\n", 31 | "def takeInput() :\n", 32 | " n = int(input())\n", 33 | " if n == 0 :\n", 34 | " return list(), 0\n", 35 | " arr = list(map(int, input().rstrip().split(\" \"))) \n", 36 | " return arr, n\n", 37 | "t = int(input()) \n", 38 | "while t > 0 : \n", 39 | " arr, n = takeInput()\n", 40 | " print(findUnique(arr, n)) \n", 41 | " t -= 1" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 6, 47 | "metadata": {}, 48 | "outputs": [ 49 | { 50 | "name": "stdout", 51 | "output_type": "stream", 52 | "text": [ 53 | "1\n", 54 | "1 1 2 2 3 3 5\n", 55 | "5\n" 56 | ] 57 | } 58 | ], 59 | "source": [ 60 | "\n", 61 | "def findSingle( ar, n): \n", 62 | " \n", 63 | " res = ar[0] \n", 64 | " for i in range(1,n): \n", 65 | " res = res ^ ar[i] \n", 66 | " \n", 67 | " return res \n", 68 | " \n", 69 | "n=int(input())\n", 70 | "ar = [int(x) for x in input().split()] \n", 71 | "print(findSingle(ar, len(ar))) " 72 | ] 73 | } 74 | ], 75 | "metadata": { 76 | "kernelspec": { 77 | "display_name": "Python 3", 78 | "language": "python", 79 | "name": "python3" 80 | }, 81 | "language_info": { 82 | "codemirror_mode": { 83 | "name": "ipython", 84 | "version": 3 85 | }, 86 | "file_extension": ".py", 87 | "mimetype": "text/x-python", 88 | "name": "python", 89 | "nbconvert_exporter": "python", 90 | "pygments_lexer": "ipython3", 91 | "version": "3.7.6" 92 | } 93 | }, 94 | "nbformat": 4, 95 | "nbformat_minor": 4 96 | } 97 | -------------------------------------------------------------------------------- /8 Arrays and Lists/Pair Sum.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "1\n", 13 | "9\n", 14 | "1 3 6 2 5 4 3 2 4\n" 15 | ] 16 | }, 17 | { 18 | "ename": "ValueError", 19 | "evalue": "invalid literal for int() with base 10: '1 3 6 2 5 4 3 2 4'", 20 | "output_type": "error", 21 | "traceback": [ 22 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 23 | "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", 24 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mn\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m(\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[0;32m 2\u001b[0m \u001b[0mlst\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mx\u001b[0m \u001b[1;32min\u001b[0m \u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstrip\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mn\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[0mnum\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m(\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\u001b[0;32m 4\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlst\u001b[0m\u001b[1;33m)\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[0;32m 5\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mj\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m+\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlst\u001b[0m\u001b[1;33m)\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", 25 | "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: '1 3 6 2 5 4 3 2 4'" 26 | ] 27 | } 28 | ], 29 | "source": [ 30 | "\n", 31 | "n=int(input())\n", 32 | "lst=[int(x) for x in input().strip().split()][:n]\n", 33 | "num=int(input())\n", 34 | "for i in range(0,len(lst)):\n", 35 | " for j in range(i+1,len(lst)):\n", 36 | " if(lst[i]+lst[j]==num):\n", 37 | " if(lst[i] 0 :\n", 82 | " arr, n = takeInput()\n", 83 | " x = int(input())\n", 84 | " print(pairSum(arr, n, x)) \n", 85 | " \n", 86 | " t -= 1" 87 | ] 88 | } 89 | ], 90 | "metadata": { 91 | "kernelspec": { 92 | "display_name": "Python 3", 93 | "language": "python", 94 | "name": "python3" 95 | }, 96 | "language_info": { 97 | "codemirror_mode": { 98 | "name": "ipython", 99 | "version": 3 100 | }, 101 | "file_extension": ".py", 102 | "mimetype": "text/x-python", 103 | "name": "python", 104 | "nbconvert_exporter": "python", 105 | "pygments_lexer": "ipython3", 106 | "version": "3.7.6" 107 | } 108 | }, 109 | "nbformat": 4, 110 | "nbformat_minor": 4 111 | } 112 | -------------------------------------------------------------------------------- /8 Arrays and Lists/Sort 0 1.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 | "2\n", 13 | "8\n", 14 | "1 0 1 1 0 1 0 1\n", 15 | "0 0 0 1 1 1 1 1 \n", 16 | "\n", 17 | "5\n", 18 | "0 1 0 1 0\n", 19 | "0 0 0 1 1 \n", 20 | "\n" 21 | ] 22 | } 23 | ], 24 | "source": [ 25 | "from sys import stdin \n", 26 | "def sortZeroesAndOne(arr, n) : \n", 27 | " nextZero = 0 \n", 28 | " for i in range(n) : \n", 29 | " if arr[i] == 0 :\n", 30 | " temp = arr[nextZero] \n", 31 | " arr[nextZero] = arr[i]\n", 32 | " arr[i] = temp \n", 33 | " nextZero += 1 \n", 34 | "def takeInput() :\n", 35 | " n = int(input().strip())\n", 36 | " if n == 0 : \n", 37 | " return list(), 0 \n", 38 | " arr = list(map(int, input().strip().split(\" \"))) \n", 39 | " return arr, n\n", 40 | "def printList(arr, n) :\n", 41 | " for i in range(n) : \n", 42 | " print(arr[i], end = ' ') \n", 43 | " print()\n", 44 | "t = int(input()) \n", 45 | "while t > 0 :\n", 46 | " arr, n = takeInput()\n", 47 | " sortZeroesAndOne(arr, n)\n", 48 | " printList(arr, n) \n", 49 | " print() \n", 50 | " \n", 51 | " t -= 1" 52 | ] 53 | } 54 | ], 55 | "metadata": { 56 | "kernelspec": { 57 | "display_name": "Python 3", 58 | "language": "python", 59 | "name": "python3" 60 | }, 61 | "language_info": { 62 | "codemirror_mode": { 63 | "name": "ipython", 64 | "version": 3 65 | }, 66 | "file_extension": ".py", 67 | "mimetype": "text/x-python", 68 | "name": "python", 69 | "nbconvert_exporter": "python", 70 | "pygments_lexer": "ipython3", 71 | "version": "3.7.6" 72 | } 73 | }, 74 | "nbformat": 4, 75 | "nbformat_minor": 4 76 | } 77 | -------------------------------------------------------------------------------- /8 Arrays and Lists/Triplet Sum.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 | "1\n", 13 | "7\n", 14 | "1 2 3 4 5 6 7\n", 15 | "12\n", 16 | "5\n" 17 | ] 18 | } 19 | ], 20 | "source": [ 21 | "from sys import stdin \n", 22 | "def findTriplet(arr, n, x) :\n", 23 | " numTriplets = 0 \n", 24 | " for i in range(n) : \n", 25 | " for j in range((i + 1), n) :\n", 26 | " for k in range((j + 1), n) : \n", 27 | " if (arr[i] + arr[j] + arr[k]) == x :\n", 28 | " numTriplets += 1 \n", 29 | " return numTriplets \n", 30 | "def takeInput() :\n", 31 | " n = int(input())\n", 32 | " if n == 0 : \n", 33 | " return list(), 0\n", 34 | " arr = list(map(int, input().strip().split(\" \")))\n", 35 | " return arr, n \n", 36 | "t = int(input().strip())\n", 37 | "while t > 0 :\n", 38 | " arr, n = takeInput()\n", 39 | " x = int(input().strip()) \n", 40 | " print(findTriplet(arr, n, x))\n", 41 | " \n", 42 | " t -= 1" 43 | ] 44 | } 45 | ], 46 | "metadata": { 47 | "kernelspec": { 48 | "display_name": "Python 3", 49 | "language": "python", 50 | "name": "python3" 51 | }, 52 | "language_info": { 53 | "codemirror_mode": { 54 | "name": "ipython", 55 | "version": 3 56 | }, 57 | "file_extension": ".py", 58 | "mimetype": "text/x-python", 59 | "name": "python", 60 | "nbconvert_exporter": "python", 61 | "pygments_lexer": "ipython3", 62 | "version": "3.7.6" 63 | } 64 | }, 65 | "nbformat": 4, 66 | "nbformat_minor": 4 67 | } 68 | -------------------------------------------------------------------------------- /9 Searching & Sorting/Binary 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 | "7\n", 13 | "1 3 7 9 11 12 45\n", 14 | "1\n", 15 | "3\n", 16 | "1\n" 17 | ] 18 | } 19 | ], 20 | "source": [ 21 | "from sys import stdin\n", 22 | "def binarySearch(arr, n, x) : \n", 23 | " start = 0;\n", 24 | " end = n - 1 \n", 25 | " mid = start\n", 26 | " while start <= end :\n", 27 | " mid = start + (end - start) // 2\n", 28 | " if arr[mid] > x :\n", 29 | " end = mid - 1\n", 30 | " elif arr[mid] < x :\n", 31 | " start = mid + 1 \n", 32 | " else : \n", 33 | " return mid \n", 34 | " return -1\n", 35 | "def takeInput() :\n", 36 | " n = int(input())\n", 37 | " if n == 0 :\n", 38 | " return list(), 0\n", 39 | " arr = list(map(int, input().strip().split(\" \"))) \n", 40 | " return arr, n\n", 41 | "arr, n = takeInput()\n", 42 | "t = int(input()) \n", 43 | "while t > 0 : \n", 44 | " x = int(input().strip()) \n", 45 | " print(binarySearch(arr, n, x)) \n", 46 | " t -= 1" 47 | ] 48 | } 49 | ], 50 | "metadata": { 51 | "kernelspec": { 52 | "display_name": "Python 3", 53 | "language": "python", 54 | "name": "python3" 55 | }, 56 | "language_info": { 57 | "codemirror_mode": { 58 | "name": "ipython", 59 | "version": 3 60 | }, 61 | "file_extension": ".py", 62 | "mimetype": "text/x-python", 63 | "name": "python", 64 | "nbconvert_exporter": "python", 65 | "pygments_lexer": "ipython3", 66 | "version": "3.7.6" 67 | } 68 | }, 69 | "nbformat": 4, 70 | "nbformat_minor": 4 71 | } 72 | -------------------------------------------------------------------------------- /9 Searching & Sorting/Bubble Sort.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "1\n", 13 | "7\n", 14 | "5 8 2 5 1 9 6\n", 15 | "1 2 5 5 6 8 9 \n" 16 | ] 17 | } 18 | ], 19 | "source": [ 20 | "from sys import stdin\n", 21 | "def bubbleSort(arr, n) :\n", 22 | " for i in range(n - 1) :\n", 23 | " for j in range(n - i - 1) :\n", 24 | " if arr[j] > arr[j+1] :\n", 25 | " temp = arr[j] \n", 26 | " arr[j] = arr[j + 1]\n", 27 | " arr[j + 1] = temp \n", 28 | "def takeInput() : \n", 29 | " n = int(input().strip()) \n", 30 | " if n == 0 : \n", 31 | " return list(), 0\n", 32 | " arr = list(map(int, input().strip().split(\" \"))) \n", 33 | " return arr, n\n", 34 | "def printList(arr, n) :\n", 35 | " for i in range(n) :\n", 36 | " print(arr[i], end = \" \") \n", 37 | " print() \n", 38 | "t = int(input().strip()) \n", 39 | "while t > 0 :\n", 40 | " arr, n = takeInput()\n", 41 | " bubbleSort(arr, n)\n", 42 | " printList(arr, n) \n", 43 | " t-= 1" 44 | ] 45 | } 46 | ], 47 | "metadata": { 48 | "kernelspec": { 49 | "display_name": "Python 3", 50 | "language": "python", 51 | "name": "python3" 52 | }, 53 | "language_info": { 54 | "codemirror_mode": { 55 | "name": "ipython", 56 | "version": 3 57 | }, 58 | "file_extension": ".py", 59 | "mimetype": "text/x-python", 60 | "name": "python", 61 | "nbconvert_exporter": "python", 62 | "pygments_lexer": "ipython3", 63 | "version": "3.7.6" 64 | } 65 | }, 66 | "nbformat": 4, 67 | "nbformat_minor": 4 68 | } 69 | -------------------------------------------------------------------------------- /9 Searching & Sorting/Check Array Rotation.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "#Check Array Rotation\n", 8 | "\n", 9 | "You have been given an integer array/list(ARR) of size N. It has been sorted(in increasing order) and then rotated by some number 'K' in the clockwise direction.\n", 10 | "Your task is to write a function that returns the value of 'K', that means, the index from which the array/list has been rotated.\n", 11 | "Input format :\n", 12 | "The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.\n", 13 | "\n", 14 | "First line of each test case or query contains an integer 'N' representing the size of the array/list.\n", 15 | "\n", 16 | "Second line contains 'N' single space separated integers representing the elements in the array/list.\n", 17 | "Output Format :\n", 18 | "For each test case, print the value of 'K' or the index from which which the array/list has been rotated.\n", 19 | "\n", 20 | "Output for every test case will be printed in a separate line.\n", 21 | "Constraints :\n", 22 | "1 <= t <= 10^2\n", 23 | "0 <= N <= 10^5\n", 24 | "Time Limit: 1 sec\n", 25 | "Sample Input 1:\n", 26 | "1\n", 27 | "6\n", 28 | "5 6 1 2 3 4" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 1, 34 | "metadata": {}, 35 | "outputs": [ 36 | { 37 | "name": "stdout", 38 | "output_type": "stream", 39 | "text": [ 40 | "1\n", 41 | "6\n", 42 | "5 6 1 2 3 4\n", 43 | "2\n" 44 | ] 45 | } 46 | ], 47 | "source": [ 48 | "from sys import stdin \n", 49 | "def arrayRotateCheck(arr, n):\n", 50 | " for i in range(n - 1):\n", 51 | " if(arr[i] > arr[i + 1]):\n", 52 | " return (i + 1) \n", 53 | " return 0 \n", 54 | "def takeInput() : \n", 55 | " n = int(input().rstrip()) \n", 56 | " if n == 0:\n", 57 | " return list(), 0 \n", 58 | " arr = list(map(int, input().rstrip().split(\" \"))) \n", 59 | " return arr, n\n", 60 | "t = int(input().rstrip())\n", 61 | "while t > 0 :\n", 62 | " arr, n = takeInput() \n", 63 | " print(arrayRotateCheck(arr, n))\n", 64 | " \n", 65 | " t -= 1" 66 | ] 67 | } 68 | ], 69 | "metadata": { 70 | "kernelspec": { 71 | "display_name": "Python 3", 72 | "language": "python", 73 | "name": "python3" 74 | }, 75 | "language_info": { 76 | "codemirror_mode": { 77 | "name": "ipython", 78 | "version": 3 79 | }, 80 | "file_extension": ".py", 81 | "mimetype": "text/x-python", 82 | "name": "python", 83 | "nbconvert_exporter": "python", 84 | "pygments_lexer": "ipython3", 85 | "version": "3.7.6" 86 | } 87 | }, 88 | "nbformat": 4, 89 | "nbformat_minor": 4 90 | } 91 | -------------------------------------------------------------------------------- /9 Searching & Sorting/Insertion Sort.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "1\n", 13 | "7\n", 14 | "2 13 4 1 3 6 28\n", 15 | "1 2 3 4 6 13 28 \n" 16 | ] 17 | } 18 | ], 19 | "source": [ 20 | "from sys import stdin \n", 21 | "def insertionSort(arr, n) :\n", 22 | " i = 1 \n", 23 | " while i < n :\n", 24 | " temp = arr[i]\n", 25 | " j = i - 1\n", 26 | " while j >= 0 :\n", 27 | " if arr[j] > temp :\n", 28 | " arr[j + 1] = arr[j] \n", 29 | " else :\n", 30 | " break \n", 31 | " \n", 32 | " j -= 1 \n", 33 | " \n", 34 | " arr[j + 1] = temp\n", 35 | " i += 1\n", 36 | "\n", 37 | "def takeInput() : \n", 38 | " n = int(input())\n", 39 | " if n == 0 : \n", 40 | " return list(), 0 \n", 41 | " arr = list(map(int, input().strip().split(\" \"))) \n", 42 | " return arr, n \n", 43 | "def printList(arr, n) : \n", 44 | " for i in range(n) :\n", 45 | " print(arr[i], end = \" \") \n", 46 | " print() \n", 47 | "\n", 48 | "t = int(input())\n", 49 | "while t > 0 :\n", 50 | " arr, n = takeInput()\n", 51 | " insertionSort(arr, n)\n", 52 | " printList(arr, n) \n", 53 | " t-= 1" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": null, 59 | "metadata": {}, 60 | "outputs": [], 61 | "source": [] 62 | } 63 | ], 64 | "metadata": { 65 | "kernelspec": { 66 | "display_name": "Python 3", 67 | "language": "python", 68 | "name": "python3" 69 | }, 70 | "language_info": { 71 | "codemirror_mode": { 72 | "name": "ipython", 73 | "version": 3 74 | }, 75 | "file_extension": ".py", 76 | "mimetype": "text/x-python", 77 | "name": "python", 78 | "nbconvert_exporter": "python", 79 | "pygments_lexer": "ipython3", 80 | "version": "3.7.6" 81 | } 82 | }, 83 | "nbformat": 4, 84 | "nbformat_minor": 4 85 | } 86 | -------------------------------------------------------------------------------- /9 Searching & Sorting/Merge Sort.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "1\n", 13 | "5\n", 14 | "1 3 4 7 11\n", 15 | "4\n", 16 | "2 4 6 13\n", 17 | "1 2 3 4 4 6 7 11 13 \n" 18 | ] 19 | } 20 | ], 21 | "source": [ 22 | "from sys import stdin\n", 23 | "def merge(arr1, n, arr2, m) : \n", 24 | " ans = (n + m) * [0]\n", 25 | " i = 0 \n", 26 | " j = 0 \n", 27 | " k = 0\n", 28 | " while i < n and j < m :\n", 29 | " if arr1[i] < arr2[j] :\n", 30 | " ans[k] = arr1[i]\n", 31 | " k += 1 \n", 32 | " i += 1 \n", 33 | " else : \n", 34 | " ans[k] = arr2[j] \n", 35 | " k += 1 \n", 36 | " j += 1 \n", 37 | " while i < n :\n", 38 | " ans[k] = arr1[i]\n", 39 | " k += 1\n", 40 | " i += 1\n", 41 | " while j < m :\n", 42 | " ans[k] = arr2[j]\n", 43 | " k += 1\n", 44 | " j += 1\n", 45 | " return ans \n", 46 | "def takeInput() :\n", 47 | " n = int(input()) \n", 48 | " if n != 0: \n", 49 | " arr = list(map(int, input().strip().split(\" \"))) \n", 50 | " return arr, n\n", 51 | " return list(), 0\n", 52 | "def printList(arr, n) : \n", 53 | " for i in range(n) :\n", 54 | " print(arr[i], end = \" \") \n", 55 | " print()\n", 56 | " \n", 57 | "t = int(input()) \n", 58 | "while t > 0 :\n", 59 | " arr1, n = takeInput()\n", 60 | " arr2, m = takeInput()\n", 61 | " ans = merge(arr1, n, arr2, m) \n", 62 | " printList(ans, (n + m)) \n", 63 | " \n", 64 | " t -= 1" 65 | ] 66 | } 67 | ], 68 | "metadata": { 69 | "kernelspec": { 70 | "display_name": "Python 3", 71 | "language": "python", 72 | "name": "python3" 73 | }, 74 | "language_info": { 75 | "codemirror_mode": { 76 | "name": "ipython", 77 | "version": 3 78 | }, 79 | "file_extension": ".py", 80 | "mimetype": "text/x-python", 81 | "name": "python", 82 | "nbconvert_exporter": "python", 83 | "pygments_lexer": "ipython3", 84 | "version": "3.7.6" 85 | } 86 | }, 87 | "nbformat": 4, 88 | "nbformat_minor": 4 89 | } 90 | -------------------------------------------------------------------------------- /9 Searching & Sorting/Push Zeros to End.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "1\n", 13 | "7\n", 14 | "2 0 0 3 0 0 1\n", 15 | "2 3 1 0 0 0 0 \n" 16 | ] 17 | } 18 | ], 19 | "source": [ 20 | "from sys import stdin \n", 21 | "def pushZerosAtEnd(arr, n) :\n", 22 | " nonZero = 0 \n", 23 | " for i in range(n):\n", 24 | " if arr[i] != 0 :\n", 25 | " temp = arr[i] \n", 26 | " arr[i] = arr[nonZero] \n", 27 | " arr[nonZero] = temp \n", 28 | " nonZero += 1 \n", 29 | "\n", 30 | "def takeInput() :\n", 31 | " n = int(input())\n", 32 | " if n == 0:\n", 33 | " return list(), 0 \n", 34 | " arr = list(map(int, input().rstrip().split())) \n", 35 | " return arr, n\n", 36 | "def printList(arr, n) :\n", 37 | " for i in range(n) : \n", 38 | " print(arr[i], end = \" \") \n", 39 | " print()\n", 40 | " \n", 41 | "t = int(input())\n", 42 | "while t > 0 : \n", 43 | " arr, n = takeInput() \n", 44 | " pushZerosAtEnd(arr, n)\n", 45 | " printList(arr, n)\n", 46 | " \n", 47 | " t -= 1" 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.6" 68 | } 69 | }, 70 | "nbformat": 4, 71 | "nbformat_minor": 4 72 | } 73 | -------------------------------------------------------------------------------- /9 Searching & Sorting/Rotate Array.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 | "1\n", 13 | "7\n", 14 | "1 2 3 4 5 6 7\n", 15 | "2\n", 16 | "3 4 5 6 7 1 2 \n" 17 | ] 18 | } 19 | ], 20 | "source": [ 21 | "from sys import stdin \n", 22 | "def swapElements(arr, start, end) :\n", 23 | " arr[start], arr[end] = arr[end], arr[start] \n", 24 | " \n", 25 | "def reverse(arr, start, end): \n", 26 | " while(start < end):\n", 27 | " swapElements(arr, start, end)\n", 28 | " start += 1\n", 29 | " end -= 1 \n", 30 | "def rotate(arr, n, d): \n", 31 | " if n == 0 :\n", 32 | " return\n", 33 | " if d >= n and n != 0 :\n", 34 | " d = d % n\n", 35 | " reverse(arr, 0, n - 1) \n", 36 | " reverse(arr, 0, n - d - 1) \n", 37 | " reverse(arr, n - d, n - 1) \n", 38 | "def takeInput() :\n", 39 | " n = int(input())\n", 40 | " if n == 0:\n", 41 | " return list(), 0 \n", 42 | " arr = list(map(int, input().rstrip().split(\" \")))\n", 43 | " return arr, n \n", 44 | "def printList(arr, n) :\n", 45 | " for i in range(n) : \n", 46 | " print(arr[i], end = \" \")\n", 47 | " print()\n", 48 | "\n", 49 | "t = int(input())\n", 50 | "while t > 0 :\n", 51 | " arr, n = takeInput()\n", 52 | " d = int(input()) \n", 53 | " rotate(arr, n, d)\n", 54 | " printList(arr, n)\n", 55 | " \n", 56 | " t -= 1" 57 | ] 58 | } 59 | ], 60 | "metadata": { 61 | "kernelspec": { 62 | "display_name": "Python 3", 63 | "language": "python", 64 | "name": "python3" 65 | }, 66 | "language_info": { 67 | "codemirror_mode": { 68 | "name": "ipython", 69 | "version": 3 70 | }, 71 | "file_extension": ".py", 72 | "mimetype": "text/x-python", 73 | "name": "python", 74 | "nbconvert_exporter": "python", 75 | "pygments_lexer": "ipython3", 76 | "version": "3.7.6" 77 | } 78 | }, 79 | "nbformat": 4, 80 | "nbformat_minor": 4 81 | } 82 | -------------------------------------------------------------------------------- /9 Searching & Sorting/Second Largest in Array.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 | "1\n", 13 | "5\n", 14 | "9 3 6 2 9\n", 15 | "6\n" 16 | ] 17 | } 18 | ], 19 | "source": [ 20 | "from sys import stdin \n", 21 | "MIN_VALUE = -2147483648 \n", 22 | "def secondLargestElement(arr, n):\n", 23 | " if n == 0 : \n", 24 | " return MIN_VALUE\n", 25 | " largest = arr[0] \n", 26 | " secondLargest = MIN_VALUE \n", 27 | " for i in range(n) : \n", 28 | " if largest < arr[i] : \n", 29 | " secondLargest = largest \n", 30 | " largest = arr[i]\n", 31 | " elif secondLargest < arr[i] and arr[i] != largest :\n", 32 | " secondLargest = arr[i] \n", 33 | " return secondLargest \n", 34 | "def takeInput() :\n", 35 | " n = int(input()) \n", 36 | " if n != 0: \n", 37 | " arr = list(map(int, input().rstrip().split(\" \"))) \n", 38 | " return arr, n\n", 39 | " return list(), 0\n", 40 | "t = int(input().rstrip()) \n", 41 | "while t > 0 :\n", 42 | " arr, n = takeInput()\n", 43 | " print(secondLargestElement(arr, n)) \n", 44 | " \n", 45 | " t -= 1" 46 | ] 47 | } 48 | ], 49 | "metadata": { 50 | "kernelspec": { 51 | "display_name": "Python 3", 52 | "language": "python", 53 | "name": "python3" 54 | }, 55 | "language_info": { 56 | "codemirror_mode": { 57 | "name": "ipython", 58 | "version": 3 59 | }, 60 | "file_extension": ".py", 61 | "mimetype": "text/x-python", 62 | "name": "python", 63 | "nbconvert_exporter": "python", 64 | "pygments_lexer": "ipython3", 65 | "version": "3.7.6" 66 | } 67 | }, 68 | "nbformat": 4, 69 | "nbformat_minor": 4 70 | } 71 | -------------------------------------------------------------------------------- /9 Searching & Sorting/Sort 0 1 2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Sort 0 1 2\n", 8 | "\n", 9 | "You are given an integer array/list(ARR) of size N. It contains only 0s, 1s and 2s. Write a solution to sort this array/list in a 'single scan'.\n", 10 | "'Single Scan' refers to iterating over the array/list just once or to put it in other words, you will be visiting each element in the array/list just once.\n", 11 | "Note:\n", 12 | "You need to change in the given array/list itself. Hence, no need to return or print anything. \n", 13 | "Input format :\n", 14 | "The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.\n", 15 | "\n", 16 | "First line of each test case or query contains an integer 'N' representing the size of the array/list.\n", 17 | "\n", 18 | "Second line contains 'N' single space separated integers(all 0s, 1s and 2s) representing the elements in the array/list.\n", 19 | "Output Format :\n", 20 | "For each test case, print the sorted array/list elements in a row separated by a single space.\n", 21 | "\n", 22 | "Output for every test case will be printed in a separate line.\n", 23 | "Constraints :\n", 24 | "1 <= t <= 10^2\n", 25 | "0 <= N <= 10^5\n", 26 | "Time Limit: 1 sec\n", 27 | "Sample Input 1:\n", 28 | "1\n", 29 | "7\n", 30 | "0 1 2 0 2 0 1" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 1, 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "name": "stdout", 40 | "output_type": "stream", 41 | "text": [ 42 | "1\n", 43 | "7\n", 44 | "0 1 2 0 2 0 1\n", 45 | "0 0 0 1 1 2 2 \n" 46 | ] 47 | } 48 | ], 49 | "source": [ 50 | "from sys import stdin\n", 51 | "def sort012(arr, n) :\n", 52 | " nextZero = 0\n", 53 | " nextTwo = (n - 1) \n", 54 | " i = 0 \n", 55 | " while i <= nextTwo :\n", 56 | " if arr[i] == 0 :\n", 57 | " temp = arr[nextZero]\n", 58 | " arr[nextZero] = arr[i] \n", 59 | " arr[i] = temp\n", 60 | " i += 1\n", 61 | " nextZero += 1\n", 62 | " elif arr[i] == 2 :\n", 63 | " temp = arr[nextTwo] \n", 64 | " arr[nextTwo] = arr[i] \n", 65 | " arr[i] = temp\n", 66 | " nextTwo -= 1\n", 67 | " else : \n", 68 | " i += 1 \n", 69 | "def takeInput() : \n", 70 | " n = int(input().rstrip())\n", 71 | " if n == 0 :\n", 72 | " return list(), 0 \n", 73 | " arr = list(map(int, input().rstrip().split(\" \")))\n", 74 | " return arr, n\n", 75 | "def printList(arr, n) : \n", 76 | " for i in range(n) :\n", 77 | " print(arr[i], end = \" \")\n", 78 | " print() \n", 79 | "t = int(input())\n", 80 | "while t > 0 :\n", 81 | " arr, n = takeInput() \n", 82 | " sort012(arr, n)\n", 83 | " printList(arr, n) \n", 84 | " t -= 1" 85 | ] 86 | } 87 | ], 88 | "metadata": { 89 | "kernelspec": { 90 | "display_name": "Python 3", 91 | "language": "python", 92 | "name": "python3" 93 | }, 94 | "language_info": { 95 | "codemirror_mode": { 96 | "name": "ipython", 97 | "version": 3 98 | }, 99 | "file_extension": ".py", 100 | "mimetype": "text/x-python", 101 | "name": "python", 102 | "nbconvert_exporter": "python", 103 | "pygments_lexer": "ipython3", 104 | "version": "3.7.6" 105 | } 106 | }, 107 | "nbformat": 4, 108 | "nbformat_minor": 4 109 | } 110 | -------------------------------------------------------------------------------- /9 Searching & Sorting/Sum of Two Arrays.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "Sum of Two Arrays\n", 10 | "--------------------\n", 11 | "Two random integer arrays/lists have been given as ARR1 and ARR2 of size N and M respectively. Both the arrays/lists contain numbers from 0 to 9(i.e. single digit integer is present at every index). The idea here is to represent each array/list as an integer in itself of digits N and M.\n", 12 | "You need to find the sum of both the input arrays/list treating them as two integers and put the result in another array/list i.e. output array/list will also contain only single digit at every index.\n", 13 | "Note:\n", 14 | "The sizes N and M can be different. \n", 15 | "\n", 16 | "Output array/list(of all 0s) has been provided as a function argument. Its size will always be one more than the size of the bigger array/list. Place 0 at the 0th index if there is no carry. \n", 17 | "\n", 18 | "No need to print the elements of the output array/list.\n", 19 | "Input format :\n", 20 | "The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.\n", 21 | "\n", 22 | "First line of each test case or query contains an integer 'N' representing the size of the first array/list.\n", 23 | "\n", 24 | "Second line contains 'N' single space separated integers representing the elements of the first array/list.\n", 25 | "\n", 26 | "Third line contains an integer 'M' representing the size of the second array/list.\n", 27 | "\n", 28 | "Fourth line contains 'M' single space separated integers representing the elements of the second array/list.\n", 29 | "Output Format :\n", 30 | "For each test case, print the required sum of the arrays/list in a row, separated by a single space.\n", 31 | "\n", 32 | "Output for every test case will be printed in a separate line.\n", 33 | "Constraints :\n", 34 | "1 <= t <= 10^2\n", 35 | "0 <= N <= 10^5\n", 36 | "0 <= M <= 10^5\n", 37 | "Time Limit: 1 sec \n", 38 | "Sample Input 1:\n", 39 | "1\n", 40 | "3\n", 41 | "6 2 4\n", 42 | "3\n", 43 | "7 5 6\n", 44 | "Sample Output 1:\n", 45 | "1 3 8 0" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 2, 51 | "metadata": {}, 52 | "outputs": [ 53 | { 54 | "name": "stdout", 55 | "output_type": "stream", 56 | "text": [ 57 | "1\n", 58 | "3\n", 59 | "6 3 4\n", 60 | "3\n", 61 | "7 5 6\n", 62 | "0 3 9 0 \n" 63 | ] 64 | } 65 | ], 66 | "source": [ 67 | "from sys import stdin\n", 68 | "def sumOfTwoArrays(arr1, n, arr2, m, output) :\n", 69 | " i = n - 1\n", 70 | " j = m - 1\n", 71 | " carry = 0\n", 72 | " k = max(n, m)\n", 73 | " #k is the current index output array \n", 74 | " while i >= 0 and j >= 0 :\n", 75 | " SUM = arr1[i] + arr2[j] + carry \n", 76 | " output[k] = SUM % 10\n", 77 | " carry = SUM // 10 \n", 78 | " i -= 1 \n", 79 | " j -= 1\n", 80 | " k -= 1 \n", 81 | " while i >= 0 :\n", 82 | " SUM = arr1[i] + carry \n", 83 | " output[k] = SUM % 10 \n", 84 | " carry = SUM // 10 \n", 85 | " i -= 1 \n", 86 | " k -= 1 \n", 87 | " while j >= 0 : \n", 88 | " SUM = arr2[j] + carry \n", 89 | " output[k] = SUM % 10 \n", 90 | " carry = SUM // 10 \n", 91 | " j -= 1\n", 92 | " k -= 1 \n", 93 | " output[0] = carry \n", 94 | "\n", 95 | "def takeInput() :\n", 96 | " n = int(input()) \n", 97 | " if n == 0 : \n", 98 | " return list(), 0 \n", 99 | " arr = list(map(int, input().rstrip().split(\" \"))) \n", 100 | " return arr, n \n", 101 | "def printList(arr, n) : \n", 102 | " for i in range(n) : \n", 103 | " print(arr[i], end = \" \") \n", 104 | " print() \n", 105 | " \n", 106 | "t = int(input().rstrip()) \n", 107 | "while t > 0 :\n", 108 | " arr1, n = takeInput()\n", 109 | " arr2, m = takeInput() \n", 110 | " outputSize = (1 + max(n, m)) \n", 111 | " output = outputSize * [0] \n", 112 | " sumOfTwoArrays(arr1, n, arr2, m, output) \n", 113 | " printList(output, outputSize) \n", 114 | " \n", 115 | " t -= 1" 116 | ] 117 | } 118 | ], 119 | "metadata": { 120 | "kernelspec": { 121 | "display_name": "Python 3", 122 | "language": "python", 123 | "name": "python3" 124 | }, 125 | "language_info": { 126 | "codemirror_mode": { 127 | "name": "ipython", 128 | "version": 3 129 | }, 130 | "file_extension": ".py", 131 | "mimetype": "text/x-python", 132 | "name": "python", 133 | "nbconvert_exporter": "python", 134 | "pygments_lexer": "ipython3", 135 | "version": "3.7.6" 136 | } 137 | }, 138 | "nbformat": 4, 139 | "nbformat_minor": 4 140 | } 141 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Coding-Ninjas (Intro 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 | 5 | Topics discussed are:- 6 | 1) [Flow-Chart](https://github.com/BabaMalik/Coinding-NInjas-Introduction-to-Python/blob/master/1%20Flow-Chart) 7 | 2) [Introduction to Python](https://github.com/BabaMalik/Coding-Ninjas-Introduction-to-Python/tree/master/2%20Introduction%20to%20Python) 8 | 3) [Conditionals and Loops](https://github.com/BabaMalik/Coding-Ninjas-Introduction-to-Python/tree/master/3%20Conditionals%20and%20Loops) 9 | 4) [Patterns 1](https://github.com/BabaMalik/Coding-Ninjas-Introduction-to-Python/tree/master/4%20Patterns%201) 10 | 5) [Patterns 2](https://github.com/BabaMalik/Coding-Ninjas-Introduction-to-Python/tree/master/5%20Patterns%202) 11 | 6) [More on Loops](https://github.com/BabaMalik/Coding-Ninjas-Introduction-to-Python/tree/master/6%20More%20on%20Loops) 12 | 7) [Functions](https://github.com/BabaMalik/Coding-Ninjas-Introduction-to-Python/tree/master/7%20Functions) 13 | 8) [Arrays and Lists](https://github.com/BabaMalik/Coding-Ninjas-Introduction-to-Python/tree/master/8%20Arrays%20and%20Lists) 14 | 9) [Searching And Sorting](https://github.com/BabaMalik/Coding-Ninjas-Introduction-to-Python/tree/master/9%20Searching%20%26%20Sorting) 15 | 10) [Strings](https://github.com/BabaMalik/Coding-Ninjas-Introduction-to-Python/tree/master/10%20Strings) 16 | 11) [Two Dimensional Lists](https://github.com/BabaMalik/Coding-Ninjas-Introduction-to-Python/tree/master/11%20Two%20Dimensional%20Lists) 17 | 12) [Tupples, Dictionary And Sets](https://github.com/BabaMalik/Coding-Ninjas-Introduction-to-Python/tree/master/12%20Tupples%2C%20Dictionary%20And%20Sets) 18 | 19 | 20 | 21 | Register the course with the following referral link to get a discount of ₹1,000/- 22 | Referral Link: https://classroom.codingninjas.com/app/invite/ZEUJL 23 | 24 | **P.S: Don't forget to sign up with a new ID to avail discount** 25 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-slate --------------------------------------------------------------------------------