Skip to content
Closed
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
111 changes: 95 additions & 16 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": 2,
"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 = \"agampodi\"\n",
"result = hash_to_range(input_string)\n",
"print(result)\n"
]
Expand Down Expand Up @@ -142,15 +150,46 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"from typing import List\n",
"\n",
"def move_zeros_to_end(nums: List[int]) -> List[int]:\n",
" # TODO\n",
" pass"
" insert_pos = 0 \n",
"\n",
" \n",
" for num in nums:\n",
" if num != 0:\n",
" nums[insert_pos] = num\n",
" insert_pos += 1\n",
"\n",
" \n",
" while insert_pos < len(nums):\n",
" nums[insert_pos] = 0\n",
" insert_pos += 1\n",
"\n",
" return nums\n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 3, 12, 0, 0]\n",
"[4, 5, 6, 0, 0, 0]\n"
]
}
],
"source": [
"print(move_zeros_to_end([0,1,0,3,12])) \n",
"print(move_zeros_to_end([4, 0, 5, 0, 0, 6])) \n"
]
},
{
Expand All @@ -169,7 +208,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"In the a given list, it need to move all the zeros to the right side end while move all the numbers to the left while maintaining the ascending order for the left side"
]
},
{
Expand All @@ -181,11 +220,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 16,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 6, 7, 0, 0]\n",
"[1, 1, 5, 0, 0, 0]\n"
]
}
],
"source": [
"# Your answer here"
"print(move_zeros_to_end([1,0,6,0,7]))\n",
"print(move_zeros_to_end([1,0,1,0,5,0]))"
]
},
{
Expand All @@ -202,7 +251,19 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"from typing import List\n",
"\n",
"def move_zeros_to_end(nums: List[int]) -> List[int]:\n",
" \n",
" insert_pos = 0 \n",
"\n",
" \n",
" for i in range(len(nums)):\n",
" if nums[i] != 0:\n",
" nums[insert_pos], nums[i] = nums[i], nums[insert_pos]\n",
" insert_pos += 1\n",
"\n",
" return nums\n"
]
},
{
Expand All @@ -219,7 +280,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"It ignore zeros. keep non zero numbers and maintain order"
]
},
{
Expand All @@ -236,7 +297,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"since all the elements visited the time complexity is O(n). Space complexity is O(1)"
]
},
{
Expand All @@ -253,7 +314,25 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer herefrom typing import List\n",
"\n",
"def move_zeros_to_end(nums: List[int]) -> List[int]:\n",
" \"\"\"\n",
" Alternative approach using an auxiliary list.\n",
" Collect non-zero elements first, then append zeros.\n",
" Time Complexity: O(n)\n",
" Space Complexity: O(n)\n",
" \"\"\"\n",
" # Step 1: Collect all non-zero elements\n",
" result = [num for num in nums if num != 0]\n",
" \n",
" # Step 2: Count how many zeros were in the original list\n",
" zero_count = len(nums) - len(result)\n",
" \n",
" # Step 3: Append zeros at the end\n",
" result.extend([0] * zero_count)\n",
" \n",
" return result\n"
]
},
{
Expand Down Expand Up @@ -301,7 +380,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -315,7 +394,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.12.3"
}
},
"nbformat": 4,
Expand Down
Loading