diff --git a/02_activities/assignments/assignment_2.ipynb b/02_activities/assignments/assignment_2.ipynb index 9cd23b81..5b260ccd 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": 1, "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 = \"augustina\"\n", "result = hash_to_range(input_string)\n", "print(result)\n" ] @@ -192,10 +200,57 @@ "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Unique list: {1, 2, 3, 5, 6, 7, 8, 10}\n" + ] + }, + { + "data": { + "text/plain": [ + "[4, 9]" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "def missing_num(nums: List) -> int:\n", - " # TODO" + "def missing_num(nums: list) -> int:\n", + " \n", + " # sort values in the list\n", + " nums.sort()\n", + "\n", + " # get unique values\n", + " nums_set = set(nums)\n", + " nums = list(nums_set)\n", + " n = len(nums)\n", + " print(f'Unique list: {nums_set}')\n", + " \n", + " # create a list to store missing values\n", + " missing = []\n", + " \n", + " for i in range(1,n):\n", + " # if value not missing within the range, append to 'missing' list\n", + " if nums[i] - nums[i-1] > 1:\n", + " for j in range(nums[i-1]+1, nums[i]):\n", + " missing.append(j)\n", + " \n", + " if missing:\n", + " return missing\n", + " # if all values within range are present, return -1\n", + " else:\n", + " return -1\n", + "\n", + "# nums = [5, 2, 4, 6, 1]\n", + "# nums = [6,8,2,3,5,7,1,1,2,10]\n", + "\n", + "\n", + "missing_num(nums)" ] }, { @@ -222,12 +277,10 @@ ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "# Your answer here" + "> The problem asks to return the first value that is a duplicate in the given list. if there are mutiple duplicate, return the first occurrence and return -1 if there are no duplicates." ] }, { @@ -244,7 +297,12 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# Example 1\n", + "input1 = [0,9,0,6,5,4]\n", + "output1 = [0]\n", + "\n", + "input2 = [3,4,7,8,4,7,8]\n", + "output2 = [4]" ] }, { @@ -257,11 +315,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 80, "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "from typing import List\n", + "\n", + "def first_duplicate(nums: List[int]) -> int:\n", + " seen = set()\n", + " for item in nums:\n", + " if item in seen:\n", + " return item\n", + " else:\n", + " seen.add(item)\n", + " return -1" ] }, { @@ -273,12 +340,10 @@ ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "# Your answer here" + "> José's solution works because he starts off creating an empty set, then for each value in the original list, if it is in the set, the value is returned. Otherwise, the value is added to the set. As the list is iterated through, the moment that it is seen again (already in the set), the \"if\" part of the statement is executed and the loop is broken, as it returns the first duplicate value." ] }, { @@ -290,12 +355,10 @@ ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "# Your answer here" + "> Since there is a loop used to iterate through the list, the time complexity is O(n) and the space complexity is O(n) because a list is used to store the data." ] }, { @@ -307,12 +370,10 @@ ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "# Your answer here" + "> I think José created an algorithm with optimal time complexity. Space complexity can't be improved as we were given the list to work with as the data structure." ] }, { @@ -333,12 +394,12 @@ ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "# Your answer here" + "> In assignment 1, I answered question 3 \"Move All Zeros to the End\". I used a bubble sort method (using a nested for loop) to reach the goal of the problem: move the zeros to the end of the list while maintaining the order of the other values. The first loop iterates through the length of the list. The second loop switches the non-zero integers with the zeros, ultimately moving the zeros to the end of the list. If the current value being evaluated is equal to zero and the next value in the list is not equal to zero, the values switch positions. The counter is incremented by one to continue the iteration.\n", + "\n", + "> My experience with my partner was straight forward. José was assigned the problem \"First Duplicate in List\". He explained his algorithm's function, time and space complexities very well. There was also a very clear explanation provided for an alternative method to solve the question he was assigned." ] }, { @@ -396,7 +457,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "algos-env (3.11.15)", "language": "python", "name": "python3" }, @@ -410,7 +471,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.5" + "version": "3.11.15" } }, "nbformat": 4, diff --git a/02_activities/assignments/assignment_2.pdf b/02_activities/assignments/assignment_2.pdf new file mode 100644 index 00000000..acf2d13a Binary files /dev/null and b/02_activities/assignments/assignment_2.pdf differ