TopcoderArchive
Archive
Algorithm problem

PopularitySort

Problem statement, definition, constraints, and public examples.

Problem Statement

You are given a String[], items, each element of which represents a single mail order item and is formatted as "<description> <price> <popularity>" (quotes for clarity only). <description> contains a textual description of the item and consists of only lowercase letters ('a'-'z') and spaces (but no leading or trailing spaces). <price> represents the item's price and is formatted as "$D.CC" (quotes for clarity only), where D is the number of dollars (an integer between 0 and 1000, inclusive, with no extra leading zeros) and CC represents the number of cents (an integer between 00 and 99, inclusive, with exactly 2 digits). <popularity> is an integer between 1 and 10, inclusive, representing the item's popularity (the higher the value, the more popular the item).

Sort items from most to least popular. If multiple items are equally popular, sort those items in ascending order by price. The constraints will guarantee that no two items will have the same price. The return value is a String[] whose elements are formatted exactly the same as items.

Definition

Class:
PopularitySort
Method:
sort
Parameters:
String[]
Returns:
String[]
Method signature:
String[] sort(String[] items)
(be sure your method is public)

Constraints

  • items will contain between 1 and 50 elements, inclusive.
  • Each element of items will be formatted as described in the statement.
  • Each <description> will be between 1 and 30 characters in length, inclusive.
  • No two elements of items will have the same <price>

Examples

  1. {"bicycle $200.00 8",
     "toaster oven $70.88 7",
     "baseball cards $0.99 7",
     "brownies $2.90 10",
     "oatmeal $1.00 2"}
    Returns: {"brownies $2.90 10", "bicycle $200.00 8", "baseball cards $0.99 7", "toaster oven $70.88 7", "oatmeal $1.00 2" }
    Both "toaster oven" and "baseball cards" have popularity values of 7, so the tie is broken using the price.
  2. {"free celery $0.00 1",
     "expensive broccoli $1000.99 1",
     "used eight tracks $8.88 1"}
    Returns: {"free celery $0.00 1", "used eight tracks $8.88 1", "expensive broccoli $1000.99 1" }
Back to all problems