Skip to content

Commit 7ff0c68

Browse files
Add Yacht exercise to the Scala track (#789)
* test * done coding Signed-off-by: wangxiaoxuan273 <[email protected]> * Update exercises/practice/yacht/.meta/config.json Co-authored-by: Erik Schierboom <[email protected]> * Update exercises/practice/yacht/src/test/scala/YachtTest.scala Co-authored-by: Erik Schierboom <[email protected]> * added pending on tests Signed-off-by: wangxiaoxuan273 <[email protected]> * reran the configlet commands Signed-off-by: wangxiaoxuan273 <[email protected]> * changed implementation Signed-off-by: wangxiaoxuan273 <[email protected]> * added extra line to make github happy Signed-off-by: wangxiaoxuan273 <[email protected]> * fixed my class name error in Example.scala Signed-off-by: wangxiaoxuan273 <[email protected]> --------- Signed-off-by: wangxiaoxuan273 <[email protected]> Co-authored-by: Erik Schierboom <[email protected]>
1 parent b26bb49 commit 7ff0c68

File tree

8 files changed

+372
-0
lines changed

8 files changed

+372
-0
lines changed

config.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,6 +1221,17 @@
12211221
"transforming"
12221222
]
12231223
},
1224+
{
1225+
"slug": "yacht",
1226+
"name": "Yacht",
1227+
"uuid": "d4b60464-31e3-47f9-a99d-08a6e3a255b6",
1228+
"practices": [],
1229+
"prerequisites": [],
1230+
"difficulty": 5,
1231+
"topics": [
1232+
"pattern_matching"
1233+
]
1234+
},
12241235
{
12251236
"slug": "sgf-parsing",
12261237
"name": "SGF Parsing",
Lines changed: 35 additions & 0 deletions
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)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
object Yacht {
2+
def yacht(dices: List[Int]): Int =
3+
if (dices.toSet.size == 1) 50 else 0
4+
5+
def diceFrequencies(dices: List[Int]): Vector[Int] =
6+
(1 to 6).map(i => dices.count(_ == i)).toVector
7+
8+
def appearances(dices: List[Int], diceValue: Int): Int =
9+
diceFrequencies(dices)(diceValue - 1)
10+
11+
def getSumOfDices(dices: List[Int]): Int =
12+
dices.sum
13+
14+
def fourOfKind(dices: List[Int]): Int = {
15+
val counterArray = diceFrequencies(dices)
16+
val scores = (0 to 5).flatMap(i =>
17+
if (counterArray(i) >= 4) Some((i + 1) * 4) else None
18+
)
19+
if (scores.isEmpty) 0 else scores.head
20+
}
21+
22+
def littleStraight(dices: List[Int]): Int =
23+
if (dices.sorted == List(1, 2, 3, 4, 5)) 30 else 0
24+
25+
def bigStraight(dices: List[Int]): Int =
26+
if (dices.sorted == List(2, 3, 4, 5, 6)) 30 else 0
27+
28+
def fullHouse(dices: List[Int]): Int = {
29+
val counterArray = diceFrequencies(dices)
30+
if (counterArray.contains(2) && counterArray.contains(3))
31+
dices.sum
32+
else
33+
0
34+
}
35+
36+
def score(dices: List[Int], category: String): Int = category match {
37+
case "yacht" => yacht(dices)
38+
case "ones" => appearances(dices, 1)
39+
case "twos" => appearances(dices, 2) * 2
40+
case "threes" => appearances(dices, 3) * 3
41+
case "fours" => appearances(dices, 4) * 4
42+
case "fives" => appearances(dices, 5) * 5
43+
case "sixes" => appearances(dices, 6) * 6
44+
case "full house" => fullHouse(dices)
45+
case "four of a kind" => fourOfKind(dices)
46+
case "little straight" => littleStraight(dices)
47+
case "big straight" => bigStraight(dices)
48+
case "choice" => getSumOfDices(dices)
49+
case _ => 0
50+
}
51+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"authors": [
3+
"wangxiaoxuan273"
4+
],
5+
"files": {
6+
"solution": [
7+
"src/main/scala/Yacht.scala"
8+
],
9+
"test": [
10+
"src/test/scala/YachtTest.scala"
11+
],
12+
"example": [
13+
".meta/Example.scala"
14+
],
15+
"invalidator": [
16+
"build.sbt"
17+
]
18+
},
19+
"blurb": "Score a single throw of dice in the game Yacht.",
20+
"source": "James Kilfiger, using wikipedia",
21+
"source_url": "https://en.wikipedia.org/wiki/Yacht_(dice_game)"
22+
}
Lines changed: 97 additions & 0 deletions
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"

