Skip to content
Open
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
210 changes: 184 additions & 26 deletions 02_activities/assignments/assignment_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 33,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\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 = \"alan\"\n",
"result = hash_to_range(input_string)\n",
"print(result)\n"
]
Expand Down Expand Up @@ -67,7 +75,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -112,7 +120,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 35,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -142,15 +150,39 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 36,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 3, 12, 0, 0]\n",
"[4, 5, 6, 0, 0, 0]\n"
]
}
],
"source": [
"from typing import List\n",
"\n",
"def move_zeros_to_end(nums: List[int]) -> List[int]:\n",
" # TODO\n",
" pass"
" # Two-pointer approach\n",
" # j is the position to place the next non-zero element\n",
" j = 0\n",
" for i in range(len(nums)):\n",
" if nums[i] != 0:\n",
" nums[j], nums[i] = nums[i], nums[j]\n",
" j += 1\n",
" return nums\n",
"\n",
"input_list = [0, 1, 0, 3, 12]\n",
"result = move_zeros_to_end(input_list)\n",
"print(result)\n",
"\n",
"input_list = [4, 0, 5, 0, 0, 6]\n",
"result = move_zeros_to_end(input_list)\n",
"print(result)\n"
]
},
{
Expand All @@ -165,11 +197,27 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 37,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"'\\nWe are given a list of integers. We need to rearrange it so that all zeros move to the end of the list, but the relative order of non-zero numbers \\nshould stay the same. We must return the new list after doing this rearrangement.\\n\\n'"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Your answer here"
"# Your answer here\n",
"'''\n",
"We are given a list of integers. We need to rearrange it so that all zeros move to the end of the list, but the relative order of non-zero numbers \n",
"should stay the same. We must return the new list after doing this rearrangement.\n",
"\n",
"'''"
]
},
{
Expand All @@ -181,11 +229,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 38,
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer here\n",
"# Example 1:\n",
"input1 = [1, 0, 2, 0, 0, 3]\n",
"output = [1, 2, 3, 0, 0, 0]\n",
"\n",
"# Example 2:\n",
"input2= [0, 6, 0, 9, 8]\n",
"output = [6, 9, 8, 0, 0]\n"
]
},
{
Expand All @@ -198,11 +253,42 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 39,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 0, 0, 0]\n",
"[6, 9, 8, 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",
" # TODO\n",
" # Two-pointer approach\n",
" # j is the position to place the next non-zero element\n",
" j = 0\n",
" for i in range(len(nums)):\n",
" if nums[i] != 0:\n",
" nums[j], nums[i] = nums[i], nums[j]\n",
" j += 1\n",
" return nums\n",
"\n",
"input_list = input1\n",
"result = move_zeros_to_end(input_list)\n",
"print(result)\n",
"\n",
"input_list = input2\n",
"result = move_zeros_to_end(input_list)\n",
"print(result)\n",
"\n"
]
},
{
Expand All @@ -215,11 +301,33 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 40,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"'\\nWe use a two-pointer method:\\n\\n- One pointer (i) scans through every element.\\n- Another pointer (j) marks where the next non-zero should go.\\n- Every time we find a non-zero, we swap it to the front.\\n- At the end, all non-zeros are in their original order, and all zeros are pushed to the back.\\n\\nThis avoids using extra space and maintains order.\\n\\n'"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Your answer here"
"# Your answer here\n",
"'''\n",
"We use a two-pointer method:\n",
"\n",
"- One pointer (i) scans through every element.\n",
"- Another pointer (j) marks where the next non-zero should go.\n",
"- Every time we find a non-zero, we swap it to the front.\n",
"- At the end, all non-zeros are in their original order, and all zeros are pushed to the back.\n",
"\n",
"This avoids using extra space and maintains order.\n",
"\n",
"'''"
]
},
{
Expand All @@ -232,11 +340,30 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 41,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"'\\n- Time Complexity: O(n)\\n - We loop through the list once.\\n\\n- Space Complexity: O(1)\\n - We modify the list in place, no extra memory used (apart from variables).\\n\\n'"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Your answer here"
"# Your answer here\n",
"'''\n",
"- Time Complexity: O(n)\n",
" - We loop through the list once.\n",
"\n",
"- Space Complexity: O(1)\n",
" - We modify the list in place, no extra memory used (apart from variables).\n",
"\n",
"'''"
]
},
{
Expand All @@ -251,9 +378,40 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"'\\nAnother way to solve this:\\n\\n- Start with the original list of numbers. \\n - Example: [0, 1, 0, 3, 12]\\n- Create an empty list to store all the non-zero numbers.\\n - Go through each element in the list.\\n - If the number is not zero, add it to this new list.\\n - After this step, we will have [1, 3, 12].\\n- Count how many zeros were in the original list.\\n - You can either count them directly as you loop, or\\n - Just subtract the length of the new list from the length of the original list.\\n - For example: 5 - 3 = 2 zeros.\\n- Add (append) that many zeros to the end of your new list.\\n - So you extend [1, 3, 12] with [0, 0].\\n- Return or print the new list.\\n - Final output: [1, 3, 12, 0, 0].\\n\\n'"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Your answer here"
"# Your answer here\n",
"\n",
"'''\n",
"Another way to solve this:\n",
"\n",
"- Start with the original list of numbers. \n",
" - Example: [0, 1, 0, 3, 12]\n",
"- Create an empty list to store all the non-zero numbers.\n",
" - Go through each element in the list.\n",
" - If the number is not zero, add it to this new list.\n",
" - After this step, we will have [1, 3, 12].\n",
"- Count how many zeros were in the original list.\n",
" - We can either count them directly as we loop, or\n",
" - Just subtract the length of the new list from the length of the original list.\n",
" - For example: 5 - 3 = 2 zeros.\n",
"- Add (append) that many zeros to the end of our new list.\n",
" - So we extend [1, 3, 12] with [0, 0].\n",
"- Return or print the new list.\n",
" - Final output: [1, 3, 12, 0, 0].\n",
"\n",
"'''\n"
]
},
{
Expand Down Expand Up @@ -301,7 +459,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "dsi_participant",
"language": "python",
"name": "python3"
},
Expand All @@ -315,7 +473,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.9.19"
}
},
"nbformat": 4,
Expand Down