Skip to content
299 changes: 283 additions & 16 deletions 02_assignments/assignment_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -56,34 +56,138 @@
},
{
"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",
" \"\"\" Check if two words or phrases are anagrams \"\"\" \n",
" # An anagram is a word or phrase formed \n",
" # by rearranging the letters of another word or phrase\n",
" \n",
" # Remove non-alphabetic characters from the input words \n",
" word_a = ''.join(char for char in word_a.lower() if char.isalpha())\n",
" word_b = ''.join(char for char in word_b.lower() if char.isalpha())\n",
" \n",
" # Check if the sorted characters in the words are the same\n",
" return sorted(word_a) == sorted(word_b)\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"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Anagram phrases with spaces\n",
"anagram_checker(\"salvages\", \"Las Vegas\") # True"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Anagram phrases with spaces and punctulation\n",
"anagram_checker(\"the public art galleries\", \"large picture halls, I bet\") #\tTrue"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"asdflkasdjf , 2302, 13 ; /-\", \"gibberish\") #\tFalse"
]
},
{
Expand All @@ -97,26 +201,184 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def anagram_checker(word_a, word_b, is_case_sensitive):\n",
" # Modify your existing code here\n",
" \"\"\" Check if two words are anagrams with the case sensitive parameter \"\"\"\n",
" \n",
" # Remove non-alphabetic characters from the input words \n",
" word_a = ''.join(char for char in word_a if char.isalpha())\n",
" word_b = ''.join(char for char in word_b if char.isalpha())\n",
" \n",
" # If is_case_sensitive is True, Sort the words not converting to lowercase and compare them \n",
" if is_case_sensitive: \n",
" return sorted(word_a) == sorted(word_b) \n",
" \n",
" # If is_case_sensitive is False, convert both words to lists in lowercase and compare them\n",
" else:\n",
" return sorted(word_a.lower()) == sorted(word_b.lower()) \n",
" \n",
" \n",
"\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": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"Iceman\", \"Cimena\", True) # False"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# anagram-phrases with spaces\n",
"anagram_checker(\"the Morse Code\", \"here come dots\", False) # True"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# phrases with spaces, punctuation, and capitalization\n",
"anagram_checker(\"departed this life\", \"He's left it, dead RIP\", False) # True"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"pairs\", \"Paris\", True) # False"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"dormitory\", \"dirty room\", False) # True"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"Slient\", \"Listen\", True) # False"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"Q!W@E#r4t5y6789\", \"Q!W@E#r,,,4//%^&*()t 5 y ;:6### 7 8-9\", False) # True"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -126,6 +388,11 @@
"|Code Execution|All code cells execute without errors.|Any code cell produces an error upon execution.|\n",
"|Code Quality|Code is well-organized, concise, and includes necessary comments for clarity. E.g. Great use of variable names.|Code is unorganized, verbose, or lacks necessary comments. E.g. Single character variable names outside of loops.|"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
Expand All @@ -144,7 +411,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
"version": "3.9.15"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion 03_homework/02_comments_and_errors.ipynb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"cells":[{"cell_type":"markdown","metadata":{"id":"jNAI57ELh-I8"},"source":["# Comments and Errors\n","## Practice Problems"]},{"cell_type":"markdown","metadata":{"id":"YNjWPDWjifTe"},"source":["### 1. Which of these expressions results in an error? Why does each error occur?"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":27,"status":"ok","timestamp":1667929891453,"user":{"displayName":"Kaylie Lau","userId":"01284785813595846851"},"user_tz":300},"id":"o3Oc7gU9ihND","outputId":"e17b9653-fc59-4c03-8bd5-132fcc865579"},"outputs":[],"source":["((((5 * 4 ** 7))))"]},{"cell_type":"markdown","metadata":{"id":"vN_Hk0Dxirg_"},"source":["<details>\n"," <summary>Answer</summary>\n","\n"," Valid -- Python accepts extraneous parentheses.\n","</details>"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":130},"executionInfo":{"elapsed":20,"status":"error","timestamp":1667929891454,"user":{"displayName":"Kaylie Lau","userId":"01284785813595846851"},"user_tz":300},"id":"JIohkU67ilDf","outputId":"2d905bb6-f573-4c1c-cc95-f248e004ec4e"},"outputs":[],"source":["84 *= 0.5 / 7"]},{"cell_type":"markdown","metadata":{"id":"EoWEA9nWiuY8"},"source":["<details>\n"," <summary>Answer</summary>\n","\n"," Error -- Python thinks we are trying to assign a value to 84, which is an integer literal. We can't change its value!\n","</details>"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":160,"status":"ok","timestamp":1667929896986,"user":{"displayName":"Kaylie Lau","userId":"01284785813595846851"},"user_tz":300},"id":"-Qms7P2DimjG","outputId":"9cd8ca29-b3d4-4e20-aba9-ca535def67a7"},"outputs":[],"source":["(-(-(-(-5 * (4 + 3)))))"]},{"cell_type":"markdown","metadata":{"id":"FvtzD77YivYE"},"source":["<details>\n"," <summary>Answer</summary>\n","\n"," Answer: Valid -- The `-` negates the value in the parentheses.\n","</details>"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":130},"executionInfo":{"elapsed":462,"status":"error","timestamp":1667929898860,"user":{"displayName":"Kaylie Lau","userId":"01284785813595846851"},"user_tz":300},"id":"d3dl1pNGioZN","outputId":"0f912a63-e784-4787-f3ab-89bd9939ab2c"},"outputs":[],"source":["5 * 3 = weight"]},{"cell_type":"markdown","metadata":{"id":"SAb5BHZ0ixm1"},"source":["<details>\n"," <summary>Answer</summary>\n","\n"," Error -- We cannot assign a value to an expression, even if `weight` had been defined.\n","</details>"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":140,"status":"ok","timestamp":1667929905100,"user":{"displayName":"Kaylie Lau","userId":"01284785813595846851"},"user_tz":300},"id":"a60xC38ziqOj","outputId":"8c79d0aa-e732-4b69-ae61-397f2e77b4e2"},"outputs":[],"source":["73 / -----------5"]},{"cell_type":"markdown","metadata":{"id":"fGDCqEGJizS-"},"source":["<details>\n"," <summary>Answer</summary>\n","\n"," Valid -- negatives cancel each other out.\n","</details>"]},{"cell_type":"markdown","metadata":{},"source":["### 2. Write a block of code to check if the value is a string. If it is, print out a message saying it is valid. Otherwise, raise an exception with a message saying the value must be a string."]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["value = 6\n","\n","# Your code goes here"]},{"cell_type":"markdown","metadata":{},"source":["<details>\n"," <summary>Answer</summary>\n","\n"," ```python\n"," value = 6\n","\n"," if type(value) == str:\n"," print(\"It is valid!\")\n"," else:\n"," raise TypeError(\"The value must be a string!\")\n"," ```\n","</details>"]},{"cell_type":"markdown","metadata":{},"source":["### 3. What are the following errors?\n","\n","```python\n","arr = [1, 2, 3]\n","print(arr[3])\n","```\n","\n","<details>\n"," <summary>Answer</summary>\n","\n"," IndexError\n","</details>\n"]},{"cell_type":"markdown","metadata":{},"source":["\n","```python\n","fullName = \"Taylor Swift\"\n","print(full_name)\n","```\n","\n","<details>\n"," <summary>Answer</summary>\n","\n"," NameError\n","</details>\n"]},{"cell_type":"markdown","metadata":{},"source":["\n","```python\n","2 + '2' + 4\n","```\n","\n","<details>\n"," <summary>Answer</summary>\n","\n"," TypeError\n","</details>"]}],"metadata":{"colab":{"authorship_tag":"ABX9TyMhUBNX+UP2C+YDtVSxQKBK","collapsed_sections":[],"provenance":[]},"kernelspec":{"display_name":"Python 3","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.11.5"}},"nbformat":4,"nbformat_minor":0}
{"cells":[{"cell_type":"markdown","metadata":{"id":"jNAI57ELh-I8"},"source":["# Comments and Errors\n","## Practice Problems"]},{"cell_type":"markdown","metadata":{"id":"YNjWPDWjifTe"},"source":["### 1. Which of these expressions results in an error? Why does each error occur?"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":27,"status":"ok","timestamp":1667929891453,"user":{"displayName":"Kaylie Lau","userId":"01284785813595846851"},"user_tz":300},"id":"o3Oc7gU9ihND","outputId":"e17b9653-fc59-4c03-8bd5-132fcc865579"},"outputs":[],"source":["((((5 * 4 ** 7))))"]},{"cell_type":"markdown","metadata":{"id":"vN_Hk0Dxirg_"},"source":["<details>\n"," <summary>Answer</summary>\n","\n"," Valid -- Python accepts extraneous parentheses.\n","</details>"]},{"cell_type":"code","execution_count":1,"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":130},"executionInfo":{"elapsed":20,"status":"error","timestamp":1667929891454,"user":{"displayName":"Kaylie Lau","userId":"01284785813595846851"},"user_tz":300},"id":"JIohkU67ilDf","outputId":"2d905bb6-f573-4c1c-cc95-f248e004ec4e"},"outputs":[{"ename":"SyntaxError","evalue":"'literal' is an illegal expression for augmented assignment (868797654.py, line 1)","output_type":"error","traceback":["\u001b[1;36m Cell \u001b[1;32mIn[1], line 1\u001b[1;36m\u001b[0m\n\u001b[1;33m 84 *= 0.5 / 7\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m 'literal' is an illegal expression for augmented assignment\n"]}],"source":["84 *= 0.5 / 7"]},{"cell_type":"markdown","metadata":{"id":"EoWEA9nWiuY8"},"source":["<details>\n"," <summary>Answer</summary>\n","\n"," Error -- Python thinks we are trying to assign a value to 84, which is an integer literal. We can't change its value!\n","</details>"]},{"cell_type":"code","execution_count":2,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":160,"status":"ok","timestamp":1667929896986,"user":{"displayName":"Kaylie Lau","userId":"01284785813595846851"},"user_tz":300},"id":"-Qms7P2DimjG","outputId":"9cd8ca29-b3d4-4e20-aba9-ca535def67a7"},"outputs":[{"data":{"text/plain":["35"]},"execution_count":2,"metadata":{},"output_type":"execute_result"}],"source":["(-(-(-(-5 * (4 + 3)))))"]},{"cell_type":"markdown","metadata":{"id":"FvtzD77YivYE"},"source":["<details>\n"," <summary>Answer</summary>\n","\n"," Answer: Valid -- The `-` negates the value in the parentheses.\n","</details>"]},{"cell_type":"code","execution_count":3,"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":130},"executionInfo":{"elapsed":462,"status":"error","timestamp":1667929898860,"user":{"displayName":"Kaylie Lau","userId":"01284785813595846851"},"user_tz":300},"id":"d3dl1pNGioZN","outputId":"0f912a63-e784-4787-f3ab-89bd9939ab2c"},"outputs":[{"ename":"SyntaxError","evalue":"cannot assign to operator (3700621706.py, line 1)","output_type":"error","traceback":["\u001b[1;36m Cell \u001b[1;32mIn[3], line 1\u001b[1;36m\u001b[0m\n\u001b[1;33m 5 * 3 = weight\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m cannot assign to operator\n"]}],"source":["5 * 3 = weight"]},{"cell_type":"markdown","metadata":{"id":"SAb5BHZ0ixm1"},"source":["<details>\n"," <summary>Answer</summary>\n","\n"," Error -- We cannot assign a value to an expression, even if `weight` had been defined.\n","</details>"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":140,"status":"ok","timestamp":1667929905100,"user":{"displayName":"Kaylie Lau","userId":"01284785813595846851"},"user_tz":300},"id":"a60xC38ziqOj","outputId":"8c79d0aa-e732-4b69-ae61-397f2e77b4e2"},"outputs":[],"source":["73 / -----------5"]},{"cell_type":"markdown","metadata":{"id":"fGDCqEGJizS-"},"source":["<details>\n"," <summary>Answer</summary>\n","\n"," Valid -- negatives cancel each other out.\n","</details>"]},{"cell_type":"markdown","metadata":{},"source":["### 2. Write a block of code to check if the value is a string. If it is, print out a message saying it is valid. Otherwise, raise an exception with a message saying the value must be a string."]},{"cell_type":"code","execution_count":9,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["The value is an integer: 6\n"]}],"source":["value = 6\n","\n","# Your code goes here\n","if type(value) == int:\n"," print(\"The value is an integer:\" + \" \" + str(value))\n","else:\n"," raise TypeError(\"The value is not an integer\")\n"," "]},{"cell_type":"markdown","metadata":{},"source":["<details>\n"," <summary>Answer</summary>\n","\n"," ```python\n"," value = 6\n","\n"," if type(value) == str:\n"," print(\"It is valid!\")\n"," else:\n"," raise TypeError(\"The value must be a string!\")\n"," ```\n","</details>"]},{"cell_type":"markdown","metadata":{},"source":["### 3. What are the following errors?\n","\n","```python\n","arr = [1, 2, 3]\n","print(arr[3])\n","```\n","\n","<details>\n"," <summary>Answer</summary>\n","\n"," IndexError\n","</details>\n"]},{"cell_type":"markdown","metadata":{},"source":["\n","```python\n","fullName = \"Taylor Swift\"\n","print(full_name)\n","```\n","\n","<details>\n"," <summary>Answer</summary>\n","\n"," NameError\n","</details>\n"]},{"cell_type":"markdown","metadata":{},"source":["\n","```python\n","2 + '2' + 4\n","```\n","\n","<details>\n"," <summary>Answer</summary>\n","\n"," TypeError\n","</details>"]}],"metadata":{"colab":{"authorship_tag":"ABX9TyMhUBNX+UP2C+YDtVSxQKBK","collapsed_sections":[],"provenance":[]},"kernelspec":{"display_name":"Python 3","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.9.15"}},"nbformat":4,"nbformat_minor":0}
Loading