Skip to content

Commit 7a01e55

Browse files
committed
Attempt 1 at assignment 2
1 parent 1ba1098 commit 7a01e55

File tree

1 file changed

+180
-20
lines changed

1 file changed

+180
-20
lines changed

02_activities/assignments/assignment_2.ipynb

Lines changed: 180 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,25 @@
2727
},
2828
{
2929
"cell_type": "code",
30-
"execution_count": null,
30+
"execution_count": 2,
3131
"metadata": {},
32-
"outputs": [],
32+
"outputs": [
33+
{
34+
"name": "stdout",
35+
"output_type": "stream",
36+
"text": [
37+
"2\n"
38+
]
39+
}
40+
],
3341
"source": [
3442
"import hashlib\n",
3543
"\n",
3644
"def hash_to_range(input_string: str) -> int:\n",
3745
" hash_object = hashlib.sha256(input_string.encode())\n",
3846
" hash_int = int(hash_object.hexdigest(), 16)\n",
3947
" return (hash_int % 3) + 1\n",
40-
"input_string = \"your_first_name_here\"\n",
48+
"input_string = \"sahil\"\n",
4149
"result = hash_to_range(input_string)\n",
4250
"print(result)\n"
4351
]
@@ -141,13 +149,99 @@
141149
"outputs": [],
142150
"source": [
143151
"# 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",
149159
"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"
151245
]
152246
},
153247
{
@@ -227,7 +321,9 @@
227321
"metadata": {},
228322
"outputs": [],
229323
"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. "
231327
]
232328
},
233329
{
@@ -244,7 +340,18 @@
244340
"metadata": {},
245341
"outputs": [],
246342
"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"
248355
]
249356
},
250357
{
@@ -257,11 +364,45 @@
257364
},
258365
{
259366
"cell_type": "code",
260-
"execution_count": null,
367+
"execution_count": 15,
261368
"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+
],
263379
"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]"
265406
]
266407
},
267408
{
@@ -278,7 +419,9 @@
278419
"metadata": {},
279420
"outputs": [],
280421
"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. "
282425
]
283426
},
284427
{
@@ -295,7 +438,10 @@
295438
"metadata": {},
296439
"outputs": [],
297440
"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. "
299445
]
300446
},
301447
{
@@ -312,7 +458,9 @@
312458
"metadata": {},
313459
"outputs": [],
314460
"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]. "
316464
]
317465
},
318466
{
@@ -338,7 +486,19 @@
338486
"metadata": {},
339487
"outputs": [],
340488
"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"
342502
]
343503
},
344504
{
@@ -396,7 +556,7 @@
396556
],
397557
"metadata": {
398558
"kernelspec": {
399-
"display_name": "Python 3 (ipykernel)",
559+
"display_name": "dsi_participant",
400560
"language": "python",
401561
"name": "python3"
402562
},
@@ -410,7 +570,7 @@
410570
"name": "python",
411571
"nbconvert_exporter": "python",
412572
"pygments_lexer": "ipython3",
413-
"version": "3.11.5"
573+
"version": "3.9.15"
414574
}
415575
},
416576
"nbformat": 4,

0 commit comments

Comments
 (0)