|
25 | 25 | },
|
26 | 26 | {
|
27 | 27 | "cell_type": "code",
|
28 |
| - "execution_count": null, |
| 28 | + "execution_count": 12, |
29 | 29 | "metadata": {},
|
30 |
| - "outputs": [], |
| 30 | + "outputs": [ |
| 31 | + { |
| 32 | + "name": "stdout", |
| 33 | + "output_type": "stream", |
| 34 | + "text": [ |
| 35 | + "2\n" |
| 36 | + ] |
| 37 | + } |
| 38 | + ], |
31 | 39 | "source": [
|
32 | 40 | "import hashlib\n",
|
33 | 41 | "\n",
|
34 | 42 | "def hash_to_range(input_string: str) -> int:\n",
|
35 | 43 | " hash_object = hashlib.sha256(input_string.encode())\n",
|
36 | 44 | " hash_int = int(hash_object.hexdigest(), 16)\n",
|
37 | 45 | " return (hash_int % 3) + 1\n",
|
38 |
| - "input_string = \"your_first_name_here\"\n", |
| 46 | + "input_string = \"sahil\"\n", |
39 | 47 | "result = hash_to_range(input_string)\n",
|
40 | 48 | "print(result)\n"
|
41 | 49 | ]
|
|
112 | 120 | },
|
113 | 121 | {
|
114 | 122 | "cell_type": "code",
|
115 |
| - "execution_count": 1, |
| 123 | + "execution_count": 5, |
116 | 124 | "metadata": {},
|
117 | 125 | "outputs": [],
|
118 | 126 | "source": [
|
|
169 | 177 | "metadata": {},
|
170 | 178 | "outputs": [],
|
171 | 179 | "source": [
|
172 |
| - "# Your answer here" |
| 180 | + "# Your answer here\n", |
| 181 | + "# The problem asks us to determine the string consisting of brackets sequence is correct. A valid sequence is that every open bracket\n", |
| 182 | + "# has a corresponding close bracket of the same type. " |
173 | 183 | ]
|
174 | 184 | },
|
175 | 185 | {
|
|
185 | 195 | "metadata": {},
|
186 | 196 | "outputs": [],
|
187 | 197 | "source": [
|
188 |
| - "# Your answer here" |
| 198 | + "# Your answer here\n", |
| 199 | + "\n", |
| 200 | + "Input: s = \"([{}])\"\n", |
| 201 | + "Output: True \n", |
| 202 | + "\n", |
| 203 | + "Input: s = \"([{)}]\"\n", |
| 204 | + "Output: False " |
189 | 205 | ]
|
190 | 206 | },
|
191 | 207 | {
|
|
202 | 218 | "metadata": {},
|
203 | 219 | "outputs": [],
|
204 | 220 | "source": [
|
205 |
| - "# Your answer here" |
| 221 | + "# Your answer here\n", |
| 222 | + "\n", |
| 223 | + "def is_valid_brackets(s: str) -> bool:\n", |
| 224 | + " stack = []\n", |
| 225 | + " matching = {')': '(', ']': '[', '}': '{'}\n", |
| 226 | + "\n", |
| 227 | + " for char in s:\n", |
| 228 | + " if char in matching.values():\n", |
| 229 | + " stack.append(char)\n", |
| 230 | + " elif char in matching:\n", |
| 231 | + " if not stack or stack[-1] != matching[char]:\n", |
| 232 | + " return False\n", |
| 233 | + " stack.pop()\n", |
| 234 | + " else:\n", |
| 235 | + " return False\n", |
| 236 | + "\n", |
| 237 | + " return not stack" |
206 | 238 | ]
|
207 | 239 | },
|
208 | 240 | {
|
|
219 | 251 | "metadata": {},
|
220 | 252 | "outputs": [],
|
221 | 253 | "source": [
|
222 |
| - "# Your answer here" |
| 254 | + "# Your answer here\n", |
| 255 | + "# The solution correctly uses stack to keep track of unmatched opening brackets. The closing bracket is mapped by the dictionary to \n", |
| 256 | + "# see if it's the correct opening pair. The opening bracket is stored in a list in the stack. The closing bracket is compard to the\n", |
| 257 | + "# stack, and if it matches, the stack is pop. If it doesn't match, code returns False. " |
223 | 258 | ]
|
224 | 259 | },
|
225 | 260 | {
|
|
236 | 271 | "metadata": {},
|
237 | 272 | "outputs": [],
|
238 | 273 | "source": [
|
239 |
| - "# Your answer here" |
| 274 | + "# Your answer here\n", |
| 275 | + "# The time complexity is O(n), where n is the length of the string. The string is only gone through once, performing constant-time \n", |
| 276 | + "# operations. \n", |
| 277 | + "# The space complexity is O(n) in the worst case as well, where all characters stored in the stack are opening brackets. " |
240 | 278 | ]
|
241 | 279 | },
|
242 | 280 | {
|
|
253 | 291 | "metadata": {},
|
254 | 292 | "outputs": [],
|
255 | 293 | "source": [
|
256 |
| - "# Your answer here" |
| 294 | + "# Your answer here\n", |
| 295 | + "# An alternative solution can be repeatedly removing matching pairs of brackets from string until all matches are removed and no more \n", |
| 296 | + "# can be removed anymore. This can be done using a loop iteration. All matched pairs will be removed using .replace(). The loop will \n", |
| 297 | + "# continue until there are no more matched pairs to be removed. In the end, if the string is empty, all brackets matched." |
257 | 298 | ]
|
258 | 299 | },
|
259 | 300 | {
|
|
301 | 342 | ],
|
302 | 343 | "metadata": {
|
303 | 344 | "kernelspec": {
|
304 |
| - "display_name": "Python 3 (ipykernel)", |
| 345 | + "display_name": "dsi_participant", |
305 | 346 | "language": "python",
|
306 | 347 | "name": "python3"
|
307 | 348 | },
|
|
315 | 356 | "name": "python",
|
316 | 357 | "nbconvert_exporter": "python",
|
317 | 358 | "pygments_lexer": "ipython3",
|
318 |
| - "version": "3.11.5" |
| 359 | + "version": "3.9.15" |
319 | 360 | }
|
320 | 361 | },
|
321 | 362 | "nbformat": 4,
|
|
0 commit comments