Skip to content
Open
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
163 changes: 147 additions & 16 deletions 02_activities/assignments/assignment_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -54,36 +54,128 @@
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Explaining my reasoning\n",
"\n",
"Anagrams are word or phrase formed by rearranging the letters of a different word or phrase, using all the original letters exactly once.\n",
"\n",
"To identify whether two words are anagrams, I *first* make sure all letters within words are lowercase and I remove any spaces. *Second*, I sort the letters in alphabetical order. If two words are anagrams, when sorted they should be identical."
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 28,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 28,
"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",
" \"\"\"\n",
" The anagram_checker will check whether two words specified by the user are anagrams. \\nThe words dont need to be case sensitive.\\nThe function will return true if the words are anagrams.\n",
" \"\"\"\n",
" \n",
" word_a_edited = word_a.lower().replace(\" \", \"\")\n",
" word_b_edited = word_b.lower().replace(\" \", \"\")\n",
" \n",
" return sorted(word_a_edited) == sorted(word_b_edited)\n",
" \n",
"\n",
"# Run your code to check using the words below:\n",
"anagram_checker(\"Silent\", \"listen\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Testing the logic of my function\n",
"\n",
"Here I use the same logic as in the function above to test whether it works using two makde up words. I can confirm the function is correct based on this check"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on function anagram_checker in module __main__:\n",
"\n",
"anagram_checker(word_a, word_b)\n",
" The anagram_checker will check whether two words specified by the user are anagrams. \n",
" The words dont need to be case sensitive.\n",
" The function will return true if the words are anagrams.\n",
"\n"
]
}
],
"source": [
"anagram_checker(\"Silent\", \"Night\")"
"word_a_test=\"Ocean\"\n",
"word_b_test=\"Water\"\n",
"\n",
"word_a_edited_test = word_a_test.lower().replace(\" \", \"\")\n",
"word_b_edited_test = word_b_test.lower().replace(\" \", \"\")\n",
"\n",
"sorted(word_a_edited_test) == sorted(word_b_edited_test)\n",
"\n",
"help(anagram_checker) # Testing the documentation of the function\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"night\", \"Thing\")"
"anagram_checker(\"Silent\", \"Night\") # Confimed correct"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"night\", \"Thing\") # Confimed correct"
]
},
{
Expand All @@ -97,12 +189,27 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 35,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def anagram_checker(word_a, word_b, is_case_sensitive):\n",
" # Modify your existing code here\n",
" if is_case_sensitive==True: return sorted(word_a) == sorted(word_b)\n",
"\n",
" else : word_a_edited = word_a.lower().replace(\" \", \"\")\n",
" word_b_edited = word_b.lower().replace(\" \", \"\")\n",
" return sorted(word_a_edited) == sorted(word_b_edited)\n",
"\n",
"# Run your code to check using the words below:\n",
"anagram_checker(\"Silent\", \"listen\", False) # True"
Expand All @@ -112,16 +219,40 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"Silent\", \"listen\", True) # False"
"anagram_checker(\"Silent\", \"listen\", True) # False\n",
"\n",
"# I am confused as to why the parameter is True. Clearly \"Silent\" and \"listen\" are not case sensitive."

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you put True here in "anagram_checker("Silent", "listen", True)" is actually meaning that you want the logics to consider case sensitivity. And since it is case sensitivity, the outcome is False - capital S for Silent and small s for listen.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I understand now. That was not clear from the question stem but I understand the logic now. I assume that I dont need to change anyting for this assignment?

]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 37,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"Silent\", \"Listen\", True) # False"
]
Expand All @@ -139,7 +270,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "new-learner",
"display_name": "python-env",
"language": "python",
"name": "python3"
},
Expand All @@ -153,7 +284,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
"version": "3.11.15"
}
},
"nbformat": 4,
Expand Down
Loading