|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "64c3433d", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "### 1.Write a python function to return the result string which should not contain digits and duplicate characters." |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "code", |
| 13 | + "execution_count": 6, |
| 14 | + "id": "29b21fa2", |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "def remove_digit_duplicate(input_str):\n", |
| 19 | + " result = \"\"\n", |
| 20 | + " seen = set()\n", |
| 21 | + " for char in input_str:\n", |
| 22 | + " if char.isdigit(): # Skip digits\n", |
| 23 | + " continue\n", |
| 24 | + " if char not in seen:\n", |
| 25 | + " seen.add(char)\n", |
| 26 | + " result += char\n", |
| 27 | + " return result\n", |
| 28 | + "\n", |
| 29 | + "# Test the function with an example input\n", |
| 30 | + "input_str = \"aabcc1233\"" |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "code", |
| 35 | + "execution_count": 7, |
| 36 | + "id": "e7463107", |
| 37 | + "metadata": {}, |
| 38 | + "outputs": [ |
| 39 | + { |
| 40 | + "data": { |
| 41 | + "text/plain": [ |
| 42 | + "'abc'" |
| 43 | + ] |
| 44 | + }, |
| 45 | + "execution_count": 7, |
| 46 | + "metadata": {}, |
| 47 | + "output_type": "execute_result" |
| 48 | + } |
| 49 | + ], |
| 50 | + "source": [ |
| 51 | + "output = remove_digit_duplicate(input_str)\n", |
| 52 | + "output" |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "cell_type": "markdown", |
| 57 | + "id": "f95f19e4", |
| 58 | + "metadata": {}, |
| 59 | + "source": [ |
| 60 | + "### 2.Write a python function to rotate original array by k ( example 3 position to right)." |
| 61 | + ] |
| 62 | + }, |
| 63 | + { |
| 64 | + "cell_type": "code", |
| 65 | + "execution_count": 8, |
| 66 | + "id": "7b14ad5f", |
| 67 | + "metadata": {}, |
| 68 | + "outputs": [ |
| 69 | + { |
| 70 | + "name": "stdout", |
| 71 | + "output_type": "stream", |
| 72 | + "text": [ |
| 73 | + "[5, 6, 7, 1, 2, 3, 4]\n" |
| 74 | + ] |
| 75 | + } |
| 76 | + ], |
| 77 | + "source": [ |
| 78 | + "def rotate_array(arr, k):\n", |
| 79 | + " n = len(arr)\n", |
| 80 | + " k = k % n # k is within the array length\n", |
| 81 | + " return arr[-k:] + arr[:-k]\n", |
| 82 | + "\n", |
| 83 | + "arr = [1, 2, 3, 4, 5, 6, 7] # Example \n", |
| 84 | + "k = 3 #The original array rotated 3 positions to the right\n", |
| 85 | + "rotated_array = rotate_array(arr, k)\n", |
| 86 | + "print(rotated_array) " |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "markdown", |
| 91 | + "id": "37a7b996", |
| 92 | + "metadata": {}, |
| 93 | + "source": [ |
| 94 | + "### 3.Write a python functon to find the length of the longest substring without repeating characters. " |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "cell_type": "code", |
| 99 | + "execution_count": 11, |
| 100 | + "id": "0a045d60", |
| 101 | + "metadata": {}, |
| 102 | + "outputs": [], |
| 103 | + "source": [ |
| 104 | + "def lon_uniq_substr(s):\n", |
| 105 | + " start = 0\n", |
| 106 | + " max_length = 0\n", |
| 107 | + " used_char = {}\n", |
| 108 | + " \n", |
| 109 | + " for i, char in enumerate(s):\n", |
| 110 | + " if char in used_char and start <= used_char[char]:\n", |
| 111 | + " start = used_char[char] + 1\n", |
| 112 | + " max_length = max(max_length, i - start + 1)\n", |
| 113 | + " used_char[char] = i\n", |
| 114 | + " \n", |
| 115 | + " return max_length" |
| 116 | + ] |
| 117 | + }, |
| 118 | + { |
| 119 | + "cell_type": "code", |
| 120 | + "execution_count": 12, |
| 121 | + "id": "e84ebc25", |
| 122 | + "metadata": {}, |
| 123 | + "outputs": [ |
| 124 | + { |
| 125 | + "name": "stdout", |
| 126 | + "output_type": "stream", |
| 127 | + "text": [ |
| 128 | + "3\n" |
| 129 | + ] |
| 130 | + } |
| 131 | + ], |
| 132 | + "source": [ |
| 133 | + "# Example\n", |
| 134 | + "s = \"abcabcbb\"\n", |
| 135 | + "result = lon_uniq_substr(s)\n", |
| 136 | + "print(result)" |
| 137 | + ] |
| 138 | + }, |
| 139 | + { |
| 140 | + "cell_type": "markdown", |
| 141 | + "id": "44da07ce", |
| 142 | + "metadata": {}, |
| 143 | + "source": [ |
| 144 | + "### 4.Write a python program to get the length of the input strings and count each character occurence excluding space." |
| 145 | + ] |
| 146 | + }, |
| 147 | + { |
| 148 | + "cell_type": "code", |
| 149 | + "execution_count": 13, |
| 150 | + "id": "db4d989c", |
| 151 | + "metadata": {}, |
| 152 | + "outputs": [], |
| 153 | + "source": [ |
| 154 | + "def string_length_char_count(s):\n", |
| 155 | + " char_count = {}\n", |
| 156 | + " length = 0\n", |
| 157 | + " for char in s.replace(\" \", \"\"):\n", |
| 158 | + " length = length + 1\n", |
| 159 | + " char_count[char] = char_count.get(char,0) + 1\n", |
| 160 | + " return length, char_count" |
| 161 | + ] |
| 162 | + }, |
| 163 | + { |
| 164 | + "cell_type": "code", |
| 165 | + "execution_count": 14, |
| 166 | + "id": "c3a20a19", |
| 167 | + "metadata": {}, |
| 168 | + "outputs": [ |
| 169 | + { |
| 170 | + "data": { |
| 171 | + "text/plain": [ |
| 172 | + "(10, {'h': 1, 'e': 1, 'l': 3, 'o': 2, 'w': 1, 'r': 1, 'd': 1})" |
| 173 | + ] |
| 174 | + }, |
| 175 | + "execution_count": 14, |
| 176 | + "metadata": {}, |
| 177 | + "output_type": "execute_result" |
| 178 | + } |
| 179 | + ], |
| 180 | + "source": [ |
| 181 | + "s = \"hello world\"\n", |
| 182 | + "result = string_length_char_count(s)\n", |
| 183 | + "result" |
| 184 | + ] |
| 185 | + }, |
| 186 | + { |
| 187 | + "cell_type": "markdown", |
| 188 | + "id": "86b0cad5", |
| 189 | + "metadata": {}, |
| 190 | + "source": [ |
| 191 | + "### 5.What is wrong in below code snippet?" |
| 192 | + ] |
| 193 | + }, |
| 194 | + { |
| 195 | + "cell_type": "code", |
| 196 | + "execution_count": 15, |
| 197 | + "id": "acabce8b", |
| 198 | + "metadata": {}, |
| 199 | + "outputs": [ |
| 200 | + { |
| 201 | + "ename": "TypeError", |
| 202 | + "evalue": "can only concatenate str (not \"int\") to str", |
| 203 | + "output_type": "error", |
| 204 | + "traceback": [ |
| 205 | + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", |
| 206 | + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", |
| 207 | + "Cell \u001b[1;32mIn[15], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mconcatenate_str\u001b[39m(str1, str2):\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m str1 \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m \u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m+\u001b[39m str2\n\u001b[1;32m----> 3\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[43mconcatenate_str\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mTest\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m 4\u001b[0m \u001b[38;5;28mprint\u001b[39m(result)\n", |
| 208 | + "Cell \u001b[1;32mIn[15], line 2\u001b[0m, in \u001b[0;36mconcatenate_str\u001b[1;34m(str1, str2)\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mconcatenate_str\u001b[39m(str1, str2):\n\u001b[1;32m----> 2\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mstr1\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m \u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mstr2\u001b[49m\n", |
| 209 | + "\u001b[1;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str" |
| 210 | + ] |
| 211 | + } |
| 212 | + ], |
| 213 | + "source": [ |
| 214 | + "def concatenate_str(str1, str2):\n", |
| 215 | + " return str1 + \" \" + str2\n", |
| 216 | + "result = concatenate_str(\"Test\", 3)\n", |
| 217 | + "print(result) # .Answer is ...> The str2 argument is an integer and it needs to be converted to a string first." |
| 218 | + ] |
| 219 | + }, |
| 220 | + { |
| 221 | + "cell_type": "markdown", |
| 222 | + "id": "599f41c6", |
| 223 | + "metadata": {}, |
| 224 | + "source": [ |
| 225 | + "### 6.What is the output of this code:" |
| 226 | + ] |
| 227 | + }, |
| 228 | + { |
| 229 | + "cell_type": "code", |
| 230 | + "execution_count": 16, |
| 231 | + "id": "fad1bf7f", |
| 232 | + "metadata": {}, |
| 233 | + "outputs": [], |
| 234 | + "source": [ |
| 235 | + "var = 10\n", |
| 236 | + "var1 = 5\n", |
| 237 | + "var2 = 3\n", |
| 238 | + "\n", |
| 239 | + "result = var // var1 + var1 % var2" |
| 240 | + ] |
| 241 | + }, |
| 242 | + { |
| 243 | + "cell_type": "code", |
| 244 | + "execution_count": 17, |
| 245 | + "id": "d56ec9c1", |
| 246 | + "metadata": {}, |
| 247 | + "outputs": [ |
| 248 | + { |
| 249 | + "name": "stdout", |
| 250 | + "output_type": "stream", |
| 251 | + "text": [ |
| 252 | + "4\n" |
| 253 | + ] |
| 254 | + } |
| 255 | + ], |
| 256 | + "source": [ |
| 257 | + "print(result)" |
| 258 | + ] |
| 259 | + }, |
| 260 | + { |
| 261 | + "cell_type": "markdown", |
| 262 | + "id": "0a8e6ce3", |
| 263 | + "metadata": {}, |
| 264 | + "source": [ |
| 265 | + "### 7.Write a python program to print spatial numbers(numbers where digits are repeating like 444, 33 etc) from 1 to a given number n." |
| 266 | + ] |
| 267 | + }, |
| 268 | + { |
| 269 | + "cell_type": "code", |
| 270 | + "execution_count": 20, |
| 271 | + "id": "1e89e7d6", |
| 272 | + "metadata": {}, |
| 273 | + "outputs": [], |
| 274 | + "source": [ |
| 275 | + "def spatial_number(n):\n", |
| 276 | + " spatial_nums = [] \n", |
| 277 | + " for num in range(1, n + 1):\n", |
| 278 | + " num_str = str(num)\n", |
| 279 | + " if all(d == num_str[0] for d in num_str):\n", |
| 280 | + " spatial_nums.append(num)\n", |
| 281 | + " return spatial_nums" |
| 282 | + ] |
| 283 | + }, |
| 284 | + { |
| 285 | + "cell_type": "code", |
| 286 | + "execution_count": 22, |
| 287 | + "id": "42c060b5", |
| 288 | + "metadata": {}, |
| 289 | + "outputs": [ |
| 290 | + { |
| 291 | + "data": { |
| 292 | + "text/plain": [ |
| 293 | + "[1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55]" |
| 294 | + ] |
| 295 | + }, |
| 296 | + "execution_count": 22, |
| 297 | + "metadata": {}, |
| 298 | + "output_type": "execute_result" |
| 299 | + } |
| 300 | + ], |
| 301 | + "source": [ |
| 302 | + "n = 60\n", |
| 303 | + "result = spatial_number(n)\n", |
| 304 | + "result" |
| 305 | + ] |
| 306 | + } |
| 307 | + ], |
| 308 | + "metadata": { |
| 309 | + "kernelspec": { |
| 310 | + "display_name": "Python 3 (ipykernel)", |
| 311 | + "language": "python", |
| 312 | + "name": "python3" |
| 313 | + }, |
| 314 | + "language_info": { |
| 315 | + "codemirror_mode": { |
| 316 | + "name": "ipython", |
| 317 | + "version": 3 |
| 318 | + }, |
| 319 | + "file_extension": ".py", |
| 320 | + "mimetype": "text/x-python", |
| 321 | + "name": "python", |
| 322 | + "nbconvert_exporter": "python", |
| 323 | + "pygments_lexer": "ipython3", |
| 324 | + "version": "3.9.12" |
| 325 | + } |
| 326 | + }, |
| 327 | + "nbformat": 4, |
| 328 | + "nbformat_minor": 5 |
| 329 | +} |
0 commit comments