Skip to content

Instantly share code, notes, and snippets.

@garybernhardt
Created March 26, 2009 14:30
Show Gist options
  • Save garybernhardt/86132 to your computer and use it in GitHub Desktop.
Save garybernhardt/86132 to your computer and use it in GitHub Desktop.
class BowlingScorer:
def __init__(self, rolls):
rolls = rolls[:]
self.score = sum(self.score_frame(rolls) for _ in range(10))
def score_frame(self, rolls_left):
roll1 = rolls_left.pop(0)
if roll1 == 10:
return 10 + rolls_left[0] + rolls_left[1]
roll2 = rolls_left.pop(0)
if roll1 + roll2 == 10:
return 10 + rolls_left[0]
return roll1 + roll2
def describe_bowling_score():
def should_be_0_for_all_gutter_balls():
assert BowlingScorer([0] * 20).score == 0
def should_be_150_for_all_spares():
assert BowlingScorer([5] * 21).score == 150
def should_be_300_for_perfect_game():
assert BowlingScorer([10] * 12).score == 300
def should_be_90_for_9_pins_per_frame():
assert BowlingScorer([4, 5] * 10).score == 90
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment