Statistics

Problem Statement for "UnitConverter"

Problem Statement

You are to build the unit conversion code that will be put into an electronic voice-enabled unit-conversion device. Your conversion routine will be called convertQuantity and will take a String[] representing a list of conversions (conversionList), each of which conforms to the formatting for a <conversion>, given below. It will also take a String, quantityToConvert, which is formatted as an <amount>, and a String, targetMeasurement, which is a <unit>. You are to perform the given conversion, from the <unit> in quantityToConvert to the targetMeasurement and return the proper measurement amount formatted similarly to an <amount>. The key difference between the formatting of the returned String and an <amount> is that the returned value may require numbers with more than 1 digit, and so, in the returned String, <integer> can represent any positive integer. (see examples)

 <conversion> ::= <amount>=<amount> 
 <amount> ::= <mixed number><sp><unit> 
 <mixed number> ::= <integer> | <integer><sp><integer>'/'<integer> | <integer>'/'<integer> 
 <unit> ::= A measurement unit (1 to 20 lowercase or uppercase letters, inclusive) 
 <integer> ::= '1' to '9', inclusive 
 <sp> ::= ' ' 

In addition to the above formatting rules, the mixed numbers in both the input and the returned String must be in one of 3 forms (quotes for clarity only):
  1. "a b/c" where a,b,c are integers greater than 0, and b is less than c and b/c is in reduced form (they have no common divisor greater than 1). This form represents the sum of the integer a with the fraction b/c
  2. "a" where a is an integer greater than 0. This form simply represents the integer a
  3. "b/c" where b,c are integers greater than 0 and b is less than c and b/c is in reduced form. This form simply represents the fraction b/c

For example(quotes for clarity): "2 3/4", "5", and "7/9" are valid whereas "9/7", "2 9/7", "3/6", "4 2/4", "0/8","0", and "3 0/4" are invalid.

The conversion list will follow these rules:

  • If there are two paths to a given conversion, they will both return the same quantity. In other words, the conversionList will always be consistent across all quantities.
  • The conversion will always be possible using the rules given.

Definition

Class:
UnitConverter
Method:
convertQuantity
Parameters:
String[], String, String
Returns:
String
Method signature:
String convertQuantity(String[] conversionList, String quantityToConvert, String targetMeasurement)
(be sure your method is public)

Notes

  • The 64 bit datatype for C++ is long long.
  • Conversions are identical to their reverse, so "1 gallon=4 quarts" means the same thing as "4 quarts=1 gallon".

Constraints

  • conversionList will contain between 1 and 5 elements, and will be consistent among its own conversions.
  • You will always be able to make the conversion using the list given.
  • Each element of conversionList will be formatted as a , as described above.
  • quantityToConvert will always be of the form of an as described above.
  • All mixed numbers will be in the form of a mixed number as described above.
  • conversionList will be consistent, and there will be a way to make the requested conversion.
  • targetMeasurement will be a as described above.
  • The in quantityToConvert will not be the same as targetMeasurement.

