Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
237 changes: 218 additions & 19 deletions 02_activities/assignments/assignment_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -56,34 +56,104 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"'silent' and 'listen' are anagrams :)\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 4,
"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",
" \"\"\"\n",
" Check if two wors are anagrams of each other, adding verbose statement.\n",
" :param word_a: First word (string)\n",
" :param word_b: Second word (string)\n",
" \"\"\"\n",
"# Convert words to lowercase\n",
" word_a = word_a.lower()\n",
" word_b = word_b.lower()\n",
" \n",
"# Check if sorted characters of both words are the same\n",
" is_anagram = sorted(word_a) == sorted(word_b)\n",
"\n",
" if is_anagram:\n",
" print(f\"'{word_a}' and '{word_b}' are anagrams :)\")\n",
" else:\n",
" print(f\"'{word_a}' and '{word_b}' are not anagrams :(\")\n",
"\n",
" return is_anagram\n",
"\n",
"# Run your code to check using the words below:\n",
"anagram_checker(\"Silent\", \"listen\")"
"anagram_checker(\"Silent\", \"listen\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"'silent' and 'night' are not anagrams :(\n"
]
},
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"Silent\", \"Night\")"
"anagram_checker(\"Silent\", \"Night\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"'night' and 'thing' are anagrams :)\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"night\", \"Thing\")"
"anagram_checker(\"night\", \"Thing\")\n"
]
},
{
Expand All @@ -97,24 +167,84 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"'silent' and 'listen' are anagrams :)\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def anagram_checker(word_a, word_b, is_case_sensitive):\n",
" # Modify your existing code here\n",
" \"\"\"\n",
" Check if two words are anagrams of each other, considering case sensitivity if required.\n",
" \n",
" :param word_a: First word (string)\n",
" :param word_b: Second word (string)\n",
" :param is_case_sensitive: Boolean flag for case sensitivity\n",
" :return: Boolean indicating whether the words are anagrams\n",
" \"\"\"\n",
" # If case insensitive, convert both words to lowercase\n",
" if not is_case_sensitive:\n",
" word_a = word_a.lower()\n",
" word_b = word_b.lower()\n",
"\n",
" # Compare sorted versions of both words\n",
" is_anagram = sorted(word_a) == sorted(word_b)\n",
"\n",
" if is_anagram:\n",
" print(f\"'{word_a}' and '{word_b}' are anagrams :)\")\n",
" else:\n",
" print(f\"'{word_a}' and '{word_b}' are not anagrams :(\")\n",
"\n",
" return is_anagram\n",
"\n",
"# # Example usage\n",
"# result = anagram_checker(\"Silent\", \"listen\", False) # Case-insensitive check, expected output: True\n",
"\n",
"# Run your code to check using the words below:\n",
"anagram_checker(\"Silent\", \"listen\", False) # True"
"anagram_checker(\"Silent\", \"listen\", False) # True\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"'Silent' and 'Listen' are not anagrams :(\n"
]
},
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"Silent\", \"Listen\", True) # False"
"anagram_checker(\"Silent\", \"Listen\", True) # False\n"
]
},
{
Expand All @@ -126,6 +256,75 @@
"|Code Execution|All code cells execute without errors.|Any code cell produces an error upon execution.|\n",
"|Code Quality|Code is well-organized, concise, and includes necessary comments for clarity. E.g. Great use of variable names.|Code is unorganized, verbose, or lacks necessary comments. E.g. Single character variable names outside of loops.|"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Part 3: Expanding the functionality of the Anagram Checker with user input and counter for efficiency. \n",
"\n",
"Sorting both words (sorted(word_a) and sorted(word_b)) takes 𝑂 ( 𝑛 log ⁡ 𝑛 ) O(nlogn) time, where 𝑛 n is the length of the word. For large words, using Counter from the collections module can improve the time complexity to 𝑂 ( 𝑛 ) O(n), as it counts character frequencies directly."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"'tello' and 'lolte' are anagrams :)\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from collections import Counter\n",
"\n",
"def anagram_checker(word_a, word_b):\n",
" \"\"\"\n",
" Check if two words are anagrams of each other.\n",
" \n",
" :param word_a: First word (string)\n",
" :param word_b: Second word (string)\n",
" :return: Boolean indicating whether the words are anagrams\n",
" \"\"\"\n",
" # Normalize the words: remove spaces and convert to lowercase\n",
" word_a = word_a.replace(\" \", \"\").lower()\n",
" word_b = word_b.replace(\" \", \"\").lower()\n",
"\n",
" if len(word_a) != len(word_b):\n",
" print(f\"'{word_a}' and '{word_b}' are not anagrams :(\")\n",
" return False\n",
"\n",
" # Check if character counts are the same using Counter\n",
" is_anagram = Counter(word_a) == Counter(word_b)\n",
"\n",
" if is_anagram:\n",
" print(f\"'{word_a}' and '{word_b}' are anagrams :)\")\n",
" else:\n",
" print(f\"'{word_a}' and '{word_b}' are not anagrams :(\")\n",
" \n",
" return is_anagram\n",
"\n",
"# Input from the user\n",
"word_a = input(\"Enter the first word: \")\n",
"word_b = input(\"Enter the second word: \")\n",
"\n",
"# Call the function with user inputs\n",
"anagram_checker(word_a, word_b)\n"
]
}
],
"metadata": {
Expand All @@ -144,7 +343,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
"version": "3.13.0"
}
},
"nbformat": 4,
Expand Down
Loading