Skip to content

Commit 175aea8

Browse files
authored
Add Yacht exercise (#339)
* Add Yacht exercise * Small bug in four-of-a-kind * Capitalize category names in test descriptions * s/Full house/Full House/
1 parent 97cc27a commit 175aea8

File tree

7 files changed

+315
-0
lines changed

7 files changed

+315
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,14 @@
590590
"practices": [],
591591
"prerequisites": [],
592592
"difficulty": 2
593+
},
594+
{
595+
"slug": "yacht",
596+
"name": "Yacht",
597+
"uuid": "039e844c-da0b-4e3e-8910-c697429c2350",
598+
"practices": [],
599+
"prerequisites": [],
600+
"difficulty": 1
593601
}
594602
]
595603
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Instructions
2+
3+
The dice game [Yacht][yacht] is from the same family as Poker Dice, Generala and particularly Yahtzee, of which it is a precursor.
4+
In the game, five dice are rolled and the result can be entered in any of twelve categories.
5+
The score of a throw of the dice depends on category chosen.
6+
7+
## Scores in Yacht
8+
9+
| Category | Score | Description | Example |
10+
| --------------- | ---------------------- | ---------------------------------------- | ------------------- |
11+
| Ones | 1 × number of ones | Any combination | 1 1 1 4 5 scores 3 |
12+
| Twos | 2 × number of twos | Any combination | 2 2 3 4 5 scores 4 |
13+
| Threes | 3 × number of threes | Any combination | 3 3 3 3 3 scores 15 |
14+
| Fours | 4 × number of fours | Any combination | 1 2 3 3 5 scores 0 |
15+
| Fives | 5 × number of fives | Any combination | 5 1 5 2 5 scores 15 |
16+
| Sixes | 6 × number of sixes | Any combination | 2 3 4 5 6 scores 6 |
17+
| Full House | Total of the dice | Three of one number and two of another | 3 3 3 5 5 scores 19 |
18+
| Four of a Kind | Total of the four dice | At least four dice showing the same face | 4 4 4 4 6 scores 16 |
19+
| Little Straight | 30 points | 1-2-3-4-5 | 1 2 3 4 5 scores 30 |
20+
| Big Straight | 30 points | 2-3-4-5-6 | 2 3 4 5 6 scores 30 |
21+
| Choice | Sum of the dice | Any combination | 2 3 3 4 6 scores 18 |
22+
| Yacht | 50 points | All five dice showing the same face | 4 4 4 4 4 scores 50 |
23+
24+
If the dice do not satisfy the requirements of a category, the score is zero.
25+
If, for example, _Four Of A Kind_ is entered in the _Yacht_ category, zero points are scored.
26+
A _Yacht_ scores zero if entered in the _Full House_ category.
27+
28+
## Task
29+
30+
Given a list of values for five dice and a category, your solution should return the score of the dice for that category.
31+
If the dice do not satisfy the requirements of the category your solution should return 0.
32+
You can assume that five values will always be presented, and the value of each will be between one and six inclusively.
33+
You should not assume that the dice are ordered.
34+
35+
[yacht]: https://en.wikipedia.org/wiki/Yacht_(dice_game)
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"authors": ["blakelewis"],
3+
"files": {
4+
"solution": [
5+
"yacht.rkt"
6+
],
7+
"test": [
8+
"yacht-test.rkt"
9+
],
10+
"example": [
11+
".meta/example.rkt"
12+
]
13+
},
14+
"blurb": "Score a single throw of dice in the game Yacht.",
15+
"source": "James Kilfiger, using wikipedia",
16+
"source_url": "https://en.wikipedia.org/wiki/Yacht_(dice_game)"
17+
}
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#lang racket
2+
3+
(provide yacht)
4+
5+
(define (sum-of-dice value dice)
6+
(* value (count (λ (d) (= d value)) dice)))
7+
8+
(define (sorted-groups dice)
9+
(sort (group-by identity dice)
10+
(λ (x y) (> (length x) (length y)))))
11+
12+
(define (full-house dice)
13+
(let ([groups (sorted-groups dice)])
14+
(if (and (= (length groups) 2)
15+
(= (length (car groups)) 3))
16+
(apply + dice)
17+
0)))
18+
19+
(define (four-of-a-kind dice)
20+
(let ([groups (sorted-groups dice)])
21+
(if (or (and (= (length groups) 2)
22+
(= (length (car groups)) 4))
23+
(= (length groups) 1))
24+
(* 4 (caar groups))
25+
0)))
26+
27+
(define (straight start dice)
28+
(let ([groups (sorted-groups dice)])
29+
(if (and (= (length groups) 5)
30+
(= (apply min dice) start)
31+
(= (apply max dice) (+ start 4)))
32+
30
33+
0)))
34+
35+
(define (five-of-a-kind dice)
36+
(if (= (length (sorted-groups dice)) 1) 50 0))
37+
38+
(define (yacht dice category)
39+
(cond [(string=? category "ones") (sum-of-dice 1 dice)]
40+
[(string=? category "twos") (sum-of-dice 2 dice)]
41+
[(string=? category "threes") (sum-of-dice 3 dice)]
42+
[(string=? category "fours") (sum-of-dice 4 dice)]
43+
[(string=? category "fives") (sum-of-dice 5 dice)]
44+
[(string=? category "sixes") (sum-of-dice 6 dice)]
45+
[(string=? category "full house") (full-house dice)]
46+
[(string=? category "four of a kind") (four-of-a-kind dice)]
47+
[(string=? category "little straight") (straight 1 dice)]
48+
[(string=? category "big straight") (straight 2 dice)]
49+
[(string=? category "choice") (apply + dice)]
50+
[(string=? category "yacht") (five-of-a-kind dice)]
51+
[else (error "unknown category")]))
52+
+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[3060e4a5-4063-4deb-a380-a630b43a84b6]
13+
description = "Yacht"
14+
15+
[15026df2-f567-482f-b4d5-5297d57769d9]
16+
description = "Not Yacht"
17+
18+
[36b6af0c-ca06-4666-97de-5d31213957a4]
19+
description = "Ones"
20+
21+
[023a07c8-6c6e-44d0-bc17-efc5e1b8205a]
22+
description = "Ones, out of order"
23+
24+
[7189afac-cccd-4a74-8182-1cb1f374e496]
25+
description = "No ones"
26+
27+
[793c4292-dd14-49c4-9707-6d9c56cee725]
28+
description = "Twos"
29+
30+
[dc41bceb-d0c5-4634-a734-c01b4233a0c6]
31+
description = "Fours"
32+
33+
[f6125417-5c8a-4bca-bc5b-b4b76d0d28c8]
34+
description = "Yacht counted as threes"
35+
36+
[464fc809-96ed-46e4-acb8-d44e302e9726]
37+
description = "Yacht of 3s counted as fives"
38+
39+
[d054227f-3a71-4565-a684-5c7e621ec1e9]
40+
description = "Fives"
41+
42+
[e8a036e0-9d21-443a-8b5f-e15a9e19a761]
43+
description = "Sixes"
44+
45+
[51cb26db-6b24-49af-a9ff-12f53b252eea]
46+
description = "Full house two small, three big"
47+
48+
[1822ca9d-f235-4447-b430-2e8cfc448f0c]
49+
description = "Full house three small, two big"
50+
51+
[b208a3fc-db2e-4363-a936-9e9a71e69c07]
52+
description = "Two pair is not a full house"
53+
54+
[b90209c3-5956-445b-8a0b-0ac8b906b1c2]
55+
description = "Four of a kind is not a full house"
56+
57+
[32a3f4ee-9142-4edf-ba70-6c0f96eb4b0c]
58+
description = "Yacht is not a full house"
59+
60+
[b286084d-0568-4460-844a-ba79d71d79c6]
61+
description = "Four of a Kind"
62+
63+
[f25c0c90-5397-4732-9779-b1e9b5f612ca]
64+
description = "Yacht can be scored as Four of a Kind"
65+
66+
[9f8ef4f0-72bb-401a-a871-cbad39c9cb08]
67+
description = "Full house is not Four of a Kind"
68+
69+
[b4743c82-1eb8-4a65-98f7-33ad126905cd]
70+
description = "Little Straight"
71+
72+
[7ac08422-41bf-459c-8187-a38a12d080bc]
73+
description = "Little Straight as Big Straight"
74+
75+
[97bde8f7-9058-43ea-9de7-0bc3ed6d3002]
76+
description = "Four in order but not a little straight"
77+
78+
[cef35ff9-9c5e-4fd2-ae95-6e4af5e95a99]
79+
description = "No pairs but not a little straight"
80+
81+
[fd785ad2-c060-4e45-81c6-ea2bbb781b9d]
82+
description = "Minimum is 1, maximum is 5, but not a little straight"
83+
84+
[35bd74a6-5cf6-431a-97a3-4f713663f467]
85+
description = "Big Straight"
86+
87+
[87c67e1e-3e87-4f3a-a9b1-62927822b250]
88+
description = "Big Straight as little straight"
89+
90+
[c1fa0a3a-40ba-4153-a42d-32bc34d2521e]
91+
description = "No pairs but not a big straight"
92+
93+
[207e7300-5d10-43e5-afdd-213e3ac8827d]
94+
description = "Choice"
95+
96+
[b524c0cf-32d2-4b40-8fb3-be3500f3f135]
97+
description = "Yacht as choice"
+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#lang racket/base
2+
3+
(require "yacht.rkt")
4+
5+
(module+ test
6+
(require rackunit rackunit/text-ui))
7+
8+
(module+ test
9+
(define suite
10+
(test-suite
11+
"yacht tests"
12+
13+
(test-equal? "Yacht"
14+
(yacht '(5 5 5 5 5) "yacht") 50)
15+
16+
(test-equal? "Not Yacht"
17+
(yacht '(1 3 3 2 5) "yacht") 0)
18+
19+
(test-equal? "Ones"
20+
(yacht '(1 1 1 3 5) "ones") 3)
21+
22+
(test-equal? "Ones, out of order"
23+
(yacht '(3 1 1 5 1) "ones") 3)
24+
25+
(test-equal? "No Ones"
26+
(yacht '(4 3 6 5 5) "ones") 0)
27+
28+
(test-equal? "Twos"
29+
(yacht '(2 3 4 5 6) "twos") 2)
30+
31+
(test-equal? "Fours"
32+
(yacht '(1 4 1 4 1) "fours") 8)
33+
34+
(test-equal? "Yacht counted as Threes"
35+
(yacht '(3 3 3 3 3) "threes") 15)
36+
37+
(test-equal? "Yacht of 3s counted as Fives"
38+
(yacht '(3 3 3 3 3) "fives") 0)
39+
40+
(test-equal? "Fives"
41+
(yacht '(1 5 3 5 3) "fives") 10)
42+
43+
(test-equal? "Sixes"
44+
(yacht '(2 3 4 5 6) "sixes") 6)
45+
46+
(test-equal? "Full House two small, three big"
47+
(yacht '(2 2 4 4 4) "full house") 16)
48+
49+
(test-equal? "Full House three small, two big"
50+
(yacht '(5 3 3 5 3) "full house") 19)
51+
52+
(test-equal? "Two pair is not a Full House"
53+
(yacht '(2 2 4 4 5) "full house") 0)
54+
55+
(test-equal? "Four of a kind is not a Full House"
56+
(yacht '(1 4 4 4 4) "full house") 0)
57+
58+
(test-equal? "Yacht is not a Full House"
59+
(yacht '(2 2 2 2 2) "full house") 0)
60+
61+
(test-equal? "Four of a Kind"
62+
(yacht '(6 6 4 6 6) "four of a kind") 24)
63+
64+
(test-equal? "Yacht can be scored as Four of a Kind"
65+
(yacht '(3 3 3 3 3) "four of a kind") 12)
66+
67+
(test-equal? "Full House is not Four of a Kind"
68+
(yacht '(3 3 3 5 5) "four of a kind") 0)
69+
70+
(test-equal? "Little Straight"
71+
(yacht '(3 5 4 1 2) "little straight") 30)
72+
73+
(test-equal? "Little Straight as Big Straight"
74+
(yacht '(1 2 3 4 5) "big straight") 0)
75+
76+
(test-equal? "Four in order but not a Little Straight"
77+
(yacht '(1 1 2 3 4) "little straight") 0)
78+
79+
(test-equal? "No pairs but not a Little Straight"
80+
(yacht '(1 2 3 4 6) "little straight") 0)
81+
82+
(test-equal? "Minimum is 1, maximum is 5, but not a Little Straight"
83+
(yacht '(1 1 3 4 5) "little straight") 0)
84+
85+
(test-equal? "Big Straight"
86+
(yacht '(4 6 2 5 3) "big straight") 30)
87+
88+
(test-equal? "Big Straight as Little Straight"
89+
(yacht '(6 5 4 3 2) "little straight") 0)
90+
91+
(test-equal? "No pairs but not a Big Straight"
92+
(yacht '(6 5 4 3 1) "big straight") 0)
93+
94+
(test-equal? "Choice"
95+
(yacht '(3 3 5 6 6) "choice") 23)
96+
97+
(test-equal? "Yacht as Choice"
98+
(yacht '(2 2 2 2 2) "choice") 10)))
99+
100+
(run-tests suite))

exercises/practice/yacht/yacht.rkt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#lang racket
2+
3+
(provide yacht)
4+
5+
(define (yacht dice category)
6+
(error "Not implemented yet"))

0 commit comments

Comments
 (0)