From ec6355ebf9aadca7f878ceff923ed936dc325bb3 Mon Sep 17 00:00:00 2001 From: Alejandro Castellanos Date: Thu, 28 Nov 2024 20:51:18 -0500 Subject: [PATCH 1/4] Anagram Checker --- 02_activities/assignments/assignment_1.ipynb | 237 +++++++++++++++++-- 1 file changed, 218 insertions(+), 19 deletions(-) diff --git a/02_activities/assignments/assignment_1.ipynb b/02_activities/assignments/assignment_1.ipynb index cbfd8c709..0e954ce39 100644 --- a/02_activities/assignments/assignment_1.ipynb +++ b/02_activities/assignments/assignment_1.ipynb @@ -56,34 +56,104 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "'silent' and 'listen' are anagrams :)\n" + ] + }, + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# This is a function, which we will learn more about next week. 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", + " Check if two wors are anagrams of each other, adding verbose statement.\n", + " :param word_a: First word (string)\n", + " :param word_b: Second word (string)\n", + " \"\"\"\n", + "# Convert words to lowercase\n", + " word_a = word_a.lower()\n", + " word_b = word_b.lower()\n", + " \n", + "# Check if sorted characters of both words are the same\n", + " anagram_checker = sorted(word_a) == sorted(word_b)\n", + "\n", + " if anagram_checker:\n", + " print(f\"'{word_a}' and '{word_b}' are anagrams :)\")\n", + " else:\n", + " print(f\"'{word_a}' and '{word_b}' are not anagrams :(\")\n", + "\n", + " return anagram_checker\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": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "'silent' and 'night' are not anagrams :(\n" + ] + }, + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "anagram_checker(\"Silent\", \"Night\")" + "anagram_checker(\"Silent\", \"Night\")\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "'night' and 'thing' are anagrams :)\n" + ] + }, + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "anagram_checker(\"night\", \"Thing\")" + "anagram_checker(\"night\", \"Thing\")\n" ] }, { @@ -97,24 +167,84 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "'silent' and 'listen' are anagrams :)\n" + ] + }, + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 7, + "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 of each other, considering case sensitivity if required.\n", + " \n", + " :param word_a: First word (string)\n", + " :param word_b: Second word (string)\n", + " :param is_case_sensitive: Boolean flag for case sensitivity\n", + " :return: Boolean indicating whether the words are anagrams\n", + " \"\"\"\n", + " # If case insensitive, convert both words to lowercase\n", + " if not is_case_sensitive:\n", + " word_a = word_a.lower()\n", + " word_b = word_b.lower()\n", + "\n", + " # Check if sorted characters of both words are the same\n", + " anagram_checker = sorted(word_a) == sorted(word_b)\n", + "\n", + " if anagram_checker:\n", + " print(f\"'{word_a}' and '{word_b}' are anagrams :)\")\n", + " else:\n", + " print(f\"'{word_a}' and '{word_b}' are not anagrams :(\")\n", + "\n", + " return anagram_checker\n", + "\n", + "# # Example usage\n", + "# result = anagram_checker(\"Silent\", \"listen\", False) # Case-insensitive check, expected output: True\n", "\n", "# Run your code to check using the words below:\n", - "anagram_checker(\"Silent\", \"listen\", False) # True" + "anagram_checker(\"Silent\", \"listen\", False) # True\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "'Silent' and 'Listen' are not anagrams :(\n" + ] + }, + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "anagram_checker(\"Silent\", \"Listen\", True) # False" + "anagram_checker(\"Silent\", \"Listen\", True) # False\n" ] }, { @@ -126,6 +256,75 @@ "|Code Execution|All code cells execute without errors.|Any code cell produces an error upon execution.|\n", "|Code Quality|Code is well-organized, concise, and includes necessary comments for clarity. E.g. Great use of variable names.|Code is unorganized, verbose, or lacks necessary comments. E.g. Single character variable names outside of loops.|" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Part 3: Expanding the functionality of the Anagram Checker with user input and counter for efficiency. \n", + "\n", + "Sorting both words (sorted(word_a) and sorted(word_b)) takes 𝑂 ( 𝑛 log ⁡ 𝑛 ) O(nlogn) time, where 𝑛 n is the length of the word. For large words, using Counter from the collections module can improve the time complexity to 𝑂 ( 𝑛 ) O(n), as it counts character frequencies directly." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "'escape' and 'home' are not anagrams :(\n" + ] + }, + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from collections import Counter\n", + "\n", + "def anagram_checker(word_a, word_b):\n", + " \"\"\"\n", + " Check if two words are anagrams of each other.\n", + " \n", + " :param word_a: First word (string)\n", + " :param word_b: Second word (string)\n", + " :return: Boolean indicating whether the words are anagrams\n", + " \"\"\"\n", + " # Normalize the words: remove spaces and convert to lowercase\n", + " word_a = word_a.replace(\" \", \"\").lower()\n", + " word_b = word_b.replace(\" \", \"\").lower()\n", + "\n", + " if len(word_a) != len(word_b):\n", + " print(f\"'{word_a}' and '{word_b}' are not anagrams :(\")\n", + " return False\n", + "\n", + " # Check if character counts are the same using Counter\n", + " anagram_checker = Counter(word_a) == Counter(word_b)\n", + "\n", + " if anagram_checker:\n", + " print(f\"'{word_a}' and '{word_b}' are anagrams :)\")\n", + " else:\n", + " print(f\"'{word_a}' and '{word_b}' are not anagrams :(\")\n", + " \n", + " return anagram_checker\n", + "\n", + "# Input from the user\n", + "word_a = input(\"Enter the first word: \")\n", + "word_b = input(\"Enter the second word: \")\n", + "\n", + "# Call the function with user inputs\n", + "anagram_checker(word_a, word_b)\n" + ] } ], "metadata": { @@ -144,7 +343,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.8" + "version": "3.13.0" } }, "nbformat": 4, From 4db93d5260b94f2253934a7756c439fc2cf473dc Mon Sep 17 00:00:00 2001 From: Alejandro Castellanos Date: Fri, 6 Dec 2024 13:22:38 -0500 Subject: [PATCH 2/4] variable names fixed --- 02_activities/assignments/assignment_1.ipynb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/02_activities/assignments/assignment_1.ipynb b/02_activities/assignments/assignment_1.ipynb index 0e954ce39..0a49a4a07 100644 --- a/02_activities/assignments/assignment_1.ipynb +++ b/02_activities/assignments/assignment_1.ipynb @@ -89,14 +89,14 @@ " word_b = word_b.lower()\n", " \n", "# Check if sorted characters of both words are the same\n", - " anagram_checker = sorted(word_a) == sorted(word_b)\n", + " is_anagram_checker = sorted(word_a) == sorted(word_b)\n", "\n", - " if anagram_checker:\n", + " if is_anagram_checker:\n", " print(f\"'{word_a}' and '{word_b}' are anagrams :)\")\n", " else:\n", " print(f\"'{word_a}' and '{word_b}' are not anagrams :(\")\n", "\n", - " return anagram_checker\n", + " return is_anagram_checker\n", "\n", "# Run your code to check using the words below:\n", "anagram_checker(\"Silent\", \"listen\")\n" @@ -167,7 +167,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -183,7 +183,7 @@ "True" ] }, - "execution_count": 7, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -203,15 +203,15 @@ " word_a = word_a.lower()\n", " word_b = word_b.lower()\n", "\n", - " # Check if sorted characters of both words are the same\n", - " anagram_checker = sorted(word_a) == sorted(word_b)\n", + " # Compare sorted versions of both words\n", + " is_anagram = sorted(word_a) == sorted(word_b)\n", "\n", - " if anagram_checker:\n", + " if is_anagram:\n", " print(f\"'{word_a}' and '{word_b}' are anagrams :)\")\n", " else:\n", " print(f\"'{word_a}' and '{word_b}' are not anagrams :(\")\n", "\n", - " return anagram_checker\n", + " return is_anagram\n", "\n", "# # Example usage\n", "# result = anagram_checker(\"Silent\", \"listen\", False) # Case-insensitive check, expected output: True\n", From 0c1f98d094815b7451ca1817970b1b46b5f8cca6 Mon Sep 17 00:00:00 2001 From: Alejandro Castellanos Date: Fri, 6 Dec 2024 13:26:15 -0500 Subject: [PATCH 3/4] fix variable names --- 02_activities/assignments/assignment_1.ipynb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/02_activities/assignments/assignment_1.ipynb b/02_activities/assignments/assignment_1.ipynb index 0a49a4a07..eda86c2ea 100644 --- a/02_activities/assignments/assignment_1.ipynb +++ b/02_activities/assignments/assignment_1.ipynb @@ -56,7 +56,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -72,7 +72,7 @@ "True" ] }, - "execution_count": 1, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -89,14 +89,14 @@ " word_b = word_b.lower()\n", " \n", "# Check if sorted characters of both words are the same\n", - " is_anagram_checker = sorted(word_a) == sorted(word_b)\n", + " is_anagram = sorted(word_a) == sorted(word_b)\n", "\n", - " if is_anagram_checker:\n", + " if is_anagram:\n", " print(f\"'{word_a}' and '{word_b}' are anagrams :)\")\n", " else:\n", " print(f\"'{word_a}' and '{word_b}' are not anagrams :(\")\n", "\n", - " return is_anagram_checker\n", + " return is_anagram\n", "\n", "# Run your code to check using the words below:\n", "anagram_checker(\"Silent\", \"listen\")\n" From 56ba8b92f2ee7c51d3702827c5b5804dadae4fb7 Mon Sep 17 00:00:00 2001 From: Alejandro Castellanos Date: Fri, 6 Dec 2024 13:28:50 -0500 Subject: [PATCH 4/4] variable names fixed --- 02_activities/assignments/assignment_1.ipynb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/02_activities/assignments/assignment_1.ipynb b/02_activities/assignments/assignment_1.ipynb index eda86c2ea..7a58ccb45 100644 --- a/02_activities/assignments/assignment_1.ipynb +++ b/02_activities/assignments/assignment_1.ipynb @@ -268,23 +268,23 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "'escape' and 'home' are not anagrams :(\n" + "'tello' and 'lolte' are anagrams :)\n" ] }, { "data": { "text/plain": [ - "False" + "True" ] }, - "execution_count": 9, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -309,14 +309,14 @@ " return False\n", "\n", " # Check if character counts are the same using Counter\n", - " anagram_checker = Counter(word_a) == Counter(word_b)\n", + " is_anagram = Counter(word_a) == Counter(word_b)\n", "\n", - " if anagram_checker:\n", + " if is_anagram:\n", " print(f\"'{word_a}' and '{word_b}' are anagrams :)\")\n", " else:\n", " print(f\"'{word_a}' and '{word_b}' are not anagrams :(\")\n", " \n", - " return anagram_checker\n", + " return is_anagram\n", "\n", "# Input from the user\n", "word_a = input(\"Enter the first word: \")\n",