diff --git a/02_activities/assignments/assignment_2.ipynb b/02_activities/assignments/assignment_2.ipynb index 26bb3864..98f4e90d 100644 --- a/02_activities/assignments/assignment_2.ipynb +++ b/02_activities/assignments/assignment_2.ipynb @@ -27,9 +27,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], "source": [ "import hashlib\n", "\n", @@ -37,7 +45,7 @@ " 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 = \"alan\"\n", "result = hash_to_range(input_string)\n", "print(result)\n" ] @@ -188,14 +196,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1]\n", + "[2, 3, 4]\n", + "[4, 9]\n" + ] + } + ], "source": [ + "from typing import List\n", + "\n", "def missing_num(nums: List) -> int:\n", - " # TODO" + " # TODO\n", + " n = max(nums)\n", + " full_set = set(range(n + 1))\n", + " missing = list(full_set - set(nums))\n", + " return sorted(missing) if missing else -1\n", + "\n", + "# Example usage:\n", + "nums = [0, 2]\n", + "print(missing_num(nums)) # Output: 1\n", + "\n", + "nums = [5, 0, 1]\n", + "print(missing_num(nums)) # Output: 2\n", + "\n", + "nums = [6, 8, 2, 3, 5, 7, 0, 1, 10]\n", + "print(missing_num(nums)) # Output: 3\n" ] }, { @@ -209,6 +243,13 @@ "You and your partner must share each other's Assignment 1 submission." ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "- Chose RAHELEH MOSLEH as partner and the assignment 1 under \"rmosleh\" github usernmame - https://github.com/rmosleh/algorithms_and_data_structures/blob/assignment_2/02_activities/assignments/assignment_1.ipynb for this review." + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -223,11 +264,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# Your answer here\n", + "# We are given a list of integers. The task is to find the first number that repeats in the list. If no duplicates exist, return -1." ] }, { @@ -240,11 +282,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "\"\\n# New Example:\\nInput: [4, 7, 2, 7, 9]\\nOutput: 7\\n\\n# Walkthrough of partner's example:\\nInput: [2, 0, 2, 0, 35, -1, 6]\\nProcess:\\n- Initialize an empty set.\\n- Check each element:\\n 2 = not seen, add to set\\n 0 = not seen, add to set\\n 2 = already in set, first duplicate found\\nOutput: 2\\n\\n\"" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your answer here" + "# Your answer here\n", + "'''\n", + "# New Example:\n", + "Input: [4, 7, 2, 7, 9]\n", + "Output: 7\n", + "\n", + "# Walkthrough of partner's example:\n", + "Input: [2, 0, 2, 0, 35, -1, 6]\n", + "Process:\n", + "- Initialize an empty set.\n", + "- Check each element:\n", + " 2 = not seen, add to set\n", + " 0 = not seen, add to set\n", + " 2 = already in set, first duplicate found\n", + "Output: 2\n", + "\n", + "'''" ] }, { @@ -257,11 +326,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# Your answer here\n", + "from typing import List\n", + "\n", + "def first_duplicate(nums: List[int]):\n", + " seen = set()\n", + " for n in nums:\n", + " if n in seen:\n", + " return n\n", + " seen.add(n)\n", + " return -1" ] }, { @@ -274,11 +352,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "' \\nThe solution uses a set to track numbers already seen.\\n- As we iterate through the list:\\n - If a number is already in the set > it is a duplicate > return it immediately.\\n - Otherwise, add it to the set.\\n-If no duplicates are found after checking all elements, return -1.\\n\\nThis ensures the first duplicate in order of appearance is returned.\\n'" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your answer here" + "# Your answer here\n", + "''' \n", + "The solution uses a set to track numbers already seen.\n", + "- As we iterate through the list:\n", + " - If a number is already in the set > it is a duplicate > return it immediately.\n", + " - Otherwise, add it to the set.\n", + "-If no duplicates are found after checking all elements, return -1.\n", + "\n", + "This ensures the first duplicate in order of appearance is returned.\n", + "'''" ] }, { @@ -291,11 +389,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 35, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'\\n- Time Complexity: O(n) — each element is checked once.\\n- Space Complexity: O(n) — storing seen numbers in a set.\\n\\n'" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your answer here" + "# Your answer here\n", + "'''\n", + "- Time Complexity: O(n) — each element is checked once.\n", + "- Space Complexity: O(n) — storing seen numbers in a set.\n", + "\n", + "'''" ] }, { @@ -308,11 +422,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 36, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'\\n- Correct and efficient, finds the first duplicate in one pass.\\n- Handles negative numbers and zero correctly.\\n- Could include type hints for clarity (List[int] → int).\\n- Could add a few comments in the code for readability.\\n\\n'" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your answer here" + "# Your answer here\n", + "'''\n", + "- Correct and efficient, finds the first duplicate in one pass.\n", + "- Handles negative numbers and zero correctly.\n", + "- Could include type hints for clarity (List[int] → int).\n", + "- Could add a few comments in the code for readability.\n", + "\n", + "'''" ] }, { @@ -334,11 +466,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 37, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'\\nFor my Assignment 1, I worked on the Missing Number in Range problem. Initially, I considered using a nested loop to check each number in the range, \\nbut I realized that approach would be inefficient. Instead, I used a set to track the numbers already in the list, which made the solution faster and simpler. \\nThis reduced the time complexity. I tested my solution with the provided examples and some additional cases to ensure it handled all situations, including \\nmissing numbers at the start or end of the range. This process reinforced the importance of choosing the right data structure and balancing efficiency with clear, readable code.\\n\\nWhen reviewing my partner’s Assignment 1, I analyzed their solution for finding the first duplicate in a list. Their code also used a set, but it focused on \\nidentifying duplicates in the order they appear. I found their loop structure clear and appreciated their explanations and examples, which made it easy to \\nunderstand their approach. Comparing our solutions showed how the same data structure can be applied to different problems effectively. This experience highlighted \\nthe importance of readability, documenting logic, and considering edge cases. Overall, working on my own assignment and reviewing my partner’s strengthened both my \\nproblem-solving skills and my ability to give and receive constructive feedback in a collaborative setting.\\n\\n'" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your answer here" + "# Your answer here\n", + "'''\n", + "For my Assignment 1, I worked on the Missing Number in Range problem. Initially, I considered using a nested loop to check each number in the range, \n", + "but I realized that approach would be inefficient. Instead, I used a set to track the numbers already in the list, which made the solution faster and simpler. \n", + "This reduced the time complexity. I tested my solution with the provided examples and some additional cases to ensure it handled all situations, including \n", + "missing numbers at the start or end of the range. This process reinforced the importance of choosing the right data structure and balancing efficiency with clear, readable code.\n", + "\n", + "When reviewing my partner’s Assignment 1, I analyzed their solution for finding the first duplicate in a list. Their code also used a set, but it focused on \n", + "identifying duplicates in the order they appear. I found their loop structure clear and appreciated their explanations and examples, which made it easy to \n", + "understand their approach. Comparing our solutions showed how the same data structure can be applied to different problems effectively. This experience highlighted \n", + "the importance of readability, documenting logic, and considering edge cases. Overall, working on my own assignment and reviewing my partner’s strengthened both my \n", + "problem-solving skills and my ability to give and receive constructive feedback in a collaborative setting.\n", + "\n", + "'''" ] }, { @@ -396,7 +552,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "dsi_participant", "language": "python", "name": "python3" }, @@ -410,7 +566,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.5" + "version": "3.9.19" } }, "nbformat": 4,