Connection Status:
Competition Arena > UnitConverter
SRM 126 · 2002-12-23 · by leadhyena_inran
Class Name: UnitConverter
Return Type: String
Method Name: convertQuantity
Arg Types: (vector<string>, string, string)
Problem Statement

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.

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
0)
{"2 wipf=3 cups"}
"1 1/2 wipf"
"cups"
Returns: "2 1/4 cups"

This is the first example from the introduction.

1)
{"1 wipf=2 lwarnok","2 wipf=3 cups"}
"9 lwarnok"
"cups"
Returns: "6 3/4 cups"

Second example from the notes.

2)
{"1 wipf=2 lwarnok","2 wipf=3 cups"}
"9 lwarnok"
"cups"
Returns: "6 3/4 cups"

Second example from the notes.

3)
{"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.

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)
{"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.

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.

Submissions are judged against all 33 archived test cases, of which 12 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class UnitConverter with a public method string convertQuantity(vector<string> conversionList, string quantityToConvert, string targetMeasurement) · 33 test cases · 2 s / 256 MB per case

Submitting as anonymous