TopcoderArchive
Archive
Algorithm problem

SalesFigures

Problem statement, definition, constraints, and public examples.

Problem Statement

A company stores sales data in a single, cumbersome file. Each line of the file represents one sale and is formatted as:
    CLIENT_ID CNT_1 PRODUCT_1 CNT_2 PRODUCT_2 ...
This indicates that CNT_1 units of PRODUCT_1 were sold to the client whose id is CLIENT_ID, and CNT_2 units of PRODUCT_2 and so on. You have been tasked with figuring out how many units of a particular product have been sold over time to a particular client. You will be given a String[] sales, a String, client, and a String, product. You are to return an int representing how many units of product were sold to client in sales.

Definition

Class:
SalesFigures
Method:
getCount
Parameters:
String[], String, String
Returns:
int
Method signature:
int getCount(String[] sales, String client, String product)
(be sure your method is public)

Constraints

  • sales will contain between 1 and 50 elements, inclusive.
  • Each element of sales will be formatted as described in the problem statement, with at least 1 product and at most 50 characters.
  • CLIENT_ID and each PRODUCT_* will contain between 1 and 10 uppercase letters ('A'-'Z'), inclusive.
  • Each CNT_* will represent an integer between 1 and 999, inclusive, with no leading zeros.
  • client will contain between 1 and 10 uppercase letters ('A'-'Z'), inclusive.
  • product will contain between 1 and 10 uppercase letters ('A'-'Z'), inclusive.

Examples

  1. {"BOB 1 SHOE",
     "JOHN 2 SHOE",
     "BOB 3 SHOE 1 HORSE"}
    "BOB"
    "SHOE"
    Returns: 4
  2. {"MEG 1 TV 1 VCR 10 DVD 3 DVD",
     "HARRY 2 TV 6 DVD",
     "MEG 11 DVD",
     "MEG 2 TV",
     "HARRY 101 DVD"}
    "MEG"
    "DVD"
    Returns: 24
    Note that "DVD" occurs twice in the first element.
  3. {"GEORGE 999 PETS"}
    "BOB"
    "SHOE"
    Returns: 0
Back to all problems