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
100 changes: 76 additions & 24 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": 1,
"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 = \"ashwinder\"\n",
"result = hash_to_range(input_string)\n",
"print(result)\n"
]
Expand Down Expand Up @@ -169,7 +177,8 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer here\n",
"# given a list of integers, we have to place all the zero's at the end of the list and maintains the remaining elements in their relative order. "
]
},
{
Expand All @@ -180,12 +189,17 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer here\n",
"**Input**: nums = [13, 12, 1, 0 , 5, 6, 0]\n",
"\n",
"**Output**: [13, 12, 1, 5, 6, 0, 0]\n",
"\n",
"**Input**: nums = [1, 1, 1, 0, 0, 2]\n",
"\n",
"**Output**: [1, 1, 1, 2, 0, 0]"
]
},
{
Expand All @@ -198,11 +212,49 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer here\n",
"from typing import List\n",
"\n",
"def move_zeros_to_end(nums: List[int]) -> List[int]:\n",
" \n",
" # check the edge case\n",
" if len(nums) == 0:\n",
" return []\n",
"\n",
" result = []\n",
" count_zero = 0\n",
"\n",
" for i in nums:\n",
" if i != 0:\n",
" result.append(i)\n",
" else:\n",
" count_zero += 1\n",
" \n",
" result = result + [0] * count_zero\n",
"\n",
" return result\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1]\n"
]
}
],
"source": [
"nums = [0, 1, 0, 3, 12]\n",
"print(move_zeros_to_end(nums))"
]
},
{
Expand All @@ -214,12 +266,10 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"The solution takes the given list and extract the non zero elements from it and add them to the result list. It also checks the zero elements in the list and keep a count of it. The result list is then appended with the count of zeros. The solution also checks the case when the given list is empty, it returns the empty list without going through all the steps.\n"
]
},
{
Expand All @@ -231,12 +281,12 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"**Time complexity** of the solution = **O(n)** because there is one loop that can run for size n of the list.\n",
"\n",
"**Space complexity** = **O(n)** as it takes an extra list to store the result"
]
},
{
Expand All @@ -248,12 +298,14 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"Another solution:\n",
" 1. Using a pointer or index, move through the entire list and move all non zero elements to the front.\n",
" 2. Then fill the remaining positions of the list with the zeros.\n",
"\n",
"This solution has the same time complexity of O(n) but since it doesn't use an extra list, it has space complexity of O(1). "
]
},
{
Expand Down Expand Up @@ -301,7 +353,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "dsi_participant",
"language": "python",
"name": "python3"
},
Expand All @@ -315,7 +367,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.9.23"
}
},
"nbformat": 4,
Expand Down