|
| 1 | +{ |
| 2 | +"cells": [ |
| 3 | +{ |
| 4 | +"cell_type": "code", |
| 5 | +"execution_count": 54, |
| 6 | +"id": "7b8b86da", |
| 7 | +"metadata": {}, |
| 8 | +"outputs": [], |
| 9 | +"source": [ |
| 10 | +"class divisibilityof7:\n", |
| 11 | +" def __init__(self,num):\n", |
| 12 | +" self.num=num\n", |
| 13 | +" def divisible_by_7(self):\n", |
| 14 | +" \"\"\"\n", |
| 15 | +" Its a generator function.\n", |
| 16 | +" This function creates range from 0 to the given args to give numbers that are divisible by 7\n", |
| 17 | +" \n", |
| 18 | +" Parameters:\n", |
| 19 | +" num (int): Its accepts integer value to create range\n", |
| 20 | +" \n", |
| 21 | +" Returns:\n", |
| 22 | +" int: Numbers Divisible by 7\n", |
| 23 | +" \"\"\"\n", |
| 24 | +" for n in range(0,self.num+1):\n", |
| 25 | +" if n%7==0:\n", |
| 26 | +" yield n" |
| 27 | +] |
| 28 | +}, |
| 29 | +{ |
| 30 | +"cell_type": "code", |
| 31 | +"execution_count": 55, |
| 32 | +"id": "acb38b0f", |
| 33 | +"metadata": {}, |
| 34 | +"outputs": [], |
| 35 | +"source": [ |
| 36 | +"obj=divisibilityof7(50)" |
| 37 | +] |
| 38 | +}, |
| 39 | +{ |
| 40 | +"cell_type": "code", |
| 41 | +"execution_count": 56, |
| 42 | +"id": "35252bf8", |
| 43 | +"metadata": {}, |
| 44 | +"outputs": [ |
| 45 | +{ |
| 46 | +"name": "stdout", |
| 47 | +"output_type": "stream", |
| 48 | +"text": [ |
| 49 | +"0 7 14 21 28 35 42 49 " |
| 50 | +] |
| 51 | +} |
| 52 | +], |
| 53 | +"source": [ |
| 54 | +"for num in obj.divisible_by_7():\n", |
| 55 | +" print(num,end=\" \")" |
| 56 | +] |
| 57 | +}, |
| 58 | +{ |
| 59 | +"cell_type": "code", |
| 60 | +"execution_count": 71, |
| 61 | +"id": "3b8f8fed", |
| 62 | +"metadata": {}, |
| 63 | +"outputs": [ |
| 64 | +{ |
| 65 | +"name": "stdout", |
| 66 | +"output_type": "stream", |
| 67 | +"text": [ |
| 68 | +"Enter your sentence: New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3\n", |
| 69 | +"2 : 2\n", |
| 70 | +"3 : 1\n", |
| 71 | +"3? : 1\n", |
| 72 | +"New : 1\n", |
| 73 | +"Python : 5\n", |
| 74 | +"Read : 1\n", |
| 75 | +"and : 1\n", |
| 76 | +"between : 1\n", |
| 77 | +"choosing : 1\n", |
| 78 | +"or : 2\n", |
| 79 | +"to : 1\n" |
| 80 | +] |
| 81 | +} |
| 82 | +], |
| 83 | +"source": [ |
| 84 | +"sentence = input(\"Enter your sentence: \").split(\" \") \n", |
| 85 | +"sentence.sort()\n", |
| 86 | +"dc={}\n", |
| 87 | +"st = set()\n", |
| 88 | +"for i in sentence:\n", |
| 89 | +" if i not in st:\n", |
| 90 | +" counts=sentence.count(i)\n", |
| 91 | +" dc.update({i:counts})\n", |
| 92 | +" st.add(i)\n", |
| 93 | +"for k,v in dc.items():\n", |
| 94 | +" print(k,\":\",v)" |
| 95 | +] |
| 96 | +}, |
| 97 | +{ |
| 98 | +"cell_type": "code", |
| 99 | +"execution_count": 76, |
| 100 | +"id": "4f47f6be", |
| 101 | +"metadata": {}, |
| 102 | +"outputs": [], |
| 103 | +"source": [ |
| 104 | +"class Person:\n", |
| 105 | +" pass\n", |
| 106 | +"class Male(Person):\n", |
| 107 | +" def __init__(self,gender):\n", |
| 108 | +" self.gender=gender\n", |
| 109 | +" def getGender(self):\n", |
| 110 | +" return \"He is \" + self.gender\n", |
| 111 | +"class Female(Person):\n", |
| 112 | +" def __init__(self,gender):\n", |
| 113 | +" self.gender=gender\n", |
| 114 | +" def getGender(self):\n", |
| 115 | +" return \"She is \" + self.gender" |
| 116 | +] |
| 117 | +}, |
| 118 | +{ |
| 119 | +"cell_type": "code", |
| 120 | +"execution_count": 77, |
| 121 | +"id": "f1a590f4", |
| 122 | +"metadata": {}, |
| 123 | +"outputs": [ |
| 124 | +{ |
| 125 | +"name": "stdout", |
| 126 | +"output_type": "stream", |
| 127 | +"text": [ |
| 128 | +"He is Male\n", |
| 129 | +"She is Female\n" |
| 130 | +] |
| 131 | +} |
| 132 | +], |
| 133 | +"source": [ |
| 134 | +"maleobj = Male(\"Male\")\n", |
| 135 | +"print(maleobj.getGender())\n", |
| 136 | +"femaleobj = Female(\"Female\")\n", |
| 137 | +"print(femaleobj.getGender())" |
| 138 | +] |
| 139 | +}, |
| 140 | +{ |
| 141 | +"cell_type": "code", |
| 142 | +"execution_count": 79, |
| 143 | +"id": "4469a33e", |
| 144 | +"metadata": {}, |
| 145 | +"outputs": [ |
| 146 | +{ |
| 147 | +"name": "stdout", |
| 148 | +"output_type": "stream", |
| 149 | +"text": [ |
| 150 | +"I Play Hockey\n", |
| 151 | +"I Play Football\n", |
| 152 | +"I Love Hockey\n", |
| 153 | +"I Love Football\n", |
| 154 | +"You Play Hockey\n", |
| 155 | +"You Play Football\n", |
| 156 | +"You Love Hockey\n", |
| 157 | +"You Love Football\n" |
| 158 | +] |
| 159 | +} |
| 160 | +], |
| 161 | +"source": [ |
| 162 | +"subjects=[\"I\", \"You\"]\n", |
| 163 | +"verbs=[\"Play\", \"Love\"]\n", |
| 164 | +"objects=[\"Hockey\",\"Football\"]\n", |
| 165 | +"\n", |
| 166 | +"for sub in subjects:\n", |
| 167 | +" for verb in verbs:\n", |
| 168 | +" for obj in objects:\n", |
| 169 | +" print(\"{} {} {}\".format(sub,verb,obj))" |
| 170 | +] |
| 171 | +}, |
| 172 | +{ |
| 173 | +"cell_type": "code", |
| 174 | +"execution_count": 85, |
| 175 | +"id": "69f11a68", |
| 176 | +"metadata": {}, |
| 177 | +"outputs": [ |
| 178 | +{ |
| 179 | +"name": "stdout", |
| 180 | +"output_type": "stream", |
| 181 | +"text": [ |
| 182 | +"The compressed value of the string is b'x\\x9c\\xcbH\\xcd\\xc9\\xc9W(\\xcf/\\xcaIQ\\xcc \\x82\\r\\x00\\xbd[\\x11\\xf5'\n", |
| 183 | +"The decompressed value of the string is b'hello world!hello world!hello world!hello world!'\n" |
| 184 | +] |
| 185 | +} |
| 186 | +], |
| 187 | +"source": [ |
| 188 | +"sent = 'hello world!hello world!hello world!hello world!'\n", |
| 189 | +"convertb = bytes(sent, 'utf-8')\n", |
| 190 | +"compressedvalue = zlib.compress(convertb)\n", |
| 191 | +"print(\"The compressed value of the string is \",compressedvalue)\n", |
| 192 | +"print(\"The decompressed value of the string is \",zlib.decompress(compressedvalue))" |
| 193 | +] |
| 194 | +}, |
| 195 | +{ |
| 196 | +"cell_type": "code", |
| 197 | +"execution_count": 109, |
| 198 | +"id": "563db8c0", |
| 199 | +"metadata": {}, |
| 200 | +"outputs": [], |
| 201 | +"source": [ |
| 202 | +"from bisect import bisect_left\n", |
| 203 | +" \n", |
| 204 | +"def BinarySearch(a, x):\n", |
| 205 | +" i = bisect_left(a, x)\n", |
| 206 | +" if i != len(a) and a[i] == x:\n", |
| 207 | +" return i\n", |
| 208 | +" else:\n", |
| 209 | +" return -1" |
| 210 | +] |
| 211 | +}, |
| 212 | +{ |
| 213 | +"cell_type": "code", |
| 214 | +"execution_count": 111, |
| 215 | +"id": "ddfdc686", |
| 216 | +"metadata": {}, |
| 217 | +"outputs": [ |
| 218 | +{ |
| 219 | +"name": "stdout", |
| 220 | +"output_type": "stream", |
| 221 | +"text": [ |
| 222 | +"8 is present at 4 th index\n" |
| 223 | +] |
| 224 | +} |
| 225 | +], |
| 226 | +"source": [ |
| 227 | +"lst = [1, 2, 4, 5, 8]\n", |
| 228 | +"x = 8\n", |
| 229 | +"res = BinarySearch(lst, x)\n", |
| 230 | +"if res == -1:\n", |
| 231 | +" print(x, \"is absent\")\n", |
| 232 | +"else:\n", |
| 233 | +" print(x, \"is present at\", res,\"th index\")" |
| 234 | +] |
| 235 | +} |
| 236 | +], |
| 237 | +"metadata": { |
| 238 | +"kernelspec": { |
| 239 | +"display_name": "Python 3", |
| 240 | +"language": "python", |
| 241 | +"name": "python3" |
| 242 | +}, |
| 243 | +"language_info": { |
| 244 | +"codemirror_mode": { |
| 245 | +"name": "ipython", |
| 246 | +"version": 3 |
| 247 | +}, |
| 248 | +"file_extension": ".py", |
| 249 | +"mimetype": "text/x-python", |
| 250 | +"name": "python", |
| 251 | +"nbconvert_exporter": "python", |
| 252 | +"pygments_lexer": "ipython3", |
| 253 | +"version": "3.6.9" |
| 254 | +} |
| 255 | +}, |
| 256 | +"nbformat": 4, |
| 257 | +"nbformat_minor": 5 |
| 258 | +} |
0 commit comments