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
168 changes: 151 additions & 17 deletions 02_assignments/assignment_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,75 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 1,
"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",
" if len(word_a) == len(word_b): # compare the number of letters from both words\n",
" list_a=sorted(list(word_a.upper())) # uppercase the word, separate the word into a list of single letters, then sort the letters alphabetically \n",
" list_b=sorted(list(word_b.upper())) # same as above\n",
" if list_a==list_b: # compare the two lists of single letter, if they are the same\n",
" #for i in range(len(list_a)): #alternative way of comparing two lists, specific to the single letter at each index position\n",
" #if list_a[i]!=list_b[i]:\n",
" return True\n",
" else:\n",
" return False\n",
" else: #the number of letters in each word is different\n",
" return False\n",
"# Run your code to check using the words below:\n",
"anagram_checker(\"Slient\", \"listen\")"
"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(\"Slient\", \"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\")"
]
Expand All @@ -72,22 +115,65 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def anagram_checker(word_a, word_b, is_case_sensitive):\n",
" # Modify your existing code here\n",
"\n",
" if is_case_sensitive==False: # if not case sensitive\n",
" if len(word_a)==len(word_b): # compare the number of single letters from both words\n",
" list_a=sorted(list(word_a.upper())) # uppercase the word, separate into single letters, then sort the list of letters alphatetically\n",
" list_b=sorted(list(word_b.upper())) # same as above\n",
" if list_a==list_b: # if the list of letters from both words are the same\n",
" return True\n",
" #return list_a, list_b (for debugging)\n",
" else:\n",
" return False\n",
" #return list_a, list_b (for debugging)\n",
" else: # the number of letters from both words is differnet\n",
" return False\n",
" else: # if case sensitive\n",
" if len(word_a)==len(word_b): # compare the number of single letters from both words\n",
" list_a=sorted(list(word_a)) # (no need to uppercase the word) separate into single letters, then sort the list of letters alphatetically\n",
" list_b=sorted(list(word_b)) # same as above\n",
" if list_a==list_b: # if the list of letters from both words are the same\n",
" return True\n",
" else:\n",
" return False \n",
" else:\n",
" return False\n",
"# Run your code to check using the words below:\n",
"anagram_checker(\"Slient\", \"listen\", False) # True"
]
},
{
"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(\"Slient\", \"Listen\", True) # False"
]
Expand All @@ -108,14 +194,62 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def anagram_checker(words_a, words_b, is_case_sensitive):\n",
" # Modify your existing code here\n",
" if is_case_sensitive == False:\n",
" if len(words_a)==len(words_b): # when the number of words in both lists are same\n",
" upper_list_a = list(map(str.upper, words_a)) # uppercase all strings in list words_a, to create a new list\n",
" upper_list_b = list(map(str.upper, words_b)) # same as above\n",
" list_a=[] # create an empty list list_a\n",
" list_b=[] # same as above\n",
" for i in range(len(words_a)):\n",
" list_a.append(sorted(list(upper_list_a[i]))) # for each word in upper_list_a, slice into single letters, sort the letters alphabetically for each word, append it to list_a\n",
" list_b.append(sorted(list(upper_list_b[i]))) # same as above for list_b\n",
" sorted_list_a=sorted(list_a) # the order of list of single letters for each word from list_a is the same with the original list word_a, now sort the list alphabetically into a new list\n",
" sorted_list_b=sorted(list_b) # same as above\n",
" if sorted_list_a==sorted_list_b:# now that the order the single letters for each word as well as the order of the list of single letters are sorted, compare the two lists\n",
" return True\n",
" else:\n",
" return False\n",
" return False\n",
" \n",
" else: #is_case_sensitive == True\n",
" if len(words_a)==len(words_b):\n",
" # no need to uppercase the list, upper_list_a = list(map(str.upper, words_a))\n",
" # no need to uppercase the list, upper_list_b = list(map(str.upper, words_b))\n",
" list_a=[]\n",
" list_b=[]\n",
" for i in range(len(words_a)):\n",
" list_a.append(sorted(list(words_a[i])))\n",
" list_b.append(sorted(list(words_b[i])))\n",
" sorted_list_a=sorted(list_a)\n",
" sorted_list_b=sorted(list_b)\n",
" if sorted_list_a==sorted_list_b:\n",
" return True\n",
" else:\n",
" return False\n",
" return False\n",
" \n",
" \n",
"# Create your own words of potential anagrams and use your checker to check it\n",
"\n",
"# Create your own words of potential anagrams and use your checker to check it"
"anagram_checker([\"Rife\", \"gas\"], [\"SAG\", \"fire\"], False) # True (postive test)\n",
"# anagram_checker([\"Rife\", \"gas\"], [\"SAG\", \"fire\"], True) # False (negative test)"
]
},
{
Expand Down Expand Up @@ -145,7 +279,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
"version": "3.9.15"
}
},
"nbformat": 4,
Expand Down