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.

 

Definition

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

Constraints

-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'.
 

Examples

0)
    
{"BOB","AAAAAAA","TOM"}
Returns: { "AAAAAAA",  "BOB",  "TOM" }
BOB = 6.333, AAAAAAA = 1.0, TOM = 16
1)
    
{"ABE","ABLE","ABE"}
Returns: { "ABE",  "ABE",  "ABLE" }
2)
    
{"PANE","TOM","BONE","AAAA"}
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.
3)
    
{"BONE","TOM","PANE","AAAA"}
Returns: { "AAAA",  "BONE",  "PANE",  "TOM" }

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=3477

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=6523&pm=3477

Writer:

dgoodman

Testers:

PabloGilberto , lbackstrom , vorthys

Problem categories:

Simple Math, Sorting