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
102 changes: 87 additions & 15 deletions 02_activities/assignments/assignment_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n"
]
}
],
"source": [
"import hashlib\n",
"\n",
Expand Down Expand Up @@ -67,7 +75,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -112,7 +120,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -142,7 +150,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -169,7 +177,9 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer here\n",
"\n",
"#The task is about \"moving all the zeros in list to the end of the array while still maintaining the relative order of the non-zero elements.\""
]
},
{
Expand All @@ -185,7 +195,12 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer here\n",
"input: example_1 = [0, 2, 4, 0, 0, 20]\n",
"output: example_1 = [2, 4, 20, 0, 0, 0]\n",
"\n",
"input: example_2 = [1, 0, 3, 0, 5, 0]\n",
"output: example_2 = [1, 3, 5, 0, 0, 0]"
]
},
{
Expand All @@ -198,11 +213,41 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 3, 12, 0, 0]\n"
]
}
],
"source": [
"# Your answer here"
"# Your answer here\n",
"\n",
"from typing import List\n",
"\n",
"def move_zeros_to_end(nums: List[int]) -> List[int]:\n",
" # keep track of the position to place the next non-zero element\n",
" k = 0\n",
"\n",
" # Scan through the list\n",
" for i in range(len(nums)):\n",
" if nums[i] != 0:\n",
" # Swap current element with the element at position k\n",
" nums[i], nums[k] = nums[k], nums[i]\n",
" # Move k to the next position\n",
" k += 1\n",
"\n",
" return nums\n",
"\n",
"# Example usage\n",
"nums = [0, 1, 0, 3, 12]\n",
"result = move_zeros_to_end(nums)\n",
"print(result) # Output: [1, 3, 12, 0, 0] \n",
" "
]
},
{
Expand All @@ -219,7 +264,25 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer here\n",
"#i and k are pointers. i iterates through the list, while k keeps track of the position to place the next non-zero element. When a non-zero element is found at index i, it is swapped with the element at index k, and k is incremented. This way, all non-zero elements are moved to the front of the list in their original order, and zeros are pushed to the end.\n",
"#For each non-zero element encountered, it is placed at the current position of k, and k is incremented to point to the next position for potential non-zero placement.\n",
"#If there are no non-zero elements, k will remain 0, and the list will be unchanged with all zeros at the end.import hashlib\n",
"#For example, in the list [0, 1, 0, 3, 12], the non-zero elements 1, 3, and 12 are moved to the front in their original order, resulting in [1, 3, 12, 0, 0].\n",
"\n",
"#If it's non-zero\n",
"\n",
"#Swap nums[i] with nums[k] (evev if i == k, it's still a swap but no change occurs. Swap does nothing)\n",
"\n",
"#Increment k - will take the next position for the next non-zero)\n",
"\n",
"#If it's zero: \n",
"#Do nothing - k stays in it place\n",
"\n",
"#The order of non-zero elements is preserved because we only swap when we find a non-zero element, and we always place it at the next available position indicated by k. (\n",
"#Only one pass is needed through the list, making this approach efficient with a time complexity of O(n).\n",
"\n",
"#Zeroes will naturally be pushed to the end as non-zero elements are swapped to the front."
]
},
{
Expand All @@ -236,7 +299,14 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer here\n",
"#The problem's time and space complexity is O(n) and O(1) respectively. This is because we only make a single pass through the list (O(n) time complexity) and use a constant amount of extra space for the pointer k (O(1) space complexity).\n",
"\n",
"#Running time grows linearly with the size of the input list. This loop runs n times, where n is the length of the input list.\n",
"\n",
"#The list has n elements, the loop runs n times, so the time complexity is O(n).\n",
"\n",
"#The algorithm modifies the list in place and uses only a fixed amount of extra space (the pointer k), regardless of the size of the input list. Therefore, the space complexity is O(1).\n"
]
},
{
Expand All @@ -253,7 +323,9 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer here\n",
"#An alternative approach could involve creating a new list to store non-zero elements and then appending zeros at the end. However, this would increase the space complexity to O(n) since we would be using additional space proportional to the size of the input list. The in-place method is more efficient in terms of space usage.\n",
"#It could also involve counting the number of zeros and then reconstructing the list, but this would also require additional space and multiple passes through the list, making it less efficient than the in-place swapping method."
]
},
{
Expand Down Expand Up @@ -301,7 +373,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "algos-env",
"language": "python",
"name": "python3"
},
Expand All @@ -315,7 +387,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.11.14"
}
},
"nbformat": 4,
Expand Down