# Initialize Otter
import otter
grader = otter.Notebook("hw4-functions.ipynb")

πŸ‘πŸ“ Homework 4 - Functions#

This assignment includes three problems on the topic of functions.

Entering Your Information for Credit#

To receive credit for assignments, it is important we can identify your work from others. To do this, we will ask you to enter your information in the following code block.

Before you begin#

Run the block of code at the top of the notebook that imports and sets up the autograder. This will allow you to check your work.

# Please provide your first name, last name, Drexel ID, and Drexel email. Make sure these are provided as strings. "STRINGS ARE TEXT ENCLOSED IN QUOTATION MARKS."

# In the assignments you will see sections of code that you need to fill in that are marked with ... (three dots). Replace the ... with your code.
first_name = ...
last_name = ...
drexel_id = ...
drexel_email = ...
grader.check("q0-Checking-Your-Name")

Question 1: Volume of a Sphere

The volume of a sphere with radius \(r\) is calculated using the equation:

\[ V = \frac{4}{3} \pi r^3\]

Write python code to do the following:

  • Define a function called sphere_volume which accepts one input argument r

  • Implement your function so that it returns the volume of a sphere with radius \(r\)

Your code replaces the prompt: ...

...
grader.check("q1-sphere")

Question 2: Simple Statistics

If you have a list which contains \(N\) values, \(x_1\) through \(x_N\), the mean \(\mu\) is calculated as follows:

\[ \mu = \sum_{i=1}^{N} \frac{x_{i}}{N} \]

To calculate the standard deviation of the data, you use the formula:

\[ \sigma = \sqrt{ \sum_{i=1}^{N} \frac{\left(x_{i} - \mu \right) ^ 2}{N - 1} } \]

Note: Python actually has a built-in statistics library that can be used to compute the mean \(\mu\) and standard deviation \(\sigma\). The code below uses the library to compute these statsics from a list of numbers L:

import statistics

L = [1,2,3,4,5]

print(statistics.mean(L))
print(statistics.stdev(L))

For this problem, the goal is to implement your own function which computes these statistics. You may not use the statistics library in your code (or any other library that can compute these statistics automatically), but may find it useful for checking your answer. A partial function is provided below and you are asked to complete it.

Your task is to do the following:

  • Read the provided code and the comments

  • Add a single line of code, indented under the first for loop, which adds to the mean variable the value: \(\frac{x_i}{N}\)

  • Add a single line of code, indented under the first for loop, which adds to the std variable the value: \(\frac{\left(x_{i} - \mu \right) ^ 2}{N - 1}\)

  • Return from the function a tuple containing (\(\mu\), \(\sigma\))

Your code replaces the prompt: ...

def stats(L):
    """This function takes in a list L and returns a tuple containing its (mean, standard deviation)"""

    N = len(L) # Compute the length of list L using the len() function, and store it in N.
    mean = 0  # Set a variable called mean to zero. We will add to this variable until it accumulates its proper value.
    std = 0   # Set a variable called std to zero. We will add to this variable until it accumulates its proper value.

    
    # The following for loop iterates over every value of x_i
    for x_i in L:
        # Your code below will add the appropriate value to mean and store the result back in mean  
        ...

    # The following for loop iterates over every value of x_i
    for x_i in L:
        # Your code below will add the appropriate value to std and store the result back in std 
        ...
        
    std = std ** (1/2) # To arrive at the standard deviation, we must take the square root of the accumulated value.

    # Your code below should return a tuple containing the mean and standard deviation.
    ...
grader.check("q2-stats")

Question 3: Kinetic Energy

The kinetic energy of an object \(E_k\) of an object is computed using its mass \(m\) and velocity \(v\) using the following equation:

\[ E_k = \frac{1}{2} m v^2 \]

Write python code to do the following:

  • Define a function called kinetic_energy that takes input arguments m and v

  • The function should return the kinetic energy

Your code replaces the prompt: ...

...
grader.check("q3-kinetic")

Submitting Your Assignment#

To submit your assignment please use the following link the assignment on GitHub classroom.

Use this link to navigate to the assignment on GitHub classroom.

If you need further instructions on submitting your assignment please look at Lab 1.

Viewing your score#

Each .ipynb file you have uploaded will have a file with the name of your file + Grade_Report.md. You can view this file by clicking on the file name. This will show you the results of the autograder.

Please make sure there are no spaces in the filename that you are submitting.

We have both public and hidden tests. You will be able to see the score of both tests, but not the specific details of why the test passed or failed.

Note

In python and particularly jupyter notebooks it is common that during testing you run cells in a different order, or run cells and modify them. This can cause there to be local variables needed for your solution that would not be recreated on running your code again from scratch. Your assignment will be graded based on running your code from scratch. This means before you submit your assignment you should restart the kernel and run all cells. You can do this by clicking Kernel and selecting Restart and Run All. If you code does not run as expected after restarting the kernel and running all cells it means you have an error in your code.

Fin#