Skip to content
Closed
Show file tree
Hide file tree
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
117 changes: 89 additions & 28 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": 1,
"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 = \"mahima\"\n",
"result = hash_to_range(input_string)\n",
"print(result)\n"
]
Expand Down Expand Up @@ -96,6 +104,7 @@
"```\n",
"```python\n",
"Input: s = \"([)]\"\n",
"\n",
"Output: False\n",
"```\n",
"```python\n",
Expand All @@ -112,13 +121,27 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def is_valid_brackets(s: str) -> bool:\n",
" # TODO\n",
" pass"
" stack = []\n",
" matching_brackets = {\n",
" ')':'(',\n",
" '}':'{',\n",
" ']':'['\n",
" }\n",
"\n",
" for char in s:\n",
" if char in matching_brackets.values():\n",
" stack.append(char)\n",
" else:\n",
" if not stack or stack[-1] != matching_brackets[char]:\n",
" return False\n",
" stack.pop()\n",
"\n",
" return len(stack) == 0"
]
},
{
Expand Down Expand Up @@ -164,12 +187,10 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"> We want to check if the open brackets have closed bracket at the same position, order and the same type."
]
},
{
Expand All @@ -179,13 +200,34 @@
"- In this .ipynb file, there are examples that illustrate how the code should work (the examples provided above). Create 2 new examples for the question you have been assigned, that demonstrate you understand the problem. For question 1 and 2, you don't need to create the tree demonstration, just the input and output.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
">Examples:\n",
"Input: s = \"([]())\"\n",
"Output: True\n",
"Input: s = \"(([])\"\n",
"Output: False\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"False\n"
]
}
],
"source": [
"# Your answer here"
"print(is_valid_brackets(\"([]())\"))\n",
"print(is_valid_brackets(\"(([])\"))"
]
},
{
Expand All @@ -202,7 +244,21 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"def is_valid_brackets(s: str) -> bool:\n",
" stack = []\n",
" matching = {')': '(', ']': '[', '}': '{'}\n",
"\n",
" for char in s:\n",
" if char in matching.values():\n",
" stack.append(char)\n",
" else:\n",
" if not stack or stack[-1] != matching[char]:\n",
" return False\n",
" stack.pop()\n",
"\n",
" return stack == []\n",
" \n",
"# Data type is stack, time complexity and space complexity is O(n)"
]
},
{
Expand All @@ -214,12 +270,10 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"> The solution works as stack follows last in first out principle and dictionary helps us match the key value pair. Using the above the function or alogorithm checks each character once making it efficient."
]
},
{
Expand All @@ -231,12 +285,10 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"> The for loop uses O(n) while checking the dictionary, push and pop in stack using O(1), hence O(n) time complexity. Meanwhile for space complexity the stack stores n open brackets and dictionary stores fixed key value brackets i.e O(1), hence O(n)*O(1) = O(n)."
]
},
{
Expand All @@ -248,12 +300,21 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"An alternative solution would be to store closing brackets in stack rather than opening brackets.\n",
"\n",
"Alternate solution:\n",
"\n",
"1. Create an empty stack and matching bracket dictionary for brackets.\n",
"2. Loop through each character of string.\n",
"- If the opening bracket, then push cclosing bracket in stack.\n",
"- If closing bracket then:\n",
" - If empty stack then invalid string.\n",
" - If the top of stack does not match current character then invalid string.\n",
" - Else pop the top of the stack.\n",
"3. After the loop, if the stack is empty then valid string, else invalid string."
]
},
{
Expand Down Expand Up @@ -301,7 +362,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": ".venv",
"language": "python",
"name": "python3"
},
Expand All @@ -315,7 +376,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.9.6"
}
},
"nbformat": 4,
Expand Down
Loading