diff --git a/02_activities/assignments/assignment_1.ipynb b/02_activities/assignments/assignment_1.ipynb index f02523c4..29122fe3 100644 --- a/02_activities/assignments/assignment_1.ipynb +++ b/02_activities/assignments/assignment_1.ipynb @@ -25,9 +25,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], "source": [ "import hashlib\n", "\n", @@ -35,7 +43,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 = \"Isra\"\n", "result = hash_to_range(input_string)\n", "print(result)\n" ] @@ -165,11 +173,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# The problem is to find the first number that is repeated when iterating through the list and return that number, \n", + "# if there was no duplicate, return -1" ] }, { @@ -181,11 +190,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# nums = [2,1,3,5,3,2], this should return 3\n", + "# nums = [5,6,7,8], this should return -1\n", + "# nums = [1,2,3,4,4,5,5,6,6,7], this should return 4" ] }, { @@ -198,11 +209,92 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from typing import List\n", + "\n", + "def first_duplicate(nums: List[int]) -> int:\n", + " found = set()\n", + " for num in nums: \n", + " if num in found:\n", + " return num\n", + " found.add(num)\n", + " return -1\n", + "\n", + "first_duplicate([2, 1, 3, 5, 3, 2]) " + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "first_duplicate([3, 1, 4, 2, 5, 1, 6])" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "-1" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "first_duplicate([7, 8, 9, 10])" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your answer here" + "first_duplicate([1,2,3,4,4,5,5,6,6,7])" ] }, { @@ -215,11 +307,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# In my code, I used a set to keep track of the the numbers that have already been found. \n", + "# As I iterate through the list, I check if the number was already in the set. \n", + "# If it was, I will return that number as it is the first duplicate. \n", + "# If it wasn't found, I will add it to the set and continue until the end of the list.\n", + "# If no duplicates were found, I return -1." ] }, { @@ -232,11 +328,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# In terms of time complexity, this solution is O(n) because we only iterate through it once. \n", + "# In terms of space complexity, this solution is O(n) in the worst case scenario, if there are no duplicates.\n", + "# In the best scenario, if the first two numbers are the same, the space complexity is O(1).\n", + "# This solution is effiencet in time complexity but not in space complexity. " ] }, { @@ -249,11 +348,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# An alterative solution would be to sort the list and then look for duplicates. \n", + "# This would add a time complexity because of the sorting step.\n", + "# making the time complexity O(n Log n) but we would need to store anything so the space complexity would be O(1)" ] }, { @@ -301,7 +402,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "dsi_participant", "language": "python", "name": "python3" }, @@ -315,7 +416,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.5" + "version": "3.9.23" } }, "nbformat": 4,