Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 151 additions & 17 deletions 02_activities/assignments/assignment_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,25 @@
},
{
"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",
"def hash_to_range(input_string: str) -> int:\n",
" 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"
]
Expand Down Expand Up @@ -67,7 +75,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -112,7 +120,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -142,7 +150,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -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"
]
},
{
Expand All @@ -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(\"[{}]\"))"
]
},
{
Expand All @@ -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(\"[{]}\"))"
]
},
{
Expand All @@ -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. "
]
},
{
Expand All @@ -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"
]
},
{
Expand All @@ -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. "
]
},
{
Expand Down Expand Up @@ -301,7 +435,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "dsi_production_only",
"language": "python",
"name": "python3"
},
Expand All @@ -315,7 +449,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.13.7"
}
},
"nbformat": 4,
Expand Down