Statistics

Problem Statement for "TurtleGraphics"

Problem Statement

Your new graphical programming language Honu contains the primitives 'L' (turn left), 'R' (turn right), 'U' (turn up), 'D' (turn down), and 'F' (move forward), to describe the actions of a trail-leaving turtle in the three-dimensional sea. Only the 'F' command actually moves the turtle, and it always moves it one unit forward. All other commands simply change the orientation of the turtle. The turn commands all are with respect to the turtle's current orientation. That is, 'R' means the turtle turns towards its current right side; 'U' means the turtle turns towards where its shell is facing.

In addition, commands can be repeated if they are immediately followed by a single digit from 1 to 9. So "F3" moves forward 3 units. There will not be adjacent digits in the command string. Digits may only follow the characters 'U', 'D', 'R', 'L', 'F', or ')'. The repeat count only applies to the immediately previous command, so "FF3" only advances four units.

Commands can also be grouped by parentheses. So "(FRFL)5" traces a zig-zag line, repeating "FRFL" five times.

Parentheses can be nested, so "((F2)2)2" moves forward 8 units.

Given a sequence of these commands, your method must return the Euclidean distance from where the turtle began to where it ended up.

Definition

Class:
TurtleGraphics
Method:
distance
Parameters:
String
Returns:
double
Method signature:
double distance(String command)
(be sure your method is public)

Constraints

  • command will contain between 0 and 50 characters, inclusive.
  • command will consist only of the characters 'R', 'L', 'U', 'D', and 'F', '(', ')', and the digits '1'-'9'.
  • command will be properly formatted according to the description above.

Examples

  1. "FRF"

    Returns: 1.4142135623730951

    Going forward, turning right, and then going forward again leaves the turtle at a distance of the square root of two.

  2. "FUFUFUF"

    Returns: 0.0

    The turtle traces a vertical square and ends up where he started.

  3. "((((((((((((((((F9)9)9)9)9)9)9)9)9)9)9)9)9)9)9)9)9"

    Returns: 1.6677181699666568E16

    the farthest distance

  4. "UFFRFFLFFDFFRFFLFDFFUFFLFFUFFLFFRFFUFFDFFDFFRFFLFF"

    Returns: 9.433981132056603

  5. "FRFUF"

    Returns: 1.7320508075688772

  6. "(((((((F9)9)9)9)9)9)9)9"

    Returns: 4.3046721E7

  7. "(((((((F9)9)9)9)9)9)9)9RFR(((((((F9)9)9)9)9)9)9)9"

    Returns: 1.0

  8. "((L5))4"

    Returns: 0.0

  9. "(RU(((U(R)2)))((FF)6(L((LRD)))7))"

    Returns: 12.0

  10. "(((((LLDUL((U)(L)6)1)8))5))"

    Returns: 0.0

  11. "(D(DDU))"

    Returns: 0.0

  12. "UD"

    Returns: 0.0

  13. "D"

    Returns: 0.0

  14. "((((U))(DRDD)1))1((((F)8)6))(DFL)5"

    Returns: 46.05431575867782

  15. "(((D4R)2UU)RR)"

    Returns: 0.0

  16. "((((((R(U))))FFU((((L)))5)9(LLU(D))7)9))"

    Returns: 2.0

  17. "(((((R)2LRL7)5)(FLD)3))"

    Returns: 1.7320508075688772

  18. "(((((DF)3)6)7))9(((((((((((DF))))D)8)))))4)2"

    Returns: 1.4142135623730951

  19. "(((((R)5)7)9))3"

    Returns: 0.0

  20. "((L7F)1)5"

    Returns: 1.0

  21. "(((((UU6)1(((((F(DRF((F1)))6))))2))4)8))3"

    Returns: 768.0

  22. "((((((D)))4)3)8((UU))2)7"

    Returns: 0.0

  23. "LDL"

    Returns: 0.0

  24. "((((((LDU((L))7)9)1)))5)1"

    Returns: 0.0

  25. "(LU1((LRD))(((((((R)UF)))5DRD7R)9))7L7(D6UFR)8)"

    Returns: 189.72348299564817

  26. "(RFF)8(L((UD8)(L))8)"

    Returns: 0.0

  27. "((((((((RD)9))8)7))(U)1))"

    Returns: 0.0

  28. "((((UDLUL(D)9(((((F)6))2)7)5)5)))"

    Returns: 1260.0

  29. "(((((((R5D)F)))(UF7(D5))3)6)(((R(RL)6D)6U))3)5"

    Returns: 207.84609690826528

  30. "(((((LF((((D(((DF)))9)))4)3(((D6)))(((DF)))F)))))"

    Returns: 2.23606797749979

  31. "F(((D)4)4)9F"

    Returns: 2.0

  32. "(L7FL7DR((R)F2ULR))((((D))6)1)8RFUR"

    Returns: 2.8284271247461903

  33. "((((((((((((((((((((((((F))))))))))))))))))))))))7"

    Returns: 7.0

  34. "FFFRFFFFUFFFFF"

    Returns: 7.0710678118654755

  35. "FUFUFLFLFUFUFLF"

    Returns: 0.0

  36. "((FRFL)9)9"

    Returns: 114.5512985522207

  37. "(FUFDLFR)4"

    Returns: 6.928203230275509

  38. "(FUFDLFRUURR)4"

    Returns: 4.0

  39. "(FL)2(F2L)2(F3L)2(F4L)2(F5L)2(F6L)2(F7L)2(F8L)2"

    Returns: 5.656854249492381

    A big flat spiral

  40. "((F2)2)2"

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