Skip to content
Open
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
124 changes: 111 additions & 13 deletions 02_activities/assignments/assignment_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -56,32 +56,83 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 44,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"# 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",
" ''' Given two valid strings, check if they are anagrams.\n",
" anagram_checker is a boolean comparator: \n",
" True if the two strings are anagrams; False if the two strings are not anagrams.\n",
" Assume uppercase letters are interchangeable with lowercase letters; therefore, \n",
" convert all string characters to lower case before comparing string equivalence. \n",
" '''\n",
" \n",
"# Assign the function string parameters to a lowercase variable\n",
" word1 = word_a.lower()\n",
" word2 = word_b.lower()\n",
"\n",
"# Perform an ascending sort of the characters contained in both string variables\n",
" sorted_word1 = sorted(str(word1))\n",
" sorted_word2 = sorted(str(word2))\n",
"\n",
"# Perform a boolean equivalence comparison for the two strings\n",
" return sorted_word1 == sorted_word2\n",
"\n",
"# Run your code to check using the words below:\n",
"anagram_checker(\"Silent\", \"listen\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 45,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"Silent\", \"Night\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 46,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"night\", \"Thing\")"
]
Expand All @@ -99,20 +150,67 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 56,
"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 valid strings, check if they are anagrams.\n",
" anagram_checker is a boolean comparator: \n",
" True if the two strings are anagrams; False if the two strings are not anagrams.\n",
" word_a is the first string parameter.\n",
" word_b is the second string parameter.\n",
" is_case_sensitive is a boolean option, indicating whether case sensitivity is important:\n",
" (a) True, include the sorted.() function optional key str.casefold parameter to sort string alphabetically, \n",
" regardless of case.\n",
" (b) False, use existing Part 1 code to sort the string alphabetically, fixing string characters as lower case. \n",
" ''' \n",
"# Assign the function string parameters to a function variable\n",
" word1 = word_a\n",
" word2 = word_b\n",
" # Conditional on whether case sensitivity is True or False and perform an ascending sort of the characters contained in both string arguments\n",
" if is_case_sensitive:\n",
" # Assign the two function string parameters to a function variable\n",
" sorted_word1 = sorted(str(word1), key=str.casefold)\n",
" sorted_word2 = sorted(str(word2), key=str.casefold)\n",
" else:\n",
" word1 = word_a.lower()\n",
" word2 = word_b.lower()\n",
" sorted_word1 = sorted(str(word1))\n",
" sorted_word2 = sorted(str(word2))\n",
" return sorted_word1 == sorted_word2\n",
"\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": 57,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"Silent\", \"Listen\", True) # False"
]
Expand All @@ -130,7 +228,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "new-learner",
"display_name": "python-env",
"language": "python",
"name": "python3"
},
Expand All @@ -144,7 +242,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
"version": "3.11.13"
}
},
"nbformat": 4,
Expand Down