Skip to content
Closed
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
108 changes: 75 additions & 33 deletions 02_activities/assignments/assignment_2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,32 @@
"outputs": [],
"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",
"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",
"def bt_path(root: TreeNode) -> List[List[int]]:\n",
" # TODO"
" paths: List[List[int]] = []\n",
"\n",
" def dfs(node:[TreeNode], path: List[int]) -> None\n",
" if node is None:\n",
" return\n",
"\n",
" path.append(node.val)\n",
"\n",
" # leaf = no children\n",
" if node.left is None and node.right is None:\n",
" paths.append(path.copy())\n",
" else:\n",
" dfs(node.left, path)\n",
" dfs(node.right, path)\n",
"\n",
" path.pop() # backtrack\n",
"\n",
" dfs(root, [])\n",
" return paths"
]
},
{
Expand Down Expand Up @@ -222,12 +241,10 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"We have a list of integers that we need to update, so that all zero elements moved to the end of the list while non-zero elements should remain in the same unchanged order."
]
},
{
Expand All @@ -239,12 +256,14 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"input = [9,4,0,0,1,-1]\n",
"\n",
"output = [9,4,1,-1,0,0,]\n",
"\n",
"the partner example clearly shows the understanding of the task assignment as in his example he had an input [8, -1 , 0, 0, 5, 6, 7, 0] and after sorting all the zeros were moved to the end, so the end result is the following - [8, -1 , 5, 6, 7, 0, 0, 0]"
]
},
{
Expand All @@ -257,11 +276,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"from typing import List\n",
"from collections import deque\n",
"\n",
"def move_zeros_to_end(nums: List[int]) -> List[int]:\n",
" #queue to record indices of zeros in ascending order\n",
" zero_inds = deque()\n",
" #forward loop through list\n",
" for i in range(len(nums)):\n",
" if nums[i] == 0:\n",
" #record index of zero\n",
" zero_inds.append(i)\n",
" elif zero_inds:\n",
" #non-zero element encountered after at least one zero element\n",
" #swap non-zero element with first zero\n",
" first_zero_ind = zero_inds.popleft()\n",
" nums[i], nums[first_zero_ind] = nums[first_zero_ind], nums[i]\n",
" #a zero now exists at index i. append i to the queue\n",
" zero_inds.append(i)\n",
" return nums"
]
},
{
Expand All @@ -273,12 +310,10 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"The algorithm scans the list once, records positions of zeros and whenever a non-zero appears after a zero - it swaps them so that all zeros are moved to the end while preserving the relative order of non-zero elements."
]
},
{
Expand All @@ -290,12 +325,10 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"Each element in this solution is added and then removed from the queue at least once, so totally making the time complexity as O(n). The deque stores indices of zeros, so in case all elements are zero, the space complexity is O(n)"
]
},
{
Expand All @@ -306,14 +339,19 @@
"- Critique your partner's solution, including explanation, and if there is anything that should be adjusted.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p style> I find the solution, including the explanation, are well written and self-explanatory. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
]
"source": []
},
{
"cell_type": "markdown",
Expand All @@ -333,12 +371,16 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"<p style>\n",
"Working on Assignment 1 helped me better understand how basic data structures like stacks and queues work in real problems. In my solution for validating brackets, I used a stack to keep track of opening brackets and match them correctly with closing ones. This helped me clearly see how the LIFO (last-in, first-out) principle works and why a stack is the right choice for problems with nested structures. While coding, I also learned to think more carefully about edge cases, such as empty strings or unmatched brackets.\n",
"\n",
"Reviewing my partner’s solution was also very helpful. Their approach for moving zeros to the end of a list used a queue to store the indices of zeros and perform swaps while keeping the order of non-zero elements unchanged. This showed me a different way to solve the problem efficiently and helped me understand what a stable solution means in practice. It also made me think more about time and space complexity and how different data structures affect performance.\n",
"\n",
"In this assignment, I mainly reviewed my partner’s code and answered the related questions, rather than having my own work reviewed. This gave me valuable experience with code review, which is a common and important practice in software development teams. Reviewing someone else’s solution required me to carefully analyze logic, clarity, and efficiency, and helped me develop stronger analytical skills.\n",
"</p>"
]
},
{
Expand Down Expand Up @@ -396,7 +438,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "openai",
"language": "python",
"name": "python3"
},
Expand All @@ -410,7 +452,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.10.11"
}
},
"nbformat": 4,
Expand Down