-
Notifications
You must be signed in to change notification settings - Fork 0
/
exam1-1.py
71 lines (64 loc) · 2.88 KB
/
exam1-1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
UNIT 1: Bowling:
You will write the function bowling(balls), which returns an integer indicating
the score of a ten-pin bowling game. balls is a list of integers indicating
how many pins are knocked down with each ball. For example, a perfect game of
bowling would be described with:
>>> bowling([10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10])
300
The rules of bowling are as follows:
(1) A game consists of 10 frames. In each frame you roll one or two balls,
except for the tenth frame, where you roll one, two, or three. Your total
score is the sum of your scores for the ten frames.
(2) If you knock down fewer than ten pins with your two balls in the frame,
you score the total knocked down. For example, bowling([8, 1, 7, ...]) means
that you knocked down a total of 9 pins in the first frame. You score 9 point
for the frame, and you used up two balls in the frame. The second frame will
start with the 7.
(3) If you knock down all ten pins on your second ball it is called a 'spare'
and you score 10 points plus a bonus: whatever you roll with your next ball.
The next ball will also count in the next frame, so the next ball counts twice
(except in the tenth frame, in which case the bonus ball counts only once).
For example, bowling([8, 2, 7, ...]) means you get a spare in the first frame.
You score 10 + 7 for the frame; the second frame starts with the 7.
(4) If you knock down all ten pins on your first ball it is called a 'strike'
and you score 10 points plus a bonus of your score on the next two balls.
(The next two balls also count in the next frame, except in the tenth frame.)
For example, bowling([10, 7, 3, ...]) means that you get a strike, you score
10 + 7 + 3 = 20 in the first frame; the second frame starts with the 7.
"""
from copy import deepcopy
def bowling(balls):
"Compute the total score for a player's game of bowling."
## bowling([int, ...]) -> int
## your code here
score = 0
for frame in xrange(10):
frame_score = 0
i = 1
while i < 3:
ball = balls.pop(0)
frame_score += ball
if i == 1 and ball == 10:
strike_balls = deepcopy(balls)
frame_score += strike_balls.pop(0)
frame_score += strike_balls.pop(0)
break
if i == 2 and frame_score == 10:
strike_balls = deepcopy(balls)
frame_score += strike_balls.pop(0)
break
i += 1
score += frame_score
return score
def test_bowling():
assert 0 == bowling([0] * 20)
assert 20 == bowling([1] * 20)
assert 80 == bowling([4] * 20)
assert 190 == bowling([9,1] * 10 + [9])
assert 300 == bowling([10] * 12)
assert 200 == bowling([10, 5,5] * 5 + [10])
assert 11 == bowling([0,0] * 9 + [10,1,0])
assert 12 == bowling([0,0] * 8 + [10, 1,0])
print "passed"
test_bowling()