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
89 changes: 73 additions & 16 deletions 02_activities/assignments/assignment_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,27 @@
"cell_type": "code",
"execution_count": null,
"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 = \"simei\"\n",
"result = hash_to_range(input_string)\n",
"print(result)\n"
"print(result)\n",
"\n",
"# assigned problem is #2"
]
},
{
Expand Down Expand Up @@ -67,7 +77,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -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",
" "
]
},
{
Expand All @@ -142,7 +161,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -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. "
]
},
{
Expand All @@ -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"
]
},
{
Expand All @@ -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\")"
]
},
{
Expand All @@ -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."
]
},
{
Expand All @@ -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"
]
},
{
Expand All @@ -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"
]
},
{
Expand Down Expand Up @@ -301,7 +358,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "dsi_participant",
"language": "python",
"name": "python3"
},
Expand All @@ -315,7 +372,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down