From 7b33843967efa69a9aced452a68bf99293b2e884 Mon Sep 17 00:00:00 2001 From: "Vinh-Phuc (Vince) Nguyen" Date: Thu, 27 Nov 2025 16:40:04 -0500 Subject: [PATCH 1/2] finish assignment-1, part 1, building the base anagram checker --- 02_activities/assignments/assignment_1.ipynb | 83 ++++++++++++++++++-- 1 file changed, 76 insertions(+), 7 deletions(-) diff --git a/02_activities/assignments/assignment_1.ipynb b/02_activities/assignments/assignment_1.ipynb index 625be0241..11df930e0 100644 --- a/02_activities/assignments/assignment_1.ipynb +++ b/02_activities/assignments/assignment_1.ipynb @@ -58,30 +58,99 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 9, + "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": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "anagram_checker(\"Silent\", \"Night\")" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "anagram_checker(\"night\", \"Thing\")" ] @@ -130,7 +199,7 @@ ], "metadata": { "kernelspec": { - "display_name": "new-learner", + "display_name": "python-env", "language": "python", "name": "python3" }, @@ -144,7 +213,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.8" + "version": "3.11.7" } }, "nbformat": 4, From 25e39e42e723d3b3b9a73310932d9c795ad4b44c Mon Sep 17 00:00:00 2001 From: "Vinh-Phuc (Vince) Nguyen" Date: Thu, 27 Nov 2025 16:48:14 -0500 Subject: [PATCH 2/2] finish assignment-1, part 2, expanding the functionality of the anagram checker --- 02_activities/assignments/assignment_1.ipynb | 50 +++++++++++++++++--- 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/02_activities/assignments/assignment_1.ipynb b/02_activities/assignments/assignment_1.ipynb index 11df930e0..891829495 100644 --- a/02_activities/assignments/assignment_1.ipynb +++ b/02_activities/assignments/assignment_1.ipynb @@ -56,7 +56,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, "outputs": [ { @@ -65,7 +65,7 @@ "True" ] }, - "execution_count": 9, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -117,7 +117,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -126,7 +126,7 @@ "False" ] }, - "execution_count": 10, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } @@ -137,7 +137,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 14, "metadata": {}, "outputs": [ { @@ -146,7 +146,7 @@ "True" ] }, - "execution_count": 11, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } @@ -172,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" ]