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
224 changes: 189 additions & 35 deletions 02_activities/assignments/assignment_2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,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 = \"Janice\"\n",
"result = hash_to_range(input_string)\n",
"print(result)\n"
]
Expand Down Expand Up @@ -188,14 +196,92 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"def missing_num(nums: List) -> int:\n",
" # TODO"
"from typing import List, Set\n",
"\n",
"# Finds the numbers missing from the range 0 to max(nums) in a list an Returns -1 if no numbers are missing within this range or empty.\n",
"def missing_num(nums: List[int]) -> List[int]:\n",
" if not nums:\n",
" return [-1] # Handle empty list case\n",
"\n",
" max_num = max(nums)\n",
" # Create a set of the numbers based on input list for efficient lookup\n",
" present_numbers = set(nums)\n",
"\n",
" # loop through the range [0, max_num] and if a number in the range is not in the set of present numbers, add it to the missing list\n",
" missing = []\n",
" for i in range(max_num + 1):\n",
" if i not in present_numbers:\n",
" missing.append(i)\n",
"\n",
" if not missing:\n",
" return [-1]\n",
" else:\n",
" return missing"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 3, 4]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"missing_num([5, 0, 1])"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[4, 9]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"missing_num( [6, 8, 2, 3, 5, 7, 0, 1, 10])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"missing_num([0, 2])"
]
},
{
Expand All @@ -209,6 +295,13 @@
"You and your partner must share each other's Assignment 1 submission."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I looked at my partner's code but not sure what is expected beyond this. It appears they did not complete part twoof the assignment. I will answer part three below based on the question 1 that that they answered in part 1 of the assignment."
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -222,12 +315,10 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"The problem is to read the array and efficiently track previously encountered elements to identify the first instance of a duplicate. Given an array of integers, the task is to find the element with the lowest index whose value has already appeared at a lower index within the array. If element does not exist, return a value of -1. Use appropriate data structure to ensure an efficient approach for tracking seen elements, allowing for minimal O(1) lookup times."
]
},
{
Expand All @@ -239,12 +330,37 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"\n",
"New Example:\n",
"\n",
"Input: nums = [1, 2, 3, 4, 1, 2]\n",
"Expected Output: 1\n",
"\n",
"Explanation:\n",
"- Iterate through the list.\n",
"- The first element is 1. We haven't seen it before, so we add it to our set of seen numbers.\n",
"- The second element is 2. We haven't seen it before, so we add it to our set.\n",
"- The third element is 3. We haven't seen it before, so we add it to our set.\n",
"- The fourth element is 4. We haven't seen it before, so we add it to our set.\n",
"- The fifth element is 1. We have seen 1 before (it's in our set). This is the first number that appears more than once, so we return 1.\n",
"\n",
"Kunal's Example Explained:\n",
"\n",
"Input: nums = [3, 1, 4, 2, 5, 1, 6]\n",
"Output: 1\n",
"\n",
"Explanation:\n",
"- Initialized an empty set called \"seen.\"\n",
"- Iterated through the input list \"nums.\"\n",
"- The first element is 3. It's not in seen, so we add it to seen. seen is now {3}.\n",
"- The next element is 1. It's not in seen, so we add it to seen. seen is now {3, 1}.\n",
"- The next element is 4. It's not in seen, so we add it to seen. seen is now {3, 1, 4}.\n",
"- The next element is 2. It's not in seen, so we add it to seen. seen is now {3, 1, 4, 2}.\n",
"- The next element is 5. It's not in seen, so we add it to seen. seen is now {3, 1, 4, 2, 5}.\n",
"- The next element is 1. It is in seen. Since this is the first time we encounter a duplicate, we immediately return this number, which is 1."
]
},
{
Expand All @@ -261,7 +377,30 @@
"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() # create an empty set\n",
" for num in nums: # for loop to iterate through the set passed to the function\n",
" if num in seen: # if a number is seen once, it is added to seen set\n",
" return num # if the for loop shows the number again, the repeating number is returned\n",
" seen.add(num)\n",
" return -1 # if a number does not repeat more than once, -1 is returned\n",
"# **Examples**\n",
"# ```python\n",
"# Input: nums = [3, 1, 4, 2, 5, 1, 6]\n",
"# Output: 1\n",
"# ```\n",
"# ```python\n",
"# Input: nums = [7, 8, 9, 10]\n",
"# Output: -1\n",
"# ```\n",
"# ```python\n",
"# Input: nums = [4, 5, 6, 4, 6]\n",
"# Output: 4\n",
"# ```\n",
"nums1 = [3, 1, 4, 2, 5, 1, 6]"
]
},
{
Expand All @@ -273,12 +412,12 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"Kunal's solution effectively finds the first duplicate by leveraging the properties of a set. It iterate through the list nums and keep a record of the numbers already seen. A set is an ideal data structure for this because it allows for very fast lookups and additions.\n",
"\n",
"The efficiency is derived from the set's ability to perform the \"in\" check and the \"add\" operation in average constant time, making the overall process of scanning the list and tracking seen elements very fast. Checking for the presence of an element in a set is an efficient operation (average O(1) time)."
]
},
{
Expand All @@ -290,12 +429,16 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"Time Complexity:\n",
"\n",
"The time complexity of this solution is O(n), where n is the number of elements in the input list nums. This is because we iterate through the list once. The operations inside the loop, checking if an element is in the set (num in seen) and adding an element to the set (seen.add(num)), take average O(1) time due to the efficient nature of hash sets. Therefore, the total time taken is proportional to the number of elements in the list.\n",
"\n",
"Space Complexity:\n",
"\n",
"The space complexity of this solution is O(n) in the worst case. This is because, in the worst-case scenario (when there are no duplicates in the list), the seen set will store all n unique elements from the input list. The amount of memory used by the set grows linearly with the number of unique elements in the input"
]
},
{
Expand All @@ -307,12 +450,15 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"Kunal's solution is concise, efficient, and correctly solves the problem.\n",
"\n",
"Using a set to track seen elements and returning the first duplicate encountered correctly addresses the problem requirements.\n",
"Using a set results in an average time complexity of O(n), which is optimal for this problem as each element needs to be examined at least once in the worst case. The space complexity is O(n) in the worst case, which is a reasonable trade-off for the time efficiency gained.\n",
"\n",
"A minor adjustment could be to add comments, but this does not impact the solution itself. "
]
},
{
Expand All @@ -333,12 +479,20 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"Assignment1 Process \n",
"\n",
"1. Read the problem statement and requirements: identify the specific algorithmic task, input/output formats, and any performance or correctness constraints. \n",
"2. Implement the core algorithm(s): write the required function(s) or class(es) in the notebook cell(s), following the assignment’s API and edge-case expectations. \n",
"3. Add short, focused helper functions and tests**: include small unit tests or example calls in the notebook to validate behavior on typical and boundary cases. \n",
"4. Measure and document complexity: analyse time and space complexity of your implementation and add brief explanatory comments or Markdown cells describing the reasoning. \n",
"5. Demonstrate results: Show the algorithm’s behavior on example inputs. \n",
"6. Test, Debug & Fix Errors: Run tests, fix bugs, and re-run until tests pass and outputs match expectations. \n",
"7. Reflect and conclude: write notes about limitations, possible optimizations, and what was learned.\n",
"\n",
"My review expereince entailed looking at Kunal's solution to both identify why it worked as well as to critique it. Their solution was a good solution and I didnt see much to critique. Perhaps with more expereince I would have had more feeback.\n"
]
},
{
Expand Down Expand Up @@ -410,7 +564,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.12.1"
}
},
"nbformat": 4,
Expand Down