Skip to content

Commit 4a02dbb

Browse files
committed
Assignment 1
1 parent b6493b7 commit 4a02dbb

File tree

1 file changed

+168
-40
lines changed

1 file changed

+168
-40
lines changed

02_activities/assignments/assignment_1.ipynb

Lines changed: 168 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,25 @@
2121
},
2222
{
2323
"cell_type": "code",
24-
"execution_count": null,
24+
"execution_count": 85,
2525
"metadata": {},
26-
"outputs": [],
26+
"outputs": [
27+
{
28+
"name": "stdout",
29+
"output_type": "stream",
30+
"text": [
31+
"2\n"
32+
]
33+
}
34+
],
2735
"source": [
2836
"import hashlib\n",
2937
"\n",
3038
"def hash_to_range(input_string: str) -> int:\n",
3139
" hash_object = hashlib.sha256(input_string.encode())\n",
3240
" hash_int = int(hash_object.hexdigest(), 16)\n",
3341
" return (hash_int % 3) + 1\n",
34-
"input_string = \"your_first_name_here\"\n",
42+
"input_string = \"nipun\"\n",
3543
"result = hash_to_range(input_string)\n",
3644
"print(result)\n"
3745
]
@@ -130,18 +138,17 @@
130138
},
131139
{
132140
"cell_type": "code",
133-
"execution_count": null,
141+
"execution_count": 23,
134142
"metadata": {},
135143
"outputs": [],
136144
"source": [
145+
"# Step 1\n",
137146
"# Definition for a binary tree node.\n",
138-
"# class TreeNode(object):\n",
139-
"# def __init__(self, val = 0, left = None, right = None):\n",
140-
"# self.val = val\n",
141-
"# self.left = left\n",
142-
"# self.right = right\n",
143-
"def bt_path(root: TreeNode) -> List[List[int]]:\n",
144-
" # TODO"
147+
"class TreeNode(object):\n",
148+
" def __init__(self, val = 0, left = None, right = None):\n",
149+
" self.val = val\n",
150+
" self.left = left\n",
151+
" self.right = right"
145152
]
146153
},
147154
{
@@ -201,12 +208,15 @@
201208
]
202209
},
203210
{
204-
"cell_type": "code",
205-
"execution_count": null,
206-
"metadata": {},
207-
"outputs": [],
211+
"cell_type": "raw",
212+
"metadata": {
213+
"vscode": {
214+
"languageId": "raw"
215+
}
216+
},
208217
"source": [
209-
"# Your answer here"
218+
"I have a binary tree, I want to find all possible way I can get from the top (or the root node) to the bottom (leaf).\n",
219+
"Since, there are multiple leaves. I have multiple paths."
210220
]
211221
},
212222
{
@@ -217,12 +227,22 @@
217227
]
218228
},
219229
{
220-
"cell_type": "code",
221-
"execution_count": null,
222-
"metadata": {},
223-
"outputs": [],
230+
"cell_type": "raw",
231+
"metadata": {
232+
"vscode": {
233+
"languageId": "raw"
234+
}
235+
},
224236
"source": [
225-
"# Your answer here"
237+
"#Tree # 1 ->\n",
238+
"[1, 2, 3 , 4 , 5, 6 , 7]\n",
239+
"#Should return\n",
240+
"[('1', '2', '4'), ('1', '2', '5'), ('1', '3', '6'), ('1', '3', '7')]\n",
241+
"\n",
242+
"# Tree # 2\n",
243+
"[1, 2, 3, 3, 5 , 'my_Leaf']\n",
244+
"#hould return\n",
245+
"[('1', '2', '3'), ('1', '2', '5'), ('1', '3', 'my_Leaf')]"
226246
]
227247
},
228248
{
@@ -235,11 +255,86 @@
235255
},
236256
{
237257
"cell_type": "code",
238-
"execution_count": null,
258+
"execution_count": 86,
239259
"metadata": {},
240260
"outputs": [],
241261
"source": [
242-
"# Your answer here"
262+
"# Step 2\n",
263+
"# As per the instructions, the input is a list. I have to first convert the list to a tree\n",
264+
"\n",
265+
"from collections import deque\n",
266+
"from typing import List, Optional\n",
267+
"\n",
268+
"\n",
269+
"def list_to_tree(lst: list[Optional[int]]) -> Optional[TreeNode]:\n",
270+
" if not lst or lst[0] is None:\n",
271+
" return None # Empty tree case\n",
272+
"\n",
273+
" root = TreeNode(lst[0]) # Create the root\n",
274+
" queue = deque([root]) # Queue for level-order insertion\n",
275+
" i = 1 # Index in the list\n",
276+
"\n",
277+
" while i < len(lst):\n",
278+
" node = queue.popleft() # Get the current parent node\n",
279+
"\n",
280+
" # Assign left child\n",
281+
" if i < len(lst) and lst[i] is not None:\n",
282+
" node.left = TreeNode(lst[i])\n",
283+
" queue.append(node.left)\n",
284+
" i += 1\n",
285+
"\n",
286+
" # Assign right child\n",
287+
" if i < len(lst) and lst[i] is not None:\n",
288+
" node.right = TreeNode(lst[i])\n",
289+
" queue.append(node.right)\n",
290+
" i += 1\n",
291+
"\n",
292+
" return root # Return the tree root"
293+
]
294+
},
295+
{
296+
"cell_type": "code",
297+
"execution_count": 87,
298+
"metadata": {},
299+
"outputs": [
300+
{
301+
"name": "stdout",
302+
"output_type": "stream",
303+
"text": [
304+
"[('1', '2', '3'), ('1', '2', '5'), ('1', '2', '6'), ('1', '2', '7')]\n"
305+
]
306+
}
307+
],
308+
"source": [
309+
"# Step 3: Recursive method for Depth first search \n",
310+
"def bt_path(root: TreeNode) -> list[list[int]]:\n",
311+
" # TODO\n",
312+
" def dfs(node, path, result): #base case\n",
313+
" if not node: \n",
314+
" return\n",
315+
" path.append(str(node.val)) # Append current node value to path\n",
316+
" if not node.left and not node.right: # If it's a leaf, add path to result, node.left and node.right will both return false for a leaf node\n",
317+
" #result.append(\"->\".join(path)) #End search\n",
318+
" #print(tuple(path))\n",
319+
" result.append(tuple(path))\n",
320+
" else:\n",
321+
" dfs(node.left, path[:], result) # Recursion for left subtree\n",
322+
" dfs(node.right, path[:], result) # Recursion for right subtree\n",
323+
" path.pop() # Backtrack\n",
324+
"\n",
325+
" result = []\n",
326+
" dfs(root, [], result)\n",
327+
" return result\n",
328+
"\n",
329+
"# Step 1: Convert list to binary tree\n",
330+
"tree_list = [1, 2, 2, 3, 5, 6, 7] # Example from above\n",
331+
"root = list_to_tree(tree_list)\n",
332+
"\n",
333+
"# Step 2: Run DFS to get all root-to-leaf paths\n",
334+
"paths = bt_path(root)\n",
335+
"\n",
336+
"# Step 3: Print result\n",
337+
"print(paths) # Expected Output: ['1->2->5', '1->3']"
243338
]
244339
},
245340
{
@@ -251,12 +346,16 @@
251346
]
252347
},
253348
{
254-
"cell_type": "code",
255-
"execution_count": null,
256-
"metadata": {},
257-
"outputs": [],
349+
"cell_type": "raw",
350+
"metadata": {
351+
"vscode": {
352+
"languageId": "raw"
353+
}
354+
},
258355
"source": [
259-
"# Your answer here"
356+
"# Your answer here\n",
357+
"\n",
358+
"It can be broken down into a recursive method, because a big tree is made up a smaller trees. A binary free is made up of 2 smaller trees"
260359
]
261360
},
262361
{
@@ -268,12 +367,19 @@
268367
]
269368
},
270369
{
271-
"cell_type": "code",
272-
"execution_count": null,
273-
"metadata": {},
274-
"outputs": [],
370+
"cell_type": "raw",
371+
"metadata": {
372+
"vscode": {
373+
"languageId": "raw"
374+
}
375+
},
275376
"source": [
276-
"# Your answer here"
377+
"# Your answer here\n",
378+
"\n",
379+
"Time: O(N) Because each path is traversed only once\n",
380+
"Space: O(LogN) for each path, because that is the depth of the tree\n",
381+
" There are N/2 leaves, so N possible paths\n",
382+
" O(NLog(N))"
277383
]
278384
},
279385
{
@@ -285,12 +391,34 @@
285391
]
286392
},
287393
{
288-
"cell_type": "code",
289-
"execution_count": null,
290-
"metadata": {},
291-
"outputs": [],
394+
"cell_type": "raw",
395+
"metadata": {
396+
"vscode": {
397+
"languageId": "raw"
398+
}
399+
},
292400
"source": [
293-
"# Your answer here"
401+
"# Your answer here\n",
402+
"\n",
403+
"Another method is using a stack data-structure. First understand how this DS works:\n",
404+
"\n",
405+
"A stack is a data structure that works like a stack of plates:\n",
406+
"\n",
407+
"1. You add items to the top (push).\n",
408+
"2. You remove items from the top (pop). - LIFO\n",
409+
"3. The last item added is the first one removed (Last In, First Out → LIFO)\n",
410+
"\n",
411+
"This can be implemented in python using lists\n",
412+
"\n",
413+
"Now depth first seach using stacks:\n",
414+
"\n",
415+
"\n",
416+
"1. Start with the root and put it in the stack.\n",
417+
"2. Take a node from the stack, check if it’s a leaf (has no children).\n",
418+
" - If it’s a leaf, save its path.\n",
419+
" - If not, add its children to the stack (right child first, then left).\n",
420+
"3. Repeat until we’ve visited all nodes.\n",
421+
"4. Since we use a stack, we process the left side first (because stacks work in Last In, First Out order)."
294422
]
295423
},
296424
{
@@ -338,7 +466,7 @@
338466
],
339467
"metadata": {
340468
"kernelspec": {
341-
"display_name": "Python 3",
469+
"display_name": "dsi_participant",
342470
"language": "python",
343471
"name": "python3"
344472
},
@@ -352,7 +480,7 @@
352480
"name": "python",
353481
"nbconvert_exporter": "python",
354482
"pygments_lexer": "ipython3",
355-
"version": "3.11.7"
483+
"version": "3.9.18"
356484
}
357485
},
358486
"nbformat": 4,

0 commit comments

Comments
 (0)