# Assignment 9 – Changing Sort Keys Solved

25.00 \$

Category:
Click Category Button to View Your Next Assignment | Homework

You'll get a download link with a: . ` zip` solution files instantly, after Payment

## Description

InÂ week 8, we saw an example of aÂ StudentÂ class that provided a staticcompare_strings_ignore_case()method. We needed to define such a method because ourÂ sort algorithm(which was in theÂ StudentArrayUtilitiesÂ (SAU) class) had to have a basis for comparing twoStudentobjects, the foundation ofÂ SAUs sorting algorithm. Since aÂ StudentÂ is a compound data type, not afloatÂ or aÂ string, there was no pre-defined less-than,Â <,Â operation available, so we created our owncompare_strings_ignore_case(), which was based on the spelling of theÂ last name. You can review those notes to see its definition.

We want to make the sort more flexible in this assignment. Sometimes we want to sort onÂ last name, as we did in the lectures, but other times we may choose to sort on theÂ first nameÂ (useful for informal communication) orÂ total pointsÂ (useful for analyzing statistics). We could go into thecompare_strings_ignore_case()Â method and change its definition to work onÂ first name.Â Or, we could rename it toÂ compare_two_numbers()Â and base it onÂ total pointsÂ (or for that possibility just changeSAU‘sfloat_largest_to_top()Â so that compare the points itself < or <=, no method call needed. But, such “solutions” only replace the inflexibility ofÂ last nameÂ with the inflexibility of some other criteria.

So here’s the plan: we will provideÂ classÂ StudentÂ with a newÂ class methodÂ set_sort_key()Â which will establish a new sort criteria. A client will invoke it whenever it wants to switch to a new basis for comparison. The client will pass an argument toÂ set_sort_key()Â telling it which one of the threeStudentattributes it wants future sort algorithms to use. TheÂ StudentÂ class will keep itscompare_strings_ignore_case()Â static method, but add a newÂ class methodcalledcompare_two_students()(which can use the older method as a helper). This name doesn’t commit tostringÂ vs.Â number, plus it gives theÂ SAUÂ class a unified method to invoke,Student.compare_two_students(),Â inside itsfloat_largest_to_top(). The currentÂ sort keyÂ will informcompare_two_students()Â how to make the comparison.

No Output (SAU class)

Another change we’ll make affects the output modality. Rather than displaying the array directly in the SAU class, we want to make the class “U.I. neutral.” So we will replaceÂ SAU‘sÂ print_array()Â method with ato_string()Â method and let the client choose how to use that. In our exampleÂ main programÂ we will be sending the string array to theÂ console.

Median (SAU class)

We’ll add one moreÂ class methodÂ toÂ SAU:Â double get_median_destructive( cls, array, array_size). This method will return theÂ medianÂ of theÂ total_tointsÂ values in an array. Look upÂ median. It is defined as the “middle-value” and is easily computed, but you first have to sort the array in order to find it. Fortunately, you already have the sort method.

Client

Our client will declareÂ threeÂ StudentÂ arraysÂ to make sure our median works: one array that has an odd number of students (15), one that has an even number of students (16) and one that has aÂ single student.

We’ll test theÂ sort_keyÂ and sort algorithm only on the even numbered array, and then we will test our median computation on all three arrays.

The Program Spec

These changes should not be complicated as long as you read carefully and follow directions. New and modified members/methods are all very short, so stay focused and apply what you learned back inÂ week 8.

We will add the following static members to the class in the modules.

static int consts:

SORT_BY_FIRSTÂ = 88

SORT_BY_LASTÂ = 98

SORT_BY_POINTSÂ = 108

These are the threeÂ sort keysÂ that will be used by the client and the class, alike, to keep track of, or set, theÂ sort key.Â Â  If the client wants to establish a newÂ sort key, it will pass one of these tokens (sayStudent.SORT_BY_FIRST) to the setter described next.Â Â Â  I have intentionally made the literal values non-contiguous so you would not rely on their particular values in your logic.Â Â  You should be able to change the values without breaking your program (but you don’t have to change them; use the three values above).

static int:

sort_keyÂ – this will always have one of the three constants above as its value. Make sure it initially hasSORT_BY_LASTÂ in it, but after the client changes it, it could be any of the above constants.

You should supply the following simpleÂ class methods:

def set_sort_key(cls, key): — aÂ mutatorÂ for the memberÂ sortKey.

def get_sort_key(cls):Â — anÂ accessorÂ forÂ sortKey.

A new class comparison method:

def compare_two_students(cls, first_stud, second_stud):– This method has to look at theÂ sort_keyÂ and compare the twoÂ StudentsÂ based on the currently activesort_key. It will return a number as follows:

IfÂ sort_keyÂ isÂ SORT_BY_FIRSTÂ it will use the two students’Â firstÂ members to make the comparison. It will do this by passing those two values toÂ compare_strings_ignore_case(), and “passing up” the returned value directly to the client.

IfÂ sort_keyÂ isÂ SORT_BY_LASTÂ it will use the two students’Â lastÂ members to make the comparison. It will do this by passing those two values toÂ compare_strings_ignore_case(), and “passing up” the returned value directly to the client.

IfÂ sort_keyÂ isÂ SORT_BY_POINTSÂ it will use the two students’Â total_pointsÂ members to make the comparison. It will do this by subtracting the second student’s points from the first student’s points and returning that number.

Notice that whatever value the currentÂ Student.sort_keyÂ has,Â Student.compare_two_students()Â returns a value value that’s negative ifÂ first_studÂ “comesÂ before”Â second_stud,Â positive ifÂ first_studÂ “comesaftersecond_studÂ and 0 if they are the sameÂ – interpreted through the current value of sort_key. (It doesn’t matter what the the exact value returned besides its being +, – or 0.)

ChangeÂ to the StudentArrayUtilities Class

ReplaceÂ print_array() with to_string().Â Generate the same kind ofÂ string, but instead of sending it to the screen, return it to the client.

AdjustÂ float_largest_to_top()Â to callÂ compare_two_students()Â rather thancompare_strings_ignore_case(). Make adjustments required by this change.

Add aÂ class methodÂ get_median_destructive(cls, array, array_size):Â – This computes and returns theÂ medianÂ of the total scores of all the students in the array The details are simple, but you have to take them each carefully:

Dispose of the case of a one-element array. A one-element array returns its one and onlyStudent‘stotal_points. (This case can actually be skipped if you handle the next cases correctly, but it doesn’t hurt to do it separately, here.)

Even-numbered arrays >= 2Â elements: find the two middle elements and return their average of their total points.

Odd-numbered arrays >= 3Â elements: return the total points of the exact middle element.

Special Note: This method has to do the following. It mustÂ sort the arrayÂ according toÂ total_pointsin order to get the medians, and that’s easy since we already have the sort method. Then it has to find the middle-student’s score (e.g., if the array is sizeÂ 21, the middle element is the score inarray[10],Â after the sort). But, before doing the sort, it also has to change theÂ sort_keyÂ of theÂ Studentclass toÂ SORT_BY_POINTS. One detail, that you may not have thought of, is that, at the very start of the method, it needs to save theÂ client’s sort key. Then, before returning, restore the client’s sort key. This method doesn’t know what that sort key might be, but there is an accessorÂ get_sort_key()that will answer that question.

This method has the word “Destructive” in its name to remind the client that it may (and usually will) modify the order of the array, since it is going to sort the array byÂ total pointsÂ in the process of computing theÂ median. However, it will not destroy or modify the client’sÂ sort_keyÂ when the method returns to client (see previous bullet).

If the user passes in a badÂ arraySizeÂ (< 1) return a 0.

The Main Program

Our client will declareÂ threeÂ StudentÂ arrays: using direct initialization, as in the modules: no user input. The array sizes should be 15, 16 and 1. The second array can be the same as the first with one extraStudenttagged onto the end. Each array should be initialized in no particular order: unsorted in all fields.

Using the largest, even numbered, array:

display the array immediately before calling a sort method,

sort the array using the default (initial)Â sort keyÂ and display,

change theÂ sort keyÂ toÂ first name, sort and display,

change theÂ sort keyÂ toÂ total score, sort and display,

set_sort_key()Â toÂ first name, call theÂ get_median_destructive()Â method and display theÂ median score. and finally

callÂ get_sort_key()Â to make sure that theÂ get_median_destructive()Â method preserved the client’sÂ sort_keyÂ value ofÂ first nameÂ that was just set prior to theÂ get_median_destructive()Â call.

Using each of the two other arrays:

get the median of each array and display. No other testing needed in this part.

Here’s a sample output, but you must not use my arrays. Make your own as per the spec above.

```Before default sort (even):
----------
name: smith, fred
total points: 95.
----------
name: bauer, jack
total points: 123.
----------
name: jacobs, carrie
total points: 195.
----------
... etc

After default sort (even):
----------
name: bauer, jack
total points: 123.
----------
name: cassar, john
total points: 321.
----------
name: charters, rodney
total points: 295.
----------
name: jacobs, carrie
total points: 195.
... etc

After sort BY FIRST:
----------
name: renquist, abe
total points: 148.
----------
name: jacobs, carrie
total points: 195.
----------
name: loceff, fred
total points: 44.
----------
name: perry, fred
total points: 225.
... etc

After sort BY POINTS:
----------
name: loceff, fred
total points: 44.
----------
name: smith, fred
total points: 95.
----------
name: zz-error, trevor
total points: 108.
----------
name: bauer, jack
total points: 123.
----------
... etc

Median of even class =   ...
Successfully preserved sort key.
Median of odd class = ...
Median of small class = ...```
• Assignment_9.zip