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
164 changes: 144 additions & 20 deletions 02_activities/assignments/assignment_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -56,32 +56,72 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"these words are anagrams\n"
]
}
],
"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",
" #converting to lowercase before sorting the letters to ensure that only lowercase letters are sorted \n",
" word_a = word_a.lower()\n",
" word_b = word_b.lower()\n",
"\n",
" #sorting the letters in each word\n",
" sorted_a = sorted(word_a)\n",
" sorted_b = sorted(word_b)\n",
"\n",
" \n",
" # Comparing the letters in each word to see if they match\n",
" # adding an if/else statement to print the outcome of the anagram matcher \n",
" if sorted_a == sorted_b:\n",
" print('these words are anagrams')\n",
" else:\n",
" print('these words are not anagrams')\n",
"\n",
"\n",
"# Run your code to check using the words below:\n",
"anagram_checker(\"Silent\", \"listen\")"
"anagram_checker('silent', 'listen')\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"these words are not anagrams\n"
]
}
],
"source": [
"anagram_checker(\"Silent\", \"Night\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"these words are anagrams\n"
]
}
],
"source": [
"anagram_checker(\"night\", \"Thing\")"
]
Expand All @@ -97,33 +137,117 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"These words are anagrams\n"
]
}
],
"source": [
"def anagram_checker(word_a, word_b, is_case_sensitive):\n",
" # Modify your existing code here\n",
" \n",
" # this adds a boolean to the code, if Check if two words are anagrams.\n",
"\n",
"# Run your code to check using the words below:\n",
"anagram_checker(\"Silent\", \"listen\", False) # True"
" #Parameters:\n",
" # word_a (str)\n",
" # word_b (str)\n",
" #is_case_sensitive (bool): True if case matters\n",
" \n",
"\n",
" # only if there is no case sensitivity, convert all letters to lowercase \n",
" if not is_case_sensitive:\n",
" word_a = word_a.lower()\n",
" word_b = word_b.lower()\n",
"\n",
" # for both true and false, sort letters for word and word b \n",
" sorted_a = sorted(word_a)\n",
" sorted_b = sorted(word_b)\n",
"\n",
" # Compare the sorted letters to check if they are the same\n",
" # if the letters are the same print that the word is an anagram\n",
" #if letters not the same, print that word is not an anagram \n",
" if sorted_a == sorted_b:\n",
" print(\"These words are anagrams\")\n",
" else:\n",
" print(\"These words are not anagrams\")\n",
"anagram_checker(\"Silent\", \"listen\", False) # True\n",
"\n",
"#the anagram checker is working"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"These words are not anagrams\n"
]
}
],
"source": [
"#this anagram has both capitals and undercase, and is case sensitive, so the code should return that words are not anagrams \n",
"anagram_checker(\"Silent\", \"listen\", True) # False"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"These words are not anagrams\n"
]
}
],
"source": [
"#this is the same \n",
"anagram_checker(\"Silent\", \"Listen\", True) # False\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"These words are anagrams\n"
]
}
],
"source": [
"anagram_checker (\"melon\", \"Lemon\", False)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"These words are anagrams\n"
]
}
],
"source": [
"anagram_checker(\"Silent\", \"Listen\", True) # False"
"anagram_checker (\"siLent\", \"Listen\", True)"
]
},
{
Expand All @@ -139,7 +263,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "new-learner",
"display_name": ".venv (3.11.14)",
"language": "python",
"name": "python3"
},
Expand All @@ -153,7 +277,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
"version": "3.11.14"
}
},
"nbformat": 4,
Expand Down