Statistics

Problem Statement for "LengthUnitCalculator"

Problem Statement

Your task is to write a calculator that will convert between four different length units: inches (in), feet (ft), yards (yd), and miles (mi).

The conversions between these units:
  • 1 ft = 12 in
  • 1 yd = 3 ft
  • 1 mi = 1760 yd
You are given an int amount and Strings fromUnit and toUnit. Compute and return the amount of toUnits that corresponds to amount fromUnits. (For example, if amount=41, fromUnit="mi", and toUnit="in", you are supposed to compute the number of inches in 41 miles.)
Note that the result will not necessarily be an integer.

Definition

Class:
LengthUnitCalculator
Method:
calc
Parameters:
int, String, String
Returns:
double
Method signature:
double calc(int amount, String fromUnit, String toUnit)
(be sure your method is public)

Notes

  • Pay attention to the unusual time limit.

Constraints

  • amount will be between 1 and 1,000, inclusive.
  • fromUnit will be one of {"in", "ft", "yd", "mi"}.
  • toUnit will be one of {"in", "ft", "yd", "mi"}.

Examples

  1. 1

    "mi"

    "ft"

    Returns: 5280.0

    We are asked to convert 1 mile into feet. From the information in the statement we know that 1 mi = 1760 yd = (1760 * 3) ft = 5280 ft.

  2. 1

    "ft"

    "mi"

    Returns: 1.893939393939394E-4

    Here we have 1 ft = 1/5280 mi, which is approximately 0.000189394 miles.

  3. 123

    "ft"

    "yd"

    Returns: 41.0

  4. 1000

    "mi"

    "in"

    Returns: 6.336E7

  5. 1

    "in"

    "mi"

    Returns: 1.5782828282828283E-5

  6. 47

    "mi"

    "mi"

    Returns: 47.0

  7. 997

    "in"

    "ft"

    Returns: 83.08333333333333

  8. 997

    "in"

    "yd"

    Returns: 27.694444444444443

  9. 997

    "in"

    "mi"

    Returns: 0.015735479797979797

  10. 998

    "ft"

    "in"

    Returns: 11976.0

  11. 348

    "ft"

    "yd"

    Returns: 116.0

  12. 2

    "ft"

    "mi"

    Returns: 3.787878787878788E-4

  13. 900

    "yd"

    "in"

    Returns: 32400.0

  14. 324

    "yd"

    "ft"

    Returns: 972.0

  15. 3

    "yd"

    "mi"

    Returns: 0.0017045454545454545

  16. 324

    "mi"

    "in"

    Returns: 2.052864E7

  17. 223

    "mi"

    "ft"

    Returns: 1177440.0

  18. 32

    "mi"

    "yd"

    Returns: 56320.0

  19. 5

    "ft"

    "yd"

    Returns: 1.6666666666666667

  20. 1

    "ft"

    "ft"

    Returns: 1.0

  21. 1

    "in"

    "yd"

    Returns: 0.027777777777777776

  22. 47

    "in"

    "mi"

    Returns: 7.417929292929293E-4

  23. 5

    "in"

    "ft"

    Returns: 0.4166666666666667

  24. 13

    "in"

    "ft"

    Returns: 1.0833333333333333

  25. 101

    "in"

    "mi"

    Returns: 0.0015940656565656566

  26. 17

    "yd"

    "mi"

    Returns: 0.009659090909090909

  27. 55

    "ft"

    "ft"

    Returns: 55.0

  28. 50

    "yd"

    "in"

    Returns: 1800.0

  29. 1

    "ft"

    "yd"

    Returns: 0.3333333333333333

  30. 5

    "mi"

    "mi"

    Returns: 5.0

  31. 3

    "mi"

    "yd"

    Returns: 5280.0

  32. 1000

    "in"

    "yd"

    Returns: 27.77777777777778

  33. 10

    "in"

    "in"

    Returns: 10.0

  34. 15

    "in"

    "ft"

    Returns: 1.25

  35. 100

    "in"

    "mi"

    Returns: 0.0015782828282828283

  36. 1

    "yd"

    "ft"

    Returns: 3.0

  37. 12

    "mi"

    "mi"

    Returns: 12.0

  38. 999

    "in"

    "in"

    Returns: 999.0

  39. 5

    "yd"

    "mi"

    Returns: 0.002840909090909091

  40. 10

    "in"

    "ft"

    Returns: 0.8333333333333334

  41. 10

    "in"

    "mi"

    Returns: 1.5782828282828284E-4

  42. 15

    "yd"

    "ft"

    Returns: 45.0


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: