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
197 changes: 155 additions & 42 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": 14,
"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 = \"jaskirat\"\n",
"result = hash_to_range(input_string)\n",
"print(result)\n"
]
Expand Down Expand Up @@ -67,15 +75,34 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"-1\n",
"4\n"
]
}
],
"source": [
"from typing import List\n",
"\n",
"def first_duplicate(nums: List[int]) -> int:\n",
" # TODO\n",
" pass"
" seen = set() \n",
" for num in nums:\n",
" if num in seen: # found a duplicate\n",
" return num\n",
" seen.add(num) # mark number as seen\n",
" return -1 # no duplicates found\n",
"\n",
"# Example test cases\n",
"print(first_duplicate([3, 1, 4, 2, 5, 1, 6])) \n",
"print(first_duplicate([7, 8, 9, 10])) \n",
"print(first_duplicate([4, 5, 6, 4, 6])) "
]
},
{
Expand Down Expand Up @@ -112,13 +139,11 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def is_valid_brackets(s: str) -> bool:\n",
" # TODO\n",
" pass"
"\n"
]
},
{
Expand All @@ -145,13 +170,7 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from typing import List\n",
"\n",
"def move_zeros_to_end(nums: List[int]) -> List[int]:\n",
" # TODO\n",
" pass"
]
"source": []
},
{
"cell_type": "markdown",
Expand All @@ -164,12 +183,10 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"The task is to identify the first repeated number in a given list of integers. As we move through the list from left to right, we must return the number that appears for the second time earliest in the sequence. If there are several duplicates, return the one whose second appearance happens first. If all the numbers in the list are unique and no duplicates exist, the function should return -1."
]
},
{
Expand All @@ -181,11 +198,33 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 16,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"7\n",
"-1\n"
]
}
],
"source": [
"# Your answer here"
"#Example 1\n",
"\n",
"# Input list with multiple duplicates, but the first duplicate is 7\n",
"nums = [5, 7, 3, 7, 3, 2, 1]\n",
"# Output should be the first number that repeats: 7\n",
"print(first_duplicate(nums)) # Expected output: 7\n",
"\n",
"\n",
"#Example 2\n",
"\n",
"# Input list with no duplicates\n",
"nums = [10, 27, 30, 42, 50]\n",
"# Since there are no duplicates, output should be -1\n",
"print(first_duplicate(nums)) # Expected output: -1"
]
},
{
Expand All @@ -198,11 +237,58 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 17,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"7\n",
"-1\n"
]
}
],
"source": [
"# Your answer here"
"from typing import List\n",
"\n",
"def first_duplicate(nums: List[int]) -> int:\n",
" \n",
" seen = set() # Abstract data type: set for O(1) lookups\n",
" \n",
" for num in nums:\n",
" if num in seen:\n",
" return num \n",
" seen.add(num) # Add number to set\n",
" return -1 # No duplicates found\n",
"\n",
"# New example test cases\n",
"\n",
"# Example 1: first duplicate is 7\n",
"nums1 = [5, 7, 3, 7, 3, 2, 1]\n",
"print(first_duplicate(nums1)) # Expected output: 7\n",
"\n",
"# Example 2: no duplicates\n",
"nums2 = [10, 27, 30, 42, 50]\n",
"print(first_duplicate(nums2)) # Expected output: -1\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This problem asks us to find the first number in a list that appears more than once. To solve it efficiently, we use a set, which allows for constant-time (O(1)) membership checks.\n",
"\n",
"Time Complexity: O(n)\n",
"Each element is checked once, and set lookups are O(1) on average.\n",
"The loop runs once per element → n iterations.\n",
"num in seen is a set lookup, which is O(1) on average (constant time).\n",
"seen.add(num) is also O(1).\n",
"Total Time Complexity = O(n)\n",
"→ The algorithm’s runtime increases linearly with the number of elements in the list.\n",
"\n",
"Space Complexity: O(n)\n",
"In the worst case (no duplicates), we store all elements in the set. The memory used by the program grows linearly with the input size."
]
},
{
Expand All @@ -214,12 +300,15 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"The solution works because it checks each number in the list only once while keeping track of the numbers that have already been seen. As the loop goes through each element; the program looks to see if the current number is already in the seen set.\n",
"If it is, that means this number has appeared before — so it is the first duplicate, and we return it immediately.\n",
"\n",
"If it is not in the set, we add it to seen and continue.\n",
"\n",
"Because we go through the list from left to right, the first time we find a repeat, it must be the earliest duplicate in the list. If the loop finishes and no duplicates are found, the function correctly returns -1. Using a set ensures the lookup and insertion operations are very fast (O(1) time each), making the algorithm both simple and efficient."
]
},
{
Expand All @@ -231,12 +320,22 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"1. Time Complexity\n",
"Loop through the list once. We iterate over nums which has n elements → O(n).\n",
"Check and add to a set:\n",
"Checking if an element is in a set (num in seen) is O(1) on average.\n",
"Adding an element to a set (seen.add(num)) is also O(1) on average.\n",
"Overall time complexity:\n",
"O(n)×O(1)=O(n)\n",
"\n",
"2. Space Complexity\n",
"We use a set called seen to store numbers that we’ve already encountered.\n",
"In the worst case, all numbers are unique, so the set will contain all n elements.\n",
"Space complexity:\n",
"O(n)"
]
},
{
Expand All @@ -248,12 +347,26 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"Alternative Solution: Using a Dictionary to Track Occurrences\n",
"\n",
"Idea:\n",
"Instead of a set, use a dictionary (hash map) to store each number and the index of its first occurrence. \n",
"This way, you can determine which duplicate appears first in the list if multiple duplicates exist.\n",
"\n",
"Create an empty dictionary to store numbers as keys and their occurrence counts as values. Loop through each number in the list from left to right:\n",
"\n",
"If the number is already in the dictionary, it means this number has appeared before. Return it immediately as the first duplicate.\n",
"\n",
"If the number is not in the dictionary, add it with a count of 1.\n",
"\n",
"If the loop finishes and no duplicates are found, return -1.\n",
"\n",
"Complexity\n",
"Time: O(n) — each number is looked up in the dictionary once.\n",
"Space: O(n) — the dictionary stores each unique number.\n"
]
},
{
Expand Down Expand Up @@ -301,7 +414,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "dsi_participant",
"language": "python",
"name": "python3"
},
Expand All @@ -315,7 +428,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.9.19"
}
},
"nbformat": 4,
Expand Down