Statistics

Problem Statement for "BearNSWE"

Problem Statement

Bear Limak is going to walk to the store. On his way to the store he likes to visit his friends and stop at various other interesting places. On his way back he is carrying heavy bags and therefore he walks straight home.


Limak's walk from his home to the store consists of M parts. In the i-th part (0-based index) Limak will walk a[i] meters in the direction indicated by the character dir[i]. That character is one of 'N', 'S', 'W', 'E', denoting North, South, West, and East, respectively.


On his way back home, Limak follows a straight line from the store to his home.


You are given the description of Limak's way to the store: the int[] a with M integers and the String dir with M characters. Compute and return the total distance (in meters) Limak will walk on his way to the store and back. Note that the correct answer may be non-integer (see the first example below).

Definition

Class:
BearNSWE
Method:
totalDistance
Parameters:
int[], String
Returns:
double
Method signature:
double totalDistance(int[] a, String dir)
(be sure your method is public)

Notes

  • Your return value must have relative error smaller than 1e-6. In other words, your returned value x will be accepted only if abs(x - ans) ≤ ans * 10-6, where ans denotes the exact correct answer.

Constraints

  • M will be between 1 and 50, inclusive.
  • a will have exactly M elements.
  • dir will have exactly M characters.
  • Each element in a will be between 1 and 50, inclusive.
  • Each character in dir will be one of 'N', 'S', 'W', 'E'.

Examples

  1. {1,3,3}

    "NES"

    Returns: 10.60555127546399

    In total, Limak's walk consists of four parts: He goes 1 meter North. 3 meters East. 3 meters South. He goes straight to his home. The distance is sqrt(2*2+3*3) = sqrt(13) = 3.6055512755. The total distance is 1 + 3 + 3 + 3.6055512755 = 10.6055512755.

  2. {10,10,10,10}

    "NWSE"

    Returns: 40.0

    In this test case the store is located at the same place as Limak's home. Thus, the length of Limak's walk from the store back home is 0 meters.

  3. {10,10,10,15,8,20,5}

    "NEWEWWE"

    Returns: 90.8062484748657

    It's possible that Limak visits some places more than once.

  4. {42}

    "E"

    Returns: 84.0

  5. {10,40,40}

    "NSE"

    Returns: 140.0

  6. {42}

    "N"

    Returns: 84.0

  7. {50}

    "S"

    Returns: 100.0

  8. {1}

    "E"

    Returns: 2.0

  9. {16}

    "W"

    Returns: 32.0

  10. {34,28,44,37,50,13,41,14,41,23}

    "WEENSEEWWN"

    Returns: 363.3275357934736

  11. {12,18,33,13,18,30,23,20,44,12,30,22,35,49,16,14,42,7,13,47,6,35,37,47,14}

    "NSWEEWWENWSESNWWNSWSSESSS"

    Returns: 736.6242942258564

  12. {25,33,15,35,44,38,27,39,4,5,33,27,40,27,45,46,35,18,48,18,3,2,37,16,45,41,32,48,32,10,18,48,37,7,20,29,33,4,21,9,41,47,19,47,22,30,15,42,44}

    "ESENWNWNEENNNWEWWSWNNNSSESSEEENSSENESENESESEENNWN"

    Returns: 1529.0976190254917

  13. {35,25,38,44,28,10,33,38,26,25,9,30,36,19,44,29,27,44,14,7,5,29,20,18,25,21,41,23,45,6,5,20,43,48,6,49,23,50,47,41,12,6,12,29,21,45,23,9,33,25}

    "NWNESNENESESSNESNEWNWNSNEEESWEWWNENENENWWSWSNWSNWS"

    Returns: 1547.6518811915344

  14. {42 }

    "E"

    Returns: 84.0

  15. {1, 3, 3 }

    "NES"

    Returns: 10.60555127546399

  16. {1, 2, 3 }

    "NSN"

    Returns: 8.0

  17. {10, 10, 10, 15, 8, 20, 5 }

    "NEWEWWE"

    Returns: 90.8062484748657

  18. {42 }

    "W"

    Returns: 84.0

  19. {1, 1 }

    "NS"

    Returns: 2.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: