Skip to content

Commit 24f62ac

Browse files
bites 142 - Improve code
1 parent 7c5e7d0 commit 24f62ac

File tree

1 file changed

+7
-26
lines changed

1 file changed

+7
-26
lines changed

142/scores.py

+7-26
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import numbers
21
from collections import namedtuple
3-
from functools import reduce
42
from typing import List
53

64
MIN_SCORE = 4
@@ -20,13 +18,9 @@ def calculate_score(scores: List[int]) -> int:
2018
Returns int of the sum of the scores.
2119
"""
2220
#accumulated score
23-
acscore = 0
24-
for score in scores:
25-
if score not in DICE_VALUES:
26-
raise ValueError
27-
if score >= MIN_SCORE:
28-
acscore += score
29-
return acscore
21+
if not all(i in DICE_VALUES for i in scores):
22+
raise ValueError("Includes invalid dice roll(s)")
23+
return sum(i for i in scores if i >= MIN_SCORE)
3024

3125
def get_winner(players):
3226
"""Given a list of Player namedtuples return the player
@@ -46,20 +40,7 @@ def get_winner(players):
4640
output:
4741
Player(name='player 3', scores=[4, 5, 1, 2])
4842
"""
49-
lencheck = set()
50-
scores = dict()
51-
for player in players:
52-
lencheck.add(len(player.scores))
53-
if len(lencheck) > 1:
54-
raise ValueError
55-
scores.update({player.name: calculate_score(player.scores)})
56-
HighestscorePlayername = sorted(scores.items(),key=lambda x:x[1])[-1][0]
57-
for player in players:
58-
if player.name == HighestscorePlayername:
59-
return player
60-
61-
print(get_winner([
62-
Player(name='player 1', scores=[1, 3, 2, 5]),
63-
Player(name='player 2', scores=[1, 1, 1, 1]),
64-
Player(name='player 3', scores=[4, 5, 1, 2]), # max 9
65-
]))
43+
lencheck = {len(player.scores) for player in players}
44+
if len(lencheck) > 1:
45+
raise ValueError("Players with different amount of score")
46+
return max(players, key=lambda x: calculate_score(x.scores))

0 commit comments

Comments
 (0)