Skip to content
Open
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
115 changes: 94 additions & 21 deletions 02_assignments/assignment_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -56,34 +56,74 @@
},
{
"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",
" '''Given two strings, check to see if they are anagrams of each other.\n",
" If yes, return `True`, else return `False`.\n",
" Uppercase and lowercase characters are the same.'''\n",
" if type(word_a) == str and type(word_b) == str: # Check if arguments are strings\n",
" if len(word_a) == len(word_b): # For efficiency there is no need to sort if lengths are not equal\n",
" return sorted(word_a.lower()) == sorted(word_b.lower()) # Convert to lowercase, sort characters, and check if same\n",
" else:\n",
" return False\n",
" else:\n",
" print('Please pass two arguments of type string')\n",
"\n",
"# Run your code to check using the words below:\n",
"anagram_checker(\"Slient\", \"listen\")"
"anagram_checker(\"Slient\", \"listen\") # True"
]
},
{
"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\")"
"anagram_checker(\"Slient\", \"Night\") # False"
]
},
{
"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\")"
"anagram_checker(\"night\", \"Thing\") # True"
]
},
{
Expand All @@ -97,24 +137,57 @@
},
{
"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",
" '''Given two strings, check to see if they are anagrams of each other.\n",
" If yes, return `True`, else return `False`.\n",
" The boolean option determines case sensitivity.'''\n",
" if type(word_a) == str and type(word_b) == str: # Check if arguments are strings\n",
" if len(word_a) == len(word_b): # For efficiency there is no need to sort if lengths are not equal\n",
" if is_case_sensitive: # Check if case sensisitivity is on\n",
" return sorted(word_a) == sorted(word_b) # Sort characters and check if same\n",
" else:\n",
" return sorted(word_a.lower()) == sorted(word_b.lower()) # Convert to lowercase, sort characters, and check if same\n",
" else:\n",
" return False\n",
" else:\n",
" print('Please pass two arguments of type string')\n",
"\n",
"# Run your code to check using the words below:\n",
"anagram_checker(\"Slient\", \"listen\", False) # True"
"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"
"anagram_checker(\"Slient\", \"Listen\", True) # False"
]
},
{
Expand Down Expand Up @@ -144,7 +217,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
"version": "3.9.15"
}
},
"nbformat": 4,
Expand Down