Bowling Scorecard
MediumOnline Assessment
Score a ten-pin bowling game that may still be in progress.
A game has 10 frames, and in each frame the bowler has a rack of 10 pins. rolls lists the number of pins knocked down by each roll so far, in order.
- Frames 1-9: a frame ends when the bowler knocks down all 10 pins with the *first* roll (a strike, frame has one roll) or after two rolls. If the two rolls together knock down all 10 pins it is a spare; otherwise it is an open frame. A roll of 10 on the *second* roll of a frame (after a first roll of 0) is a spare, not a strike.
- Frame 10: the bowler gets two rolls, and a third roll if the first roll was a strike or the first two rolls made a spare. After a strike, or when a spare has just been completed, the rack is set up again with 10 pins for the next roll.
- Frame score: the pins knocked down in the frame's own rolls, plus a bonus: a strike adds the next two rolls and a spare adds the next one roll (rolls of the following frames; in frame 10 there is no bonus, its extra rolls simply count as part of the frame).
A frame is settled once its own rolls and all of its bonus rolls have happened.
Input: rolls.
Output: [totals, standing]. totals is the running total after each settled frame, in order, listing frames from frame 1 up to the last frame that is settled with every earlier frame also settled. standing is the number of pins standing for the next roll, or 0 if the game is complete.
Example 1
- Input:
- [[10,7,3,9,0,10]]
- Output:
- [[20,39,48],10]
- Explanation
- Frame 1 is a strike worth 10 + 7 + 3 = 20, frame 2 is the spare 7 + 3 followed by 9 so it is 10 + 9 = 19 (running total 39), and frame 3 is the open frame 9 + 0 = 9 (running total 48). Frame 4 is a strike whose two bonus rolls have not happened yet, so it is not reported. After a strike, 10 pins are standing again.
Example 2
- Input:
- [[3,7,0,10,4]]
- Output:
- [[10,24],6]
- Explanation
- Frame 1 is a spare (3 + 7) with bonus 0, so it scores 10. Frame 2 is the roll 0 followed by 10: a 10 on the second roll of a frame is a spare, not a strike, so its bonus is the next single roll, 4 (14, running total 24). Frame 3 has only its first roll (4), so it is not complete and is not reported; 6 pins are standing.
Example 3
- Input:
- [[10,10,10,10,10,10,10,10,10,10,10,10]]
- Output:
- [[30,60,90,120,150,180,210,240,270,300],0]
- Explanation
- Twelve strikes make a perfect game: every frame scores 30, so the running totals go 30, 60, ..., 300, and no pins are standing because the game is over.
Constraints
- 0 ≤ rolls.length ≤ 21
- 0 ≤ rolls[i] ≤ 10; the rolls form a legal game prefix: no frame knocks down more than 10 pins except as the 10th frame allows, and there are no rolls after the game is over