# Initialize Otter
import otter
grader = otter.Notebook("hw3-types.ipynb")

πŸ‘πŸ“ Homework 3 - Understanding Data Types#

This assignment includes three quesitons on the topic of data types.

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: Creating a Password

In this problem you will implement a function that generates a password for a user based on their favorite color, pet’s name and an integer number.

If their favorite color is yellow, their pet is called Daisy, and their favorite number is 6, the suggested password should be: "yellow_Daisy6".

Write python code to do the following:

  • Inside the provided create_password function, combine the variables fav_color,pet_name, and number to make a password in the proper format specified above.

  • Store the the final string in a variable called password.

Your code replaces the prompt: ...

# This line creates a function called create_password which has 3 arguments: fav_color, pet_name, and number
def create_password(fav_color, pet_name, number):
    ...
    # This line outputs from the function the password that you generated
    return password

# this line runs the function and prints its output
print(create_password("yellow", "Daisy", 6))
grader.check("q1-password")

Question 2: Caffeine Levels

A half-life is the amount of time it takes for a substance or entity to fall to half its original value. Caffeine has a half-life of about 6 hours in humans. The amount of caffeine left after \(h\) hours, \(C_h\), is approximated using the following equation:

\[C_h = C_0 \left( \frac{1}{2} \right) ^{ h/6 }\]

Where \(C_0\) is the initial amount of caffeine.

You will write code that generates a brief report of the caffeine level for a given intial amount of caffeine (in mg) after a given amount amount of time (in hours). If the initial caffeine amount is 100 mg, and the given time passed is 12 hours, the report should be:

"After 12.00 hours: 25.00 mg"

Notice that both values should be reported with two digits after the decimal point. You should use a string formatting expression to do this.

Example: if you have a variable x = 5, we can get the string "5.00" with the following expression: f"{x:.2f}"

Write python code to do the following:

  • Inside the provided caffeine_levels function, calculate the final caffeine level based on the starting amount amt and the hours passed hours.

  • Generate the report string and store it in a variable called report.

Your code replaces the prompt: ...

# This line creates a function called caffeine_levels which has 2 arguments: amt and hours
def caffeine_levels(amt, hours):
    ...
    # this line outputs the report from the function
    return report

# this line runs the function and prints its output
print(caffeine_levels(100, 12))
caffeine_levels(100, 12)
grader.check("q2-caffeine")

Question 3: Phone Number Parsing

This problem involves parsing a phone number. Parsing means to evaluate a collection of symbols. Parsing is an important task in many computer applications.

You will write a program that takes a string containing a formatted phone number and parses it into a single integer. The format we will use for phone numbers is "XXX-XXX-XXXX" where X is an integer in \([0,9]\).

For example, a given phone number string "123-456-7890" should result in the integer 1234567890.

Hint: use slicing to extract part of a string. If S = "123-456-7890", we can use S[0:3] to slice just the first 3 characters of the string, producing "123".

Write python code to do the following:

  • Inside the provided parse_phone_number function, parse the formatted phone number string S to produce an integer

  • Store the resulting integer in a variable called parsed.

Your code replaces the prompt: ...

# This line creates a function called parse_phone_number which has 1 argument: S
def parse_phone_number(S):
    ...
    # this line outputs the parsed number from the function
    return parsed

# this line runs the function and prints its output
print(parse_phone_number("123-456-7890"))
grader.check("q3-phone-parsing")

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.

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#