Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignment_1 #1

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
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
279 changes: 265 additions & 14 deletions 02_activities/assignments/assignment_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -56,32 +56,165 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 56,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 56,
"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",
" # using list function to create a list of each letter in the word_a & word_b\n",
" # using string.lower() method to change the string to lowercase.\n",
" # sort function will sort the alphabets in ascending order which is default. It modifies the elements in a list and doesn't return the sorted list.\n",
" alphabet_list_a = list(word_a.lower())\n",
" alphabet_list_a.sort()\n",
" alphabet_list_b = list(word_b.lower())\n",
" alphabet_list_b.sort()\n",
"\n",
" # == is for comparing two lists. It compares two lists elementwise. \n",
" return(alphabet_list_a == alphabet_list_b)\n",
"\n",
"# Run your code to check using the words below:\n",
"anagram_checker(\"Silent\", \"listen\")\n"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"Silent\", \"Night\")"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"night\", \"Thing\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Solution# 2\n",
"Using for loop"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def anagram_checker(word_a, word_b):\n",
" \n",
" # Creatinig an empty list and changing word_a to lowercase\n",
" alphabet_list_a = []\n",
" word_a = word_a.lower()\n",
"\n",
"# for loop goes through each index of the string (from 0 to (length - 1)) \n",
"# 1. It retrieves the letter at the current index\n",
"# 2. Adds the letter to the list. For each iteration, it keeps adding the letters to the updated list from the previous iteration. \n",
" for i in range(0, len(word_a)):\n",
" alphabet_list_a.append(word_a[i])\n",
"\n",
" alphabet_list_b = []\n",
" word_b = word_b.lower()\n",
" \n",
" for i in range(0, len(word_b)):\n",
" alphabet_list_b.append(word_b[i])\n",
"\n",
" # == is for comparing two lists. It compares two lists elementwise. \n",
" return(sorted(alphabet_list_a) == sorted(alphabet_list_b))\n",
"\n",
"# Run your code to check using the words below:\n",
"anagram_checker(\"Silent\", \"listen\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 63,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"Silent\", \"Night\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 64,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"night\", \"Thing\")"
]
Expand All @@ -97,22 +230,140 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 65,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def anagram_checker(word_a, word_b, is_case_sensitive):\n",
"\n",
"# Using if else to set the conditions about the case sensitivity\n",
" if is_case_sensitive == False:\n",
" \n",
" alphabet_list_a = list(word_a.lower())\n",
" alphabet_list_a.sort()\n",
" alphabet_list_b = list(word_b.lower())\n",
" alphabet_list_b.sort()\n",
"\n",
" return(alphabet_list_a == alphabet_list_b)\n",
"\n",
" else:\n",
" \n",
" alphabet_list_a = list(word_a)\n",
" alphabet_list_a.sort()\n",
" alphabet_list_b = list(word_b)\n",
" alphabet_list_b.sort()\n",
"\n",
" return(alphabet_list_a == alphabet_list_b)\n",
"\n",
"# Run your code to check using the words below:\n",
"anagram_checker(\"Silent\", \"listen\", False) # True"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"Silent\", \"Listen\", True) # False"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Modifying Solution# 2"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 67,
"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:\n",
"\n",
" alphabet_list_a = []\n",
" word_a = word_a.lower()\n",
"\n",
" for i in range(0, len(word_a)):\n",
" alphabet_list_a.append(word_a[i])\n",
"\n",
" alphabet_list_b = []\n",
" word_b = word_b.lower()\n",
" \n",
" for i in range(0, len(word_b)):\n",
" alphabet_list_b.append(word_b[i])\n",
"\n",
" return(sorted(alphabet_list_a) == sorted(alphabet_list_b))\n",
"\n",
" else:\n",
"\n",
" alphabet_list_a = []\n",
" for i in range(0, len(word_a)):\n",
" alphabet_list_a.append(word_a[i])\n",
"\n",
" alphabet_list_b = []\n",
" for i in range(0, len(word_b)):\n",
" alphabet_list_b.append(word_b[i])\n",
"\n",
" return(sorted(alphabet_list_a) == sorted(alphabet_list_b))\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": 68,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"Silent\", \"Listen\", True) # False"
]
Expand All @@ -130,7 +381,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "new-learner",
"display_name": "dsi_participant",
"language": "python",
"name": "python3"
},
Expand All @@ -144,7 +395,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
"version": "3.9.15"
}
},
"nbformat": 4,
Expand Down
Loading