Skip to content

Commit 1ba1098

Browse files
committed
Attempt 1 at assignment 1
1 parent 4dba3ed commit 1ba1098

File tree

1 file changed

+53
-12
lines changed

1 file changed

+53
-12
lines changed

02_activities/assignments/assignment_1.ipynb

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,25 @@
2525
},
2626
{
2727
"cell_type": "code",
28-
"execution_count": null,
28+
"execution_count": 12,
2929
"metadata": {},
30-
"outputs": [],
30+
"outputs": [
31+
{
32+
"name": "stdout",
33+
"output_type": "stream",
34+
"text": [
35+
"2\n"
36+
]
37+
}
38+
],
3139
"source": [
3240
"import hashlib\n",
3341
"\n",
3442
"def hash_to_range(input_string: str) -> int:\n",
3543
" hash_object = hashlib.sha256(input_string.encode())\n",
3644
" hash_int = int(hash_object.hexdigest(), 16)\n",
3745
" return (hash_int % 3) + 1\n",
38-
"input_string = \"your_first_name_here\"\n",
46+
"input_string = \"sahil\"\n",
3947
"result = hash_to_range(input_string)\n",
4048
"print(result)\n"
4149
]
@@ -112,7 +120,7 @@
112120
},
113121
{
114122
"cell_type": "code",
115-
"execution_count": 1,
123+
"execution_count": 5,
116124
"metadata": {},
117125
"outputs": [],
118126
"source": [
@@ -169,7 +177,9 @@
169177
"metadata": {},
170178
"outputs": [],
171179
"source": [
172-
"# Your answer here"
180+
"# Your answer here\n",
181+
"# The problem asks us to determine the string consisting of brackets sequence is correct. A valid sequence is that every open bracket\n",
182+
"# has a corresponding close bracket of the same type. "
173183
]
174184
},
175185
{
@@ -185,7 +195,13 @@
185195
"metadata": {},
186196
"outputs": [],
187197
"source": [
188-
"# Your answer here"
198+
"# Your answer here\n",
199+
"\n",
200+
"Input: s = \"([{}])\"\n",
201+
"Output: True \n",
202+
"\n",
203+
"Input: s = \"([{)}]\"\n",
204+
"Output: False "
189205
]
190206
},
191207
{
@@ -202,7 +218,23 @@
202218
"metadata": {},
203219
"outputs": [],
204220
"source": [
205-
"# Your answer here"
221+
"# Your answer here\n",
222+
"\n",
223+
"def is_valid_brackets(s: str) -> bool:\n",
224+
" stack = []\n",
225+
" matching = {')': '(', ']': '[', '}': '{'}\n",
226+
"\n",
227+
" for char in s:\n",
228+
" if char in matching.values():\n",
229+
" stack.append(char)\n",
230+
" elif char in matching:\n",
231+
" if not stack or stack[-1] != matching[char]:\n",
232+
" return False\n",
233+
" stack.pop()\n",
234+
" else:\n",
235+
" return False\n",
236+
"\n",
237+
" return not stack"
206238
]
207239
},
208240
{
@@ -219,7 +251,10 @@
219251
"metadata": {},
220252
"outputs": [],
221253
"source": [
222-
"# Your answer here"
254+
"# Your answer here\n",
255+
"# The solution correctly uses stack to keep track of unmatched opening brackets. The closing bracket is mapped by the dictionary to \n",
256+
"# see if it's the correct opening pair. The opening bracket is stored in a list in the stack. The closing bracket is compard to the\n",
257+
"# stack, and if it matches, the stack is pop. If it doesn't match, code returns False. "
223258
]
224259
},
225260
{
@@ -236,7 +271,10 @@
236271
"metadata": {},
237272
"outputs": [],
238273
"source": [
239-
"# Your answer here"
274+
"# Your answer here\n",
275+
"# The time complexity is O(n), where n is the length of the string. The string is only gone through once, performing constant-time \n",
276+
"# operations. \n",
277+
"# The space complexity is O(n) in the worst case as well, where all characters stored in the stack are opening brackets. "
240278
]
241279
},
242280
{
@@ -253,7 +291,10 @@
253291
"metadata": {},
254292
"outputs": [],
255293
"source": [
256-
"# Your answer here"
294+
"# Your answer here\n",
295+
"# An alternative solution can be repeatedly removing matching pairs of brackets from string until all matches are removed and no more \n",
296+
"# can be removed anymore. This can be done using a loop iteration. All matched pairs will be removed using .replace(). The loop will \n",
297+
"# continue until there are no more matched pairs to be removed. In the end, if the string is empty, all brackets matched."
257298
]
258299
},
259300
{
@@ -301,7 +342,7 @@
301342
],
302343
"metadata": {
303344
"kernelspec": {
304-
"display_name": "Python 3 (ipykernel)",
345+
"display_name": "dsi_participant",
305346
"language": "python",
306347
"name": "python3"
307348
},
@@ -315,7 +356,7 @@
315356
"name": "python",
316357
"nbconvert_exporter": "python",
317358
"pygments_lexer": "ipython3",
318-
"version": "3.11.5"
359+
"version": "3.9.15"
319360
}
320361
},
321362
"nbformat": 4,

0 commit comments

Comments
 (0)