TopCoder problem "Baseball" used in SRM 28 (Division I Level Three , Division II Level Three)

Problem Statement

Class name: Baseball
Method name: stats
Parameters: int, String
Returns: int[]

The game of baseball is played between two teams.
Each team has ten players.  One team plays in the field, and the other team
The teams take turns batting and playing in the field until the game is over.
Each of these turns is called

The baseball field is shaped like a diamond, with a base on each corner, and a
pitcher's mound in the center.

Baseball (field) diamond:

   / \
  /   \
 /     \
3   P   1
 \     /
  \   /
   \ /

P = Pitcher's mound.
1 = First Base.
2 = Second Base.
3 = Third Base.
H = Home Plate (Fourth Base).

A member of the batting team (the batter) stands at the bottom of the diamond
at the base called home plate.
A player from the opposing team stands on the pitcher's mound and throws a
baseball toward home plate.
The batter tries to hit the ball into the playing field with a bat (just a big

The batter then runs toward the base on the right side of the diamond (first
If the ball is caught, or a player on the opposing team tags the batter with
the baseball,
the batter is called OUT, and leaves the field.

If the batter is not called OUT, he will try to go around the diamond
counter-clockwise as far as he can
without danger of being called OUT.

He can stop at any of the corners of the diamond, which are the bases.
If he stays on a base, he is called SAFE, and he stays on the base until the
INNING is over or
until another player hits the baseball, and gives him another chance to
complete rounding the bases.

Two players CANNOT be on the same base.  If a player chooses to run to a base,
and another player is on it,
The first player must run back to the base he came from or the second player
must run forward to the next base.

For example, if a player is on first base, and the next player hits the ball,
that second player gets
to run to first base, and the first player is FORCED to run to second base.

Players CANNOT go around each other.

If a player ever reaches Home Plate (Fourth Base) he immediately leaves the
field.  Nobody can ever be ON Home Plate.
They can only reach Home Plate.

An INNING is over when a total of three players have been called OUT that
inning.  At that point,
all players on base leave the field, the teams switch roles, with the pitching
team becoming the batting team,
and vice versa.

The players on a team bat in a particular order.   That order is preserved
throughout the
entire game.  Player 1 always bats first for his team in the game, followed by
Player 2.
Player 10 is followed by Player 1.

The end of an INNING does not affect this order.  If Player 5 batted last in an
INNING, the next time
his team comes to bat, Player 6 will bat, no matter when that may happen.


Team A is batting.
Player 1 hits the ball, and makes it to first base (see diagram).  He stays
Player 2 hits the ball, and he runs to first base.  Meanwhile, Player 1 runs
all the way to third base.

Now Player 1 is on Third Base, and Player 2 is on First Base.

Player 3 gets called OUT.  Nothing else happens.

Player 4 hits the ball out of the park.  Everybody rounds the bases until they
reach Home Plate, whereupon
they leave the field.

Player 1 is the first to round the bases, followed by Player 2, and finally by
Player 4.

Player 5 hits the ball and gets to Second Base.

Et cetera, ...

A Team scores a point (called a RUN) when a player reaches Home Plate after
rounding the bases.
The player is also said to have scored a RUN.

When a player hits the ball and gets to a base, successfully, he is said to
have gotten a HIT.

When a player hits the ball, and because of that HIT, another player scores a
RUN, the player who
got the hit is said to have gotten an RBI (Run Batted In).

There is one way a player can get on base without getting a HIT.  This way is
called a WALK.
When a player gets a WALK, he goes to first base.  Everybody who is FORCED to
move because of this,
moves one base (possibly FORCING others to move).  Nobody can be called OUT
during a WALK.
If a Player scores a RUN as a result of a WALK, he gets a RUN, but nobody gets
an RBI.

How to calculate statistics: (HITS, RUNS, RBI, WALKS)

If a player is batting, and he hits the ball, getting to a base, he gets 1 HIT.
If a player rounds all the bases, crossing Home Plate, regardless of who is
batting, he gets 1 RUN.
If a player hits the ball, getting to a base, and another player scores a RUN,
he gets an RBI.
If a player gets a WALK, he gets 1 WALK.

Implement a class Baseball, which has a method stats.

Method signature: public int[] stats(int player, String whatHappened)

*player is between 1 and 10, inclusive
*whatHappened is a String between 1 and 100 characters long, inclusive

*stats returns a int[] that represents the stats of the hitter in the "player"
batting order, in the following order

{ Hits, Runs, Runs Batted In, Walks }

Hits are the number of HITS the player got in the game.
Runs are the number of times the player crossed home plate.
Runs Batted In is the number of runs scored on hits by the player.
Walks are the number of walks the player got.

The parameter whatHappened contains the results of each at-bat of the game.
The first character in the string is
the result of the first at-bat, the second character the results of the second
at-bat, etc.


W = walk
1 = base hit
2 = double
3 = triple
H = home run
O = out

All at-bat results are for the same team.

What to do:

If you have a W, move the batter to first base.  Move anybody else who is
FORCED to move.

If you have a 1, 2, 3, or H, move the batter to that base.  Anybody who is on
second or third base automatically
crosses Home Plate and scores a RUN.
The person on first merely moves to the next available base.

Be sure to clear the bases between INNINGS - 3 outs is an INNING.

There are always 10 batters.

The game can end at any time - there does not have to be a specified number of
outs or innings.


player = 2
whatHappened = "12OOW11OHOO13O"

player 1 gets a single - put him on first.
When player 2 is at bat for the first time, he hits a double - GIVE PLAYER 2 A
The runner at first moves to 3rd. 
Then two outs occur.
player 5 gets a walk, loading the bases.
player 6 hits a single, allowing player 1 to score.
player 7 hits a single, allowing player 2 to score - GIVE PLAYER 2 A RUN
player 8 is out - clear the bases.
player 9 hits a home run
player 10 is out
player 1 is out
player 2 gets a single - GIVE PLAYER 2 A HIT
player 3 gets a triple - GIVE PLAYER 2 A RUN
player 4 is out - game over

player 2 has 2 HITS and 2 RUNS.

Return { 2, 2, 0, 0 }

Example 2:

player = 5
whatHappened = "OO32HOOOOOOOOOW"

player 1 and 2 are out.
player 3 gets a triple.
player 4 hits a double, and 3 scores.
player 5 gets a home run, and 4 scores - GIVE PLAYER 5 1 HIT, 1 RUNS, and 2 RBI
players 6 - 10, 1 - 4 are out.
player 5 gets a walk - GIVE PLAYER 5 A WALK

player 5 has 1 HIT, 1 RUN, 2 RBI, and 1 WALK

Return { 1, 1, 2, 1 }


Parameters:int, String
Method signature:int[] stats(int param0, String param1)
(be sure your method is public)

Problem url:

Problem stats url:




Problem categories: