|
27 | 27 | },
|
28 | 28 | {
|
29 | 29 | "cell_type": "code",
|
30 |
| - "execution_count": null, |
| 30 | + "execution_count": 2, |
31 | 31 | "metadata": {},
|
32 |
| - "outputs": [], |
| 32 | + "outputs": [ |
| 33 | + { |
| 34 | + "name": "stdout", |
| 35 | + "output_type": "stream", |
| 36 | + "text": [ |
| 37 | + "2\n" |
| 38 | + ] |
| 39 | + } |
| 40 | + ], |
33 | 41 | "source": [
|
34 | 42 | "import hashlib\n",
|
35 | 43 | "\n",
|
36 | 44 | "def hash_to_range(input_string: str) -> int:\n",
|
37 | 45 | " hash_object = hashlib.sha256(input_string.encode())\n",
|
38 | 46 | " hash_int = int(hash_object.hexdigest(), 16)\n",
|
39 | 47 | " return (hash_int % 3) + 1\n",
|
40 |
| - "input_string = \"your_first_name_here\"\n", |
| 48 | + "input_string = \"sahil\"\n", |
41 | 49 | "result = hash_to_range(input_string)\n",
|
42 | 50 | "print(result)\n"
|
43 | 51 | ]
|
|
141 | 149 | "outputs": [],
|
142 | 150 | "source": [
|
143 | 151 | "# Definition for a binary tree node.\n",
|
144 |
| - "# class TreeNode(object):\n", |
145 |
| - "# def __init__(self, val = 0, left = None, right = None):\n", |
146 |
| - "# self.val = val\n", |
147 |
| - "# self.left = left\n", |
148 |
| - "# self.right = right\n", |
| 152 | + "from typing import List\n", |
| 153 | + "\n", |
| 154 | + "class TreeNode(object):\n", |
| 155 | + " def __init__(self, val = 0, left = None, right = None):\n", |
| 156 | + " self.val = val\n", |
| 157 | + " self.left = left\n", |
| 158 | + " self.right = right\n", |
149 | 159 | "def bt_path(root: TreeNode) -> List[List[int]]:\n",
|
150 |
| - " # TODO" |
| 160 | + " def dfs(node, path, result):\n", |
| 161 | + " if not node:\n", |
| 162 | + " return\n", |
| 163 | + " path.append(node.val)\n", |
| 164 | + " if not node.left and not node.right: # If it's a leaf\n", |
| 165 | + " result.append(path[:]) # Copy the path\n", |
| 166 | + " else:\n", |
| 167 | + " dfs(node.left, path, result)\n", |
| 168 | + " dfs(node.right, path, result)\n", |
| 169 | + " path.pop() # Backtrack\n", |
| 170 | + "\n", |
| 171 | + " result = []\n", |
| 172 | + " dfs(root, [], result)\n", |
| 173 | + " return result" |
| 174 | + ] |
| 175 | + }, |
| 176 | + { |
| 177 | + "cell_type": "code", |
| 178 | + "execution_count": 11, |
| 179 | + "metadata": {}, |
| 180 | + "outputs": [], |
| 181 | + "source": [ |
| 182 | + "from collections import deque\n", |
| 183 | + "\n", |
| 184 | + "def build_tree(level_order):\n", |
| 185 | + " if not level_order:\n", |
| 186 | + " return None\n", |
| 187 | + " root = TreeNode(level_order[0])\n", |
| 188 | + " queue = deque([root])\n", |
| 189 | + " i = 1\n", |
| 190 | + " while queue and i < len(level_order):\n", |
| 191 | + " node = queue.popleft()\n", |
| 192 | + " if i < len(level_order):\n", |
| 193 | + " node.left = TreeNode(level_order[i])\n", |
| 194 | + " queue.append(node.left)\n", |
| 195 | + " i += 1\n", |
| 196 | + " if i < len(level_order):\n", |
| 197 | + " node.right = TreeNode(level_order[i])\n", |
| 198 | + " queue.append(node.right)\n", |
| 199 | + " i += 1\n", |
| 200 | + " return root" |
| 201 | + ] |
| 202 | + }, |
| 203 | + { |
| 204 | + "cell_type": "code", |
| 205 | + "execution_count": 12, |
| 206 | + "metadata": {}, |
| 207 | + "outputs": [ |
| 208 | + { |
| 209 | + "data": { |
| 210 | + "text/plain": [ |
| 211 | + "[[1, 2, 3], [1, 2, 5], [1, 2, 6], [1, 2, 7]]" |
| 212 | + ] |
| 213 | + }, |
| 214 | + "execution_count": 12, |
| 215 | + "metadata": {}, |
| 216 | + "output_type": "execute_result" |
| 217 | + } |
| 218 | + ], |
| 219 | + "source": [ |
| 220 | + "root = build_tree([1, 2, 2, 3, 5, 6, 7])\n", |
| 221 | + "paths = bt_path(root)\n", |
| 222 | + "paths" |
| 223 | + ] |
| 224 | + }, |
| 225 | + { |
| 226 | + "cell_type": "code", |
| 227 | + "execution_count": 13, |
| 228 | + "metadata": {}, |
| 229 | + "outputs": [ |
| 230 | + { |
| 231 | + "data": { |
| 232 | + "text/plain": [ |
| 233 | + "[[10, 9, 8], [10, 7]]" |
| 234 | + ] |
| 235 | + }, |
| 236 | + "execution_count": 13, |
| 237 | + "metadata": {}, |
| 238 | + "output_type": "execute_result" |
| 239 | + } |
| 240 | + ], |
| 241 | + "source": [ |
| 242 | + "root1 = build_tree([10, 9, 7, 8])\n", |
| 243 | + "paths1 = bt_path(root1)\n", |
| 244 | + "paths1" |
151 | 245 | ]
|
152 | 246 | },
|
153 | 247 | {
|
|
227 | 321 | "metadata": {},
|
228 | 322 | "outputs": [],
|
229 | 323 | "source": [
|
230 |
| - "# Your answer here" |
| 324 | + "# Your answer here\n", |
| 325 | + "# In the given list of integers, I need to move all the zeros to the end of the list while keeping the non-zero numbers in the original \n", |
| 326 | + "# order which they appear in. " |
231 | 327 | ]
|
232 | 328 | },
|
233 | 329 | {
|
|
244 | 340 | "metadata": {},
|
245 | 341 | "outputs": [],
|
246 | 342 | "source": [
|
247 |
| - "# Your answer here" |
| 343 | + "# Your answer here\n", |
| 344 | + "# Example from me:\n", |
| 345 | + "# Input: nums = [8, 6, 0, 2, 0, 0, 1]\n", |
| 346 | + "# Output: [8, 6, 2, 1, 0, 0, 0]\n", |
| 347 | + "\n", |
| 348 | + "# Example from partner:\n", |
| 349 | + "# Custom Test Case 1\n", |
| 350 | + "# nums1 = [9, 0, 0, 3, 0, 2, 0]\n", |
| 351 | + "# Expected: [9, 3, 2, 0, 0, 0, 0]\n", |
| 352 | + "# The above example by my partner illustrates a list of numbers 9, 0, 0, 3, 0, 2, 0. Once put through the code, the expected output\n", |
| 353 | + "# is correctly displayed as 9, 3, 2, 0, 0, 0, 0. The zeros have been moved to the end and the non-zero numbers are in the order they\n", |
| 354 | + "# appear. \n" |
248 | 355 | ]
|
249 | 356 | },
|
250 | 357 | {
|
|
257 | 364 | },
|
258 | 365 | {
|
259 | 366 | "cell_type": "code",
|
260 |
| - "execution_count": null, |
| 367 | + "execution_count": 15, |
261 | 368 | "metadata": {},
|
262 |
| - "outputs": [], |
| 369 | + "outputs": [ |
| 370 | + { |
| 371 | + "name": "stdout", |
| 372 | + "output_type": "stream", |
| 373 | + "text": [ |
| 374 | + "[9, 3, 2, 0, 0, 0, 0]\n", |
| 375 | + "[3, 0, 0, 0, 0]\n" |
| 376 | + ] |
| 377 | + } |
| 378 | + ], |
263 | 379 | "source": [
|
264 |
| - "# Your answer here" |
| 380 | + "# Your answer here\n", |
| 381 | + "from typing import List\n", |
| 382 | + "\n", |
| 383 | + "def move_zeros_to_end(nums: List[int]) -> List[int]:\n", |
| 384 | + " insert_pos = 0\n", |
| 385 | + "\n", |
| 386 | + " for num in nums:\n", |
| 387 | + " if num != 0:\n", |
| 388 | + " nums[insert_pos] = num\n", |
| 389 | + " insert_pos += 1\n", |
| 390 | + "\n", |
| 391 | + " while insert_pos < len(nums):\n", |
| 392 | + " nums[insert_pos] = 0\n", |
| 393 | + " insert_pos += 1\n", |
| 394 | + "\n", |
| 395 | + " return nums\n", |
| 396 | + "\n", |
| 397 | + "# Validation\n", |
| 398 | + "\n", |
| 399 | + "# Custom Test Case 1\n", |
| 400 | + "nums1 = [9, 0, 0, 3, 0, 2, 0]\n", |
| 401 | + "print(move_zeros_to_end(nums1)) # Expected: [9, 3, 2, 0, 0, 0, 0]\n", |
| 402 | + "\n", |
| 403 | + "# Custom Test Case 2\n", |
| 404 | + "nums2 = [0, 0, 0, 3, 0]\n", |
| 405 | + "print(move_zeros_to_end(nums2)) # Expected: [3, 0, 0, 0, 0]" |
265 | 406 | ]
|
266 | 407 | },
|
267 | 408 | {
|
|
278 | 419 | "metadata": {},
|
279 | 420 | "outputs": [],
|
280 | 421 | "source": [
|
281 |
| - "# Your answer here" |
| 422 | + "# Your answer here\n", |
| 423 | + "# The solution works because the code loops through the list and everytime there is a non-zero number, it is moved ahead to the next\n", |
| 424 | + "# available position. Once all non-zero numbers are moved forward, the remaining spaces are filled with the zeros. " |
282 | 425 | ]
|
283 | 426 | },
|
284 | 427 | {
|
|
295 | 438 | "metadata": {},
|
296 | 439 | "outputs": [],
|
297 | 440 | "source": [
|
298 |
| - "# Your answer here" |
| 441 | + "# Your answer here\n", |
| 442 | + "# The time complexity for this problem is O(n). It passes through the list 2 times. First to move all non-zero numbers and second to\n", |
| 443 | + "# fill in the zeros. \n", |
| 444 | + "# The space complexity for this problem is O(1). It modifies the list in place. " |
299 | 445 | ]
|
300 | 446 | },
|
301 | 447 | {
|
|
312 | 458 | "metadata": {},
|
313 | 459 | "outputs": [],
|
314 | 460 | "source": [
|
315 |
| - "# Your answer here" |
| 461 | + "# Your answer here\n", |
| 462 | + "# Overall the code is correct, clean, and efficient. The explainations are all correct as well. Only minor tweak could be to change the \n", |
| 463 | + "# unnecessary overwrite when all the numbers are in the correct order for example [5, 9, 1, 2, 0, 0, 0]. " |
316 | 464 | ]
|
317 | 465 | },
|
318 | 466 | {
|
|
338 | 486 | "metadata": {},
|
339 | 487 | "outputs": [],
|
340 | 488 | "source": [
|
341 |
| - "# Your answer here" |
| 489 | + "# Your answer here\n", |
| 490 | + "# Working through the \"Valid Bracket Sequence\" problem helped me to deepen my understanding of how to use a stack data structure. \n", |
| 491 | + "# Executing the code required deliberation of matching bracket pairs and the order in which they appear. I chose a stack data structure \n", |
| 492 | + "# to track opening brackets and ensured each closing bracket had a valid corresponding opener. This exercise improved my analytical skills \n", |
| 493 | + "# and highlighted the importance of testing, such as empty strings or strings with only one type of bracket.\n", |
| 494 | + "\n", |
| 495 | + "# Examining my partners \"Move All Zeros to End\" problem was a valuable learning experience in peer code reviews as done in industry. \n", |
| 496 | + "# The logic behind using a two-pointer approach to preserve the relative order of non-zero elements while pushing zeros to the end was \n", |
| 497 | + "# efficient. I analyzed its time and space complexity, recognizing that the in-place nature of the solution made it optimal. \n", |
| 498 | + "# Critiquing the solution helped me understand subtle improvements and why certain decisions were made.\n", |
| 499 | + "\n", |
| 500 | + "# Overall, the process of writing and reviewing code strengthened my problem-solving skills, reinforced algorithmic thinking, and \n", |
| 501 | + "# emphasized the value of reading someone else’s code.\n" |
342 | 502 | ]
|
343 | 503 | },
|
344 | 504 | {
|
|
396 | 556 | ],
|
397 | 557 | "metadata": {
|
398 | 558 | "kernelspec": {
|
399 |
| - "display_name": "Python 3 (ipykernel)", |
| 559 | + "display_name": "dsi_participant", |
400 | 560 | "language": "python",
|
401 | 561 | "name": "python3"
|
402 | 562 | },
|
|
410 | 570 | "name": "python",
|
411 | 571 | "nbconvert_exporter": "python",
|
412 | 572 | "pygments_lexer": "ipython3",
|
413 |
| - "version": "3.11.5" |
| 573 | + "version": "3.9.15" |
414 | 574 | }
|
415 | 575 | },
|
416 | 576 | "nbformat": 4,
|
|
0 commit comments