Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Learning objective: Apply control structures and data structures to solve problems mimicking different types of data cleanup and processing. Later, we’ll learn some real-world library functions to complete some of these tasks more efficiently!

Setting Up

You can find the starter code for this homework on JupyterHub.

Expectations: In hw1.py, you should not use any import statements or features in Python we have not yet discussed in class, section, or homework specs. All of these problems should be solved using the fundamental constructs we’ve learned in class so far. hw1_test.py requires the import hw1 statement so that you can write assert statements. Don’t remove this line!

Programming

total and test_total

The total function should take in an int number n and returns the sum of the integers from 0 (inclusive) to n (inclusive). If n is negative, the function should return the value None instead. A preliminary solution has been provided for you, but it has a few bugs!

📝 Task: Identify and correct the bug, and then explain the bugs you encountered and what drew you to your specific fixes in answers.txt. For this task, you are already given one test case in hw1_test.py. Write at least 3 new test cases to help you debug total.

Documentation is already given to you for this task. Do not modify either doc-string!

five_number_summary and median

The function five_number_summary is defined already for you. This function takes a sorted list of at least 5 numbers and returns a tuple containing the five-number summary of the input: the input list’s (minimum, first_quartile, median, third_quartile, maximum).

For reference, here is how each value is defined:

Here are some examples of inputs and expected outputs, which you are expected to turn into assert tests in hw1_test.py.

Here are some examples of invalid function calls, which will not be tested.

📝 Task: The function five_number_summary is correct, but the helper function median is not. Write a test function in hw1_test.py that checks the output of median with assert statements so you can debug this function. Then, write a test function in hw1_test.py that checks for the correctness of five_number_summary, using assert statements. For the five_number_summary test function, include test cases for all of the valid examples above (there should be six of them) as well as 2 additional test cases (for a total of 8 tests).

num_outliers

An outlier is an extreme data point that can influence the shape and distribution of numeric data. xx is considered an outlier if either:

The interquartile range is defined as the third quartile minus the first quartile.

📝 Task: Write a function num_outliers that takes a sorted list of at least five numbers and returns the number of data points that would be considered outliers using your five_number_summary to calculate the first and third quartiles.

The following examples of invalid function calls should not be tested:

📝 Task: Write a test that calls the function with some inputs and compares the output of the program with the expected value using assert statements. Include test cases for all of the valid examples above (there should be five of them) as well as 2 additional test cases (for a total of 7 tests).

text_normalize

📝 Task: Write a function text_normalize that takes a string and returns a new string that keeps only alphabetical characters (ignore whitespace, numbers, non-alphabet characters, etc.) and turns all alphabetical characters to lowercase.

We recommend using a variable or a data structure to store all alphabetic characters, and then using this to keep only alphabetic characters in the original string.

📝 Task: Write a test function that calls the function with some inputs and compares the output of the program with the expected value using assert statements. Include test cases for all of the examples above as well as 2 additional test cases (for a total of 5 tests).

Code Quality

Hoemwork submissions should adhere to the code quality guidelines. The code quality guidelines are very thorough. For this assessment, the most relevant rules can be found in these sections:

Note: Make sure to provide a descriptive file header in docstring format, not something generic like “implements functions for Processing”. Consider: what do these functions have in common? What is the common theme of this assignment?

Note: You do not need to verify that your submission adheres to flake8 style checks discussed in the code quality guide for this homework, however, this will be needed for future homeworks.

Submission

Submit your work by uploading the following files to Gradescope:

You will receive a warning from Gradescope if any file is missing! Submit as often as you want until the deadline for the initial submission. Note that we will only grade your most recent submission.

Please make sure you are familiar with the resources and policies outlined in the syllabus.