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
157 changes: 143 additions & 14 deletions 02_activities/assignments/assignment_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"source": [
"### Part 1: Building the base Anagram Checker\n",
"\n",
"Given two valid strings, check to see if they are anagrams of each other. If it is, return `True`, else `False`. For this part, we can assume that uppercase letters are the same as if it was a lowercase character.\n",
"Given two valid strings, check to see if they are anagrams of each other. If it is, return `True`, else `False`. For this part, we can assume that case letters are the same as if it was a lowercase character.\n",
"\n",
"Examples of anagrams:\n",
"* Silent and Listen\n",
Expand All @@ -58,30 +58,117 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"# In this example we don't care about the case sensitivity, hence we will convert all the text to lower casefold/caseless matching\n",
"\n",
"# Approach: I am thinking about folding, sorting the string, and then comparing strings. If these are really anagrams:\n",
"# Once sorted they should ideally be the same string representation\n",
"\n",
"# We should also handle for string length difference because in that situation, those are not really anagrams.\n",
"\n",
"def anagram_checker(word_a, word_b):\n",
" word_a_fold = word_a.casefold()\n",
" word_b_fold = word_b.casefold()\n",
"\n",
" # Check lengths first because if not, these are not anagrams\n",
" if len(word_a_fold) != len(word_b_fold):\n",
" print(f'Sorry! The words \"{word_a}\" and \"{word_b}\" are NOT anagrams — their lengths differ.')\n",
" return False\n",
" # Compare sorted versions\n",
" if sorted(word_a_fold) == sorted(word_b_fold):\n",
" print(f'Great job! The words \"{word_a}\" and \"{word_b}\" are anagrams.')\n",
" return True\n",
" else:\n",
" print(f'Sorry! The words \"{word_a}\" and \"{word_b}\" are NOT anagrams.')\n",
" return False\n",
"\n",
"anagram_checker(\"Silent\", \"listen\")"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# For testing purposes, we will write our code in the function\n",
"def anagram_checker(word_a, word_b):\n",
" # Your code here\n",
" word_a_fold = word_a.casefold()\n",
" word_b_fold = word_b.casefold()\n",
"\n",
" # Check lengths first because if not, these are not anagrams\n",
" if len(word_a_fold) != len(word_b_fold):\n",
" return False\n",
" # Compare sorted versions\n",
" if sorted(word_a_fold) == sorted(word_b_fold):\n",
" return True\n",
" else:\n",
" return False\n",
" \n",
"# Run your code to check using the words below:\n",
"anagram_checker(\"Silent\", \"listen\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 35,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"Silent\", \"Night\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 36,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"night\", \"Thing\")"
]
Expand All @@ -97,22 +184,64 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 37,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# This is the updated function for case sensitive anagrams. Here my approach changed in two places. \n",
"\n",
"# 1. I changed the variable naming in the function to word_a_str or word_a_string to denote which string I am comparing against\n",
"# 2. I incorporated is_case_sensitive first code block to ensure whether I will approach it with the fold case or leave it as is for case sensitive\n",
"\n",
"def anagram_checker(word_a, word_b, is_case_sensitive):\n",
" # Modify your existing code here\n",
" # code block 1: check whether we are transforming the text to or not\n",
" if is_case_sensitive : \n",
" word_a_str = word_a\n",
" word_b_str = word_b\n",
" else:\n",
" word_a_str = word_a.casefold()\n",
" word_b_str = word_b.casefold()\n",
" # code block 2: Check for anagram\n",
" # Check lengths first because if not, these are not anagrams\n",
" if len(word_a_str) != len(word_b_str):\n",
" return False\n",
" # Compare sorted versions\n",
" if sorted(word_a_str) == sorted(word_b_str):\n",
" return True\n",
" else:\n",
" return False\n",
"\n",
"# Run your code to check using the words below:\n",
"anagram_checker(\"Silent\", \"listen\", False) # True"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 38,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"Silent\", \"Listen\", True) # False"
]
Expand All @@ -130,7 +259,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "new-learner",
"display_name": "dsi_participant",
"language": "python",
"name": "python3"
},
Expand All @@ -144,7 +273,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
"version": "3.9.18"
}
},
"nbformat": 4,
Expand Down