Skip to content

Instantly share code, notes, and snippets.

@hjwp
Last active November 23, 2019 12:23
Show Gist options
  • Save hjwp/c2911e48b2dcecc3ae14169e879eaab9 to your computer and use it in GitHub Desktop.
Save hjwp/c2911e48b2dcecc3ae14169e879eaab9 to your computer and use it in GitHub Desktop.
Bowling TDD Kata
STRIKE = 'X-'
def score_frame(frame, next_frame):
if frame[:2] == STRIKE:
if next_frame == STRIKE:
return 20
if next_frame is LAST_FRAME:
first_ball_next_frame = int(frame[2])
second_ball_next_frame = int(frame[3])
else:
first_ball_next_frame = int(next_frame[0])
second_ball_next_frame = int(next_frame[1])
return 10 + first_ball_next_frame + second_ball_next_frame
first_ball = int(frame[0])
second_ball = int(frame[1])
if first_ball + second_ball == 10:
if next_frame is LAST_FRAME:
third_ball = int(frame[2])
return 10 + third_ball
return 10 + int(next_frame[0])
return first_ball + second_ball
LAST_FRAME = object()
def score(game):
frames = game.split()
next_frames = frames[1:] + [LAST_FRAME]
result = 0
for frame, next_frame in zip(frames, next_frames):
result += score_frame(frame, next_frame)
return result
from bowling import score
def test_all_zeroes():
assert score('00 00 00 00 00 00 00 00 00 00') == 0
def test_one_point():
assert score('01 00 00 00 00 00 00 00 00 00') == 1
assert score('00 00 00 00 00 00 00 00 00 01') == 1
def test_more_than_one_point():
assert score('04 00 00 00 00 00 00 00 00 00') == 4
assert score('40 00 00 00 00 00 00 00 00 00') == 4
def test_scoring_in_muliple_frames():
assert score('04 06 07 00 00 00 00 00 00 00') == 17
def test_scoring_multiple_in_one_frame():
assert score('26 00 00 00 00 00 00 00 00 00') == 8
def test_spare_followed_by_zero():
assert score('28 00 00 00 00 00 00 00 00 00') == 10
def test_spare_followed_by_nonzero():
# the score for a spare = 10 plus your first ball in the next frame
# and the next frame is scored separately
assert score('28 30 00 00 00 00 00 00 00 00') == 10 + 3 + 3
def test_two_spares():
assert score('28 37 40 00 00 00 00 00 00 00') == (10 + 3) + (10 +4) + 4
def test_strike_followed_by_zero():
assert score('X- 00 00 00 00 00 00 00 00 00') == 10
def test_strike_followed_by_nonzero():
assert score('X- 40 00 00 00 00 00 00 00 00') == (10 + 4) + (4 + 0)
def test_strike_followed_by_two_nonzero_balls():
assert score('X- 43 00 00 00 00 00 00 00 00') == (10 + 4 + 3) + (4 + 3)
def test_two_strikes_in_a_row():
assert score('X- X- 30 00 00 00 00 00 00 00') == (10 + 10) + (10 + 3) + 3
def test_three_strikes_in_a_row():
assert score('X- X- X- 60 00 00 00 00 00 00') == 20 + 20 + (10 + 6) + 6
def test_last_frame_is_spare():
assert score('00 00 00 00 00 00 00 00 00 376') == (10 + 6)
def test_last_frame_is_strike_with_zero():
assert score('00 00 00 00 00 00 00 00 00 X-00') == 10 + 0 + 0
def test_last_frame_is_strike_with_one_nonzero():
assert score('00 00 00 00 00 00 00 00 00 X-10') == 10 + 1 + 0
def test_last_frame_is_strike_with_two_nonzero():
assert score('00 00 00 00 00 00 00 00 00 X-18') == 10 + 1 + 8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment