From 5de8d07ea997bab563245a4ad81c803aacbe6725 Mon Sep 17 00:00:00 2001 From: liliywu Date: Fri, 19 Jun 2026 16:09:06 -0400 Subject: [PATCH] assignment 1 completed --- 01_materials/slides/2_ds_search_sort.ipynb | 4 +- 02_activities/assignments/assignment_1.ipynb | 114 ++++++++++++++++--- 2 files changed, 103 insertions(+), 15 deletions(-) diff --git a/01_materials/slides/2_ds_search_sort.ipynb b/01_materials/slides/2_ds_search_sort.ipynb index 43ba3333..c4ff5590 100644 --- a/01_materials/slides/2_ds_search_sort.ipynb +++ b/01_materials/slides/2_ds_search_sort.ipynb @@ -422,7 +422,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "algos-env", "language": "python", "name": "python3" }, @@ -436,7 +436,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_1.ipynb b/02_activities/assignments/assignment_1.ipynb index f02523c4..60b868eb 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": [ + "2\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 = \"liliana\"\n", "result = hash_to_range(input_string)\n", "print(result)\n" ] @@ -112,13 +120,58 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def is_valid_brackets(s: str) -> bool:\n", - " # TODO\n", - " pass" + " #create an empty list to store valid strings\n", + " stack = [] \n", + " #define the valid pairs\n", + " pairs = {\")\": \"(\", \"}\": \"{\", \"]\": \"[\"} \n", + "\n", + "#create a for loop:\n", + " for char in s:\n", + " if char in \"({[\":\n", + " #append the first left characters\n", + " stack.append(char)\n", + " else:\n", + " if not stack:\n", + " return False\n", + " # POP = remove the last item from the list AND hand that item back to you \n", + " # ugh suffered on this one. \n", + " # the whole idea was to remove the items (i.e., pop) out if they match . \n", + " # Once the stack is empty, it means that everything matched up \n", + " top = stack.pop()\n", + " if top != pairs[char]:\n", + " return False\n", + " #stack empty = False, therefore not stack = True \n", + " return not stack " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n", + "True\n", + "False\n" + ] + } + ], + "source": [ + "#sanity check?\n", + "\n", + "print(is_valid_brackets(\"([]{})\")) # true\n", + "print(is_valid_brackets(\"([)]\")) # false\n", + "print(is_valid_brackets(\"()[]{}\")) # true\n", + "print(is_valid_brackets(\"[{]}\")) # false" ] }, { @@ -169,7 +222,8 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# In this problem we had to determine whether a pair of strings is valid, where every opening bracket had a matching closing bracket\n", + "#of the same type of the same order. " ] }, { @@ -185,7 +239,33 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# ex 1) descriptiom: A string representing a sequence of food containers and their lids.\n", + "\n", + "#The possible characters are:\n", + "\n", + "#'B' = open Burger bun\n", + "#'b' = close Burger bun\n", + "#'T' = open Taco shell\n", + "#'t' = close Taco shell\n", + "#'P' = open Pizza box\n", + "#'p' = close Pizza box\n", + "\n", + "# input: \"BbTt\"\n", + "# ouput: True\n", + "\n", + "\n", + "#ex 2) description: brand packaging check!\n", + "\n", + "#each brand uses a matching pair:\n", + "#'N' = Nike box is sealed\n", + "#'n' = Nike box is unsealed\n", + "#'A' = Apple package is sealed\n", + "#'a' = Apple package is unsealed\n", + "#'S' = Samsung package is sealed\n", + "#'s' = Samsung package is unsealed\n", + "\n", + "# input: \"NsAa\"\n", + "# ouput: False\n" ] }, { @@ -202,7 +282,10 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# For example 2 :\n", + "\n", + "#Time complexity: I've looped through each string and did a pop plus a dictionary look up. I think the time : O(n) since every character needs to be iterated\n", + "#Space complexity: 0(n) because in the worst case the string is all opening brackets. Every character gets pushed and none get popped, so the stack grows until it holds all n characters " ] }, { @@ -219,7 +302,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# as soon as it finds a matching pair it drops ( last in first out). At the end, if the stack is empty , it means that everything was matched" ] }, { @@ -236,7 +319,12 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# Time complexity is O(n): I make a single pass through the string, and each character costs constant time a push, or a pop plus a dictionary lookup. \n", + "# Since I must look at every character to decide validity, O(n) is the best possible. \n", + "# \n", + "# Space complexity is also O(n): the stack is the only structure that grows with input size,\n", + "# and in the worst case (a string of all opening brackets) every character gets pushed with nothing popped,\n", + "# so the stack reaches size n" ] }, { @@ -301,7 +389,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "algos-env", "language": "python", "name": "python3" }, @@ -315,7 +403,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.5" + "version": "3.11.15" } }, "nbformat": 4,