Skip to content

Commit c722287

Browse files
committed
Completed Part 2
1 parent a50fb8c commit c722287

1 file changed

Lines changed: 66 additions & 25 deletions

File tree

‎02_activities/assignments/assignment_1.ipynb‎

Lines changed: 66 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
},
5757
{
5858
"cell_type": "code",
59-
"execution_count": null,
59+
"execution_count": 44,
6060
"metadata": {},
6161
"outputs": [
6262
{
@@ -65,7 +65,7 @@
6565
"True"
6666
]
6767
},
68-
"execution_count": 45,
68+
"execution_count": 44,
6969
"metadata": {},
7070
"output_type": "execute_result"
7171
}
@@ -78,31 +78,28 @@
7878
" ''' Given two valid strings, check if they are anagrams.\n",
7979
" anagram_checker is a boolean comparator: \n",
8080
" True if the two strings are anagrams; False if the two strings are not anagrams.\n",
81-
" Print message to inddicate whether the two strings are anagrams. '''\n",
81+
" Assume uppercase letters are interchangeable with lowercase letters; therefore, \n",
82+
" convert all string characters to lower case before comparing string equivalence. \n",
83+
" '''\n",
8284
" \n",
83-
" # Perform an ascending sort of the characters contained in both string arguments\n",
84-
" word1 = 'word_a'\n",
85-
" sorted_word1 = sorted(word1)\n",
86-
" word2 = 'word_b'\n",
87-
" sorted_word2 = sorted(word2)\n",
85+
"# Assign the function string parameters to a lowercase variable\n",
86+
" word1 = word_a.lower()\n",
87+
" word2 = word_b.lower()\n",
8888
"\n",
89-
" # Print the two sorted string lists \n",
90-
" print('String one: ', sorted_word1)\n",
91-
" print('String two: ', sorted_word2)\n",
89+
"# Perform an ascending sort of the characters contained in both string variables\n",
90+
" sorted_word1 = sorted(str(word1))\n",
91+
" sorted_word2 = sorted(str(word2))\n",
9292
"\n",
93-
" # Perform a boolean comparison and print the result\n",
94-
" if word1 == word2:\n",
95-
" print('The two strings are anagrams of each other')\n",
96-
" else:\n",
97-
" print('The two strings are not anagrams.')\n",
93+
"# Perform a boolean equivalence comparison for the two strings\n",
94+
" return sorted_word1 == sorted_word2\n",
9895
"\n",
9996
"# Run your code to check using the words below:\n",
10097
"anagram_checker(\"Silent\", \"listen\")"
10198
]
10299
},
103100
{
104101
"cell_type": "code",
105-
"execution_count": 46,
102+
"execution_count": 45,
106103
"metadata": {},
107104
"outputs": [
108105
{
@@ -111,7 +108,7 @@
111108
"False"
112109
]
113110
},
114-
"execution_count": 46,
111+
"execution_count": 45,
115112
"metadata": {},
116113
"output_type": "execute_result"
117114
}
@@ -122,7 +119,7 @@
122119
},
123120
{
124121
"cell_type": "code",
125-
"execution_count": 47,
122+
"execution_count": 46,
126123
"metadata": {},
127124
"outputs": [
128125
{
@@ -131,7 +128,7 @@
131128
"True"
132129
]
133130
},
134-
"execution_count": 47,
131+
"execution_count": 46,
135132
"metadata": {},
136133
"output_type": "execute_result"
137134
}
@@ -153,22 +150,66 @@
153150
"cell_type": "code",
154151
"execution_count": null,
155152
"metadata": {},
156-
"outputs": [],
153+
"outputs": [
154+
{
155+
"data": {
156+
"text/plain": [
157+
"True"
158+
]
159+
},
160+
"execution_count": 56,
161+
"metadata": {},
162+
"output_type": "execute_result"
163+
}
164+
],
157165
"source": [
158166
"def anagram_checker(word_a, word_b, is_case_sensitive):\n",
159167
" # Modify your existing code here\n",
168+
" ''' Given two valid strings, check if they are anagrams.\n",
169+
" anagram_checker is a boolean comparator: \n",
170+
" True if the two strings are anagrams; False if the two strings are not anagrams.\n",
171+
" word_a is the first string parameter.\n",
172+
" word_b is the second string parameter.\n",
173+
" is_case_sensitive is a boolean option, indicating whether case sensitivity is important:\n",
174+
" (a) True, include the sorted.() function str.casefold to sort string alphabetically, regardless of case.\n",
175+
" (b) False, use existing Part 1 code to sort the string alphabetically, fixing stricng characters as lower case. \n",
176+
" ''' \n",
177+
"# Assign the function string parameters to a function variable\n",
178+
" word1 = word_a\n",
179+
" word2 = word_b\n",
180+
" # Conditional on whether case sensitivity is True or False and perform an ascending sort of the characters contained in both string arguments\n",
181+
" if is_case_sensitive:\n",
182+
" # Assign the two function string parameters to a function variable\n",
183+
" sorted_word1 = sorted(str(word1), key=str.casefold)\n",
184+
" sorted_word2 = sorted(str(word2), key=str.casefold)\n",
185+
" else:\n",
186+
" word1 = word_a.lower()\n",
187+
" word2 = word_b.lower()\n",
188+
" sorted_word1 = sorted(str(word1))\n",
189+
" sorted_word2 = sorted(str(word2))\n",
190+
" return sorted_word1 == sorted_word2\n",
160191
"\n",
161-
" \n",
162192
"\n",
163193
"# Run your code to check using the words below:\n",
164-
"anagram_checker(\"Silent\", \"listen\", False) # True"
194+
"anagram_checker(\"Silent\", \"listen\", False) # True\n"
165195
]
166196
},
167197
{
168198
"cell_type": "code",
169-
"execution_count": null,
199+
"execution_count": 57,
170200
"metadata": {},
171-
"outputs": [],
201+
"outputs": [
202+
{
203+
"data": {
204+
"text/plain": [
205+
"False"
206+
]
207+
},
208+
"execution_count": 57,
209+
"metadata": {},
210+
"output_type": "execute_result"
211+
}
212+
],
172213
"source": [
173214
"anagram_checker(\"Silent\", \"Listen\", True) # False"
174215
]

0 commit comments

Comments
 (0)