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
173 changes: 149 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": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\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 = \"anika\"\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": 3,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -112,7 +120,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -165,11 +173,23 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"\" Find the first integer in a list that repeats. 'First' means the duplicate that appears first in the list when reading left to right. Return the duplicate value. If no duplicates, return -1. \""
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Your answer here"
"# Your answer here\n",
"\" Find the first integer in a list that repeats. 'First' means the duplicate that appears first in the list when reading left to right. Return the duplicate value. If no duplicates, return -1. \""
]
},
{
Expand All @@ -181,11 +201,37 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 21,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Example 1:\n",
"Input: nums = [2, 3, 3, 2]\n",
"Output: 3\n",
"Explanation: 3 appears twice at indices 1 and 2. 2 appears later (index 3).\n",
"\n",
"Example 2:\n",
"Input: nums = [1, 2, 3, 4, 5]\n",
"Output: -1\n",
"Explanation: No duplicates.\n"
]
}
],
"source": [
"# Your answer here"
"# Your answer here\n",
"\n",
"print(\"Example 1:\")\n",
"print(\"Input: nums = [2, 3, 3, 2]\")\n",
"print(\"Output: 3\")\n",
"print(\"Explanation: 3 appears twice at indices 1 and 2. 2 appears later (index 3).\")\n",
"print()\n",
"print(\"Example 2:\")\n",
"print(\"Input: nums = [1, 2, 3, 4, 5]\")\n",
"print(\"Output: -1\")\n",
"print(\"Explanation: No duplicates.\")"
]
},
{
Expand All @@ -198,11 +244,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer here\n",
"from typing import List\n",
"\n",
"def first_duplicate(nums: List[int]) -> int:\n",
" seen = set()\n",
" for num in nums:\n",
" if num in seen:\n",
" return num\n",
" seen.add(num)\n",
" return -1"
]
},
{
Expand All @@ -215,11 +270,38 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 24,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"\" We track seen numbers in a set. Iterate through the list. If the current number is already in the set, it's the first duplicate → return it. Otherwise, add it to the set. If loop finishes, no duplicates → return -1.\\n\\nExample walkthrough: nums = [3, 1, 4, 2, 5, 1, 6]\\n\\nnum = 3, not in set(), add → {3}\\n\\nnum = 1, not in {3}, add → {3, 1}\\n\\nnum = 4, not in {3, 1}, add → {3, 1, 4}\\n\\nnum = 2, not in {3, 1, 4}, add → {3, 1, 4, 2}\\n\\nnum = 5, not in {3, 1, 4, 2}, add → {3, 1, 4, 2, 5}\\n\\nnum = 1, found in set → return 1\\n\""
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Your answer here"
"# Your answer here\n",
"\"\"\" We track seen numbers in a set. Iterate through the list. If the current number is already in the set, it's the first duplicate → return it. Otherwise, add it to the set. If loop finishes, no duplicates → return -1.\n",
"\n",
"Example walkthrough: nums = [3, 1, 4, 2, 5, 1, 6]\n",
"\n",
"num = 3, not in set(), add → {3}\n",
"\n",
"num = 1, not in {3}, add → {3, 1}\n",
"\n",
"num = 4, not in {3, 1}, add → {3, 1, 4}\n",
"\n",
"num = 2, not in {3, 1, 4}, add → {3, 1, 4, 2}\n",
"\n",
"num = 5, not in {3, 1, 4, 2}, add → {3, 1, 4, 2, 5}\n",
"\n",
"num = 1, found in set → return 1\n",
"\"\"\""
]
},
{
Expand All @@ -232,11 +314,35 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 29,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"'\\nTime complexity: O(n) - single pass through list, set operations O(1) average.\\nn = length of input list nums.\\nWe loop through list once: for num in nums: → O(n) iterations.\\nEach iteration: check if num in seen: (O(1) for set lookup) and seen.add(num) (O(1) )\\nTotal: O(n) × O(1) = O(n)\\n\\nSpace complexity: O(n) - in worst case store all elements in set.\\nseen = set() stores unique elements.\\nWorst case: no duplicates, store all n elements → O(n) space.\\nBest case: duplicate at beginning → store only 1 element → O(1) space.\\nWe report worst case: O(n).\\n'"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Your answer here"
"# Your answer here\n",
"\"\"\"\n",
"Time complexity: O(n) - single pass through list, set operations O(1) average.\n",
"n = length of input list nums.\n",
"We loop through list once: for num in nums: → O(n) iterations.\n",
"Each iteration: check if num in seen: (O(1) for set lookup) and seen.add(num) (O(1) )\n",
"Total: O(n) × O(1) = O(n)\n",
"\n",
"Space complexity: O(n) - in worst case store all elements in set.\n",
"seen = set() stores unique elements.\n",
"Worst case: no duplicates, store all n elements → O(n) space.\n",
"Best case: duplicate at beginning → store only 1 element → O(1) space.\n",
"We report worst case: O(n).\n",
"\"\"\""
]
},
{
Expand All @@ -249,11 +355,30 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 28,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"\" \\nUse two nested loops. For each element, check if it appears again later in the list. Return the first one found.\\n\\nHow it works:\\n-Outer loop picks each element (index i).\\n-Inner loop checks all elements after it (index j).\\n-If match found, it's the first duplicate (because we check left to right).\\n\""
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Your answer here"
"# Your answer here\n",
"\"\"\" \n",
"Use two nested loops. For each element, check if it appears again later in the list. Return the first one found.\n",
"\n",
"How it works:\n",
"-Outer loop picks each element (index i).\n",
"-Inner loop checks all elements after it (index j).\n",
"-If match found, it's the first duplicate (because we check left to right).\n",
"\"\"\""
]
},
{
Expand Down Expand Up @@ -301,7 +426,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "algos-env",
"language": "python",
"name": "python3"
},
Expand All @@ -315,7 +440,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.11.3"
}
},
"nbformat": 4,
Expand Down
Loading