Connection Status:
Competition Arena > NameSort
TCCC '03 NE/SE Regional · 2003-02-18 · by dgoodman · Sorting, String Parsing
Class Name: NameSort
Return Type: String[]
Method Name: newList
Arg Types: (vector<string>)
Problem Statement

Problem Statement

A certain business maintains a list of all its customers' names. The list is arranged in order of importance, with the last customer in the list being the most important. We want to create a new list sorted alphabetically according to customers' last names, but among customers with the same last name we want the more important ones to appear earlier in the new list. Alphabetical order (and equality of last names) should not be case sensitive.

Create a class NameSort that contains a method newList that takes a String[] list of names as input and returns the new sorted list as a String[]. The last name of a customer is defined to be the part of the name following the last space character, or the whole name if it has no space characters. The last name of "Bob E Jones" is "Jones". The last name of "Madona" is "Madona".

The names in the new sorted list should retain the same capitalization as they had in the original list.

Constraints

  • list will contain between 1 and 50 elements, inclusive
  • each element of list will contain between 1 and 20 characters, inclusive
  • each character in each element of list will be a space, ' ', or a letter ('A'-'Z' or 'a'-'z')
  • no element of list will contain leading or trailing spaces
  • no element of list will contain two or more adjacent spaces
Examples
0)
{"Tom Jones","ADAMS","BOB ADAMS",
"Tom Jones","STEVE jONeS"}
Returns: { "BOB ADAMS",  "ADAMS",  "STEVE jONeS",  "Tom Jones",  "Tom Jones" }

ADAMS comes before JONES. The ADAMS names are listed in reverse order as compared to the original list. Likewise for the JONES names.

1)
{"C A R Hoare","Kenny G",
"A DeForest Hoar","Kenny Gee"}
Returns: { "Kenny G",  "Kenny Gee",  "A DeForest Hoar",  "C A R Hoare" }

No two of these names have the same last name. So the final list is the case-insensitive alphabetically ordered by last name version of the original.

2)
{"Trudy","Trudy","TRUDY"}
Returns: { "TRUDY",  "Trudy",  "Trudy" }

All three have the same last name. So they are sorted by importance, which corresponds to the reverse order as compared with the original list.

3)
{"a b c d","A","Thomas a","d d",
"ed edgars","al Adams"}
Returns: { "Thomas a",  "A",  "al Adams",  "d d",  "a b c d",  "ed edgars" }
4)
{"TurnipHeadJohnson"}
Returns: { "TurnipHeadJohnson" }

Submissions are judged against all 29 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class NameSort with a public method vector<string> newList(vector<string> list) · 29 test cases · 2 s / 256 MB per case

Submitting as anonymous