Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from itertools import count
- values = dict(zip('-123456789X', count()))
- def score(marks):
- """Return the score for only the first of a list of marks"""
- if marks[0] == '/':
- return 10 + values[marks[1]]
- elif marks[0] == 'X':
- return 10 + (10 if marks[2] == '/' else values[marks[1]] + values[marks[2]])
- elif marks[1] == '/':
- return 0
- else:
- return values[marks[0]]
- def game(marks):
- """Return the game score for a list of marks
- >>> game("X-/X5-8/9-X811-4/X")
- 137
- >>> game("9/8/9/8/9/8/9/8/9/81")
- 175
- >>> game("9/8/9/8/9/8/9/8/9/8/9")
- 185
- >>> game("XXXXXXXXXX9/")
- 289
- >>> game("XXXXXXXXXXXX")
- 300
- >>> game("--------------------")
- 0
- >>> game("4/5-----------------")
- 20
- >>> game("-------------------/1")
- 11
- >>> game("-------------------X-1")
- 11
- >>> game("1-1-1-1-1-1-1-1-1-1-")
- 10
- >>> game("11111111111111111111")
- 20
- """
- marks = list(marks + '-')
- tally = 0
- for frame in range(10):
- mark = marks.pop(0)
- tally += score([mark] + marks)
- if mark == 'X':
- continue
- tally += score([marks.pop(0)] + marks)
- return tally
- if __name__ == '__main__':
- import doctest
- doctest.testmod()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement