TopcoderArchive
Archive
Algorithm problem

RecipeFraction

Problem statement, definition, constraints, and public examples.

Problem Statement

Each element of recipe has the form (quotes for clarity) "Amount Ingredient" where Amount is a positive integer. For example, "4 SUGAR" means the recipe calls for 4 units of SUGAR. Return what fraction of the total recipe is accounted for by the elements of ingredients (see examples for further clarifications).

Definition

Class:
RecipeFraction
Method:
getFraction
Parameters:
String[], String[]
Returns:
double
Method signature:
double getFraction(String[] recipe, String[] ingredients)
(be sure your method is public)

Notes

  • The return value must be within 1e-9 absolute or relative error of the actual result.

Constraints

  • ingredients will contain between 1 and 50 elements inclusive.
  • Each element of ingredients will contain between 1 and 50 characters inclusive.
  • Each element of ingredients will contain only uppercase letters ('A'-'Z').
  • Each element of ingredients will be distinct.
  • recipe will contain between 1 and 50 elements inclusive.
  • Each element of recipe will contain between 3 and 50 characters inclusive.
  • Each element of recipe will have the format (quotes for clarity) "Amount Ingredient" where Amount is an integer with no leading zeros between 1 and 10 inclusive, and Ingredient is a positive length string of uppercase letters.
  • Each Ingredient in recipe will be distinct.

Examples

  1. {"2 GRAPES",
     "1 APPLES",
     "3 STRAWBERRIES"}
    {"APPLES"}
    Returns: 0.16666666666666666
    The recipe requires 2+1+3 = 6 total units. APPLES account for 1/6 of the total.
  2. {"2 GRAPES",
     "1 APPLES",
     "3 STRAWBERRIES"}
    {"GRAPES"}
    Returns: 0.3333333333333333
    GRAPES account for 2/6 of the total recipe.
  3. {"2 GRAPES",
     "1 APPLES",
     "3 STRAWBERRIES"}
    {"FROGS"}
    Returns: 0.0
    There are no FROGS in our recipe.
  4. {"1 A","1 B","1 C","5 D","4 E"}
    {"A","E"}
    Returns: 0.4166666666666667
    The recipe requires 1+1+1+5+4=12 total units. A and E account for 1+4=5 of these 12.
  5. {"9 A","1 B","10 C","5 D","4 E"}
    {"A","B","F"}
    Returns: 0.3448275862068966
Back to all problems