From 23f551ac21919ece5b510c4a291e90141b8163ef Mon Sep 17 00:00:00 2001 From: Grace Gosselin Date: Fri, 10 Oct 2025 19:32:55 -0400 Subject: [PATCH] First_Push --- 02_activities/assignments/assignment_1.ipynb | 168 +++++++++++++++++-- 1 file changed, 151 insertions(+), 17 deletions(-) diff --git a/02_activities/assignments/assignment_1.ipynb b/02_activities/assignments/assignment_1.ipynb index f02523c4..43d5fd2a 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": 20, "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 = \"grace\"\n", "result = hash_to_range(input_string)\n", "print(result)\n" ] @@ -67,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, "outputs": [], "source": [ @@ -112,7 +120,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -142,7 +150,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": {}, "outputs": [], "source": [ @@ -169,7 +177,11 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# Your answer here\n", + "# I completed question 2 based on the input I received from my name. \n", + "# The goal output is to tell if a string is true or false based on if it mets the criteria of open brackets are closed by the same type of brackets, and open brackets are \n", + "# closed in the correct order. To be closed in order, the first item added to the stack is the last item removed. \n", + "# This means we will always have to be removing from the top of the string, so we are being asked to code a stack, which is an abstract data type.\n" ] }, { @@ -181,11 +193,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "None\n" + ] + } + ], "source": [ - "# Your answer here" + "# New example 1. This should not work because the brackets are not in the correct order and therefore do not close.\n", + "\n", + "print(is_valid_brackets(\")(\"))" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "None\n" + ] + } + ], + "source": [ + "# New example 2. This example should work because the brackets are in the correct order. \n", + "\n", + "print(is_valid_brackets(\"[{}]\"))" ] }, { @@ -198,11 +239,96 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# Your answer here\n", + "def is_valid_brackets(s: str) -> bool:\n", + " stack = []\n", + " closeToOpen = { \")\" : \"(\", \"]\" : \"[\", \"}\" : \"{\"}\n", + "\n", + " for c in s:\n", + " if c in closeToOpen:\n", + " if stack and stack[-1] == closeToOpen[c]:\n", + " stack.pop()\n", + " else:\n", + " return False\n", + " else:\n", + " stack.append(c)\n", + " return True if not stack else False " + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "# Example input one\n", + "print(is_valid_brackets(\"([]{})\")) # Example input one" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], + "source": [ + "# Example input two\n", + "print(is_valid_brackets(\"([)]\"))" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "# Example input three\n", + "print(is_valid_brackets(\"()[]{}\"))" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], + "source": [ + "# Example input four\n", + "print(is_valid_brackets(\"[{]}\"))" ] }, { @@ -219,7 +345,12 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "\n", + "# The stack soloution works because it supports the first in, last out (FILO) behaviour, which is what is needed to ensure brackets are closed in the correct order. \n", + "\n", + "# Open brackets are pushed at the beginning of the string, then popping occurs as items are removed from the top of the string. \n", + "\n", + "# To have a true string, the string must be empty at the end with all the brackets popped. For each close bracket, a open bracket must exist before it to be true. " ] }, { @@ -236,7 +367,9 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "\n", + "# This is an O(n) algorithm because you have to go through every input character once, and the string can hold unlimited characters. \n", + "# Worst case, the string would hold all open brackets. Best case, the string has equal and ordered open to close brackets. The string is ultimately up to the input. \n" ] }, { @@ -253,7 +386,8 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# Alternatively, you could use the delete method in python lists. You could go through thr string and delete all pairs (ex: \"()\") and if you are left with no more brackets, \n", + "# then it is true. If you still have brackets left over, it is false. This is O (n) time. " ] }, { @@ -301,7 +435,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "dsi_production_only", "language": "python", "name": "python3" }, @@ -315,7 +449,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.5" + "version": "3.13.7" } }, "nbformat": 4,