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
136 changes: 117 additions & 19 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": 11,
"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 = \"abraham\"\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": 12,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -112,7 +120,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -142,7 +150,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -165,11 +173,27 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"'\\nAnswer: \\n\\nIn the list above, there are non-zero elements and zero elements, sort the list in descending order, then move all the zeros elements behind the non-zero elements\\n'"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Your answer here"
"# Your answer here\n",
"'''\n",
"Answer: \n",
"\n",
"In the list above, there are non-zero elements and zero elements, sort the list in descending order, then move all the zeros elements behind the non-zero elements\n",
"'''"
]
},
{
Expand All @@ -181,11 +205,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer here\n",
"\n",
"example_1 = [1,3,4,0,2,0]\n",
"output_1 = [0,0,1,2,3,4]\n",
"\n",
"example_2 = [8,0,3,2,5]\n",
"output_2 = [0,2,3,5,8]\n",
"\n",
"\n",
"\n",
"\n"
]
},
{
Expand All @@ -198,11 +232,44 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 17,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[2, 3, 5, 8, 0]"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Your answer here"
"\n",
"def move_zeros_to_end(nums: List[int]) -> List[int]:\n",
" total = len(nums)\n",
" \n",
" for i in range(total-1):\n",
" min_value = i\n",
" for j in range (i+1,total):\n",
" if nums[min_value]==0:\n",
" if nums[j] != 0:\n",
" min_value = j\n",
"\n",
"\n",
" else:\n",
" if nums[j] != 0 and nums[j]<nums[min_value]:\n",
" min_value = j\n",
" \n",
" nums[i],nums[min_value] = nums[min_value],nums[i]\n",
" \n",
" return nums\n",
"\n",
"\n",
"\n",
"move_zeros_to_end(example_2)"
]
},
{
Expand All @@ -219,7 +286,18 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer here\n",
"'''\n",
"I = current first value or left\n",
"J = current second value or right\n",
"\n",
"\n",
"It checks if the current position i is zero.\n",
"If it is zero, the goal is to find any non-zero element at J and swap it with I.\n",
"If it is not zero, the goal is to find the smallest non-zero element J and swap it with I.\n",
"The smallest non-zero is swapped to its correct first position I in the list.\n",
"\n",
"'''\n"
]
},
{
Expand All @@ -236,7 +314,20 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer here\n",
"\n",
"'''\n",
"Time complexity\n",
"This implementation takes two for loops (nested loops)\n",
"They both run at n-1 and n-2 times\n",
"Since its based on the value on n times its o(n^2)\n",
"\n",
"\n",
"\n",
"Space complexity\n",
"I created total, min_value, i and j \n",
"These are all constant variables because they do not grow with each iteration, they just hold temporary values 0(n)\n",
"'''"
]
},
{
Expand All @@ -253,7 +344,14 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer here\n",
"\n",
"'''\n",
"Another solution some one could do would be to first\n",
"\n",
"use the sorted function to sort the python list - create a new list \n",
"Then looping through this new list, for each non-zero value, move the value to the left\n",
"'''"
]
},
{
Expand Down Expand Up @@ -301,7 +399,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "algos-env (3.11.15)",
"language": "python",
"name": "python3"
},
Expand All @@ -315,7 +413,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.11.15"
}
},
"nbformat": 4,
Expand Down
Loading