diff --git a/02_activities/assignments/assignment_1.ipynb b/02_activities/assignments/assignment_1.ipynb index 625be024..a3917313 100644 --- a/02_activities/assignments/assignment_1.ipynb +++ b/02_activities/assignments/assignment_1.ipynb @@ -44,7 +44,7 @@ "\n", "Examples of anagrams:\n", "* Silent and Listen\n", - "* Night and Think\n", + "* Night and Thing\n", "\n", "Example outputs:\n", "```python\n", @@ -56,32 +56,80 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 6, + "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", + "def anagram_checker(str1, str2):\n", + " str1 = str1.lower()\n", + " str2 = str2.lower()\n", + "\n", + " if len(str1) != len(str2):\n", + " return False\n", + "\n", + " char_count = {}\n", + " for char in str1:\n", + " char_count[char] = char_count.get(char, 0) + 1\n", + "\n", + " for char in str2:\n", + " if char not in char_count:\n", + " return False\n", + " char_count[char] -= 1\n", + " if char_count[char] < 0:\n", + " return False\n", + "\n", + " return True\n", "\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,12 +145,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "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", + "def anagram_checker(str1, str2, is_case_sensitive=False):\n", + " if not is_case_sensitive:\n", + " str1 = str1.lower()\n", + " str2 = str2.lower()\n", + "\n", + " return sorted(str1) == sorted(str2)\n", "\n", "# Run your code to check using the words below:\n", "anagram_checker(\"Silent\", \"listen\", False) # True" @@ -110,9 +173,20 @@ }, { "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 +204,7 @@ ], "metadata": { "kernelspec": { - "display_name": "new-learner", + "display_name": "python-env", "language": "python", "name": "python3" }, @@ -144,7 +218,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.8" + "version": "3.11.14" } }, "nbformat": 4,