dataset.py: 23 points
In this section you’ll build the DataSet
class that we’ll use to hold the data that our sorting algorithms will sort. The data itself will just be a list of integers (at least for this exercise), but having our own class lets us add some methods that will come in handy later.
DataSet
Class Summary below to get a high-level view of how it works.Tip: Don’t forget your docstrings!
Writing proper documentation is an important part of writing code (and, therefore, also part of your grade for this assignment). Remember to fill out the docstring for each method you implement. A useful docstring will have at least the following components:
self
), including their data type and purpose.DataSet
Class Summarysize
(int): The total number of values in the dataset.minimum
(int): The lowest value in the dataset.maximum
(int): The highest value in the dataset.data
(list): A list containing the values themselves.__init__
populate_data
get_data
set_data
is_sorted
display
__str__
This section contains the requirements for each method your DataSet
class will need to include, as well as some implementation suggestions.
__init__(self, size, minimum, maximum)
Initialize all of the attributes to the appropriate values. To accomplish this, you’ll need to do two things:
populate_data
method to initialize the data
attribute (see description below).populate_data(self)
Initialize self.data
by assigning it to a list of random integers with length equal to self.size
. This method will generate the list so the integers in it are chosen at random but are all greater than or equal to self.minimum
and less than or equal to self.maximum
. Note that it’s OK if some values appear more than once.
get_data(self)
Returns the list self.data
.
set_data(self, new_data)
Accepts a list of integers and assigns it to the attribute self.data
, replacing the random values generated by default. Although we will primarily want to use the randomly generated values, this method is convenient for testing that the object and our sorting methods are functioning as expected.
In order to make sure our object is internally consistent, this method will also need to update the size
, minimum
and maximum
attributes to reflect the relevant characteristics of the new data
attribute. For example, if the new value of self.data
is [0, 3, 10]
, this method should update self.size
to 3, self.minimum
to 0, and self.maximum
to 10.
is_sorted(self)
Checks whether self.data
is sorted from lowest to highest and returns True
if it is, False
otherwise.
display(self)
This method has been implemented for you, but requires you to complete the __str__
method below. When the __str__
method is completed correctly, display
will print a tab-delimited representation of the dataset to the terminal and pause until the user hits the Return key.
__str__(self)
Returns a string containing all the values in the dataset, separated by tabs (\t
). Python uses this method whenever a program prints a DataSet
object.
The test_dataset_class()
function is intended to run a number of quick consistency checks to make sure the methods of the DataSet
class are functioning correctly. When the DataSet
class and the test function are complete, running python3 dataset.py
should produce output similar to the example below (some of the actual values will differ since they are generated at random). Note that it will pause after each call to the display
method and you will have to hit the Return key to advance the program.
*******************************************************************************
Testing __init__, populate_data, and get_data.
-------------------------------------------------------------------------------
Creating dataset object with 10 values between 0 and 10.
The generated values are:
[8, 5, 5, 5, 7, 8, 4, 9, 1, 6]
*******************************************************************************
Testing display and __str__.
-------------------------------------------------------------------------------
8 5 5 5 7 8 4 9 1 6
*******************************************************************************
Testing set_data.
-------------------------------------------------------------------------------
Setting data to [4,1,9]
Display new data:
4 1 9
*******************************************************************************
Testing is_sorted.
-------------------------------------------------------------------------------
Sorted: False
Setting data to [1,2,3,4]
Sorted: True
Note on Evaluation
Your grade on this section is based on how well you implement the requirements listed above. Here are a few things you’ll want to double check: