Problem 174135 · hard · Phase 01 Prerequisites & Setup

The Bowling Alley Score Sheet

strings · lists · loops · conditionals

A ten-pin bowling alley prints each game as one string of symbols, one symbol per ball:

  • X is a strike (all 10 pins with the first ball of a frame),
  • / is a spare (the second ball knocks down all the pins the first ball left),
  • - is a ball that knocks down no pins,
  • a digit 1 to 9 is that many pins.

A game has 10 frames. In frames 1 to 9 a strike ends the frame; otherwise the frame has two balls. A frame's score is its pins plus a bonus: a strike adds the pins of the next two balls, a spare adds the pins of the next one ball, an open frame (fewer than 10 pins) adds nothing. The tenth frame gets its bonus balls inside itself: after a strike or spare in frame 10 the bowler throws the extra balls needed, so frame 10 has 2 or 3 balls.

The string may be a finished game or a game still in progress. Write frame_totals(sheet) that returns the list of running totals (the score after frame 1, after frame 2, ...) for every frame that can already be scored. A frame can be scored only when all its own balls and all its bonus balls appear in the string. Stop at the first frame that cannot be scored yet.

Examples

Input:  sheet = "X7/9-"
Output: [20, 39, 48]
Explanation: strike 10+7+3 = 20; spare 7+3+9 = 19 (39); open 9+0 = 9 (48).

Input:  sheet = "XX"
Output: []
Explanation: the first strike still waits for its second bonus ball.

Input:  sheet = "XXXXXXXXXXXX"
Output: [30, 60, 90, 120, 150, 180, 210, 240, 270, 300]

Constraints

  • 0 <= len(sheet) <= 21
  • sheet is always a correct beginning (or all) of a legal game: / never starts a frame, two balls of a frame never exceed 10 pins, and no ball appears after the game is over.
  • Return [] when no frame can be scored yet.

Goals

  • Translate a symbol string into a plain list of numbers first
  • Look ahead in a list without running off its end
  • Notice when a special case (the tenth frame) needs no special code
Starting Python…