Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions 02_activities/assignments/assignment_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "dsi_participant",
"language": "python",
"name": "python3"
},
Expand All @@ -315,7 +315,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.9.19"
}
},
"nbformat": 4,
Expand Down
255 changes: 222 additions & 33 deletions 02_activities/assignments/assignment_2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,23 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n"
]
}
],
"source": [
"import hashlib\n",
"\n",
"def hash_to_range(input_string: str) -> int:\n",
" hash_object = hashlib.sha256(input_string.encode())\n",
" hash_int = int(hash_object.hexdigest(), 16)\n",
" return (hash_int % 3) + 1\n",
"input_string = \"your_first_name_here\"\n",
"input_string = \"jaskirat\"\n",
"result = hash_to_range(input_string)\n",
"print(result)\n"
]
Expand Down Expand Up @@ -86,18 +94,75 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n",
"10\n",
"-1\n"
]
}
],
"source": [
"# Definition for a binary tree node.\n",
"# class TreeNode(object):\n",
"# def __init__(self, val = 0, left = None, right = None):\n",
"# self.val = val\n",
"# self.left = left\n",
"# self.right = right\n",
"\n",
"from collections import deque\n",
"class TreeNode:\n",
" def __init__(self, val=0, left=None, right=None):\n",
" self.val = val\n",
" self.left = left\n",
" self.right = right\n",
"def is_duplicate(root: TreeNode) -> int:\n",
" # TODO"
" if not root:\n",
" return -1\n",
"\n",
" seen = set()\n",
" queue = deque([root])\n",
"\n",
" while queue:\n",
" node = queue.popleft()\n",
"\n",
" if node.val in seen:\n",
" return node.val # first duplicate found (closest to root)\n",
" seen.add(node.val)\n",
"\n",
" if node.left:\n",
" queue.append(node.left)\n",
" if node.right:\n",
" queue.append(node.right)\n",
"\n",
" return -1 # no duplicates\n",
"\n",
" # Helper to build the tree easily\n",
"def build_tree(level_list):\n",
" if not level_list:\n",
" return None\n",
" nodes = [TreeNode(val) if val is not None else None for val in level_list]\n",
" kids = nodes[::-1]\n",
" root = kids.pop()\n",
" for node in nodes:\n",
" if node:\n",
" if kids: node.left = kids.pop()\n",
" if kids: node.right = kids.pop()\n",
" return root\n",
"\n",
"root = build_tree([1, 2, 2, 3, 5, 6, 7])\n",
"print(is_duplicate(root)) # ✅ Output: 2\n",
"\n",
"root = build_tree([1, 10, 2, 3, 10, 12, 12])\n",
"print(is_duplicate(root)) # ✅ Output: 10\n",
" \n",
"root = build_tree([10, 9, 8, 7])\n",
"print(is_duplicate(root)) # ✅ Output: -1\n"
]
},
{
Expand Down Expand Up @@ -136,9 +201,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "IndentationError",
"evalue": "expected an indented block (1667981786.py, line 8)",
"output_type": "error",
"traceback": [
"\u001b[1;36m Cell \u001b[1;32mIn[10], line 8\u001b[1;36m\u001b[0m\n\u001b[1;33m # TODO\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m expected an indented block\n"
]
}
],
"source": [
"# Definition for a binary tree node.\n",
"# class TreeNode(object):\n",
Expand Down Expand Up @@ -222,12 +296,13 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"The task is to check whether a string made up of only brackets — round (), square [], and curly\n",
"{} — forms a valid sequence. A bracket sequence is considered valid when every opening\n",
"bracket has a corresponding closing bracket of the same type, and all brackets are closed in the\n",
"correct order."
]
},
{
Expand All @@ -242,9 +317,70 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Input: {[()()]}\n",
"Output: True\n"
]
}
],
"source": [
"def is_valid_brackets(s: str) -> bool:\n",
" # Check if the length of the input string is even\n",
" # If it's odd, we can immediately return False\n",
" if len(s) % 2 != 0:\n",
" return False\n",
" \n",
" # Use a stack to keep track of opening brackets\n",
" stack = []\n",
" bracket_map = {')': '(', '}': '{', ']': '['}\n",
"\n",
" for char in s:\n",
" # print(\"Current char:\", char)\n",
" # print(\"Current stack:\", stack)\n",
" if char in bracket_map.values(): \n",
" stack.append(char)\n",
" # print(f\"Append: {char} to stack:\", stack)\n",
" elif char in bracket_map.keys():\n",
" if stack == [] or bracket_map[char] != stack.pop():\n",
" # print(f\"Unmatched closing bracket: {char}\")\n",
" return False\n",
" # print(f\"Pop from stack for closing bracket:{bracket_map[char]} {char}\")\n",
" return stack == []\n",
"# New example\n",
"s = \"{[()()]}\"\n",
"print(\"Input:\", s)\n",
"print(\"Output:\", is_valid_brackets(s))\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Input: {[()()]}\n",
"Output: True\n"
]
}
],
"source": [
"# Your answer here"
"#partner's example\n",
"\n",
"Input: s = \"{[()()][]}\"\n",
"print(\"Input:\", s)\n",
"Output: True\n",
"print(\"Output:\", is_valid_brackets(s))\n",
"# he stack is empty at the end — all brackets match correctly"
]
},
{
Expand All @@ -259,9 +395,43 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Input: {[()()]}\n",
"Output: True\n"
]
}
],
"source": [
"# Your answer here"
"def is_valid_brackets(s: str) -> bool:\n",
" # Check if the length of the input string is even\n",
" # If it's odd, we can immediately return False\n",
" if len(s) % 2 != 0:\n",
" return False\n",
" \n",
" # Use a stack to keep track of opening brackets\n",
" stack = []\n",
" bracket_map = {')': '(', '}': '{', ']': '['}\n",
"\n",
" for char in s:\n",
" # print(\"Current char:\", char)\n",
" # print(\"Current stack:\", stack)\n",
" if char in bracket_map.values(): \n",
" stack.append(char)\n",
" # print(f\"Append: {char} to stack:\", stack)\n",
" elif char in bracket_map.keys():\n",
" if stack == [] or bracket_map[char] != stack.pop():\n",
" # print(f\"Unmatched closing bracket: {char}\")\n",
" return False\n",
" # print(f\"Pop from stack for closing bracket:{bracket_map[char]} {char}\")\n",
" return stack == []\n",
"Input: s = \"{[()()][]}\"\n",
"print(\"Input:\", s)\n",
"Output: True\n",
"print(\"Output:\", is_valid_brackets(s))"
]
},
{
Expand All @@ -273,12 +443,10 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"The solution works on Stack ADT is based on the idea that the most recent opening bracket must be closed first, it follows the Last In, First Out (LIFO) principle. The algorithm works because it uses the stack to ensure that every opening bracket is properly closed in the correct order."
]
},
{
Expand All @@ -290,12 +458,15 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"Time Complexity – O(n):\n",
"The function goes through each character in the string exactly once.Each operation inside the loop—appending to a list (push), popping from a list (pop) and dictionary lookups—takes constant time. Since none of these actions depend on anything other than the number of characters in the input, the total time grows linearly with the length of the string. Therefore, the time complexity is O(n), where n is the number of characters.\n",
"\n",
"Space Complexity – O(n):\n",
"In the worst case, if the string contains only opening brackets, all of them are pushed onto the stack before any closing brackets appear. This means the stack could hold up to n elements at once. Even though not all cases use that much space, the worst-case scenario determines the overall complexity. Hence, thespace complexity is O(n) because the extra memory used by the stack grows in\n",
"proportion to the input size."
]
},
{
Expand All @@ -307,12 +478,18 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"My partner’s solution is well-structured . The logic correctly checks for matching pairs of brackets and ensures they are closed in the correct order. The function also includes a clear docstring, meaningful variable names, and helpful comments,\n",
"which make the code easy to read and understand.\n",
"However, there are a few minor points that could be improved:\n",
"\n",
"1. Input Validation: The code assumes that the input will only contain bracket characters. It could be more\n",
"robust by handling unexpected characters (e.g., letters or digits) or ignoring them safely.\n",
"2. Readability Enhancement: could be simplified \n",
"Overall, the solution is accurate, efficient, and well-documented, with only small refinements\n",
"suggested for better readability and resilience."
]
},
{
Expand All @@ -333,12 +510,24 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Reflection\n",
"During these assignments, I focused on solving two different but related problems involving lists and sequences: \n",
"finding the first duplicate in a list and validating bracket sequences. For the first problem, I carefully analyzed \n",
"the requirement to return the first value that repeats in a list of integers, or -1 if no duplicates exist. \n",
"I implemented a solution using a set to track seen numbers, which allowed me to efficiently check for duplicates in O(n) time.\n",
"I tested the function with multiple examples to ensure correctness, including cases with no duplicates and multiple repeating numbers.\n",
"\n",
"During the review with my partner - for the bracket validation problem, I understood that a stack was the ideal data structure \n",
"to ensure that brackets are opened and closed in the correct order and every opening bracket has a corresponding closing\n",
"bracket of the same type. I traced several examples to verify that the function correctly identifies valid.\n",
"Reviewing my partner’s solution provided additional insight into readability and input validation considerations. \n",
"Through both assignments, I learned the importance of clear documentation, step-by-step reasoning, and collaborative review. \n",
"These exercises reinforced my understanding of abstract data types and efficient algorithm design for common sequence problems.\n",
"\n",
"\n"
]
},
{
Expand Down Expand Up @@ -396,7 +585,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "dsi_participant",
"language": "python",
"name": "python3"
},
Expand All @@ -410,7 +599,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.9.19"
}
},
"nbformat": 4,
Expand Down