Examples

  1. {"2 wipf=3 cups"}

    "1 1/2 wipf"

    "cups"

    Returns: "2 1/4 cups"

    This is the first example from the introduction.

  2. {"1 wipf=2 lwarnok","2 wipf=3 cups"}

    "9 lwarnok"

    "cups"

    Returns: "6 3/4 cups"

    Second example from the notes.

  3. {"1 wipf=2 lwarnok","2 wipf=3 cups"}

    "9 lwarnok"

    "cups"

    Returns: "6 3/4 cups"

    Second example from the notes.

  4. {"9 4/7 a=9 8/9 b","9 4/7 b=9 8/9 c","9 4/7 c=9 8/9 d","9 4/7 d=9 8/9 e","9 4/7 e=9 8/9 f"}

    "9 3/5 f"

    "a"

    Returns: "8 72678310901944/469256435796715 a"

    Testing to see if longs have been used, and used properly.

  5. {"9 4/7 a=9 8/9 b","9 4/7 b=9 8/9 c","9 4/7 c=9 8/9 d","9 4/7 d=9 8/9 e","9 4/7 e=9 8/9 f"}

    "9 3/5 f"

    "a"

    Returns: "8 72678310901944/469256435796715 a"

    Testing to see if longs have been used, and used properly.

  6. {"9 4/7 a=9 8/9 b","9 4/7 b=9 8/9 c","9 4/7 c=9 8/9 d","9 4/7 d=9 8/9 e","9 4/7 e=9 8/9 f"}

    "9 3/5 f"

    "a"

    Returns: "8 72678310901944/469256435796715 a"

    Testing to see if longs have been used, and used properly.

  7. {"6 gill=1 1/2 cup", "2 cup=1 teacup"}

    "1 teacup"

    "gill"

    Returns: "8 gill"

    Gramatically this should be 8 gills and not 8 gill, but we are not taking plurality into account.

  8. {"6 gill=1 1/2 cup", "2 cup=1 teacup"}

    "1 teacup"

    "gill"

    Returns: "8 gill"

    Gramatically this should be 8 gills and not 8 gill, but we are not taking plurality into account.

  9. {"6 gill=1 1/2 cup", "2 cup=1 teacup"}

    "1 teacup"

    "gill"

    Returns: "8 gill"

    Gramatically this should be 8 gills and not 8 gill, but we are not taking plurality into account.

  10. {"6 gill=1 1/2 cup", "2 cup=1 teacup"}

    "1 teacup"

    "gill"

    Returns: "8 gill"

    Gramatically this should be 8 gills and not 8 gill, but we are not taking plurality into account.

  11. {"6 gill=1 1/2 cup", "2 cup=1 teacup"}

    "1 teacup"

    "gill"

    Returns: "8 gill"

    Gramatically this should be 8 gills and not 8 gill, but we are not taking plurality into account.

  12. {"6 gill=1 1/2 cup", "2 cup=1 teacup"}

    "1 teacup"

    "gill"

    Returns: "8 gill"

    Gramatically this should be 8 gills and not 8 gill, but we are not taking plurality into account.

  13. {"2 peck=1/2 bushel", "1/2 peck=4 dryquarts", "1/2 dryquarts=1 drypint", "1/2 drypint=1 UScups", "4 UScups=5 Britishcups"}

    "9 bushel"

    "Britishcups"

    Returns: "1440 Britishcups"

  14. {"2 peck=1/2 bushel", "1/2 peck=4 dryquarts", "1/2 dryquarts=1 drypint", "1/2 drypint=1 UScups", "4 UScups=5 Britishcups"}

    "9 bushel"

    "Britishcups"

    Returns: "1440 Britishcups"

  15. {"1/3 jour=8 heures","2 jour=2/7 semaine","8 2/3 semaine=1/6 ans"}

    "1 ans"

    "jour"

    Returns: "364 jour"

  16. {"1/3 jour=8 heures","2 jour=2/7 semaine","8 2/3 semaine=1/6 ans"}

    "1 ans"

    "jour"

    Returns: "364 jour"

  17. {"1/3 jour=8 heures","2 jour=2/7 semaine","8 2/3 semaine=1/6 ans"}

    "1 ans"

    "jour"

    Returns: "364 jour"

  18. {"1/7 stunden=8 4/7 minute","8 4/7 sekund=1/7 minute","1/3 tage=8 stunden","7 tage=1 woche","8 2/3 woche=1 jahre"}

    "1 sekund"

    "jahre"

    Returns: "1/5241600 jahre"

  19. {"1 a=2 b","2 2/3 b=1 1/3 A"}

    "1 a"

    "A"

    Returns: "1 A"

  20. {"1/8 a=8 aa","1/8 aa=8 aaa","1/8 aaaaa=8 aaaaaa","1/8 aaaa=8 aaaaa","1/8 aaa=8 aaaa"}

    "8 a"

    "aaaaaa"

    Returns: "8589934592 aaaaaa"

  21. {"1/9 a=9 8/9 b" ,"1/9 b=9 8/9 c" ,"1/9 c=9 8/9 d" ,"1/9 d=9 8/9 e" ,"1/9 e=9 8/9 f"}

    "9 8/9 a"

    "f"

    Returns: "55220143440 1/9 f"

  22. {"1/9 a=9 8/9 b" ,"1/9 b=9 8/9 c" ,"1/9 c=9 8/9 d" ,"1/9 d=9 8/9 e" ,"1/9 e=9 8/9 f"}

    "1/9 f"

    "a"

    Returns: "1/50256535041 a"

  23. {"1/9 a=9 8/9 b" ,"1/9 b=9 8/9 c" ,"1/9 c=9 8/9 d" ,"1/9 e=9 8/9 d" ,"1/9 f=9 8/9 e"}

    "1/9 a"

    "f"

    Returns: "9 8/9 f"

  24. { "1 a=9 8/9 b", "1 b=9 8/9 c", "1 c=9 8/9 d", "1 d=9 8/9 e", "1 e=9 8/9 f" }

    "9 a"

    "f"

    Returns: "851098 5471/6561 f"

  25. { "3 1/7 bushel=1 1/4 cup", "1 1/7 cup=3 1/3 tea", "2 1/8 cup=9 1/5 pico", "9 1/8 pico=3 1/2 coolio" }

    "2 1/7 bushel"

    "coolio"

    Returns: "1 5669/13651 coolio"

  26. { "9 a=1/9 b", "9 b=1/9 c", "9 c=1/9 d", "9 d=1/9 e", "9 e=1/9 f" }

    "1/9 a"

    "f"

    Returns: "1/31381059609 f"

  27. { "1/9 a=9 b", "1/9 b=9 c", "1/9 c=9 d", "1/9 d=9 e", "1/9 e=9 f" }

    "9 a"

    "f"

    Returns: "31381059609 f"

  28. { "9 8/9 b=1/9 a", "9 8/9 c=1/9 b", "9 8/9 d=1/9 c", "9 8/9 e=1/9 d", "9 8/9 f=1/9 e" }

    "7/9 a"

    "f"

    Returns: "4343157349 2/9 f"

  29. { "1 a=2 b", "1 b=2 c", "1 b=1 1/2 d", "1 d=3 e" }

    "1/2 a"

    "e"

    Returns: "4 1/2 e"

  30. { "1 a=1 1/4 b" }

    "1 a"

    "b"

    Returns: "1 1/4 b"

  31. { "1/9 A=1 B", "3/4 B=1 C", "2/5 C=1 D" }

    "1 D"

    "A"

    Returns: "1/30 A"

  32. { "1 a=9 1/9 b", "1 b=9 1/9 c", "1 c=9 1/9 d", "1 d=9 1/9 e", "1 e=9 1/9 f" }

    "9 1/9 a"

    "f"

    Returns: "572042 98902/531441 f"

  33. { "1/9 A=9 8/9 B", "1/9 B=9 8/9 C", "1/9 C=9 8/9 D", "1/9 D=9 8/9 E", "1/9 E=9 8/9 F" }

    "9 8/9 A"

    "F"

    Returns: "55220143440 1/9 F"


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: