Skip to content

Commit c855042

Browse files
committed
Refactor out dict for scores
1 parent 86c01aa commit c855042

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

python/yacht/yacht.py

+29-17
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,49 @@
11
from collections import Counter
22

33
# Score categories
4-
YACHT = 50
5-
ONES = 1
6-
TWOS = 2
7-
THREES = 3
8-
FOURS = 4
9-
FIVES = 5
10-
SIXES = 6
4+
ONES = "ONES"
5+
TWOS = "TWOS"
6+
THREES = "THREES"
7+
FOURS = "FOURS"
8+
FIVES = "FIVES"
9+
SIXES = "SIXES"
1110
FULL_HOUSE = "FULL_HOUSE"
1211
FOUR_OF_A_KIND = "FOUR_OF_A_KIND"
13-
STRAIGHT = 30
12+
STRAIGHT = "STRAIGHT"
1413
LITTLE_STRAIGHT = "LITTLE_STRAIGHT"
1514
BIG_STRAIGHT = "BIG_STRAIGHT"
1615
CHOICE = "CHOICE"
16+
YACHT = "YACHT"
17+
18+
# Score per category
19+
scores = {
20+
ONES: 1,
21+
TWOS: 2,
22+
THREES: 3,
23+
FOURS: 4,
24+
FIVES: 5,
25+
SIXES: 6,
26+
STRAIGHT: 30,
27+
YACHT: 50,
28+
}
1729

1830
def score_ones(dice):
19-
return ONES * dice.count(1)
31+
return scores[ONES] * dice.count(1)
2032

2133
def score_twos(dice):
22-
return TWOS * dice.count(2)
34+
return scores[TWOS] * dice.count(2)
2335

2436
def score_threes(dice):
25-
return THREES * dice.count(3)
37+
return scores[THREES] * dice.count(3)
2638

2739
def score_fours(dice):
28-
return FOURS * dice.count(4)
40+
return scores[FOURS] * dice.count(4)
2941

3042
def score_fives(dice):
31-
return FIVES * dice.count(5)
43+
return scores[FIVES] * dice.count(5)
3244

3345
def score_sixes(dice):
34-
return SIXES * dice.count(6)
46+
return scores[SIXES] * dice.count(6)
3547

3648
def score_full_house(dice):
3749
return sum(dice) if sorted(set(Counter(dice).values())) == [2, 3] else 0
@@ -47,16 +59,16 @@ def score_four_of_a_kind(dice):
4759
return 0
4860

4961
def score_little_straight(dice):
50-
return STRAIGHT if sorted(dice) == [1, 2, 3, 4, 5] else 0
62+
return scores[STRAIGHT] if sorted(dice) == [1, 2, 3, 4, 5] else 0
5163

5264
def score_big_straight(dice):
53-
return STRAIGHT if sorted(dice) == [2, 3, 4, 5, 6] else 0
65+
return scores[STRAIGHT] if sorted(dice) == [2, 3, 4, 5, 6] else 0
5466

5567
def score_choice(dice):
5668
return sum(dice)
5769

5870
def score_yacht(dice):
59-
return YACHT if len(set(dice)) == 1 else 0
71+
return scores[YACHT] if len(set(dice)) == 1 else 0
6072

6173
def score(dice, category):
6274
if (category == ONES):

python/yacht/yacht_test.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ def test_big_straight_as_little_straight(self):
8484
def test_no_pairs_but_not_a_big_straight(self):
8585
self.assertEqual(yacht.score([6, 5, 4, 3, 1], yacht.BIG_STRAIGHT), 0)
8686

87-
# def test_choice(self):
88-
# self.assertEqual(yacht.score([3, 3, 5, 6, 6], yacht.CHOICE), 23)
87+
def test_choice(self):
88+
self.assertEqual(yacht.score([3, 3, 5, 6, 6], yacht.CHOICE), 23)
8989

90-
# def test_yacht_as_choice(self):
91-
# self.assertEqual(yacht.score([2, 2, 2, 2, 2], yacht.CHOICE), 10)
90+
def test_yacht_as_choice(self):
91+
self.assertEqual(yacht.score([2, 2, 2, 2, 2], yacht.CHOICE), 10)
9292

9393

9494
if __name__ == "__main__":

0 commit comments

Comments
 (0)