Tally a Ten-Frame Pin-Knocking Game from Raw Throws
Arrays & HashingAuto-graded · 17 test casesLast updated
Convert a flat list of throws into a ten-frame total, applying look-ahead bonuses for strikes and spares, including fill throws in the last frame.
Ten-Frame Pin Game Total
A game of ten-pin bowling is recorded as a flat list of throws, where each value is the number of pins knocked down by one throw. Your task is to compute the final score of the game.
Game structure
A game has exactly 10 frames. Each frame starts with a fresh set of 10 pins and allows up to two throws.
How each frame is scored
- Strike: The first throw of the frame knocks down all 10 pins. The frame ends right away, using only that one throw. Its score is 10 plus the pin counts of the next two throws in the list.
- Spare: The two throws of the frame together knock down exactly 10 pins, and the first throw alone was not a strike. Its score is 10 plus the pin count of the next single throw in the list.
- Open frame: The two throws together knock down fewer than 10 pins. Its score is just their sum.
The tenth frame
If the tenth frame is a strike, the player receives two extra throws. If it is a spare, the player receives one extra throw. These extra throws only serve as the bonus for the tenth frame. They are not frames of their own and never contribute a separate frame score.
A throw of 10 counts as a strike only when it is the first throw of a frame. For example, the pair 0, 10 is a spare, not a strike.
Task
Write a function that takes the list of throws and returns the total score of the game, which is the sum of the scores of all 10 frames.
Constraints
- Every element of the list is an integer from 0 to 10.
- The list describes a fully completed game, so every throw needed for bonuses and for the tenth frame's extra throws is present.
- The frame count is always exactly 10.
Examples
Example 1
Input: score_game([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
Output: 0
Example 2
Input: score_game([5, 5, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
Output: 20
Hints
Hint 1
The input is a list of throws, but scoring is per frame. Keep a pointer to the first throw of the current frame instead of mapping frames to fixed indices.
Hint 2
A strike consumes one throw from the pointer's perspective; spares and open frames consume two. Bonus throws are only peeked at, not consumed.
Hint 3
Loop exactly 10 times rather than until the list ends. Then the tenth frame's extra throws are naturally read as bonuses and never scored as separate frames.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The input lists throws, but scores belong to frames, and a frame may use one throw (strike) or two (open or spare). So fixed index arithmetic does not work. Instead, keep a pointer pos at the first throw of the current frame and process exactly 10 frames:
- If
throws[pos] == 10, it is a strike. Add 10 plus the next two throws, and moveposforward by 1. - Otherwise, add the two throws of the frame. If they sum to 10, it is a spare, so add 10 plus the next throw. If they sum to less than 10, add just the sum. Move
posforward by 2.
Bonus throws are only read ahead, not consumed. They are then scored again as ordinary throws of later frames, which is what the rules require.
Why the tenth frame needs no special case
Because the loop runs exactly 10 times, no eleventh frame is ever started. In the tenth frame:
- A strike reads the two extra throws as its bonus.
- A spare reads the one extra throw as its bonus.
Those extra throws are never scored as frames of their own. Looping while pos < len(throws) would wrongly score them, producing totals above 300 for a perfect game.
Why it is correct
Invariant: at the start of each iteration, pos points to the first throw of the next unscored frame and total equals the sum of the scores of all earlier frames. Each branch adds exactly the score defined by the rules for that frame and advances pos by the number of throws that frame actually used (1 for a strike, 2 otherwise). After 10 iterations, total is the sum of all 10 frame scores. The guarantee that the game is complete means every index read exists.
A throw of 10 only triggers the strike branch when it is the first throw of a frame, so 0, 10 is correctly handled as a spare.
Complexity
- Time: O(1) for a fixed 10 frames (O(n) in the number of throws, with n at most 21).
- Space: O(1).
Reference solution
from typing import List
def score_game(throws: List[int]) -> int:
total = 0
pos = 0 # index of the first throw of the current frame
for _ in range(10):
if throws[pos] == 10:
# Strike: 10 plus the next two throws.
total += 10 + throws[pos + 1] + throws[pos + 2]
pos += 1
else:
frame_sum = throws[pos] + throws[pos + 1]
if frame_sum == 10:
# Spare: 10 plus the next single throw.
total += 10 + throws[pos + 2]
else:
total += frame_sum
pos += 2
return total