Statistics

Problem Statement for "ShellBallGame"

Problem Statement

A street gambler is trying to win money from people by having them guess which of his three shells he has hidden a ball under. The gambler can do only two actions: swap shells and expose shells. Follow these actions and return the number of the shell that the ball is under at the end.

Shells are numbered 1, 2, and 3. The number represents the position of the shell, not the shell itself, so if the 1 and 3 shells are swapped, the former 3 shell is now referenced at 1, and vice versa. Actions are all recorded in 2 character blocks.

A swapping action is recorded by the numbers of the shells swapped: example "13" means he swaps the first shell with the third shell.

An expose action is recorded by the number of the shell exposed, and whether the ball is present. "b" is for Ball, "n" is for No ball. example: "1b" means the gambler exposed the first shell and the ball was underneath, while "2n" means the gambler exposed the second shell and there was no ball underneath.

You are guaranteed that the gambler will expose enough times that you can identify which shell has the ball (expose a shell that has the ball, or expose both empty shells). Also, there is only one ball and the gambler doesn't do anything tricky with it (it moves with its shell during a swap).

Definition

Class:
ShellBallGame
Method:
findBall
Parameters:
String
Returns:
int
Method signature:
int findBall(String actions)
(be sure your method is public)

Constraints

  • actions is a string representing 1-25 valid actions (2-50 characters inclusive).
  • actions contains only valid 2 character actions (# in this example can be 1, 2, or 3):## - swaps the shells # and #, cannot be the same (ie 11 is not a legal action)#n - exposes shell # and ball is not found#b - exposes shell # with ball underneath

Examples

  1. "2331122n1213321232231313311n3n1313312113212n133113"

    Returns: 1

  2. "3n132n231223133231322323133131322n23232n2113311232"

    Returns: 1

  3. "1b"

    Returns: 1

  4. "1n3n"

    Returns: 2

  5. "1n2n3b1331133113311331"

    Returns: 3

  6. "1n3b13311331133113311331133113311331"

    Returns: 3

  7. "1n133n311n133n313n"

    Returns: 2

  8. "131313131313231323132313231b"

    Returns: 1

  9. "131313131n1313131313131313132323212n"

    Returns: 1

  10. "31133121122n2312121331211321123n313231211221132113"

    Returns: 3

  11. "13122132211n121232313123321331312b2323212131232n23"

    Returns: 2

  12. "12311221233223212n3n31131321131313123232133232"

    Returns: 2

  13. "32133121312b233232133223231b123213211312211232311n"

    Returns: 3

  14. "13122n1313123223122n211221311213121n12122131312132"

    Returns: 2

  15. "211212323212123b3112311321313131211212231323313121"

    Returns: 3

  16. "121332321n132n21123n3n231221322n3131123221131b213n"

    Returns: 2

  17. "3n1n"

    Returns: 2

  18. "2b"

    Returns: 2

  19. "121b"

    Returns: 1

  20. "2b31122113233221"

    Returns: 1

  21. "212112312131212b"

    Returns: 2

  22. "2b12"

    Returns: 1

  23. "2n1n"

    Returns: 3

  24. "3b31"

    Returns: 1

  25. "1b1231"

    Returns: 2

  26. "313b"

    Returns: 3

  27. "1b1332"

    Returns: 2

  28. "2b21"

    Returns: 1

  29. "122n3n"

    Returns: 1

  30. "1b12"

    Returns: 2

  31. "1b13"

    Returns: 3

  32. "3b"

    Returns: 3

  33. "1n2n"

    Returns: 3

  34. "2b311221132332"

    Returns: 2

  35. "1b21"

    Returns: 2

  36. "1b1b"

    Returns: 1

  37. "1n232323122323231n"

    Returns: 2

  38. "2n3n"

    Returns: 1

  39. "2b13"

    Returns: 2

  40. "122n321n123132"

    Returns: 2

  41. "1b"

    Returns: 1

    The ball is under the first shell and has not moved.

  42. "3n1n"

    Returns: 2

    The ball must be under the second shell since it is

  43. "1n121n31"

    Returns: 1

    The ball must have been under the third shell, and moved to the first when they swapped.

  44. "122n1n32123132"

    Returns: 2

  45. "2b311221132332"

    Returns: 2

  46. "1n12233n32211n2b232n323n"

    Returns: 2

  47. "13231223133231322323131b"

    Returns: 1


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: