|
56 | 56 | }, |
57 | 57 | { |
58 | 58 | "cell_type": "code", |
59 | | - "execution_count": null, |
| 59 | + "execution_count": 44, |
60 | 60 | "metadata": {}, |
61 | 61 | "outputs": [ |
62 | 62 | { |
|
65 | 65 | "True" |
66 | 66 | ] |
67 | 67 | }, |
68 | | - "execution_count": 45, |
| 68 | + "execution_count": 44, |
69 | 69 | "metadata": {}, |
70 | 70 | "output_type": "execute_result" |
71 | 71 | } |
|
78 | 78 | " ''' Given two valid strings, check if they are anagrams.\n", |
79 | 79 | " anagram_checker is a boolean comparator: \n", |
80 | 80 | " 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", |
82 | 84 | " \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", |
88 | 88 | "\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", |
92 | 92 | "\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", |
98 | 95 | "\n", |
99 | 96 | "# Run your code to check using the words below:\n", |
100 | 97 | "anagram_checker(\"Silent\", \"listen\")" |
101 | 98 | ] |
102 | 99 | }, |
103 | 100 | { |
104 | 101 | "cell_type": "code", |
105 | | - "execution_count": 46, |
| 102 | + "execution_count": 45, |
106 | 103 | "metadata": {}, |
107 | 104 | "outputs": [ |
108 | 105 | { |
|
111 | 108 | "False" |
112 | 109 | ] |
113 | 110 | }, |
114 | | - "execution_count": 46, |
| 111 | + "execution_count": 45, |
115 | 112 | "metadata": {}, |
116 | 113 | "output_type": "execute_result" |
117 | 114 | } |
|
122 | 119 | }, |
123 | 120 | { |
124 | 121 | "cell_type": "code", |
125 | | - "execution_count": 47, |
| 122 | + "execution_count": 46, |
126 | 123 | "metadata": {}, |
127 | 124 | "outputs": [ |
128 | 125 | { |
|
131 | 128 | "True" |
132 | 129 | ] |
133 | 130 | }, |
134 | | - "execution_count": 47, |
| 131 | + "execution_count": 46, |
135 | 132 | "metadata": {}, |
136 | 133 | "output_type": "execute_result" |
137 | 134 | } |
|
153 | 150 | "cell_type": "code", |
154 | 151 | "execution_count": null, |
155 | 152 | "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 | + ], |
157 | 165 | "source": [ |
158 | 166 | "def anagram_checker(word_a, word_b, is_case_sensitive):\n", |
159 | 167 | " # 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", |
160 | 191 | "\n", |
161 | | - " \n", |
162 | 192 | "\n", |
163 | 193 | "# 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" |
165 | 195 | ] |
166 | 196 | }, |
167 | 197 | { |
168 | 198 | "cell_type": "code", |
169 | | - "execution_count": null, |
| 199 | + "execution_count": 57, |
170 | 200 | "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 | + ], |
172 | 213 | "source": [ |
173 | 214 | "anagram_checker(\"Silent\", \"Listen\", True) # False" |
174 | 215 | ] |
|
0 commit comments