Question
In this exercise you will continue building list utility functions. In contrast with the previous exercise, for EX05 you will get practice writing unit tests
In this exercise you will continue building list utility functions. In contrast with the previous exercise, for EX05 you will get practice writing unit tests for your functions which gives you more reassurance of their correctness and makes testing your functions easier. Your function implementations may only make use of the built-in len function, and a list objects methods append and pop.
Specifically off-limits in this exercise are the following capabilities which you would only know if you have prior experience in Python. Making use of any of the following will result in no credit for the function you use them in:
Cannot use other built-in function besides len - specifically not max, min, slice
Cannot use slice notation in conjunction with the subscription operator
Cannot use the in operator of Python with lists (but you can use for..in loops!)
Cannot use the list classs + or == operators (you can still use + with int and str types, though!) nor built-in methods beyond append and pop
Note: You can use + and == for individual items and values, just not entire list objects.
Assignment Outline
only_evens 25 Points Autograded
Unit Tests 5 Points Autograded
concat 25 Points Autograded
Unit Tests 5 Points Autograded
sub 30 Points Autograded
Unit Tests 5 Points Autograded
Style, Linting, Typing 20 Points Autograded
Note: Even if your functions are not 100% correct or finished, you can get full credit for the unit tests if you set up a function skeleton and write your tests assuming correct functionality.
0. Skeleton Code and Testing Setup
Create a new directory in exercises named ex05. (Right click on exercises and select new folder.)
Inside the exercises/ex05 directory, create a file named utils.py. Add a docstring and establish an __author__ variable to be assigned a string with the digits of your PID. This is where you will implement your function skeletons and implementations below.
1. only_evens 25 Points
This is the first function you will write in utils.py. The other two functions will also be defined in this file.
Given a list of ints, only_evens should return a new listcontaining only the elements of the input list that were even. The only_evens function must not modify the list it is given a reference to as a parameter. Example usage:
>>> only_evens([1, 2, 3]) [2] >>> only_evens([1, 5, 3]) [] >>> only_evens([4, 4, 4]) [4, 4, 4]
Continue by defining a skeleton function with the following signature:
Name: only_evens
Arguments: A list of integers.
Returns: A list of integers, containing only the even elements of the input parameter.
2. concat 25 Points
In this exercise you will write a function named concat. Given two Lists of ints, concat should generate a new List which contains all of the elements of the first list followed by all of the elements of the second list. Your concat function may not mutate (modify) either of its list parameters.
Define your function with the following signature.
Name: concat
Parameters: Two lists of ints.
Returns: A list containing all elements of the first list, followed by all elements of the second list.
concat must NOT mutate (modify) either of the arguments passed to it.
3. sub 30 Points
In this exercise you will write a function named sub. Given a listof ints, a start index, and an end index (not inclusive), sub should generate a List which is a subset of the given list, between the specified start index and the end index - 1. This function should not mutate its input list.
Example usage:
>>> a_list = [10, 20, 30, 40] >>> sub(a_list, 1, 3) [20, 30]
Next, define a skeleton function with the following signature in ex05/utils.py:
Name: sub
Parameters: A list and two ints, where the first int serves as a start index and the second int serves as an end index (not inclusive).
Returns: A List which is a subset of the given list, between the specified start index and the end index - 1.
If the start index is negative, start from the beginning of the list. If the end index is greater than the length of the list, end with the end of the list.
If the length of the list is 0, start is greater than or equal to the length of the list, or end is at most 0, return the empty list.
4. Unit Tests
Also inside the exercises/ex05 directory, create a file named utils_test.py. Add a docstring and establish an __author__ in this file as well.
For each function from below (only_evens, sub, concat), you are to define at least 3x unit test functions. Remember that a unit test function starts with test_.
The 3 unit tests should consist of:
One edge case
Two use cases
Include descriptive function names and docstrings, so that it captures what is being tested.
The command to run your tests is python -m pytest exercises/ex05 or you can run them using the beaker tab in VSCode if it is working (do note the VSCode testing feature tends to be a bit flaky).
Once youre ready to import and begin testing your skeletons, you can add the following import lines, corresponding to the functions you have completed, to do so:
from exercises.ex05.utils import only_evens from exercises.ex05.utils import sub from exercises.ex05.utils import concat
Once you have completed all functions, you can reduce the three import lines down into a single one for less redundancy in your testing code:
from exercises.ex05.utils import only_evens, sub, concat
If your screen is large enough, you are encouraged to open these files side-by-side in VSCode by dragging the tab of one to the right side of VSCode so that it changes to a split pane view. Closing your file explorer can help give you additional horizontal space.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started