From c8507c751e3ecfc85eda6bb85eea2afd0d8b3572 Mon Sep 17 00:00:00 2001 From: ibraassi Date: Sat, 4 May 2024 13:01:47 -0400 Subject: [PATCH] Completed assignment_1 --- 02_assignments/assignment_1.ipynb | 115 ++++++++++++++++++++++++------ 1 file changed, 94 insertions(+), 21 deletions(-) diff --git a/02_assignments/assignment_1.ipynb b/02_assignments/assignment_1.ipynb index 946889317..68823bd15 100644 --- a/02_assignments/assignment_1.ipynb +++ b/02_assignments/assignment_1.ipynb @@ -56,34 +56,74 @@ }, { "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": [ - "# 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", + " '''Given two strings, check to see if they are anagrams of each other.\n", + " If yes, return `True`, else return `False`.\n", + " Uppercase and lowercase characters are the same.'''\n", + " if type(word_a) == str and type(word_b) == str: # Check if arguments are strings\n", + " if len(word_a) == len(word_b): # For efficiency there is no need to sort if lengths are not equal\n", + " return sorted(word_a.lower()) == sorted(word_b.lower()) # Convert to lowercase, sort characters, and check if same\n", + " else:\n", + " return False\n", + " else:\n", + " print('Please pass two arguments of type string')\n", "\n", - "# Run your code to check using the words below:\n", - "anagram_checker(\"Slient\", \"listen\")" + "anagram_checker(\"Slient\", \"listen\") # True" ] }, { "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(\"Slient\", \"Night\")" + "anagram_checker(\"Slient\", \"Night\") # False" ] }, { "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\")" + "anagram_checker(\"night\", \"Thing\") # True" ] }, { @@ -97,24 +137,57 @@ }, { "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", + " '''Given two strings, check to see if they are anagrams of each other.\n", + " If yes, return `True`, else return `False`.\n", + " The boolean option determines case sensitivity.'''\n", + " if type(word_a) == str and type(word_b) == str: # Check if arguments are strings\n", + " if len(word_a) == len(word_b): # For efficiency there is no need to sort if lengths are not equal\n", + " if is_case_sensitive: # Check if case sensisitivity is on\n", + " return sorted(word_a) == sorted(word_b) # Sort characters and check if same\n", + " else:\n", + " return sorted(word_a.lower()) == sorted(word_b.lower()) # Convert to lowercase, sort characters, and check if same\n", + " else:\n", + " return False\n", + " else:\n", + " print('Please pass two arguments of type string')\n", "\n", - "# Run your code to check using the words below:\n", - "anagram_checker(\"Slient\", \"listen\", False) # True" + "anagram_checker(\"Slient\", \"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(\"Slient\", \"Listen\", True) # False" + "anagram_checker(\"Slient\", \"Listen\", True) # False" ] }, { @@ -144,7 +217,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.8" + "version": "3.9.15" } }, "nbformat": 4,