Write a program or function that takes a positive integer
n and randomly generate a legal series of pitches (henceforth called a Pitch string) of length
A non-zero, positive integer
n <= 100
Return a random string, or list of characters, that represent a possible, valid pitch string of length
n. The characters used will be:
- B - Ball. If you accumulate 4 of these, the batter is walked and finished batting.
- S - Strike. If you accumulate 3 of these, the batter is out and finished batting.
- F - Foul. Will also increase the Strike count but cannot get the batter out. I.e., you cannot have a Foul be the last pitch in a valid string. Any fouls past two strikes/fouls will not increase the Strike count (the batter already has 2 strikes at that point and a 3rd would get him out).
- H - Hit. The batter has hit a ball into play and is finished batting.
(This is simplified slightly but don't you worry about that)
Valid pitch strings are those that end in a strike-out, a walk, or a hit.
I.e., an invalid pitch string has either
- additional pitches after the 4th Ball, 3rd Strike, or Hit
- terminated before generating a 4th ball, 3rd strike, or Hit.
- Your program must be able to produce all possible results for a given input.
- Your program does not have to be uniformly random but still must follow the previous rule.
- This is code-golf.
Input => Possible Outputs 1 => [H] #Can only end with a hit 2 => [S,H], [B,H], [F,H] #Can only end with a hit 3 => [S,S,S], [F,F,S], [B,B,H], ... #Can now strike-out, otherwise must end with a hit 4 => [B,B,B,B], [S,B,S,S], [B,F,S,S], [B,B,B,H], ... #Can now be walked, struck-out, or get a hit 6 => [S,B,S,B,B,H], [F,F,F,F,F,S], ... #Can now have a full-count (3 balls, 2 strikes) before finishing Input => Invalid Outputs 1 => [S], [B] #Not enough for a strike-out/walk 2 => [S,S] #Not enough for a strike-out/walk 2 => [H,H] #Batter has already scored a hit 3 => [S,S,F] #Fouls will not cause a strike-out 4 => [S,S,S,H] #Batter has already struck out 5 => [B,B,B,B,B] #Batter has already walked