From e0336d0b67759ea3e9ffa306226b3fb941589ba5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Go=CC=81mez?= Date: Thu, 30 Jun 2011 14:06:03 +0200 Subject: [PATCH] Bowling kata terminada. --- alejandrogomez/bowling.py | 64 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 alejandrogomez/bowling.py diff --git a/alejandrogomez/bowling.py b/alejandrogomez/bowling.py new file mode 100644 index 0000000..8133532 --- /dev/null +++ b/alejandrogomez/bowling.py @@ -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()