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.
hw1.pyis the file to put your implementations for each problem.hw1.pyis not a runnable program, so we don’t use the main-method pattern here.hw1_test.pyis the file for you to put your own tests. Running this file executes the tests written within it.answers.txtis a text file for you to write your solutions to the debugging problems.
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:
The
minimumis the smallest number in the list.The
maximumis the largest number in the list.The
medianis the middle value in the list. If there is an even number of values in the list, take the average of the middle two values.The
first_quartileis the median of the lower half of the data (including theminimum), and thethird_quartileis the median of the upper half of the data (including themaximum).The
medianshould be excluded from the calculations of the first and third quartiles.
Here are some examples of inputs and expected outputs, which you are expected to turn into assert tests in hw1_test.py.
five_number_summary([1, 2, 3, 4, 5])should return(1, 1.5, 3, 4.5, 5)five_number_summary([1, 1, 1, 1, 1])should return(1, 1, 1, 1, 1)five_number_summary([30, 31, 31, 34, 36, 38, 39, 51, 53])should return(30, 31, 36, 45, 53)five_number_summary([5, 13, 14, 15, 16, 17, 25])should return(5, 13, 15, 17, 25)five_number_summary([5, 12, 12, 13, 13, 15, 16, 26, 26, 29, 29, 30])should return(5, 12.5, 15.5, 27.5, 30)five_number_summary([12, 12, 13, 13, 15, 16, 26, 26, 29, 29])should return(12, 13, 15.5, 26, 29)
Here are some examples of invalid function calls, which will not be tested.
five_number_summary([1])since the input list does not have at least five numbersfive_number_summary([5, 4, 3, 2, 1])since the input list is not sorted from least to greatest
📝 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. is considered an outlier if either:
is less than the first quartile minus 1.5 times the interquartile range
is greater than the third quartile plus 1.5 times the interquartile range
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.
num_outliers([1, 2, 3, 4, 5])should return0num_outliers([1, 99, 200, 500, 506, 507])should return0num_outliers([5, 13, 14, 15, 16, 17, 25])should return2(the outliers are 5 and 25)num_outliers([33, 34, 35, 36, 36, 36, 37, 37, 100, 101])should return2(the outliers are 100 and 101)num_outliers([8, 11, 11, 11, 11, 11])should return1(the outlier is 8)
The following examples of invalid function calls should not be tested:
num_outliers([3, 3, 3])input list should contain at least five numbersnum_outliers([3, 2, 1, 0, 5])input list should be sorted from least to greatest
📝 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.
text_normalize("Hello")should return"hello"text_normalize("Hello!")should return"hello"text_normalize("heLLo tHEr3!!!")should return"hellother"
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:
Boolean Zen
Loop Zen
Factoring
Unnecessary Cases
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:
hw1.pyhw1_test.pyanswers.txt
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.