Skip to content

Commit e9ad0a8

Browse files
Completed assigment-2
1 parent 4dba3ed commit e9ad0a8

File tree

1 file changed

+96
-12
lines changed

1 file changed

+96
-12
lines changed

02_activities/assignments/assignment_2.ipynb

Lines changed: 96 additions & 12 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": 1,
3131
"metadata": {},
32-
"outputs": [],
32+
"outputs": [
33+
{
34+
"name": "stdout",
35+
"output_type": "stream",
36+
"text": [
37+
"1\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 = \"lakshmi\"\n",
4149
"result = hash_to_range(input_string)\n",
4250
"print(result)\n"
4351
]
@@ -86,18 +94,94 @@
8694
},
8795
{
8896
"cell_type": "code",
89-
"execution_count": null,
97+
"execution_count": 29,
9098
"metadata": {},
9199
"outputs": [],
92100
"source": [
93101
"# 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",
99108
"def is_duplicate(root: TreeNode) -> int:\n",
100-
" # TODO"
109+
" if not root:\n",
110+
" return -1\n",
111+
" \n",
112+
" seen = set()\n",
113+
" queue = [root]\n",
114+
" \n",
115+
" while queue:\n",
116+
" node = queue.pop(0)\n",
117+
" if node.val in seen:\n",
118+
" return node.val\n",
119+
" seen.add(node.val)\n",
120+
" \n",
121+
" if node.left:\n",
122+
" queue.append(node.left)\n",
123+
" if node.right:\n",
124+
" queue.append(node.right)\n",
125+
" \n",
126+
" return -1\n",
127+
"\n",
128+
"# Helper function to create test trees\n",
129+
"def create_tree_from_list(values):\n",
130+
" \"\"\"Create a binary tree from a list using level-order insertion\"\"\"\n",
131+
" if not values:\n",
132+
" return None\n",
133+
" \n",
134+
" root = TreeNode(values[0])\n",
135+
" queue = [root] # Use simple list instead of deque\n",
136+
" i = 1\n",
137+
" \n",
138+
" while queue and i < len(values):\n",
139+
" node = queue.pop(0) # Remove from front\n",
140+
" \n",
141+
" # Add left child\n",
142+
" if i < len(values) and values[i] is not None:\n",
143+
" node.left = TreeNode(values[i])\n",
144+
" queue.append(node.left)\n",
145+
" i += 1\n",
146+
" \n",
147+
" # Add right child\n",
148+
" if i < len(values) and values[i] is not None:\n",
149+
" node.right = TreeNode(values[i])\n",
150+
" queue.append(node.right)\n",
151+
" i += 1\n",
152+
" \n",
153+
" return root\n"
154+
]
155+
},
156+
{
157+
"cell_type": "code",
158+
"execution_count": 34,
159+
"metadata": {},
160+
"outputs": [],
161+
"source": [
162+
"def test_examples():\n",
163+
" # Example : [1, 2, 2, 3, 5, 6, 7] -> Expected output: 2\n",
164+
" tree1 = create_tree_from_list([1, 2, 2, 3, 5, 6, 7])\n",
165+
" result1 = is_duplicate(tree1)\n",
166+
" print(f\"Example: {result1}\") # Should print 2\n",
167+
" "
168+
]
169+
},
170+
{
171+
"cell_type": "code",
172+
"execution_count": 36,
173+
"metadata": {},
174+
"outputs": [
175+
{
176+
"name": "stdout",
177+
"output_type": "stream",
178+
"text": [
179+
"Example: 2\n"
180+
]
181+
}
182+
],
183+
"source": [
184+
"test_examples()"
101185
]
102186
},
103187
{
@@ -396,7 +480,7 @@
396480
],
397481
"metadata": {
398482
"kernelspec": {
399-
"display_name": "Python 3 (ipykernel)",
483+
"display_name": "dsi_participant",
400484
"language": "python",
401485
"name": "python3"
402486
},
@@ -410,7 +494,7 @@
410494
"name": "python",
411495
"nbconvert_exporter": "python",
412496
"pygments_lexer": "ipython3",
413-
"version": "3.11.5"
497+
"version": "3.9.15"
414498
}
415499
},
416500
"nbformat": 4,

0 commit comments

Comments
 (0)