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
4 changes: 2 additions & 2 deletions 01_materials/slides/2_ds_search_sort.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "algos-env",
"language": "python",
"name": "python3"
},
Expand All @@ -436,7 +436,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.11.15"
}
},
"nbformat": 4,
Expand Down
114 changes: 101 additions & 13 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 = \"liliana\"\n",
"result = hash_to_range(input_string)\n",
"print(result)\n"
]
Expand Down Expand Up @@ -112,13 +120,58 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def is_valid_brackets(s: str) -> bool:\n",
" # TODO\n",
" pass"
" #create an empty list to store valid strings\n",
" stack = [] \n",
" #define the valid pairs\n",
" pairs = {\")\": \"(\", \"}\": \"{\", \"]\": \"[\"} \n",
"\n",
"#create a for loop:\n",
" for char in s:\n",
" if char in \"({[\":\n",
" #append the first left characters\n",
" stack.append(char)\n",
" else:\n",
" if not stack:\n",
" return False\n",
" # POP = remove the last item from the list AND hand that item back to you \n",
" # ugh suffered on this one. \n",
" # the whole idea was to remove the items (i.e., pop) out if they match . \n",
" # Once the stack is empty, it means that everything matched up \n",
" top = stack.pop()\n",
" if top != pairs[char]:\n",
" return False\n",
" #stack empty = False, therefore not stack = True \n",
" return not stack "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"False\n",
"True\n",
"False\n"
]
}
],
"source": [
"#sanity check?\n",
"\n",
"print(is_valid_brackets(\"([]{})\")) # true\n",
"print(is_valid_brackets(\"([)]\")) # false\n",
"print(is_valid_brackets(\"()[]{}\")) # true\n",
"print(is_valid_brackets(\"[{]}\")) # false"
]
},
{
Expand Down Expand Up @@ -169,7 +222,8 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# In this problem we had to determine whether a pair of strings is valid, where every opening bracket had a matching closing bracket\n",
"#of the same type of the same order. "
]
},
{
Expand All @@ -185,7 +239,33 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# ex 1) descriptiom: A string representing a sequence of food containers and their lids.\n",
"\n",
"#The possible characters are:\n",
"\n",
"#'B' = open Burger bun\n",
"#'b' = close Burger bun\n",
"#'T' = open Taco shell\n",
"#'t' = close Taco shell\n",
"#'P' = open Pizza box\n",
"#'p' = close Pizza box\n",
"\n",
"# input: \"BbTt\"\n",
"# ouput: True\n",
"\n",
"\n",
"#ex 2) description: brand packaging check!\n",
"\n",
"#each brand uses a matching pair:\n",
"#'N' = Nike box is sealed\n",
"#'n' = Nike box is unsealed\n",
"#'A' = Apple package is sealed\n",
"#'a' = Apple package is unsealed\n",
"#'S' = Samsung package is sealed\n",
"#'s' = Samsung package is unsealed\n",
"\n",
"# input: \"NsAa\"\n",
"# ouput: False\n"
]
},
{
Expand All @@ -202,7 +282,10 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# For example 2 :\n",
"\n",
"#Time complexity: I've looped through each string and did a pop plus a dictionary look up. I think the time : O(n) since every character needs to be iterated\n",
"#Space complexity: 0(n) because in the worst case the string is all opening brackets. Every character gets pushed and none get popped, so the stack grows until it holds all n characters "
]
},
{
Expand All @@ -219,7 +302,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# as soon as it finds a matching pair it drops ( last in first out). At the end, if the stack is empty , it means that everything was matched"
]
},
{
Expand All @@ -236,7 +319,12 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Time complexity is O(n): I make a single pass through the string, and each character costs constant time a push, or a pop plus a dictionary lookup. \n",
"# Since I must look at every character to decide validity, O(n) is the best possible. \n",
"# \n",
"# Space complexity is also O(n): the stack is the only structure that grows with input size,\n",
"# and in the worst case (a string of all opening brackets) every character gets pushed with nothing popped,\n",
"# so the stack reaches size n"
]
},
{
Expand Down Expand Up @@ -301,7 +389,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "algos-env",
"language": "python",
"name": "python3"
},
Expand All @@ -315,7 +403,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.11.15"
}
},
"nbformat": 4,
Expand Down
Loading