diff --git a/02_activities/assignments/assignment_1.ipynb b/02_activities/assignments/assignment_1.ipynb index 2dca19d0b..4fe519f64 100644 --- a/02_activities/assignments/assignment_1.ipynb +++ b/02_activities/assignments/assignment_1.ipynb @@ -62,7 +62,26 @@ "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", + " # first we need to make into a set for the whole words\n", + " set(word_a)\n", + " set(word_b)\n", + " \n", + " # we want to ensure the lengths of the words are included \n", + " len(word_a) == len(word_b)\n", + " \n", + " \n", + "\n", + " #we then want to compare the sorted letters in alpha order and have then be equal\n", + " sorted(word_a) == sorted(word_b)\n", + " \n", + "\n", + "\n", + " #we need to return to then need to use the and operator so that 'True' or 'False' is produced \n", + " return (True, False) if (sorted(word_a) == sorted(word_b)) and (len(word_a) == len(word_b))\n", + "\n", + "\n", + "\n", + "\n", "\n", "# Run your code to check using the words below:\n", "anagram_checker(\"Silent\", \"listen\")" @@ -70,7 +89,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ @@ -79,7 +98,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -99,10 +118,37 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "AttributeError", + "evalue": "'str' object has no attribute 'symmetric_difference'", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mAttributeError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[7]\u001b[39m\u001b[32m, line 19\u001b[39m\n\u001b[32m 16\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m'\u001b[39m\u001b[33mFalse\u001b[39m\u001b[33m'\u001b[39m)\n\u001b[32m 18\u001b[39m \u001b[38;5;66;03m# Run your code to check using the words below:\u001b[39;00m\n\u001b[32m---> \u001b[39m\u001b[32m19\u001b[39m \u001b[43managram_checker\u001b[49m\u001b[43m(\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mSilent\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mlisten\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# True\u001b[39;00m\n", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[7]\u001b[39m\u001b[32m, line 7\u001b[39m, in \u001b[36managram_checker\u001b[39m\u001b[34m(word_a, word_b, is_case_sensitive)\u001b[39m\n\u001b[32m 4\u001b[39m \u001b[38;5;28mset\u001b[39m(word_b)\n\u001b[32m 6\u001b[39m \u001b[38;5;66;03m# we want to then see all values only in one of two sets \u001b[39;00m\n\u001b[32m----> \u001b[39m\u001b[32m7\u001b[39m \u001b[43mword_a\u001b[49m\u001b[43m.\u001b[49m\u001b[43msymmetric_difference\u001b[49m(word_b)\n\u001b[32m 8\u001b[39m \u001b[38;5;66;03m# if symmetric_difference is True then we want to print True and if False then we print False \u001b[39;00m\n\u001b[32m 9\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m word_a.symmetric_difference(word_b) == \u001b[38;5;28mset\u001b[39m(): \n", + "\u001b[31mAttributeError\u001b[39m: 'str' object has no attribute 'symmetric_difference'" + ] + } + ], "source": [ "def anagram_checker(word_a, word_b, is_case_sensitive):\n", - " # Modify your existing code here\n", + " # first we need to make into a set for the whole words\n", + " set(word_a)\n", + " set(word_b)\n", + " \n", + " # we want to ensure the lengths of the words are included \n", + " len(word_a) == len(word_b)\n", + "\n", + " #we then want to compare the sorted letters in alpha order and have then be equal\n", + " sorted(word_a) == sorted(word_b)\n", + "\n", + " #consider if it is_case_sensitive by making all the letters lowercase \n", + " lower (word_a, word_b)\n", + "\n", + " #we then need to use the and operator so that 'True' or 'False' is produced \n", + " (sorted(word_a) == sorted(word_b)) and (len(word_a) == len(word_b))\n", "\n", "# Run your code to check using the words below:\n", "anagram_checker(\"Silent\", \"listen\", False) # True" @@ -139,7 +185,7 @@ ], "metadata": { "kernelspec": { - "display_name": "new-learner", + "display_name": "python-env (3.11.14)", "language": "python", "name": "python3" }, @@ -153,7 +199,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.8" + "version": "3.11.14" } }, "nbformat": 4,