-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alejandro Gómez
committed
Jun 30, 2011
1 parent
6cec550
commit e0336d0
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#!/usr/bin/env python | ||
# encoding: utf-8 | ||
|
||
import unittest | ||
|
||
|
||
class Bowling: | ||
def __init__(self): | ||
self._score = 0 | ||
|
||
def score(self, rolls): | ||
strike = lambda x: x is 'X' | ||
spare = lambda x: x is '/' | ||
miss = lambda x: x is '-' | ||
|
||
def get_score(throw): | ||
throw_ = rolls[throw] | ||
if strike(throw_): | ||
return 10 | ||
elif spare(throw_): | ||
return 10 - get_score(throw - 1) | ||
elif miss(throw_): | ||
return 0 | ||
else: | ||
return int(throw_) | ||
|
||
self._score = 0 | ||
throws = range(len(rolls)) | ||
for throw in throws: | ||
throw_ = rolls[throw] | ||
self._score += get_score(throw) | ||
if strike(throw_): | ||
self._score += get_score(throw + 1) + get_score(throw + 2) | ||
# last throw | ||
if throw == len(rolls) - 3: | ||
break | ||
elif spare(throw_): | ||
self._score += get_score(throw + 1) | ||
# last throw | ||
if throw == len(rolls) - 2: | ||
break | ||
return self._score | ||
|
||
|
||
class BowlingTests(unittest.TestCase): | ||
_ROLLS = { | ||
"XXXXXXXXXXXX" : 300, | ||
"9-9-9-9-9-9-9-9-9-9-" : 90, | ||
"5/5/5/5/5/5/5/5/5/5/5" : 150, | ||
"X--X--X--X--XX-/" : 80, | ||
"--------------------" : 0, | ||
} | ||
|
||
def setUp(self): | ||
self.sut = Bowling() | ||
|
||
def test_score(self): | ||
for roll in self._ROLLS: | ||
score = self._ROLLS[roll] | ||
self.assertEqual(score, self.sut.score(roll)) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |