Advertisement
Guest User

Bowling!

a guest
Oct 15th, 2015
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.30 KB | None | 0 0
  1. from itertools import count
  2.  
  3. values = dict(zip('-123456789X', count()))
  4.  
  5. def score(marks):
  6.     """Return the score for only the first of a list of marks"""
  7.     if marks[0] == '/':
  8.         return 10 + values[marks[1]]
  9.     elif marks[0] == 'X':
  10.         return 10 + (10 if marks[2] == '/' else values[marks[1]] + values[marks[2]])
  11.     elif marks[1] == '/':
  12.         return 0
  13.     else:
  14.         return values[marks[0]]
  15.  
  16. def game(marks):
  17.     """Return the game score for a list of marks
  18.  
  19.    >>> game("X-/X5-8/9-X811-4/X")
  20.    137
  21.    >>> game("9/8/9/8/9/8/9/8/9/81")
  22.    175
  23.    >>> game("9/8/9/8/9/8/9/8/9/8/9")
  24.    185
  25.    >>> game("XXXXXXXXXX9/")
  26.    289
  27.    >>> game("XXXXXXXXXXXX")
  28.    300
  29.    >>> game("--------------------")
  30.    0
  31.    >>> game("4/5-----------------")
  32.    20
  33.    >>> game("-------------------/1")
  34.    11
  35.    >>> game("-------------------X-1")
  36.    11
  37.    >>> game("1-1-1-1-1-1-1-1-1-1-")
  38.    10
  39.    >>> game("11111111111111111111")
  40.    20
  41.    """
  42.     marks = list(marks + '-')
  43.     tally = 0
  44.     for frame in range(10):
  45.         mark = marks.pop(0)
  46.         tally += score([mark] + marks)
  47.         if mark == 'X':
  48.             continue
  49.         tally += score([marks.pop(0)] + marks)
  50.     return tally
  51.  
  52. if __name__ == '__main__':
  53.     import doctest
  54.     doctest.testmod()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement