TopCoder problem "Fairness" used in TCCC05 Qual 1 (Division I Level One)

Problem Statement

    Why should the alphabetic ordering of names put so much emphasis on the first letter in each name? In the spirit of fairness to all the letters in a name, let's compute the average alphabetical position of the letters in each name, and order the names accordingly.

We can define the "value" of a name to be the average position of the letters in that name, counting an 'A' as 1, 'B' as 2, and so on up to 'Z' as 26. So, for example, the value of "BOB" would be (2+15+2)/3 = 6.3333. When given a list of names, we will order them by putting them in order of increasing value. When 2 or more names have exactly the same value, we will break the tie by placing the names that were earlier on the original list earlier on the ordered list.

Create a class Fairness that contains a method fairSort that is given a String[] names and that returns a String[] containing the same names sorted according to the above method. The elements in the return should, of course, not contain any leading or trailing spaces.



Method signature:String[] fairSort(String[] names)
(be sure your method is public)


-names will contain between 1 and 50 elements inclusive.
-Each element of names will contain between 1 and 20 characters inclusive.
-Each character in each element of names will be an uppercase letter, 'A'-'Z'.


Returns: { "AAAAAAA",  "BOB",  "TOM" }
BOB = 6.333, AAAAAAA = 1.0, TOM = 16
Returns: { "ABE",  "ABE",  "ABLE" }
Returns: { "AAAA",  "PANE",  "BONE",  "TOM" }
PANE and BONE are tied, and since PANE comes before BONE in the given list, it must come before BONE in the sorted list.
Returns: { "AAAA",  "BONE",  "PANE",  "TOM" }

Problem url:

Problem stats url:




PabloGilberto , lbackstrom , vorthys

Problem categories:

Simple Math, Sorting