From f8f6f702d285a16b9e1297ed5842a75d72b35050 Mon Sep 17 00:00:00 2001 From: Anjishnu Bose <39477277+Anjishnubose@users.noreply.github.com> Date: Fri, 14 Feb 2025 11:12:43 -0500 Subject: [PATCH 1/2] added answers to assignment1 notebook --- 02_activities/assignments/assignment_1.ipynb | 120 +++++++++++++++++-- 1 file changed, 108 insertions(+), 12 deletions(-) diff --git a/02_activities/assignments/assignment_1.ipynb b/02_activities/assignments/assignment_1.ipynb index 14faec566..d5cd9292e 100644 --- a/02_activities/assignments/assignment_1.ipynb +++ b/02_activities/assignments/assignment_1.ipynb @@ -56,13 +56,44 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 2, + "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", + " word_a = word_a.lower() ##### Convert the word_a to lowercase\n", + " word_b = word_b.lower() ##### Convert the word_b to lowercase\n", + " character_count = {} ##### Create an empty dictionary to store the count of each character in the word_a\n", + " for character in word_a:\n", + " ##### Check if the character is already in the dictionary\n", + " if character in character_count:\n", + " character_count[character] += 1\n", + " else:\n", + " character_count[character] = 1\n", + " ########### Loop through the characters in word_b and check if they are in the dictionary\n", + " for character in word_b:\n", + " ##### Check if the character is not in the dictionary ==> word_b has a character that is not in word_a\n", + " if character not in character_count:\n", + " return False\n", + " else:\n", + " character_count[character] -= 1 ##### Decrement the count of the character in the dictionary\n", + " if character_count[character] < 0: ##### Check if the count of the character is less than 0 ===> word_b has more of the character than word_a\n", + " return False\n", + " \n", + " return True\n", "\n", "# Run your code to check using the words below:\n", "anagram_checker(\"Silent\", \"listen\")" @@ -70,18 +101,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "anagram_checker(\"Silent\", \"Night\")" ] }, { "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": [ "anagram_checker(\"night\", \"Thing\")" ] @@ -97,12 +150,44 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "def anagram_checker(word_a, word_b, is_case_sensitive):\n", " # Modify your existing code here\n", + " if not is_case_sensitive:\n", + " word_a = word_a.lower() ##### Convert the word_a to lowercase\n", + " word_b = word_b.lower() ##### Convert the word_b to lowercase\n", + " character_count = {} ##### Create an empty dictionary to store the count of each character in the word_a\n", + " for character in word_a:\n", + " ##### Check if the character is already in the dictionary\n", + " if character in character_count:\n", + " character_count[character] += 1\n", + " else:\n", + " character_count[character] = 1\n", + " ########### Loop through the characters in word_b and check if they are in the dictionary\n", + " for character in word_b:\n", + " ##### Check if the character is not in the dictionary ==> word_b has a character that is not in word_a\n", + " if character not in character_count:\n", + " return False\n", + " else:\n", + " character_count[character] -= 1 ##### Decrement the count of the character in the dictionary\n", + " if character_count[character] < 0: ##### Check if the count of the character is less than 0 ===> word_b has more of the character than word_a\n", + " return False\n", + " \n", + " return True\n", "\n", "# Run your code to check using the words below:\n", "anagram_checker(\"Silent\", \"listen\", False) # True" @@ -110,9 +195,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "anagram_checker(\"Silent\", \"Listen\", True) # False" ] @@ -130,7 +226,7 @@ ], "metadata": { "kernelspec": { - "display_name": "new-learner", + "display_name": "dsi_participant", "language": "python", "name": "python3" }, @@ -144,7 +240,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.8" + "version": "3.9.21" } }, "nbformat": 4, From 590b1fa8d5effda5e9a13b11e765d405ae74a6e1 Mon Sep 17 00:00:00 2001 From: Anjishnu Bose <39477277+Anjishnubose@users.noreply.github.com> Date: Mon, 17 Feb 2025 13:18:55 -0500 Subject: [PATCH 2/2] using pythons in built functions --- 02_activities/assignments/assignment_1.ipynb | 73 ++++++-------------- 1 file changed, 23 insertions(+), 50 deletions(-) diff --git a/02_activities/assignments/assignment_1.ipynb b/02_activities/assignments/assignment_1.ipynb index d5cd9292e..0911be3e0 100644 --- a/02_activities/assignments/assignment_1.ipynb +++ b/02_activities/assignments/assignment_1.ipynb @@ -56,7 +56,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -65,7 +65,7 @@ "True" ] }, - "execution_count": 2, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -74,26 +74,11 @@ "# 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", - " word_a = word_a.lower() ##### Convert the word_a to lowercase\n", - " word_b = word_b.lower() ##### Convert the word_b to lowercase\n", - " character_count = {} ##### Create an empty dictionary to store the count of each character in the word_a\n", - " for character in word_a:\n", - " ##### Check if the character is already in the dictionary\n", - " if character in character_count:\n", - " character_count[character] += 1\n", - " else:\n", - " character_count[character] = 1\n", - " ########### Loop through the characters in word_b and check if they are in the dictionary\n", - " for character in word_b:\n", - " ##### Check if the character is not in the dictionary ==> word_b has a character that is not in word_a\n", - " if character not in character_count:\n", - " return False\n", - " else:\n", - " character_count[character] -= 1 ##### Decrement the count of the character in the dictionary\n", - " if character_count[character] < 0: ##### Check if the count of the character is less than 0 ===> word_b has more of the character than word_a\n", - " return False\n", - " \n", - " return True\n", + " ####### define a function that takes a string and returns a dictionary of the count of each unique character in the string \n", + " def character_count(word: str) -> dict:\n", + " return {c:word.count(c) for c in set(word)}\n", + " ######### check if the character count of the two words are the same\n", + " return character_count(word_a.lower()) == character_count(word_b.lower()) \n", "\n", "# Run your code to check using the words below:\n", "anagram_checker(\"Silent\", \"listen\")" @@ -101,7 +86,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -110,7 +95,7 @@ "False" ] }, - "execution_count": 3, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -121,7 +106,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -130,7 +115,7 @@ "True" ] }, - "execution_count": 4, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -150,7 +135,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -159,7 +144,7 @@ "True" ] }, - "execution_count": 5, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -167,27 +152,15 @@ "source": [ "def anagram_checker(word_a, word_b, is_case_sensitive):\n", " # Modify your existing code here\n", + " ####### define a function that takes a string and returns a dictionary of the count of each character in the string\n", + " def character_count(word: str) -> dict:\n", + " return {c:word.count(c) for c in set(word)}\n", + " ######### check if the character count of the two words are the same \n", " if not is_case_sensitive:\n", - " word_a = word_a.lower() ##### Convert the word_a to lowercase\n", - " word_b = word_b.lower() ##### Convert the word_b to lowercase\n", - " character_count = {} ##### Create an empty dictionary to store the count of each character in the word_a\n", - " for character in word_a:\n", - " ##### Check if the character is already in the dictionary\n", - " if character in character_count:\n", - " character_count[character] += 1\n", - " else:\n", - " character_count[character] = 1\n", - " ########### Loop through the characters in word_b and check if they are in the dictionary\n", - " for character in word_b:\n", - " ##### Check if the character is not in the dictionary ==> word_b has a character that is not in word_a\n", - " if character not in character_count:\n", - " return False\n", - " else:\n", - " character_count[character] -= 1 ##### Decrement the count of the character in the dictionary\n", - " if character_count[character] < 0: ##### Check if the count of the character is less than 0 ===> word_b has more of the character than word_a\n", - " return False\n", - " \n", - " return True\n", + " ##### convert the words to lowercase before checking if case sensitivity is not required\n", + " return character_count(word_a.lower()) == character_count(word_b.lower())\n", + " else:\n", + " return character_count(word_a) == character_count(word_b) \n", "\n", "# Run your code to check using the words below:\n", "anagram_checker(\"Silent\", \"listen\", False) # True" @@ -195,7 +168,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 10, "metadata": {}, "outputs": [ { @@ -204,7 +177,7 @@ "False" ] }, - "execution_count": 6, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" }