From 34b5da7fa020d10adb401e54fad73e947a70d4f3 Mon Sep 17 00:00:00 2001 From: Amy Li Date: Sun, 12 Oct 2025 18:59:22 -0400 Subject: [PATCH] new sf\tiff --- 02_activities/assignments/assignment_1.ipynb | 89 ++++++++++++++++---- 1 file changed, 73 insertions(+), 16 deletions(-) diff --git a/02_activities/assignments/assignment_1.ipynb b/02_activities/assignments/assignment_1.ipynb index f02523c4..ad7611b4 100644 --- a/02_activities/assignments/assignment_1.ipynb +++ b/02_activities/assignments/assignment_1.ipynb @@ -27,7 +27,15 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], "source": [ "import hashlib\n", "\n", @@ -35,9 +43,11 @@ " 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 = \"simei\"\n", "result = hash_to_range(input_string)\n", - "print(result)\n" + "print(result)\n", + "\n", + "# assigned problem is #2" ] }, { @@ -67,7 +77,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -112,13 +122,22 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "passed\n" + ] + } + ], "source": [ "def is_valid_brackets(s: str) -> bool:\n", " # TODO\n", - " pass" + " pass\n", + " " ] }, { @@ -142,7 +161,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -169,7 +188,8 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# Your answer here\n", + "# You want a valid bracket sequence of open bracket followed by closed bracket of the same type. " ] }, { @@ -185,7 +205,13 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# Your answer here\n", + "# Input: s = \"(]{})\"\n", + "# Output: False\n", + "\n", + "# Input: s = \"([)\"\n", + "# Output: False\n", + "\n" ] }, { @@ -202,7 +228,35 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# Your answer here\n", + "# stack was used above. it is efficient as it processes each bracket exactly once.\n", + "\n", + "def is_valid_brackets(s: str) -> bool:\n", + " stack = []\n", + " open_close_pairs = {'(': ')', '[': ']', '{': '}'}\n", + "\n", + " for char in s:\n", + " if char in open_close_pairs: \n", + " stack.append(char)\n", + " else: \n", + " if not stack:\n", + " return False\n", + " last_opening_bracket = stack.pop()\n", + " expected_closing_bracket = open_close_pairs[last_opening_bracket]\n", + " if char != expected_closing_bracket:\n", + " return False\n", + " return len(stack) == 0\n", + "\n", + " # pass\n", + "\n", + "def test_str(s: str, output: bool):\n", + " assert is_valid_brackets(s) == output\n", + " \n", + "test_str(\"([]{})\", True)\n", + "test_str(\"([)]\", False)\n", + "test_str(\"()[]{}\", True)\n", + "test_str(\"[{]}\", False)\n", + "print(\"passed\")" ] }, { @@ -219,7 +273,8 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# Your answer here\n", + "# It starts with an empty stack and checks the character from the string. if the expected end bracket does not match what the bracket is, then false, otherwise, remove it from the stack and an empty stack returns True." ] }, { @@ -236,7 +291,8 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# Your answer here\n", + "# each bracket is being processed once, the time time it takes grows linearly. the length of the stack depends on how many unopened brackets there are before a closed one" ] }, { @@ -253,7 +309,8 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# Your answer here\n", + "# go through the string, if there is a valid bracket pair, then remove it. if the final string is not empty, then False, otherwise if the string is empty, then True" ] }, { @@ -301,7 +358,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "dsi_participant", "language": "python", "name": "python3" }, @@ -315,7 +372,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.5" + "version": "3.13.5" } }, "nbformat": 4,