TopCoder problem "ItemizedList" used in TCHS07 Gamma 2 (Division I Level One)



Problem Statement

    

You are in charge of writing some software to generate an itemized invoice. You have a raw list of items that were purchased, but the raw list is not very user friendly. Your code should determine how many of each item were purchased, and return a list of items and their quantity. You are given a String[] items, each element representing a single item. You must return a String[] listing, in alphabetical order, each item and its quantity. Each element of the return should be formatted as "item - quantity" (quotes added for clarity), where the quantity has no leading zeroes.

 

Definition

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

Constraints

-items will contain between 1 and 50 elements, inclusive.
-Each element of items will contain between 1 and 50 lowercase letters ('a'-'z'), inclusive.
 

Examples

0)
    
{"apple", "orange", "apple", "banana", "apple", "orange"}
Returns: {"apple - 3", "banana - 1", "orange - 2" }
A grocery list of someone getting ready to make a fruit salad.
1)
    
{"apple", "apple", "apple", "apple"}
Returns: {"apple - 4" }
The itemized format is a lot more concise.
2)
    
{"apple", "orange", "banana"}
Returns: {"apple - 1", "banana - 1", "orange - 1" }
Each item only appears once.
3)
    
{"apple", "orange", "apple", "banana", "apple",
 "apple", "orange", "apple", "banana", "apple",
 "apple", "orange", "apple", "banana", "apple"}
Returns: {"apple - 9", "banana - 3", "orange - 3" }
Lots of items.
4)
    
{"apple"}
Returns: {"apple - 1" }
A very short list.

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10718&pm=7570

Writer:

timmac

Testers:

PabloGilberto , brett1479 , Olexiy

Problem categories:

Simple Search, Iteration