Skip to content

Commit f19d979

Browse files
committed
feat(day 185): calculate figure skating final score with judge trimming and penalties
1 parent 3f0ffee commit f19d979

1 file changed

Lines changed: 66 additions & 0 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""
2+
3+
2026 Winter Games Day 6: Figure Skating
4+
Given an array of judge scores and optional penalties, calculate the final score for a figure skating routine.
5+
6+
The first argument is an array of 10 judge scores, each a number from 0 to 10. Remove the highest and lowest judge scores and sum the remaining 8 scores to get the base score.
7+
8+
Any additional arguments passed to the function are penalties. Subtract all penalties from the base score to get the final score.
9+
"""
10+
11+
12+
import unittest
13+
14+
15+
class FigureSkatingTest(unittest.TestCase):
16+
17+
def test1(self):
18+
self.assertEqual(compute_score([10, 8, 9, 6, 9, 8, 8, 9, 7, 7], 1), 64)
19+
20+
def test2(self):
21+
self.assertEqual(compute_score([10, 10, 10, 10, 10, 10, 10, 10, 10, 10]), 80)
22+
23+
def test3(self):
24+
self.assertEqual(compute_score([10, 8, 9, 10, 9, 8, 8, 9, 10, 7], 1, 2, 1), 67)
25+
26+
def test4(self):
27+
self.assertEqual(compute_score([8.0, 8.5, 9.0, 8.5, 9.0, 8.0, 9.0, 8.5, 9.0, 8.5], 0.5, 1.0), 67.5)
28+
29+
def test5(self):
30+
self.assertEqual(compute_score([6.0, 8.5, 7.0, 9.0, 7.5, 8.0, 6.5, 9.5, 7.0, 8.0], 1.5, 0.5, 0.5), 59)
31+
32+
33+
def compute_score(judge_scores, *penalties):
34+
35+
judge_scores.sort(reverse=True)
36+
# we can use the sorted(judge_scores) here and this judge_score.sort will return None.
37+
judge_scores.pop(0)
38+
judge_scores.pop()
39+
40+
total_score = sum(judge_scores)
41+
if not penalties:
42+
return total_score
43+
else:
44+
for penalty in penalties:
45+
total_score -= penalty
46+
47+
return total_score
48+
49+
50+
"""
51+
We can improve this code by making some few changes
52+
53+
=> You can simplify penalty subtraction with sum(penalties)
54+
"""
55+
56+
def compute_score(judge_scores, *penlties):
57+
scores = sorted(judge_scores)
58+
base_score = sum(scores[1: -1])
59+
60+
total_score = base_score - sum(penlties)
61+
62+
return total_score
63+
64+
65+
if __name__ == "__main__":
66+
unittest.main()

0 commit comments

Comments
 (0)