|
131 | 131 | },
|
132 | 132 | {
|
133 | 133 | "cell_type": "code",
|
134 |
| - "execution_count": 6, |
| 134 | + "execution_count": 7, |
135 | 135 | "metadata": {},
|
136 | 136 | "outputs": [
|
137 | 137 | {
|
138 | 138 | "ename": "SyntaxError",
|
139 |
| - "evalue": "invalid syntax (<ipython-input-6-da6edc7350c1>, line 4)", |
| 139 | + "evalue": "invalid syntax (<ipython-input-7-70de1be1fcb7>, line 4)", |
140 | 140 | "output_type": "error",
|
141 | 141 | "traceback": [
|
142 |
| - "\u001b[1;36m File \u001b[1;32m\"<ipython-input-6-da6edc7350c1>\"\u001b[1;36m, line \u001b[1;32m4\u001b[0m\n\u001b[1;33m Accidents each year = [25, 79, 46, 90, 29, 88, 79, 93, 20, 11]\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" |
| 142 | + "\u001b[1;36m File \u001b[1;32m\"<ipython-input-7-70de1be1fcb7>\"\u001b[1;36m, line \u001b[1;32m4\u001b[0m\n\u001b[1;33m Accidents each year = [25, 79, 46, 90, 29, 88, 79, 93, 20, 11]\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" |
143 | 143 | ]
|
144 | 144 | }
|
145 | 145 | ],
|
|
149 | 149 | "Type = [Pickup, SUV, Sedan, Sedan, Sedan, SUV, SUV, SUV, Pickup, Pickup]\n",
|
150 | 150 | "Accidents each year = [25, 79, 46, 90, 29, 88, 79, 93, 20, 11]\n",
|
151 | 151 | "#Find the brand and model of the safest car\n",
|
152 |
| - "print(\"a\", min(Accidents each year))" |
| 152 | + "min(Accidents each year)" |
| 153 | + ] |
| 154 | + }, |
| 155 | + { |
| 156 | + "cell_type": "markdown", |
| 157 | + "metadata": {}, |
| 158 | + "source": [ |
| 159 | + "5. Classify daily temperatures (in degrees Celsius) stored in the temperatures list belowinto four different classes:\n" |
153 | 160 | ]
|
154 | 161 | },
|
155 | 162 | {
|
156 | 163 | "cell_type": "code",
|
157 |
| - "execution_count": null, |
| 164 | + "execution_count": 22, |
158 | 165 | "metadata": {},
|
159 |
| - "outputs": [], |
| 166 | + "outputs": [ |
| 167 | + { |
| 168 | + "name": "stdout", |
| 169 | + "output_type": "stream", |
| 170 | + "text": [ |
| 171 | + "[-5.4, -4.8, -4.4, -2.2, -3.9, -2.5, -4.6, -2.4, -3.3, -4.8, -2.8, -4.7, -5.6, -2.7, -4.6]\n", |
| 172 | + "[1.0, -1.3, 0.1, 1.9, 1.0, -0.8, -0.1, -0.4, -0.9, 1.6, 1.8, 1.6, 1.5, 1.4, 1.6, -0.0]\n", |
| 173 | + "[3.9, 4.0, 4.4, 5.1, 2.1, 2.6, 3.4, 3.1, 2.4, 4.2, 3.5, 2.6, 3.1, 2.2, 3.3, 4.7, 4.0, 3.6, 4.9, 4.8, 5.3, 5.6, 4.1, 3.7, 7.6, 6.9, 5.1, 6.4, 3.8, 4.0, 8.6, 4.1, 8.9, 3.0, 8.5, 4.7, 6.6, 8.1, 4.5, 4.8, 11.3, 4.7, 5.2, 11.5, 6.2, 2.9, 4.3, 2.8, 2.8, 6.3, 2.6, 7.3, 3.4, 4.7, 9.3, 6.4, 5.4, 7.6, 5.2]\n", |
| 174 | + "[]\n", |
| 175 | + "-3.913333333333333\n", |
| 176 | + "0.625\n", |
| 177 | + "5.018644067796609\n" |
| 178 | + ] |
| 179 | + } |
| 180 | + ], |
160 | 181 | "source": [
|
161 |
| - "\n" |
| 182 | + "temperatures = [-5.4, 1.0, -1.3, -4.8, 3.9, 0.1, -4.4, 4.0, -2.2, -3.9, 4.4, -2.5, -4.6, \n", |
| 183 | + " 5.1, 2.1, -2.4, 1.9, -3.3, -4.8, 1.0, -0.8, -2.8, -0.1, -4.7, -5.6, 2.6,\n", |
| 184 | + "-2.7, -4.6, 3.4, -0.4, -0.9, 3.1, 2.4, 1.6, 4.2, 3.5, 2.6, 3.1, 2.2, 1.8, 3.3,\n", |
| 185 | + "1.6, 1.5, 4.7, 4.0, 3.6, 4.9, 4.8, 5.3, 5.6, 4.1, 3.7, 7.6, 6.9, 5.1, 6.4, 3.8,\n", |
| 186 | + "4.0, 8.6, 4.1, 1.4, 8.9, 3.0, 1.6, 8.5, 4.7, 6.6, 8.1, 4.5, 4.8, 11.3, 4.7, 5.2,\n", |
| 187 | + "11.5, 6.2, 2.9, 4.3, 2.8, 2.8, 6.3, 2.6, -0.0, 7.3, 3.4, 4.7, 9.3, 6.4, 5.4, 7.6,\n", |
| 188 | + "5.2]\n", |
| 189 | + "cold = [n for n in temperatures if n <= -2]\n", |
| 190 | + "slippery = [n for n in temperatures if n <= 2 and n > -2]\n", |
| 191 | + "comfortable = [n for n in temperatures if n > 2 and n <= 15]\n", |
| 192 | + "warm = [n for n in temperatures if n > 15]\n", |
| 193 | + "print(cold)\n", |
| 194 | + "print(slippery)\n", |
| 195 | + "print(comfortable)\n", |
| 196 | + "print(warm)\n", |
| 197 | + "def ave(num):\n", |
| 198 | + " nsum = 0\n", |
| 199 | + " for i in range(len(num)):\n", |
| 200 | + " nsum += num[i]\n", |
| 201 | + " return nsum / len(num)\n", |
| 202 | + "print(ave(cold))\n", |
| 203 | + "print(ave(slippery))\n", |
| 204 | + "print(ave(comfortable))\n" |
162 | 205 | ]
|
| 206 | + }, |
| 207 | + { |
| 208 | + "cell_type": "markdown", |
| 209 | + "metadata": {}, |
| 210 | + "source": [ |
| 211 | + "6. Given a string, find the length of the longest substring without repeating characters." |
| 212 | + ] |
| 213 | + }, |
| 214 | + { |
| 215 | + "cell_type": "code", |
| 216 | + "execution_count": 36, |
| 217 | + "metadata": { |
| 218 | + "scrolled": true |
| 219 | + }, |
| 220 | + "outputs": [ |
| 221 | + { |
| 222 | + "ename": "IndentationError", |
| 223 | + "evalue": "unindent does not match any outer indentation level (<tokenize>, line 17)", |
| 224 | + "output_type": "error", |
| 225 | + "traceback": [ |
| 226 | + "\u001b[1;36m File \u001b[1;32m\"<tokenize>\"\u001b[1;36m, line \u001b[1;32m17\u001b[0m\n\u001b[1;33m pos[string[i]] = i\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m unindent does not match any outer indentation level\n" |
| 227 | + ] |
| 228 | + } |
| 229 | + ], |
| 230 | + "source": [ |
| 231 | + "def findLongestSubstring(string): \n", |
| 232 | + " n = len(string) \n", |
| 233 | + " st = 0\n", |
| 234 | + " maxlen = 0\n", |
| 235 | + " start = 0\n", |
| 236 | + " pos = {} \n", |
| 237 | + " pos[string[0]] = 0\n", |
| 238 | + " for i in range(1, n): \n", |
| 239 | + " if string[i] not in pos: \n", |
| 240 | + " pos[string[i]] = i \n", |
| 241 | + " else: \n", |
| 242 | + " currlen = i - st \n", |
| 243 | + " if maxlen < currlen: \n", |
| 244 | + " maxlen = currlen \n", |
| 245 | + " start = st \n", |
| 246 | + " st = pos[string[i]] + 1\n", |
| 247 | + " pos[string[i]] = i \n", |
| 248 | + " if maxlen < i - st: \n", |
| 249 | + " maxlen = i - st \n", |
| 250 | + " start = st \n", |
| 251 | + " return string[start : start + maxlen] \n", |
| 252 | + "findLongestSubstring(\"hhhhhasdhhha\"){}" |
| 253 | + ] |
| 254 | + }, |
| 255 | + { |
| 256 | + "cell_type": "markdown", |
| 257 | + "metadata": {}, |
| 258 | + "source": [ |
| 259 | + "7. Given an array nums of n integers, are there elements a, b, c in nums such that a + b +\n", |
| 260 | + "c = 0? Find all unique triplets in the array which gives the sum of zero. The solution set\n", |
| 261 | + "must not contain duplicate triplets." |
| 262 | + ] |
| 263 | + }, |
| 264 | + { |
| 265 | + "cell_type": "code", |
| 266 | + "execution_count": 38, |
| 267 | + "metadata": {}, |
| 268 | + "outputs": [ |
| 269 | + { |
| 270 | + "name": "stdout", |
| 271 | + "output_type": "stream", |
| 272 | + "text": [ |
| 273 | + "0 -1 1\n", |
| 274 | + "2 -3 1\n" |
| 275 | + ] |
| 276 | + } |
| 277 | + ], |
| 278 | + "source": [ |
| 279 | + "def findTriplets(arr, n): \n", |
| 280 | + " found = True\n", |
| 281 | + " for i in range(0, n-2): \n", |
| 282 | + " for j in range(i+1, n-1): \n", |
| 283 | + " for k in range(j+1, n): \n", |
| 284 | + " if (arr[i] + arr[j] + arr[k] == 0): \n", |
| 285 | + " print(arr[i], arr[j], arr[k]) \n", |
| 286 | + " found = True\n", |
| 287 | + " if (found == False): \n", |
| 288 | + " print(\" not exist \") \n", |
| 289 | + "\n", |
| 290 | + "arr = [0, -1, 2, -3, 1] \n", |
| 291 | + "n = len(arr) \n", |
| 292 | + "findTriplets(arr, n) " |
| 293 | + ] |
| 294 | + }, |
| 295 | + { |
| 296 | + "cell_type": "code", |
| 297 | + "execution_count": null, |
| 298 | + "metadata": {}, |
| 299 | + "outputs": [], |
| 300 | + "source": [] |
163 | 301 | }
|
164 | 302 | ],
|
165 | 303 | "metadata": {
|
|
0 commit comments