diff --git a/02_activities/assignments/assignment_1.ipynb b/02_activities/assignments/assignment_1.ipynb index 2dca19d0b..20fd296b4 100644 --- a/02_activities/assignments/assignment_1.ipynb +++ b/02_activities/assignments/assignment_1.ipynb @@ -56,36 +56,95 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ - "# For testing purposes, we will write our code in the function\n", + "# Answers/Solution to Part 1\n", + "# Function to check if two words are anagrams\n", "def anagram_checker(word_a, word_b):\n", - " # Your code here\n", "\n", - "# Run your code to check using the words below:\n", + "# Convert both words to lowercase\n", + " word_a = word_a.lower()\n", + " word_b = word_b.lower()\n", + "\n", + "# Compare sorted letters\n", + " if sorted(word_a) == sorted(word_b):\n", + " return True\n", + " else:\n", + " return False" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Check if Silent and listen are anagrams\n", "anagram_checker(\"Silent\", \"listen\")" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ + "# Check if Silent and Night are anagrams\n", "anagram_checker(\"Silent\", \"Night\")" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ + "# Check if night and Thing are anagrams\n", "anagram_checker(\"night\", \"Thing\")" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -97,32 +156,83 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ + "# Answers/Solutions to Part 2 \n", + "# Modify existing code\n", + "# Check if two words are anagrams with case sensitivity option\n", "def anagram_checker(word_a, word_b, is_case_sensitive):\n", - " # Modify your existing code here\n", "\n", - "# Run your code to check using the words below:\n", + " if not is_case_sensitive:\n", + " word_a = word_a.lower()\n", + " word_b = word_b.lower()\n", + "\n", + " return sorted(word_a) == sorted(word_b)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Run your code to check using the words \"Silent\", \"listen\"\n", + "# Add boolean option False and check to see if they are anagrams of each other\n", "anagram_checker(\"Silent\", \"listen\", False) # True" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ + "# Add boolean option True and check to see if they are anagrams of each other\n", "anagram_checker(\"Silent\", \"listen\", True) # False" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ + "# Add boolean option True and check to see if they are anagrams of each other\n", "anagram_checker(\"Silent\", \"Listen\", True) # False" ] }, @@ -139,7 +249,7 @@ ], "metadata": { "kernelspec": { - "display_name": "new-learner", + "display_name": "python-env", "language": "python", "name": "python3" }, @@ -153,7 +263,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.8" + "version": "3.11.14" } }, "nbformat": 4, diff --git a/02_activities/assignments/assignment_2.ipynb b/02_activities/assignments/assignment_2.ipynb index 1ae6fe242..d90c53c7d 100644 --- a/02_activities/assignments/assignment_2.ipynb +++ b/02_activities/assignments/assignment_2.ipynb @@ -95,8 +95,15 @@ "\n", "with open(all_paths[0], 'r') as f:\n", " # YOUR CODE HERE: Use the readline() or readlines() method to read the .csv file into a variable\n", + " # Open the first file inflammation_01.csv\n", + " with open(r'/Users/osahenilawani/DSI_projects/python/05_src/data/assignment_2_data/inflammation_01.csv', 'r') as f: \n", + " lines = f.readlines() # Read all lines in the dataset from the file (inflammation_01.csv)\n", + " print(f.readlines()) # Print all lines in the dataset from the file (inflammation_01.csv)\n", " \n", - " # YOUR CODE HERE: Iterate through the variable using a for loop and print each row for inspection" + " # YOUR CODE HERE: Iterate through the variable using a for loop and print each row for inspection\n", + "\n", + "for line in lines: # Loop through each line (observations for each patient) in the dataset and print it\n", + " print(line.strip())" ] }, { @@ -130,10 +137,8 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "82-bk4CBB1w4" - }, + "execution_count": 133, + "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", @@ -144,21 +149,30 @@ "\n", " # Implement the specific operation based on the 'operation' argument\n", " if operation == 'mean':\n", - " # YOUR CODE HERE: Calculate the mean (average) number of flare-ups for each patient\n", + " # YOUR CODE HERE: \n", + " patient_summary = np.mean(data, axis=ax) # Calculate mean (average) flare-ups for each patient in the dataset\n", "\n", " elif operation == 'max':\n", - " # YOUR CODE HERE: Calculate the maximum number of flare-ups experienced by each patient\n", + " # YOUR CODE HERE: \n", + " patient_summary = np.max(data, axis=ax) # Calculate the maximum number of flare-ups experienced by each patient in the dataset\n", "\n", " elif operation == 'min':\n", - " # YOUR CODE HERE: Calculate the minimum number of flare-ups experienced by each patient\n", - "\n", + " # YOUR CODE HERE: \n", + " patient_summary = np.min(data, axis=ax) # Calculate the minimum number of flare-ups experienced by each patient in the dataset\n", " else:\n", " # If the operation is not one of the expected values, raise an error\n", - " raise ValueError(\"Invalid operation. Please choose 'mean', 'max', or 'min'.\")\n", + " raise ValueError(\"Invalid operation. Please choose 'mean', 'max', or 'min'.\") #flags an error if the mean, max or min is not specified in the operation\n", "\n", - " return summary_values" + " return patient_summary" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": null, @@ -169,7 +183,7 @@ "source": [ "# Test it out on the data file we read in and make sure the size is what we expect i.e., 60\n", "# Your output for the first file should be 60\n", - "data_min = patient_summary(all_paths[0], 'min')\n", + "data_min = patient_summary(r'/Users/osahenilawani/DSI_projects/python/05_src/data/assignment_2_data/inflammation_01.csv', 'min')\n", "print(len(data_min))" ] }, @@ -228,7 +242,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 114, "metadata": { "id": "_svDiRkdIwiT" }, @@ -251,18 +265,38 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "LEYPM5v4JT0i" - }, + "execution_count": 115, + "metadata": {}, "outputs": [], "source": [ - "# Define your function `detect_problems` here\n", + "# Run this cell so you can use this helper function\n", "\n", + "def check_zeros(x):\n", + " '''\n", + " Given an array, x, check whether any values in x equal 0.\n", + " Return True if any values found, else returns False.\n", + " '''\n", + " # np.where() checks every value in x against the condition (x == 0) and returns a tuple of indices where it was True (i.e. x was 0)\n", + " flag = np.where(x == 0)[0]\n", + "\n", + " # Checks if there are any objects in flag (i.e. not empty)\n", + " # If not empty, it found at least one zero so flag is True, and vice-versa.\n", + " return len(flag) > 0" + ] + }, + { + "cell_type": "code", + "execution_count": 106, + "metadata": {}, + "outputs": [], + "source": [ + "# Define your function `detect_problems` here\n", "def detect_problems(file_path):\n", " #YOUR CODE HERE: Use patient_summary() to get the means and check_zeros() to check for zeros in the means\n", - "\n", - " return" + " # Get mean inflammation values for each patient\n", + " means = patient_summary(r'/Users/osahenilawani/DSI_projects/python/05_src/data/assignment_2_data/inflammation_01.csv', 'mean')\n", + " has_problem = check_zeros(means) # Check if any mean values are zero\n", + " return has_problem" ] }, { @@ -273,7 +307,8 @@ "source": [ "# Test out your code here\n", "# Your output for the first file should be False\n", - "print(detect_problems(all_paths[0]))" + "print(detect_problems(r'/Users/osahenilawani/DSI_projects/python/05_src/data/assignment_2_data/inflammation_01.csv')) \n", + "# prints output to detetect if any mean values are zero " ] }, { @@ -314,7 +349,8 @@ "provenance": [] }, "kernelspec": { - "display_name": "Python 3", + "display_name": "python-env", + "language": "python", "name": "python3" }, "language_info": { @@ -327,7 +363,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.8" + "version": "3.11.14" } }, "nbformat": 4,