Skip to content

Commit 4ab39ca

Browse files
[#722] New practice exercise yacht (#760)
Co-authored-by: Angelika Tyborska <[email protected]>
1 parent adcbf3a commit 4ab39ca

File tree

11 files changed

+484
-0
lines changed

11 files changed

+484
-0
lines changed

config.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2429,6 +2429,25 @@
24292429
"basics"
24302430
],
24312431
"difficulty": 4
2432+
},
2433+
{
2434+
"slug": "yacht",
2435+
"name": "Yacht",
2436+
"uuid": "d2ee3ff4-2f01-404a-a4f0-d15823ac0482",
2437+
"prerequisites": [
2438+
"lists",
2439+
"cond",
2440+
"case",
2441+
"if",
2442+
"multiple-clause-functions",
2443+
"pattern-matching",
2444+
"guards",
2445+
"enum"
2446+
],
2447+
"practices": [
2448+
"multiple-clause-matching"
2449+
],
2450+
"difficulty": 5
24322451
}
24332452
],
24342453
"foregone": [
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Score a single throw of dice in *Yacht*
2+
3+
The dice game [Yacht](https://en.wikipedia.org/wiki/Yacht_(dice_game)) is from
4+
the same family as Poker Dice, Generala and particularly Yahtzee, of which it
5+
is a precursor. In the game, five dice are rolled and the result can be entered
6+
in any of twelve categories. The score of a throw of the dice depends on
7+
category chosen.
8+
9+
## Scores in Yacht
10+
11+
| Category | Score | Description | Example |
12+
| -------- | ----- | ----------- | ------- |
13+
| Ones | 1 × number of ones | Any combination | 1 1 1 4 5 scores 3 |
14+
| Twos | 2 × number of twos | Any combination | 2 2 3 4 5 scores 4 |
15+
| Threes | 3 × number of threes | Any combination | 3 3 3 3 3 scores 15 |
16+
| Fours | 4 × number of fours | Any combination | 1 2 3 3 5 scores 0 |
17+
| Fives | 5 × number of fives| Any combination | 5 1 5 2 5 scores 15 |
18+
| Sixes | 6 × number of sixes | Any combination | 2 3 4 5 6 scores 6 |
19+
| Full House | Total of the dice | Three of one number and two of another | 3 3 3 5 5 scores 19 |
20+
| Four of a Kind | Total of the four dice | At least four dice showing the same face | 4 4 4 4 6 scores 16 |
21+
| Little Straight | 30 points | 1-2-3-4-5 | 1 2 3 4 5 scores 30 |
22+
| Big Straight | 30 points | 2-3-4-5-6 | 2 3 4 5 6 scores 30 |
23+
| Choice | Sum of the dice | Any combination | 2 3 3 4 6 scores 18 |
24+
| Yacht | 50 points | All five dice showing the same face | 4 4 4 4 4 scores 50 |
25+
26+
If the dice do not satisfy the requirements of a category, the score is zero.
27+
If, for example, *Four Of A Kind* is entered in the *Yacht* category, zero
28+
points are scored. A *Yacht* scores zero if entered in the *Full House* category.
29+
30+
## Task
31+
Given a list of values for five dice and a category, your solution should return
32+
the score of the dice for that category. If the dice do not satisfy the requirements
33+
of the category your solution should return 0. You can assume that five values
34+
will always be presented, and the value of each will be between one and six
35+
inclusively. You should not assume that the dice are ordered.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Used by "mix format"
2+
[
3+
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
4+
]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# The directory Mix will write compiled artifacts to.
2+
/_build/
3+
4+
# If you run "mix test --cover", coverage assets end up here.
5+
/cover/
6+
7+
# The directory Mix downloads your dependencies sources to.
8+
/deps/
9+
10+
# Where third-party dependencies like ExDoc output generated docs.
11+
/doc/
12+
13+
# Ignore .fetch files in case you like to edit your project deps locally.
14+
/.fetch
15+
16+
# If the VM crashes, it generates a dump, let's ignore it too.
17+
erl_crash.dump
18+
19+
# Also ignore archive artifacts (built via "mix archive.build").
20+
*.ez
21+
22+
# Ignore package tarball (built via "mix hex.build").
23+
yacht-*.tar
24+
25+
# Temporary files, for example, from tests.
26+
/tmp/
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"blurb": "Score a single throw of dice in the game Yacht." ,
3+
"authors": [
4+
"elibdev"
5+
],
6+
"contributors": [
7+
"angelikatyborska"
8+
],
9+
"files": {
10+
"solution": [
11+
"lib/yacht.ex"
12+
],
13+
"test": [
14+
"test/yacht_test.exs"
15+
],
16+
"example": [
17+
".meta/example.ex"
18+
]
19+
},
20+
"source": "James Kilfiger, using wikipedia",
21+
"source_url": "https://en.wikipedia.org/wiki/Yacht_(dice_game)"
22+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
defmodule Yacht do
2+
@type category ::
3+
:ones
4+
| :twos
5+
| :threes
6+
| :fours
7+
| :fives
8+
| :sixes
9+
| :full_house
10+
| :four_of_a_kind
11+
| :little_straight
12+
| :big_straight
13+
| :choice
14+
| :yacht
15+
16+
defp die_frequencies(dice) do
17+
Enum.reduce(dice, %{}, fn die, frequencies ->
18+
Map.update(frequencies, die, 1, &(&1 + 1))
19+
end)
20+
end
21+
22+
@doc """
23+
Calculate the score of the list of 5 dice rolls using the given category.
24+
"""
25+
@spec score(category :: category(), dice :: [integer]) :: integer
26+
def score(category, dice)
27+
28+
def score(:ones, dice), do: score(1, dice)
29+
def score(:twos, dice), do: score(2, dice)
30+
def score(:threes, dice), do: score(3, dice)
31+
def score(:fours, dice), do: score(4, dice)
32+
def score(:fives, dice), do: score(5, dice)
33+
def score(:sixes, dice), do: score(6, dice)
34+
35+
def score(number, dice) when is_integer(number) do
36+
Enum.count(dice, &(&1 == number)) * number
37+
end
38+
39+
def score(:full_house, dice) do
40+
full_house =
41+
die_frequencies(dice)
42+
|> Map.values()
43+
|> MapSet.new() == MapSet.new([3, 2])
44+
45+
if full_house do
46+
Enum.sum(dice)
47+
else
48+
0
49+
end
50+
end
51+
52+
def score(:four_of_a_kind, dice) do
53+
frequencies =
54+
die_frequencies(dice)
55+
|> Enum.to_list()
56+
|> Enum.filter(fn {_, frequency} -> frequency >= 4 end)
57+
58+
case frequencies do
59+
[{number, _frequencies}] ->
60+
number * 4
61+
62+
_ ->
63+
0
64+
end
65+
end
66+
67+
def score(:little_straight, dice) do
68+
if MapSet.new(dice) == MapSet.new([1, 2, 3, 4, 5]) do
69+
30
70+
else
71+
0
72+
end
73+
end
74+
75+
def score(:big_straight, dice) do
76+
if MapSet.new(dice) == MapSet.new([2, 3, 4, 5, 6]) do
77+
30
78+
else
79+
0
80+
end
81+
end
82+
83+
def score(:choice, dice) do
84+
Enum.sum(dice)
85+
end
86+
87+
def score(:yacht, dice) do
88+
unique_dice = MapSet.size(MapSet.new(dice))
89+
90+
case unique_dice do
91+
1 -> 50
92+
_ -> 0
93+
end
94+
end
95+
end
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# This is an auto-generated file. Regular comments will be removed when this
2+
# file is regenerated. Regenerating will not touch any manually added keys,
3+
# so comments can be added in a "comment" key.
4+
5+
[3060e4a5-4063-4deb-a380-a630b43a84b6]
6+
description = "Yacht"
7+
8+
[15026df2-f567-482f-b4d5-5297d57769d9]
9+
description = "Not Yacht"
10+
11+
[36b6af0c-ca06-4666-97de-5d31213957a4]
12+
description = "Ones"
13+
14+
[023a07c8-6c6e-44d0-bc17-efc5e1b8205a]
15+
description = "Ones, out of order"
16+
17+
[7189afac-cccd-4a74-8182-1cb1f374e496]
18+
description = "No ones"
19+
20+
[793c4292-dd14-49c4-9707-6d9c56cee725]
21+
description = "Twos"
22+
23+
[dc41bceb-d0c5-4634-a734-c01b4233a0c6]
24+
description = "Fours"
25+
26+
[f6125417-5c8a-4bca-bc5b-b4b76d0d28c8]
27+
description = "Yacht counted as threes"
28+
29+
[464fc809-96ed-46e4-acb8-d44e302e9726]
30+
description = "Yacht of 3s counted as fives"
31+
32+
[e8a036e0-9d21-443a-8b5f-e15a9e19a761]
33+
description = "Sixes"
34+
35+
[51cb26db-6b24-49af-a9ff-12f53b252eea]
36+
description = "Full house two small, three big"
37+
38+
[1822ca9d-f235-4447-b430-2e8cfc448f0c]
39+
description = "Full house three small, two big"
40+
41+
[b208a3fc-db2e-4363-a936-9e9a71e69c07]
42+
description = "Two pair is not a full house"
43+
44+
[b90209c3-5956-445b-8a0b-0ac8b906b1c2]
45+
description = "Four of a kind is not a full house"
46+
47+
[32a3f4ee-9142-4edf-ba70-6c0f96eb4b0c]
48+
description = "Yacht is not a full house"
49+
50+
[b286084d-0568-4460-844a-ba79d71d79c6]
51+
description = "Four of a Kind"
52+
53+
[f25c0c90-5397-4732-9779-b1e9b5f612ca]
54+
description = "Yacht can be scored as Four of a Kind"
55+
56+
[9f8ef4f0-72bb-401a-a871-cbad39c9cb08]
57+
description = "Full house is not Four of a Kind"
58+
59+
[b4743c82-1eb8-4a65-98f7-33ad126905cd]
60+
description = "Little Straight"
61+
62+
[7ac08422-41bf-459c-8187-a38a12d080bc]
63+
description = "Little Straight as Big Straight"
64+
65+
[97bde8f7-9058-43ea-9de7-0bc3ed6d3002]
66+
description = "Four in order but not a little straight"
67+
68+
[cef35ff9-9c5e-4fd2-ae95-6e4af5e95a99]
69+
description = "No pairs but not a little straight"
70+
71+
[fd785ad2-c060-4e45-81c6-ea2bbb781b9d]
72+
description = "Minimum is 1, maximum is 5, but not a little straight"
73+
74+
[35bd74a6-5cf6-431a-97a3-4f713663f467]
75+
description = "Big Straight"
76+
77+
[87c67e1e-3e87-4f3a-a9b1-62927822b250]
78+
description = "Big Straight as little straight"
79+
80+
[c1fa0a3a-40ba-4153-a42d-32bc34d2521e]
81+
description = "No pairs but not a big straight"
82+
83+
[207e7300-5d10-43e5-afdd-213e3ac8827d]
84+
description = "Choice"
85+
86+
[b524c0cf-32d2-4b40-8fb3-be3500f3f135]
87+
description = "Yacht as choice"
88+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
defmodule Yacht do
2+
@type category ::
3+
:ones
4+
| :twos
5+
| :threes
6+
| :fours
7+
| :fives
8+
| :sixes
9+
| :full_house
10+
| :four_of_a_kind
11+
| :little_straight
12+
| :big_straight
13+
| :choice
14+
| :yacht
15+
16+
@doc """
17+
Calculate the score of 5 dice using the given category's scoring method.
18+
"""
19+
@spec score(category :: category(), dice :: [integer]) :: integer
20+
def score(category, dice) do
21+
end
22+
end

exercises/practice/yacht/mix.exs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
defmodule Yacht.MixProject do
2+
use Mix.Project
3+
4+
def project do
5+
[
6+
app: :yacht,
7+
version: "0.1.0",
8+
# elixir: "~> 1.8",
9+
start_permanent: Mix.env() == :prod,
10+
deps: deps()
11+
]
12+
end
13+
14+
# Run "mix help compile.app" to learn about applications.
15+
def application do
16+
[
17+
extra_applications: [:logger]
18+
]
19+
end
20+
21+
# Run "mix help deps" to learn about dependencies.
22+
defp deps do
23+
[
24+
# {:dep_from_hexpm, "~> 0.3.0"},
25+
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
26+
]
27+
end
28+
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ExUnit.start()
2+
ExUnit.configure(exclude: :pending, trace: true)

0 commit comments

Comments
 (0)