TopcoderArchiveVisit Topcoder
SRM10 Nov 2007
SRM 375

MixtureDensity

Problem statement, definition, constraints, and public examples.

Problem Statement

A pharmacist is making a mixture of several ingredients. She needs to know the density of the resulting mixture. Density is defined as the mass of the mixture divided by its volume.

You're given a String[] ingredients, each element of which describes a single ingredient and is formatted "<Volume> ml of <Name>, weighing <Mass> g" (quotes for clarity). <Volume> and <Mass> are integers representing the volume in milliliters and mass in grams, respectively. <Name> is the name of the ingredient.

Return the density of the resulting mixture in grams per milliliters.

Definition

Class:
MixtureDensity
Method:
getDensity
Parameters:
String[]
Returns:
double
Method signature:
double getDensity(String[] ingredients)
(be sure your method is public)

Notes

  • The returned value must be accurate to within a relative or absolute value of 1E-9.

Constraints

  • ingredients will contain between 1 and 50 elements, inclusive.
  • Each element of ingredients will contain between 23 and 50 characters, inclusive.
  • Each element of ingredients will be formatted as described in the statement.
  • Each <Volume> in ingredients will be an integer between 1 and 1000, inclusive, with no leading zeroes.
  • Each <Mass> in ingredients will be an integer between 1 and 1000, inclusive, with no leading zeroes.
  • Each <Name> in ingredients will contain only lowercase letters ('a'-'z') and spaces (' ').
  • Each <Name> in ingredients will begin and end with a lowercase letter.

Examples

  1. {"200 ml of oil, weighing 180 g"}
    Returns: 0.9
    The density is mass/volume = 180g/200ml = 0.9g/ml, the answer is 0.9.
  2. {"100 ml of dichloromethane, weighing 130 g", "100 ml of sea water, weighing 103 g"}
    Returns: 1.165
    The volume of the resulting mixture is 200 ml and the mass is 233 g. Therefore, the density is 233g/200ml = 1.165g/ml.
  3. {"1000 ml of water, weighing 1000 g", "500 ml of orange juice concentrate, weighing 566 g"}
    Returns: 1.044
  4. {"1000 ml of something   l i g h t, weighing 1 g"}
    Returns: 0.0010
Back to all problems