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
219 changes: 205 additions & 14 deletions 02_activities/assignments/assignment_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -56,32 +56,84 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 114,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 114,
"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",
" '''Check if two words are anagrams'''\n",
"\n",
" # Trim words to remove any white space\n",
" word_a = word_a.strip()\n",
" word_b = word_b.strip()\n",
"\n",
" # If the words are not of same length, it is likely they are not anagrams\n",
" if(len(word_a) != len(word_b)):\n",
" return False\n",
" \n",
" # lower case words to make function case insensitive and then sort them\n",
" word_a = \"\".join(sorted(word_a.lower()))\n",
" word_b = \"\".join(sorted(word_b.lower()))\n",
"\n",
" # If the sorted words are not equal then it is likely they are not anagrams\n",
" if(word_a != word_b):\n",
" return False\n",
"\n",
" return True\n",
"\n",
"# Run your code to check using the words below:\n",
"anagram_checker(\"Silent\", \"listen\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 115,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 115,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"Silent\", \"Night\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 116,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 116,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"night\", \"Thing\")"
]
Expand All @@ -97,35 +149,174 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 122,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 122,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def anagram_checker(word_a, word_b, is_case_sensitive):\n",
" # Modify your existing code here\n",
" '''\n",
" Check if two words are anagrams. \\n\n",
" If the check needs to be case sensitive set the third argument to True, else set the third argument to False.\n",
" '''\n",
"\n",
" # Trim words to remove any white space\n",
" word_a = word_a.strip()\n",
" word_b = word_b.strip()\n",
"\n",
" # If the words are not of same length, it is likely they are not anagrams\n",
" if(len(word_a) != len(word_b)):\n",
" return False\n",
" \n",
" if not is_case_sensitive:\n",
" # if not case sensitive - case insensitive\n",
" word_a = word_a.lower()\n",
" word_b = word_b.lower()\n",
" \n",
" # sort words\n",
" word_a = \"\".join(sorted(word_a))\n",
" word_b = \"\".join(sorted(word_b))\n",
"\n",
" if(word_a != word_b):\n",
" return False\n",
"\n",
" return True\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": 123,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 123,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"Silent\", \"listen\", True) # False"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 124,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 124,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"Silent\", \"Listen\", True) # False"
]
},
{
"cell_type": "code",
"execution_count": 125,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 125,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"Silent\", \"Listen\", False) # True"
]
},
{
"cell_type": "code",
"execution_count": 126,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 126,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"night\", \"Thing\", True) # False"
]
},
{
"cell_type": "code",
"execution_count": 127,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 127,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"Silent\", \"Night\", False) # False"
]
},
{
"cell_type": "code",
"execution_count": 128,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 128,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"Silent Night\", \"night silent\", False) # True"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -139,7 +330,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "new-learner",
"display_name": "python-env",
"language": "python",
"name": "python3"
},
Expand All @@ -153,7 +344,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
"version": "3.11.14"
}
},
"nbformat": 4,
Expand Down