Skip to content

Commit 97cb780

Browse files
committed
feat(day 154): implement golf score evaluator based on par and strokes
1 parent 127b4c1 commit 97cb780

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+
Par for the Hole
4+
Given two integers, the par for a golf hole and the number of strokes a golfer took on that hole, return the golfer's score using golf terms.
5+
6+
Return:
7+
8+
"Hole in one!" if it took one stroke.
9+
"Eagle" if it took two strokes less than par.
10+
"Birdie" if it took one stroke less than par.
11+
"Par" if it took the same number of strokes as par.
12+
"Bogey" if it took one stroke more than par.
13+
"Double bogey" if took two strokes more than par.
14+
"""
15+
16+
import unittest
17+
18+
class ParOfTheHoleTest(unittest.TestCase):
19+
20+
def test1(self):
21+
self.assertEqual(golf_score(3, 3), "Par")
22+
23+
def test2(self):
24+
self.assertEqual(golf_score(4, 3), "Birdie")
25+
26+
def test3(self):
27+
self.assertEqual(golf_score(3, 1), "Hole in one!")
28+
29+
def test4(self):
30+
self.assertEqual(golf_score(5, 7), "Double bogey")
31+
32+
def test5(self):
33+
self.assertEqual(golf_score(4, 5), "Bogey")
34+
35+
def test6(self):
36+
self.assertEqual(golf_score(5, 3), "Eagle")
37+
38+
39+
40+
def golf_score(par, strokes):
41+
if strokes == 1:
42+
return "Hole in one!"
43+
elif strokes == par-2:
44+
return "Eagle"
45+
elif strokes == par-1:
46+
return "Birdie"
47+
elif strokes == par:
48+
return "Par"
49+
elif strokes == par+1:
50+
return "Bogey"
51+
elif strokes == par+2:
52+
return "Double bogey"
53+
else:
54+
return "No term"
55+
56+
"""
57+
58+
=> "Hole in one!" overrides everything else(always check first).
59+
=> The other terms are relatives to par.
60+
=> If strokes don't match any of the listed cases, return "No term" (which is an optional safegaurd).
61+
"""
62+
63+
if __name__ == "__main__":
64+
print(golf_score(5, 3))
65+
print(golf_score(5, 18))
66+
unittest.main()

0 commit comments

Comments
 (0)