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
313 changes: 296 additions & 17 deletions 02_activities/assignments/assignment_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -56,36 +56,103 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"'True'"
]
},
"execution_count": 2,
"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",
" # make letters lower case in word_a and word_b for easier comparison\n",
" word_a = word_a.lower()\n",
" word_b = word_b.lower()\n",
" # Check if word_a and word_b are the same length\n",
" if len(word_a) != len(word_b):\n",
" # If the words are of different lengths, return the following line\n",
" return \"Word lengths do not match :( \"\n",
" # If the words are the same length, sort the words alphabetically and see if they match to one another\n",
" if sorted(word_a) == sorted(word_b):\n",
" # If they match, return the following line\n",
" return \"True\"\n",
" else:\n",
" # If they do not match, return the following line\n",
" return \"False\"\n",
"\n",
"\n",
"# Run your code to check using the words below:\n",
"anagram_checker(\"Silent\", \"listen\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"'Word lengths do not match :( '"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"Silent\", \"Night\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"'True'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"night\", \"Thing\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'True'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"night\", \"thing\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -97,33 +164,245 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"'True'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# This block was me learning the functionality and is very long, the function I made in the next block is the better version\n",
"\n",
"def anagram_checker(word_a, word_b, is_case_sensitive):\n",
" # Modify your existing code here\n",
" # If is_case_sensitive == False, run the following block of code\n",
" if is_case_sensitive == False:\n",
" # make letters lower case in word_a and word_b for easier comparison\n",
" word_a = word_a.lower()\n",
" word_b = word_b.lower()\n",
" # Check if word_a and word_b are the same length\n",
" if len(word_a) != len(word_b):\n",
" # If the words are of different lengths, return the following line\n",
" return \"Word lengths do not match :( \"\n",
" # If the words are the same length, sort the words alphabetically and see if they match to one another\n",
" if sorted(word_a) == sorted(word_b):\n",
" # If they match, return the following line\n",
" return \"True\"\n",
" else:\n",
" # If they do not match, return the following line\n",
" return \"False\"\n",
" # If is_case_sensitive == True, run the following block of code\n",
" elif is_case_sensitive == True:\n",
" # Check if word_a and word_b are the same length\n",
" if len(word_a) != len(word_b):\n",
" # If the words are of different lengths, return the following line\n",
" return \"Word lengths do not match :( \"\n",
" # If the words are the same length, sort the words alphabetically and see if they match to one another\n",
" if sorted(word_a) == sorted(word_b):\n",
" # If they match, return the following line\n",
" return \"True\"\n",
" else:\n",
" # If they do not match, return the following line\n",
" return \"False\"\n",
"\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": 8,
"metadata": {},
"outputs": [],
"source": [
"anagram_checker(\"Silent\", \"listen\", True) # False"
"# THIS IS THE SHORTER FUNCTION WITH SAME FUNCTIONALITY -------------------------------------------------------\n",
"# This is the condensed code using an 'if not' statement that is more streamlined but achieves the same results as the block above\n",
"\n",
"def anagram_checker(word_a, word_b, is_case_sensitive):\n",
" # If is_case_sensitive is set to False, run the following if statement\n",
" # It converts all letters to lower case for comparison.\n",
" # Otherwise (i.e., is_case_sensitive is set to True) the if statement is skipped\n",
" if not is_case_sensitive:\n",
" word_a = word_a.lower()\n",
" word_b = word_b.lower()\n",
" # Check if word_a and word_b are the same length\n",
" if len(word_a) != len(word_b):\n",
" # If the words are of different lengths, return the following line\n",
" return \"Word lengths do not match :( \"\n",
" # If the words are the same length, sort the words alphabetically and see if they match to one another\n",
" if sorted(word_a) == sorted(word_b):\n",
" # If they match, return the following line\n",
" return \"True\"\n",
" else:\n",
" # If they do not match, return the following line\n",
" return \"False\""
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"'False'"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check if Silent and listen are anagrams, case sensitive\n",
"anagram_checker(\"Silent\", \"listen\", True)\n",
"\n",
"# Should produce 'False', because of captial 'S' and lower case 'l'"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'True'"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check if Silent and listen are anagrams, case doesn't matter\n",
"anagram_checker(\"Silent\", \"listen\", False)\n",
"\n",
"# Should produce 'True', because case sensitivity doesn't matter"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'False'"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check if TomMarvoloRiddle and IamLordVoldemort are anagrams, case sensitive\n",
"anagram_checker(\"TomMarvoloRiddle\", \"IamLordVoldemort\", True)\n",
"\n",
"# Should produce 'False', because of the capital letters"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'True'"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check if TomMarvoloRiddle and IamLordVoldemort are anagrams, case doesn't matter\n",
"anagram_checker(\"TomMarvoloRiddle\", \"IamLordVoldemort\", False)\n",
"\n",
"# Should produce 'True', because case sensitivity doesn't matter"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'False'"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check if ElvisPresley and PresleyLives are anagrams, case sensitive\n",
"anagram_checker(\"ElvisPresley\", \"PresleyLives\", True)\n",
"\n",
"# Should produce 'False', because of the capital letters"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'True'"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check if ElvisPresley and PresleyLives are anagrams, case doesn't matter\n",
"anagram_checker(\"ElvisPresley\", \"PresleyLives\", False)\n",
"\n",
"# Should produce 'True', because case sensitivity doesn't matter"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Word lengths do not match :( '"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"Silent\", \"Listen\", True) # False"
"# A dummy test to see if the output still works when words are completely mismatched\n",
"anagram_checker(\"thisisatest\", \"pleasepassme\", False)"
]
},
{
Expand All @@ -139,7 +418,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "new-learner",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -153,7 +432,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
"version": "3.12.0"
}
},
"nbformat": 4,
Expand Down