exercises/practice/yacht/build.sbt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
scalaVersion := "2.13.6"
2+
3+
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.10" % "test"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
object Yacht {
2+
3+
def score(dices: List[Int], category: String): Int = ???
4+
}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
import org.scalatest.funsuite.AnyFunSuite
2+
import org.scalatest.matchers.should.Matchers
3+
4+
class YachtTests extends AnyFunSuite with Matchers {
5+
6+
test("Yacht") {
7+
Yacht.score(List(5, 5, 5, 5, 5), "yacht") shouldEqual 50
8+
}
9+
10+
test("Not Yacht") {
11+
pending
12+
Yacht.score(List(1, 3, 3, 2, 5), "yacht") shouldEqual 0
13+
}
14+
15+
test("Ones") {
16+
pending
17+
Yacht.score(List(1, 1, 1, 3, 5), "ones") shouldEqual 3
18+
}
19+
20+
test("Ones, out of order") {
21+
pending
22+
Yacht.score(List(3, 1, 1, 5, 1), "ones") shouldEqual 3
23+
}
24+
25+
test("No ones") {
26+
pending
27+
Yacht.score(List(4, 3, 6, 5, 5), "ones") shouldEqual 0
28+
}
29+
30+
test("Twos") {
31+
pending
32+
Yacht.score(List(2, 3, 4, 5, 6), "twos") shouldEqual 2
33+
}
34+
35+
test("Fours") {
36+
pending
37+
Yacht.score(List(1, 4, 1, 4, 1), "fours") shouldEqual 8
38+
}
39+
40+
test("Yacht counted as threes") {
41+
pending
42+
Yacht.score(List(3, 3, 3, 3, 3), "threes") shouldEqual 15
43+
}
44+
45+
test("Yacht of 3s counted as fives") {
46+
pending
47+
Yacht.score(List(3, 3, 3, 3, 3), "fives") shouldEqual 0
48+
}
49+
50+
test("Fives") {
51+
pending
52+
Yacht.score(List(1, 5, 3, 5, 3), "fives") shouldEqual 10
53+
}
54+
55+
test("Sixes") {
56+
pending
57+
Yacht.score(List(2, 3, 4, 5, 6), "sixes") shouldEqual 6
58+
}
59+
60+
test("Full house two small, three big") {
61+
pending
62+
Yacht.score(List(2, 2, 4, 4, 4), "full house") shouldEqual 16
63+
}
64+
65+
test("Full house three small, two big") {
66+
pending
67+
Yacht.score(List(5, 3, 3, 5, 3), "full house") shouldEqual 19
68+
}
69+
70+
test("Two pair is not a full house") {
71+
pending
72+
Yacht.score(List(2, 2, 4, 4, 5), "full house") shouldEqual 0
73+
}
74+
75+
test("Four of a kind is not a full house") {
76+
pending
77+
Yacht.score(List(1, 4, 4, 4, 4), "full house") shouldEqual 0
78+
}
79+
80+
test("Yacht is not a full house") {
81+
pending
82+
Yacht.score(List(2, 2, 2, 2, 2), "full house") shouldEqual 0
83+
}
84+
85+
test("Four of a Kind") {
86+
pending
87+
Yacht.score(List(6, 6, 4, 6, 6), "four of a kind") shouldEqual 24
88+
}
89+
90+
test("Yacht can be scored as Four of a Kind") {
91+
pending
92+
Yacht.score(List(3, 3, 3, 3, 3), "four of a kind") shouldEqual 12
93+
}
94+
95+
test("Full house is not Four of a Kind") {
96+
pending
97+
Yacht.score(List(3, 3, 3, 5, 5), "four of a kind") shouldEqual 0
98+
}
99+
100+
test("Little Straight") {
101+
pending
102+
Yacht.score(List(3, 5, 4, 1, 2), "little straight") shouldEqual 30
103+
}
104+
105+
test("Little Straight as Big Straight") {
106+
pending
107+
Yacht.score(List(1, 2, 3, 4, 5), "big straight") shouldEqual 0
108+
}
109+
110+
test("Four in order but not a little straight") {
111+
pending
112+
Yacht.score(List(1, 1, 2, 3, 4), "little straight") shouldEqual 0
113+
}
114+
115+
test("No pairs but not a little straight") {
116+
pending
117+
Yacht.score(List(1, 2, 3, 4, 6), "little straight") shouldEqual 0
118+
}
119+
120+
test("Minimum is 1, maximum is 5, but not a little straight") {
121+
pending
122+
Yacht.score(List(1, 1, 3, 4, 5), "little straight") shouldEqual 0
123+
}
124+
125+
test("Big Straight") {
126+
pending
127+
Yacht.score(List(4, 6, 2, 5, 3), "big straight") shouldEqual 30
128+
}
129+
130+
test("Big Straight as little straight") {
131+
pending
132+
Yacht.score(List(6, 5, 4, 3, 2), "little straight") shouldEqual 0
133+
}
134+
135+
test("No pairs but not a big straight") {
136+
pending
137+
Yacht.score(List(6, 5, 4, 3, 1), "big straight") shouldEqual 0
138+
}
139+
140+
test("Choice") {
141+
pending
142+
Yacht.score(List(3, 3, 5, 6, 6), "choice") shouldEqual 23
143+
}
144+
145+
test("Yacht as choice") {
146+
pending
147+
Yacht.score(List(2, 2, 2, 2, 2), "choice") shouldEqual 10
148+
}
149+
}

0 commit comments

Comments
 (0)