CS21 Lab 9: Sorting
Due Monday, November 23, by 11:59pm
Please read through the entire lab before starting!
In this lab you will continue using the Graduates data set from Lab 8, this time supporting filtering and sorting operations instead of just searching.
Additionally, all the "standard" requirements from previous labs such as programming tips, function comments, etc. still apply.
As you write programs, use good programming practices:
-
Use a comment at the top of the file to describe the purpose of the program (see example).
-
All programs should have a
main()function (see example). -
Use variable names that describe the contents of the variables.
-
Write your programs incrementally and test them as you go. This is really crucial to success: don’t write lots of code and then test it all at once! Write a little code, make sure it works, then add some more and test it again.
-
Don’t assume that if your program passes the sample tests we provide that it is completely correct. Come up with your own test cases and verify that the program is producing the right output on them.
-
Avoid writing any lines of code that exceed 80 columns.
-
Always work in a terminal window that is 80 characters wide (resize it to be this wide)
-
In
vscode, at the bottom right in the window, there is an indication of both the line and the column of the cursor.
-
Function Comments
All functions should include a comment explaining their purpose, parameters, return value, and describe any side effects. Please see our function example page if you are confused about writing function comments.
Goals
-
Write a program that filters data by different criteria
-
Write a program that sorts data by different fields
-
Continue practicing Top-Down Design (TDD)
-
Connect CS topics with real-world data
1. Provided Library
The graduates_lab library has some helpful tools for managing graduate
records in your program.
1.1. Graduates Class
The library provides an Graduates class, which encapsulates all the
information you’ll need to keep track of one record. You can create an
instance object of this class by calling the Graduates() constructor with
each field of information from one row of the graduates.txt file passed in
as separate parameters. You may use any of these methods in your
solution, but you may not need to use all of them.
You can call the following methods on an Graduates object to retrieve information about it:
-
get_id(): Get the id field. (string) -
get_year(): Get the year field. (int) -
get_total(): Get the total graduates from that record. (int) -
get_major(): Get the major field. (string) -
get_sal(): Get the mean salary field. (float) -
get_employed(): Get the total employed field. (int)
2. Filtering the Graduates Data
In this lab, you will implement a program called sort_graduates.py, which will allow the user to filter this data set and sort by different fields. This will be an
extension of work done for the previous lab, so you are encouraged to use
portions of your previous program (you don’t need to don’t re-implement
functions you’ve already written).
2.1. Program Behavior
-
Your program should read in data from a file, storing a list of records. This is likely the same as your solution to lab 8 and you may copy over the code to read this data over to your lab 9 solution.
As before, the data is stored in the file:
/usr/local/doc/graduates.txtNote: once again, please don’t copy this file to your
labs/09directory, just use"/usr/local/doc/graduates.txt"as the file name in your python program.As you read in the data, each line of the file corresponds to one record, which your program should represent using a Graduates object and then store it in a list.
-
After reading the data, your program should repeatedly present a menu to the user with the six choices shown below:
Please select one of the following choices: 1. Filter records by major 2. Filter records by mean salary 3. Sort by year 4. Sort by mean salary 5. Reset list to all records 0. Quit Choice?
Depending on the user’s selection, prompt them for additional information and then do one of:
-
Filter the data set by major.
-
Filter the data set by min and max mean salary.
-
Sort the current set of records by year. (Bubble Sort)
-
Sort the current set of records by mean salary. (Selection Sort)
-
Reset the current set of records to the original data set.
-
Exit the program.
-
When filtering, your program should prompt for the filter criteria (major or min/max mean salary) and return a new list of records that match only the filter criteria. For this program, your program can continue to implement prefix matching like we did in lab 8.
-
When sorting, your program should perform the sort in place, meaning it should modify the list passed in to the sorting function.
-
You must use different sorting algorithms for the two sort operations (e.g. Selection Sort for sort by salary and Bubble Sort for sort by year).
-
Ensure that the user provides valid input (menu choices and numerical salaries).
-
After a filter or sort step, print all current records in the appropriate order
-
2.2. Example Output
-
Here are some examples of how the program might look in operation:
2.3. Requirements
-
Your program should read data from the
graduates.txtdata set and store the data in a list ofGraduatesobjects. -
Your program must repeatedly prompt the user for menu options as described above. It should validate it is one of the allowable options.
-
For filtering by mean salary, the program must repeatedly prompt the user for valid max and min salary. You can reuse your validation code from Lab 8.
-
When the user selects filtering by major, the program should subsequently ONLY show records from the selected major until the user selects option 5 (reset list to all records).
-
When the user selects filtering by mean salary, the program should subsequently ONLY show records within the selected mean salary range until the user selects option 5 (reset list to all records).
|
Filtering Details
The initial set of records should be every record in the data file. However, anytime you apply a filter operation, the filter operation replaces the current set of records with the records matching the filter. Thus if you filter by the major "Biology" and then sort by mean salary, you should only display the records from Biology sorted by mean salary. The reset option restores the current set of records to be all records. You will therefore need separate variables to keep track of the "original" data set and the "current working set". Note that filtering is algorithmically very close to the searches you did in Lab 8 that returned multiple matches (though remember you can’t rely on data to be sorted here, so you can’t use binary search). A sample non-trivial workflow might be to filter by a major (e.g., "Statistics"), sort by mean salary, then filter by mean salary (e.g. 77000-100000). The current list of records after this sequence would years where Statistics graduates with a mean salary between $77000 and $100000. When searching for matching records and applying the filter, you do not know how the records are sorted (they could change after a sort operation). For this reason, you should use a linear search strategy for all your filters. When getting min and max values to filter by min and maximum salary, you just need to check if the value entered by the user is a non-negative float. Salaries of 5, or 20000000000 are fine inputs, even if there are no records in the data set that have values this extreme. |
-
When the user selects sorting by year, you should sort the current set of records by year and print the results. The records should remain sorted by year until the user requests sorting them by salary or opts to reset the records. The implementation of sort by year should be done using bubble sort.
-
When the user selects sorting by mean salary, you should sort the current set of records by mean salary and print the results. The records should remain sorted by mean salary until the user requests sorting them by year or opts to reset the records. The implementation of sort by mean salary should be done using selection sort.
-
When user selects the reset records option, the list of records should revert back to its original state before any filtering and/or sorting.
-
After filtering, sorting, or reset, your program must return to the main menu and allow the user to select another operation or quit the program.
|
For the two sorting options, you must use two different sorting algorithms (e.g., bubble sort for sorting by year and selection sort for sorting by salary). |
2.4. Tips
2.4.1. Continue to use TDD and incremental development
We strongly recommend that you create a top-down design with a fully fleshed
out main and stubs for all other functions. Likewise, you should continue to
implement and test one function at a time, making sure it works correctly
before moving on to the next one.
2.4.2. Re-use code from Lab 8
Since this lab works with the same data as we used for Lab 8 and performs a number of similar operations, you should try to re-use functions you wrote for Lab 8 when completing this lab, rather than re-implementing them from scratch. Simply copy/paste these functions into your new python file, and then modify them if necessary.
It’s fine to copy your own code; however, be sure that you’re only re-using code that you personally wrote. Using code from other students, the internet, etc. is still the same violation of academic ethics that it has been all semester.
2.4.3. Create modular, reusable functions
Avoid duplicating similar code in multiple places throughout the program. Think about how you could re-use the same function. For example, we are doing two types of filters (by major and by mean salary), but the results of those searches are displayed in exactly the same way. Consider having a function that takes in a list of results and displays them. Then you could call this function for either type of filter or sort.
Note also that you may be able to re-use functions you wrote for the previous lab; you should always re-use code that you wrote in the past to make your current life easier!
2.4.4. Copying a list
Normally, assigning two variables to refer to the same list is what you want to do, since copying a list is computationally more expensive. However, in this program you’ll need to modify a list while still maintaining a copy of the original list (so you can implement the "reset" functionality).
In Python, the easiest way to do this is to use the .copy() method of the
list class. To see how it works, try running code like the following:
a = [3, 7, 2, 4]
b = a.copy()
a[0] = 9
print(a)
print(b)
2.4.5. Adapting sort
You should use two of the quadratic sorts discussed in class. However, you will need to adapt your algorithms to sort objects instead of just a list of ints. You will likely need to have different but similar code to handle sorting by mean salary and sorting by year; the way you do the comparisons will be different, since you’ll need to access the correct field from the object.
Answer the Questionnaire
After each lab, please complete the short Google Forms questionnaire. Please select the right lab number (Lab 09) from the dropdown menu on the first question.
Once you’re done with that, you should run handin21 again.
Submitting lab assignments
Remember to run handin21 to turn in your lab files! You may run handin21
as many times as you want. Each time it will turn in any new work. We
recommend running handin21 after you complete each program or after you
complete significant work on any one program.
Logging out
When you’re done working in the lab, you should log out of the computer you’re using.
First quit any applications you are running, including your vscode editor, the browser and the
terminal. Then click on the logout icon (
) and choose "log out".
If you plan to leave the lab for just a few minutes, you do not need to log
out. It is, however, a good idea to lock your machine while you are gone. You
can lock your screen by clicking on the lock
icon.
PLEASE do not leave a session locked for a long period of time. Power may go
out, someone might reboot the machine, etc. You don’t want to lose any work!