Skip to content
323 changes: 307 additions & 16 deletions 02_activities/assignments/assignment_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -56,36 +56,202 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 61,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is an Anagram!\n"
]
}
],
"source": [
"# For testing purposes, we will write our code in the function\n",
"def anagram_checker(word_a, word_b):\n",
" # Your code here\n",
" '''This function takes in 2 words, and then determines if the two words \n",
" have the same number of unique letters to determine if they are an anagaram.\n",
" \n",
" First it makes them comparable by case. \n",
" Second, it filters words that are not the same length.\n",
" Third, we strip the string to get the individual letters of the first word\n",
" Fourth, we extract the individual letters and put it into a list for each \n",
" word with a for loop. \n",
" Fith, we sort both lists alphabetically\n",
" Sixth, we compare the two sorted lists of letters to see if they are the same \n",
" '''\n",
" if type(word_a) is not str or type(word_b) is not str:\n",
" print(\"Not a String! Incorrect Input\")\n",
" return\n",
" word_a = (word_a).casefold() # changing the case of both words\n",
" word_b = (word_b).casefold()\n",
" if len(word_a) != len(word_b): # checking if they are the same length \n",
" print(\"Not the same number of letters, so its not an Anagram. :(\") # printing\n",
" return\n",
" \n",
" word_a_list = []\n",
" position = 0\n",
" for position in range(0,len(word_a)):\n",
" word_a_list.append(word_a[position])\n",
"\n",
" word_b_list = []\n",
" position = 0\n",
" for position in range(0,len(word_b)):\n",
" word_b_list.append(word_b[position])\n",
" \n",
" sorted_a=word_a_list.sort()\n",
" sorted_b=word_b_list.sort()\n",
" \n",
" if sorted_a == sorted_b:\n",
" print(\"This is an Anagram!\")\n",
" else:\n",
" print(\"Same number of letters, but This is not an Anagram. :(\")\n",
" \n",
"\n",
"# Run your code to check using the words below:\n",
"anagram_checker(\"Silent\", \"listen\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Updated after getting feedback: \n",
"# - remove unncessary loops \n",
"# - change to boolean, not printed text returns\n",
"\n",
"def anagram_checker(word_a, word_b):\n",
" '''This function takes in 2 words, and then determines if the two words \n",
" have the same number of unique letters to determine if they are an anagaram.\n",
" \n",
" First it makes them comparable by case. \n",
" Second, it filters words that are not the same length.\n",
" Third, we strip the string to get the individual letters of the first word\n",
" Fourth, we extract the individual letters and put it into a list for each \n",
" word with a for loop. \n",
" Fith, we sort both lists alphabetically\n",
" Sixth, we compare the two sorted lists of letters to see if they are the same \n",
" '''\n",
" if type(word_a) is not str or type(word_b) is not str:\n",
" print(\"Not a String! Incorrect Input\")\n",
" return False\n",
" word_a = (word_a).casefold() # changing the case of both words\n",
" word_b = (word_b).casefold()\n",
" if len(word_a) != len(word_b): # checking if they are the same length \n",
" return False\n",
" \n",
" word_a_list = sorted(word_a)\n",
" word_b_list = sorted(word_b)\n",
" \n",
" if word_a_list == word_b_list:\n",
" return True\n",
" else:\n",
" False\n",
" \n",
"\n",
"anagram_checker(\"Silent\", \"listen\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"Silent\", \"Night\")"
]
},
{
"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": [
"anagram_checker(\"night\", \"Thing\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"552\", \"255\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Not a String! Incorrect Input\n"
]
},
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(552, 252)"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -97,35 +263,160 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 21,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def extract_compare_letters(word_a, word_b):\n",
" '''extract the letters from two words, compare if they are the same'''\n",
" \n",
" sorted_a=sorted(word_a)\n",
" sorted_b=sorted(word_b)\n",
" \n",
" if sorted_a == sorted_b:\n",
" return True\n",
" else:\n",
" return False\n",
"\n",
"def make_same_case(word_a, word_b):\n",
" '''changing the case of both words to be comparable'''\n",
" word_a = (word_a).lower() \n",
" word_b = (word_b).lower()\n",
" return word_a.lower(), word_b.lower()\n",
" \n",
"def check_string(word_a, word_b):\n",
" '''checking if the inputs are a string or not'''\n",
" if type(word_a) is not str or type(word_b) is not str:\n",
" return False\n",
"\n",
"def length_check(word_a, word_b):\n",
" if len(word_a) != len(word_b): \n",
" return False\n",
" \n",
"\n",
"def anagram_checker(word_a, word_b, is_case_sensitive):\n",
" # Modify your existing code here\n",
" \"\"\"This function compares word_a and word_b to see if they have the same letters. \n",
" First it converts them to the same case so that capitals don't affect comparison\n",
" Second it gets individual characters in each word and puts it into two different lists\n",
" Third it sorts the lists for each word\n",
" Fourth it compares the lists - if they are the same, it returns that they are anagrams\n",
" if not, it returns that it is not an anagram.\"\"\"\n",
"\n",
" # checking if the inputs are strings\n",
" check_string(word_a, word_b)\n",
"\n",
" # checking if they are the same length as an easy first check \n",
" length_check(word_a, word_b)\n",
" \n",
" if is_case_sensitive == True:\n",
" if extract_compare_letters(word_a, word_b) == True:\n",
" return True\n",
" else: \n",
" return False\n",
"\n",
" if is_case_sensitive == False:\n",
" word_a, word_b = make_same_case(word_a, word_b)\n",
" if extract_compare_letters(word_a, word_b) == True:\n",
" return True\n",
" else:\n",
" return False \n",
" \n",
"# Run your code to check using the words below:\n",
"anagram_checker(\"Silent\", \"listen\", False) # True"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 16,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"Silent\", \"listen\", True) # False"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 17,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"Silent\", \"Listen\", True) # False"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"SilenT\", 'LisTen', True) # False"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"SilenT\", 'LisTennnn', True) # False"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -139,7 +430,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "new-learner",
"display_name": "python-env",
"language": "python",
"name": "python3"
},
Expand All @@ -153,7 +444,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
"version": "3.11.14"
}
},
"nbformat": 4,
Expand Down