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
310 changes: 300 additions & 10 deletions 02_assignments/assignment_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,53 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 85,
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "IndentationError",
"evalue": "unindent does not match any outer indentation level (<tokenize>, line 14)",
"output_type": "error",
"traceback": [
"\u001b[0;36m File \u001b[0;32m<tokenize>:14\u001b[0;36m\u001b[0m\n\u001b[0;31m if len(word_a) != len(word_b):\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m unindent does not match any outer indentation level\n"
]
}
],
"source": [
"# This is a function, which we will learn more about next week. For testing purposes, we will write our code in the function\n",
"def anagram_checker(word_a, word_b):\n",
" # Your code here\n",
" \"\"\"Compares two strings to see if they are anagrams (case in-sensistive).\n",
"\n",
" Compare two strings to see if they are an anagram of each other using two\n",
" dicts. For each letter in a dictionary, the char's value is incremented.\n",
" So \"see\" would translate to a dict value of {\"s\":1, \"e\":2 }.\n",
"\n",
" Doing it this way instead of sorting the words and comparing them moves the\n",
" complexity from O(nlogn) to O(n). Does that matter for something\n",
" this small? Absolutely not.\n",
" \"\"\"\n",
" # early guard, if the word lengths do not match they cannot be an anagram\n",
" if len(word_a) != len(word_b):\n",
" return False\n",
" \n",
" # we do not care about case sensitivity, so make everything lowercase\n",
" word_a = word_a.lower()\n",
" word_b = word_b.lower()\n",
" \n",
" # create empty dicts to store results. We can compare the dicts for equality\n",
" # and return that.\n",
" word_a_dict = {}\n",
" word_b_dict = {}\n",
" \n",
" # for each letter in words, increment the letter in the dictionary by 1.\n",
" # (Could also use a defaultDict here and simply += as well, I think)\n",
" for x in range(len(word_a)):\n",
" word_a_dict[word_a[x]] = 1 + word_a_dict.get(word_a[x], 0)\n",
" word_b_dict[word_b[x]] = 1 + word_b_dict.get(word_b[x], 0)\n",
" \n",
" return word_a_dict == word_b_dict\n",
"\n",
"\n",
"\n",
"# Run your code to check using the words below:\n",
"anagram_checker(\"Slient\", \"listen\")"
Expand All @@ -72,7 +112,18 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"Slient\", \"Night\")"
]
Expand All @@ -81,7 +132,18 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"night\", \"Thing\")"
]
Expand All @@ -99,24 +161,252 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def anagram_checker(word_a, word_b, is_case_sensitive):\n",
" # Modify your existing code here\n",
" \"\"\"Compares two strings to see if they are anagrams (case sensistive).\"\"\"\n",
" \n",
" # early guard\n",
" if len(word_a) != len(word_b):\n",
" return False\n",
" \n",
" # if case sensitivity does not matter, make everything lower case, like in\n",
" # Part 1\n",
" if is_case_sensitive == False: \n",
" word_a = word_a.lower()\n",
" word_b = word_b.lower()\n",
" \n",
" # same logic as part one.\n",
" word_a_dict = {}\n",
" word_b_dict = {}\n",
" for x in range(len(word_a)):\n",
" word_a_dict[word_a[x]] = 1 + word_a_dict.get(word_a[x], 0)\n",
" word_b_dict[word_b[x]] = 1 + word_b_dict.get(word_b[x], 0)\n",
"\n",
" return word_a_dict == word_b_dict\n",
"\n",
"\n",
"# Run your code to check using the words below:\n",
"anagram_checker(\"Slient\", \"listen\", False) # True"
"anagram_checker(\"Slient\", \"listen\", False) # True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"Slient\", \"Listen\", True) # False"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### (DEPRECATED) Part 3: Expanding the functionality once more\n",
"\n",
"I had pulled this repo before the changes came through that removed this upcoming part in PR #28 (commit [here](722958aae60513a21a3043ff075de7c5c679948f))\n",
"I've left part 3 up in the PR because it takes a different approach to anagram checking (dict vs set), which was fun.\n",
"\n",
"---\n",
"Given an array of words, check to see if they are anagrams of each other.\n",
"\n",
"```python\n",
"anagram_checker([\"Slient\", \"night\"], [\"thing\", \"listen\"], False) # True\n",
"anagram_checker([\"Slient\", \"thing\"], [\"night\", \"slient\"], True) # False\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def _sum_of_word_arr(arr):\n",
" # private helper method. Gets the sum of characters in an array\n",
" # so _sum_of_word_arr[\"hi\", \"mom\"] would return 5\n",
" sum = 0\n",
" [(sum := len(x) + sum) for x in arr]\n",
" return sum\n",
"\n",
"def anagram_checker(words_a, words_b, is_case_sensitive):\n",
" # early guard\n",
" if len(words_a) != len(words_b):\n",
" return False\n",
"\n",
" # return early if the values inside words_a and words_b don't sum to the\n",
" # same value, because we'll know that the total characters aren't the same.\n",
" if _sum_of_word_arr(words_a) != _sum_of_word_arr(words_b):\n",
" return False\n",
"\n",
" if is_case_sensitive == False:\n",
" words_a = [a.lower() for a in words_a]\n",
" words_b = [b.lower() for b in words_b]\n",
"\n",
" # switch to sets because dicts aren't hashable in python.\n",
" # As such, we'll sort the characters in each word, and throw them in the set\n",
" # if the sets equal, then we know the arrays are anagrams of each other.\n",
" # Note: this does mean that array ordering is ignored. Sets will also mean\n",
" # that arrays with the same anagrams will be paired down. \n",
" # i.e: ['thing','night'] as a set will only contain {'ghint'}. This should \n",
" # be fine though, as I'm under the assumption that the all values in the \n",
" # compared arrays must be `anagramable`, not only one value.\n",
" a_set = set()\n",
" b_set = set()\n",
"\n",
" [a_set.add(\"\".join(sorted(a))) for a in words_a]\n",
" [b_set.add(\"\".join(sorted(b))) for b in words_b]\n",
" return a_set == b_set\n",
"\n",
"\n",
"# Create your own words of potential anagrams and use your checker to check it\n",
"# anagram_checker(x, y, False)\n",
"anagram_checker([\"Slient\", \"night\"], [\"thing\", \"listen\"], False) # True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker([\"Slient\", \"thing\"], [\"night\", \"slient\"], True) # False"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "'list' object has no attribute 'lower'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[81], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43managram_checker\u001b[49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mbig\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mthing\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mnight\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mslient\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# False\u001b[39;00m\n",
"Cell \u001b[0;32mIn[79], line 11\u001b[0m, in \u001b[0;36managram_checker\u001b[0;34m(word_a, word_b, is_case_sensitive)\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[38;5;66;03m# if case sensitivity does not matter, make everything lower case, like in\u001b[39;00m\n\u001b[1;32m 9\u001b[0m \u001b[38;5;66;03m# Part 1\u001b[39;00m\n\u001b[1;32m 10\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m is_case_sensitive \u001b[38;5;241m==\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m: \n\u001b[0;32m---> 11\u001b[0m word_a \u001b[38;5;241m=\u001b[39m \u001b[43mword_a\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mlower\u001b[49m()\n\u001b[1;32m 12\u001b[0m word_b \u001b[38;5;241m=\u001b[39m word_b\u001b[38;5;241m.\u001b[39mlower()\n\u001b[1;32m 14\u001b[0m \u001b[38;5;66;03m# same logic as part one.\u001b[39;00m\n",
"\u001b[0;31mAttributeError\u001b[0m: 'list' object has no attribute 'lower'"
]
}
],
"source": [
"anagram_checker([\"big\", \"thing\"], [\"night\", \"slient\"], False) # False"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker([\"big\", \"thing\"], [\"night\", \"slient\", \"any\"], False) # False"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker([\"big\", \"thing\", \"no\"], [\"gib\", \"night\", \"on\"], False) # true"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker([\"Big\", \"thing\", \"no\"], [\"gib\", \"night\", \"on\"], True) # false"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -144,7 +434,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
"version": "3.9.19"
}
},
"nbformat": 4,
Expand Down