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
123 changes: 115 additions & 8 deletions 02_activities/assignments/assignment_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -56,32 +56,101 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 12,
"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",
" # Dictionary used to count the frequency of each character\n",
" frequency = {}\n",
" # Case-insensitive comparison\n",
" word_a = word_a.lower()\n",
" word_b = word_b.lower()\n",
"\n",
" # If the length of word_a is not equal to the length of word_b,\n",
" # the words are definitely not anagrams\n",
" if len(word_a) != len(word_b):\n",
" return False\n",
" \n",
" # Count the occurrence of each character in word_a\n",
" # O(n)\n",
" for char in word_a:\n",
" if char not in frequency:\n",
" frequency[char] = 1\n",
" else:\n",
" frequency[char] += 1\n",
"\n",
" # If there is a character in word_b that does not exist in word_a,\n",
" # or if a character occurs more times in word_b than in word_a,\n",
" # then the words are not anagrams \n",
" # O(m)\n",
" for char in word_b:\n",
" if char not in frequency or frequency[char] == 0:\n",
" return False\n",
" else:\n",
" frequency[char] -= 1\n",
" \n",
" # If the frequency of each character in word_a and word_b is exactly the same,\n",
" # then the frequency differences must all be zero\n",
" # O(k)\n",
" for count in frequency.values():\n",
" if count != 0:\n",
" return False\n",
" # O(n) + O(m) + O(k) ~> O(n)\n",
" return True\n",
"# Run your code to check using the words below:\n",
"anagram_checker(\"Silent\", \"listen\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"Silent\", \"Night\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"night\", \"Thing\")"
]
Expand All @@ -103,7 +172,45 @@
"source": [
"def anagram_checker(word_a, word_b, is_case_sensitive):\n",
" # Modify your existing code here\n",
" # Dictionary used to count the frequency of each character\n",
" frequency = {}\n",
"\n",
" if not is_case_sensitive:\n",
" # Case-insensitive comparison\n",
" word_a = word_a.lower()\n",
" word_b = word_b.lower()\n",
"\n",
" # If the length of word_a is not equal to the length of word_b,\n",
" # the words are definitely not anagrams\n",
" if len(word_a) != len(word_b):\n",
" return False\n",
" \n",
" # Count the occurrence of each character in word_a\n",
" # O(n)\n",
" for char in word_a:\n",
" if char not in frequency:\n",
" frequency[char] = 1\n",
" else:\n",
" frequency[char] += 1\n",
"\n",
" # If there is a character in word_b that does not exist in word_a,\n",
" # or if a character occurs more times in word_b than in word_a,\n",
" # then the words are not anagrams \n",
" # O(m)\n",
" for char in word_b:\n",
" if char not in frequency or frequency[char] == 0:\n",
" return False\n",
" else:\n",
" frequency[char] -= 1\n",
" \n",
" # If the frequency of each character in word_a and word_b is exactly the same,\n",
" # then the frequency differences must all be zero\n",
" # O(k)\n",
" for count in frequency.values():\n",
" if count != 0:\n",
" return False\n",
" # O(n) + O(m) + O(k) ~> O(n)\n",
" return True\n",
"# Run your code to check using the words below:\n",
"anagram_checker(\"Silent\", \"listen\", False) # True"
]
Expand All @@ -130,7 +237,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "new-learner",
"display_name": "python-env",
"language": "python",
"name": "python3"
},
Expand All @@ -144,7 +251,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
"version": "3.11.7"
}
},
"nbformat": 4,
Expand Down