|
27 | 27 | },
|
28 | 28 | {
|
29 | 29 | "cell_type": "code",
|
30 |
| - "execution_count": null, |
| 30 | + "execution_count": 1, |
31 | 31 | "metadata": {},
|
32 |
| - "outputs": [], |
| 32 | + "outputs": [ |
| 33 | + { |
| 34 | + "name": "stdout", |
| 35 | + "output_type": "stream", |
| 36 | + "text": [ |
| 37 | + "1\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 = \"ken\"\n", |
41 | 49 | "result = hash_to_range(input_string)\n",
|
42 | 50 | "print(result)\n"
|
43 | 51 | ]
|
|
86 | 94 | },
|
87 | 95 | {
|
88 | 96 | "cell_type": "code",
|
89 |
| - "execution_count": null, |
| 97 | + "execution_count": 3, |
90 | 98 | "metadata": {},
|
91 | 99 | "outputs": [],
|
92 | 100 | "source": [
|
93 | 101 | "# Definition for a binary tree node.\n",
|
94 |
| - "# class TreeNode(object):\n", |
95 |
| - "# def __init__(self, val = 0, left = None, right = None):\n", |
96 |
| - "# self.val = val\n", |
97 |
| - "# self.left = left\n", |
98 |
| - "# self.right = right\n", |
| 102 | + "class TreeNode(object):\n", |
| 103 | + " def __init__(self, val = 0, left = None, right = None):\n", |
| 104 | + " self.val = val\n", |
| 105 | + " self.left = left\n", |
| 106 | + " self.right = right\n", |
| 107 | + " \n", |
99 | 108 | "def is_duplicate(root: TreeNode) -> int:\n",
|
100 |
| - " # TODO" |
| 109 | + " if root is None:\n", |
| 110 | + " return False\n", |
| 111 | + " if root.left is not None and root.left.val == root.val:\n", |
| 112 | + " return True\n", |
| 113 | + " if root.right is not None and root.right.val == root.val:\n", |
| 114 | + " return True\n", |
| 115 | + " return is_duplicate(root.left) or is_duplicate(root.right)\n", |
| 116 | + " \n", |
| 117 | + " \n", |
| 118 | + " " |
101 | 119 | ]
|
102 | 120 | },
|
103 | 121 | {
|
|
222 | 240 | ]
|
223 | 241 | },
|
224 | 242 | {
|
225 |
| - "cell_type": "code", |
226 |
| - "execution_count": null, |
| 243 | + "cell_type": "markdown", |
227 | 244 | "metadata": {},
|
228 |
| - "outputs": [], |
229 | 245 | "source": [
|
230 |
| - "# Your answer here" |
| 246 | + "rearrange a list of integers by moving all zeros to the end while keeping the relative order of non-zero elements intact, it's done in-place without creating a new list; [0, 1, 0, 3, 12] becomes [1, 3, 12, 0, 0]." |
231 | 247 | ]
|
232 | 248 | },
|
233 | 249 | {
|
|
240 | 256 | },
|
241 | 257 | {
|
242 | 258 | "cell_type": "code",
|
243 |
| - "execution_count": null, |
| 259 | + "execution_count": 4, |
244 | 260 | "metadata": {},
|
245 |
| - "outputs": [], |
| 261 | + "outputs": [ |
| 262 | + { |
| 263 | + "ename": "IndentationError", |
| 264 | + "evalue": "unexpected indent (3596805496.py, line 9)", |
| 265 | + "output_type": "error", |
| 266 | + "traceback": [ |
| 267 | + "\u001b[0;36m Cell \u001b[0;32mIn[4], line 9\u001b[0;36m\u001b[0m\n\u001b[0;31m nums[0] = 0, skip (write_idx stays 0)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m unexpected indent\n" |
| 268 | + ] |
| 269 | + } |
| 270 | + ], |
246 | 271 | "source": [
|
247 |
| - "# Your answer here" |
| 272 | + "# New example that demonstrates understanding of the problem:\n", |
| 273 | + "# Input: [2, 0, 0, 8, 0, 15]\n", |
| 274 | + "# Output: [2, 8, 15, 0, 0, 0]\n", |
| 275 | + "# The non-zero elements (2, 8, 15) maintain their relative order and move to the front,\n", |
| 276 | + "# while all zeros move to the end.\n", |
| 277 | + "\n", |
| 278 | + "# Tracing through partner's Example 1: [0, 1, 1, 3, 0] -> [1, 1, 3, 0, 0]\n", |
| 279 | + "# Step 1: write_idx = 0, scan through list\n", |
| 280 | + "# - nums[0] = 0, skip (write_idx stays 0)\n", |
| 281 | + "# - nums[1] = 1, place at nums[0], write_idx = 1\n", |
| 282 | + "# - nums[2] = 1, place at nums[1], write_idx = 2 \n", |
| 283 | + "# - nums[3] = 3, place at nums[2], write_idx = 3\n", |
| 284 | + "# - nums[4] = 0, skip (write_idx stays 3)\n", |
| 285 | + "# Step 2: Fill positions 3 and 4 with zeros\n", |
| 286 | + "# Final result: [1, 1, 3, 0, 0]\n" |
248 | 287 | ]
|
249 | 288 | },
|
250 | 289 | {
|
|
261 | 300 | "metadata": {},
|
262 | 301 | "outputs": [],
|
263 | 302 | "source": [
|
264 |
| - "# Your answer here" |
| 303 | + "from typing import List\n", |
| 304 | + "\n", |
| 305 | + "def move_zeros_to_end(nums: List[int]) -> List[int]:\n", |
| 306 | + " \"\"\"\n", |
| 307 | + " Move all 0's in nums to the end, maintaining the order of non-zero elements.\n", |
| 308 | + " Modifies nums in-place and also returns it.\n", |
| 309 | + " \"\"\"\n", |
| 310 | + " write_idx = 0\n", |
| 311 | + "\n", |
| 312 | + " # 1) Pull all non-zero values forward\n", |
| 313 | + " for num in nums:\n", |
| 314 | + " if num != 0:\n", |
| 315 | + " nums[write_idx] = num\n", |
| 316 | + " write_idx += 1\n", |
| 317 | + "\n", |
| 318 | + " # 2) Fill the remainder with zeros\n", |
| 319 | + " for i in range(write_idx, len(nums)):\n", |
| 320 | + " nums[i] = 0\n", |
| 321 | + "\n", |
| 322 | + " return nums" |
265 | 323 | ]
|
266 | 324 | },
|
267 | 325 | {
|
|
278 | 336 | "metadata": {},
|
279 | 337 | "outputs": [],
|
280 | 338 | "source": [
|
281 |
| - "# Your answer here" |
| 339 | + "they did a two-pass approach that efficiently rearranges the array. First, it maintains a write pointer that tracks where the next non-zero element should be placed. As it scans through the array, it copies each non-zero element to the front positions sequentially, preserving their original order. Then in the second pass, it fills all remaining positions with zeros. This approach is elegant because it maintains the relative order of non-zero elements while moving all zeros to the end, and it does everything in-place without needing extra memory." |
282 | 340 | ]
|
283 | 341 | },
|
284 | 342 | {
|
|
290 | 348 | ]
|
291 | 349 | },
|
292 | 350 | {
|
293 |
| - "cell_type": "code", |
294 |
| - "execution_count": null, |
| 351 | + "cell_type": "markdown", |
295 | 352 | "metadata": {},
|
296 |
| - "outputs": [], |
297 | 353 | "source": [
|
298 |
| - "# Your answer here" |
| 354 | + "time: it makes two passes through the array - once to move non-zero elements to the front, and once to fill the remaining positions with zeros.\n", |
| 355 | + "\n", |
| 356 | + "space; algorithm only uses a single extra variable (write_idx) to track the write position, and all operations are performed in-place on the original array without requiring any additional data structures." |
299 | 357 | ]
|
300 | 358 | },
|
301 | 359 | {
|
|
307 | 365 | ]
|
308 | 366 | },
|
309 | 367 | {
|
310 |
| - "cell_type": "code", |
311 |
| - "execution_count": null, |
| 368 | + "cell_type": "markdown", |
312 | 369 | "metadata": {},
|
313 |
| - "outputs": [], |
314 | 370 | "source": [
|
315 |
| - "# Your answer here" |
| 371 | + "The solution is solid and gets the job done efficiently. The code is clean and easy to follow. Honestly, the alternative approach they mentioned (using left/right pointers with swapping) seems unnecessarily complicated compared to this straightforward two-pass method. Overall, this is a well-thought-out solution that works optimally for the problem." |
316 | 372 | ]
|
317 | 373 | },
|
318 | 374 | {
|
|
332 | 388 | "### Reflection"
|
333 | 389 | ]
|
334 | 390 | },
|
| 391 | + { |
| 392 | + "cell_type": "markdown", |
| 393 | + "metadata": {}, |
| 394 | + "source": [ |
| 395 | + "My biggest learning was seeing that there's different ways to solve the same problem and depending on your appraoch it effect the space time complexity. I tend to go for solutions like loops because it's easy to understand, but using things like sets are more efficent. It helps me think through how more production code is likely structured and how I should be thinking of making things efficent. \n", |
| 396 | + "\n", |
| 397 | + "I didn't get a partner but I went through the excerise and completed the assignment to get as much of the learnings as I could.\n", |
| 398 | + "\n" |
| 399 | + ] |
| 400 | + }, |
335 | 401 | {
|
336 | 402 | "cell_type": "code",
|
337 | 403 | "execution_count": null,
|
338 | 404 | "metadata": {},
|
339 | 405 | "outputs": [],
|
340 | 406 | "source": [
|
341 |
| - "# Your answer here" |
| 407 | + "# Your answer here\n", |
| 408 | + "\n" |
342 | 409 | ]
|
343 | 410 | },
|
344 | 411 | {
|
|
396 | 463 | ],
|
397 | 464 | "metadata": {
|
398 | 465 | "kernelspec": {
|
399 |
| - "display_name": "Python 3 (ipykernel)", |
| 466 | + "display_name": "dsi_p", |
400 | 467 | "language": "python",
|
401 | 468 | "name": "python3"
|
402 | 469 | },
|
|
410 | 477 | "name": "python",
|
411 | 478 | "nbconvert_exporter": "python",
|
412 | 479 | "pygments_lexer": "ipython3",
|
413 |
| - "version": "3.11.5" |
| 480 | + "version": "3.9.7" |
414 | 481 | }
|
415 | 482 | },
|
416 | 483 | "nbformat": 4,
|
|
0 commit comments