TopcoderArchive
Archive
Algorithm problem

PriceSearch

Problem statement, definition, constraints, and public examples.

Problem Statement

You are given a String[], items, each element of which represents a single retail item and is formatted as "<description> <price>" (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).

Return all the items whose prices are between low dollars and high dollars, inclusive, sorted 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:
PriceSearch
Method:
search
Parameters:
String[], int, int
Returns:
String[]
Method signature:
String[] search(String[] items, int low, int high)
(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>
  • low and high will each be between 0 and 1001, inclusive.
  • low will be less than or equal to high.

Examples

  1. {"toothpaste $1.74",
     "toaster oven $48.00",
     "compact disc $11.99",
     "gum $0.24",
     "giftcard $10.00",
     "giftcard $20.00"}
    10
    20
    Returns: {"giftcard $10.00", "compact disc $11.99", "giftcard $20.00" }
    The items within the specified price range are "compact disc", "giftcard", and "giftcard".
  2. {"paper clip $0.00",
     "photocopier $1000.99",
     "television $250.14",
     "television $789.36"}
    0
    1001
    Returns: {"paper clip $0.00", "television $250.14", "television $789.36", "photocopier $1000.99" }
  3. {"toaster $20.01",
     "tea kettle $19.99"}
    20
    20
    Returns: { }
Back to all problems