From 8303085d3da81f1ac0bb47028b2d6904faaa3989 Mon Sep 17 00:00:00 2001 From: amera6 Date: Fri, 24 Oct 2025 23:58:54 -0400 Subject: [PATCH] Update assignment_1.ipynb: Added own code for anagram checker --- 02_activities/assignments/assignment_1.ipynb | 101 ++++++++++++++++--- 1 file changed, 87 insertions(+), 14 deletions(-) diff --git a/02_activities/assignments/assignment_1.ipynb b/02_activities/assignments/assignment_1.ipynb index bee48d5a0..b295673b2 100644 --- a/02_activities/assignments/assignment_1.ipynb +++ b/02_activities/assignments/assignment_1.ipynb @@ -56,32 +56,72 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 1, + "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", - "\n", + " string_a = [] # creating an empty string corresponding to word_a to add elements to\n", + " string_a.extend(word_a.lower()) # first converting word_a to lowercase and then adding each letter as its own element to str_a\n", + " string_b = [] # same thing but for word_b\n", + " string_b.extend(word_b.lower())\n", + " if sorted(string_a) == sorted(string_b): # if a pair of words are anagrams, they will have the same list of letters. Sorting these lists ensures that they match exactly element-by-element\n", + " return True\n", + " else:\n", + " return False\n", "# Run your code to check using the words below:\n", "anagram_checker(\"Silent\", \"listen\")" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "anagram_checker(\"Silent\", \"Night\")" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "anagram_checker(\"night\", \"Thing\")" ] @@ -97,22 +137,55 @@ }, { "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": [ "def anagram_checker(word_a, word_b, is_case_sensitive):\n", " # Modify your existing code here\n", - "\n", + " string_a = [] # creating empty strings to populate later\n", + " string_b = [] \n", + " if is_case_sensitive == True: \n", + " string_a.extend(word_a) # since we check case sensitivity, the elements won't be forced into lowercase before being compared\n", + " string_b.extend(word_b) \n", + " else:\n", + " string_a.extend(word_a.lower()) # if we don't care about case sensitivty, convert both words to lowercase first\n", + " string_b.extend(word_b.lower())\n", + " if sorted(string_a) == sorted(string_b): # compare the two strings, sorting them first to ensure they match element-by-element if they are indeed anagrams\n", + " return True\n", + " else:\n", + " return False\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": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "anagram_checker(\"Silent\", \"Listen\", True) # False" ] @@ -130,7 +203,7 @@ ], "metadata": { "kernelspec": { - "display_name": "new-learner", + "display_name": "python-env", "language": "python", "name": "python3" }, @@ -144,7 +217,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.8" + "version": "3.11.14" } }, "nbformat": 4,