KatInterview

Tally a Ten-Frame Pin-Knocking Game from Raw Throws

Arrays & HashingAuto-graded · 17 test cases
MediumAuto-gradedArrays & Hashingsimulationarraylook-ahead

Last 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.