Skip to content

Commit ff91466

Browse files
[#767] New practice exercise darts (#777)
Co-authored-by: Angelika Tyborska <[email protected]>
1 parent 482c655 commit ff91466

File tree

11 files changed

+233
-0
lines changed

11 files changed

+233
-0
lines changed

config.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2464,6 +2464,20 @@
24642464
],
24652465
"difficulty": 5
24662466
},
2467+
{
2468+
"slug": "darts",
2469+
"name": "Darts",
2470+
"uuid": "a68113f8-92be-40e0-a97f-eb4239303ef5",
2471+
"prerequisites": [
2472+
"cond",
2473+
"integers",
2474+
"floating-point-numbers"
2475+
],
2476+
"practices": [
2477+
"cond"
2478+
],
2479+
"difficulty": 2
2480+
},
24672481
{
24682482
"slug": "affine-cipher",
24692483
"name": "Affine Cipher",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## General
2+
3+
- The distance of a point `(x, y)` from the center of the target `(0, 0)` can be calculated with `sqrt(x*x + y *y)`
4+
5+
- You can calculate the square root of `x` by raising it to the power of `1/2`. In other words, `sqrt(x) == pow(x, 0.5)`
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Description
2+
3+
Write a function that returns the earned points in a single toss of a Darts game.
4+
5+
[Darts](https://en.wikipedia.org/wiki/Darts) is a game where players
6+
throw darts to a [target](https://en.wikipedia.org/wiki/Darts#/media/File:Darts_in_a_dartboard.jpg).
7+
8+
In our particular instance of the game, the target rewards with 4 different amounts of points, depending on where the dart lands:
9+
10+
* If the dart lands outside the target, player earns no points (0 points).
11+
* If the dart lands in the outer circle of the target, player earns 1 point.
12+
* If the dart lands in the middle circle of the target, player earns 5 points.
13+
* If the dart lands in the inner circle of the target, player earns 10 points.
14+
15+
The outer circle has a radius of 10 units (This is equivalent to the total radius for the entire target), the middle circle a radius of 5 units, and the inner circle a radius of 1. Of course, they are all centered to the same point (That is, the circles are [concentric](http://mathworld.wolfram.com/ConcentricCircles.html)) defined by the coordinates (0, 0).
16+
17+
Write a function that given a point in the target (defined by its `real` cartesian coordinates `x` and `y`), returns the correct amount earned by a dart landing in that point.
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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": ["jiegillet"],
3+
"contributors": [
4+
"angelikatyborska"
5+
],
6+
"files": {
7+
"example": [
8+
".meta/example.ex"
9+
],
10+
"solution": [
11+
"lib/darts.ex"
12+
],
13+
"test": [
14+
"test/darts_test.exs"
15+
]
16+
},
17+
"blurb": "Write a function that returns the earned points in a single toss of a Darts game.",
18+
"source": "Inspired by an exercise created by a professor Della Paolera in Argentina"
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
defmodule Darts do
2+
@type position :: {number, number}
3+
4+
@doc """
5+
Calculate the score of a single dart hitting a target
6+
"""
7+
@spec score(position :: position) :: integer
8+
def score({x, y}) do
9+
radius = :math.sqrt(x * x + y * y)
10+
11+
cond do
12+
radius > 10 -> 0
13+
radius > 5 -> 1
14+
radius > 1 -> 5
15+
true -> 10
16+
end
17+
end
18+
end
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
[9033f731-0a3a-4d9c-b1c0-34a1c8362afb]
12+
description = "Missed target"
13+
14+
[4c9f6ff4-c489-45fd-be8a-1fcb08b4d0ba]
15+
description = "On the outer circle"
16+
17+
[14378687-ee58-4c9b-a323-b089d5274be8]
18+
description = "On the middle circle"
19+
20+
[849e2e63-85bd-4fed-bc3b-781ae962e2c9]
21+
description = "On the inner circle"
22+
23+
[1c5ffd9f-ea66-462f-9f06-a1303de5a226]
24+
description = "Exactly on centre"
25+
26+
[b65abce3-a679-4550-8115-4b74bda06088]
27+
description = "Near the centre"
28+
29+
[66c29c1d-44f5-40cf-9927-e09a1305b399]
30+
description = "Just within the inner circle"
31+
32+
[d1012f63-c97c-4394-b944-7beb3d0b141a]
33+
description = "Just outside the inner circle"
34+
35+
[ab2b5666-b0b4-49c3-9b27-205e790ed945]
36+
description = "Just within the middle circle"
37+
38+
[70f1424e-d690-4860-8caf-9740a52c0161]
39+
description = "Just outside the middle circle"
40+
41+
[a7dbf8db-419c-4712-8a7f-67602b69b293]
42+
description = "Just within the outer circle"
43+
44+
[e0f39315-9f9a-4546-96e4-a9475b885aa7]
45+
description = "Just outside the outer circle"
46+
47+
[045d7d18-d863-4229-818e-b50828c75d19]
48+
description = "Asymmetric position between the inner and middle circles"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
defmodule Darts do
2+
@type position :: {number, number}
3+
4+
@doc """
5+
Calculate the score of a single dart hitting a target
6+
"""
7+
@spec score(position :: position) :: integer
8+
def score({x, y}) do
9+
end
10+
end

exercises/practice/darts/mix.exs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
defmodule Darts.MixProject do
2+
use Mix.Project
3+
4+
def project do
5+
[
6+
app: :darts,
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: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
defmodule DartsTest do
2+
use ExUnit.Case
3+
4+
# @tag :pending
5+
test "Missed target" do
6+
assert Darts.score({-9, 9}) == 0
7+
end
8+
9+
@tag :pending
10+
test "On the outer circle" do
11+
assert Darts.score({0, 10}) == 1
12+
end
13+
14+
@tag :pending
15+
test "On the middle circle" do
16+
assert Darts.score({-5, 0}) == 5
17+
end
18+
19+
@tag :pending
20+
test "On the inner circle" do
21+
assert Darts.score({0, -1}) == 10
22+
end
23+
24+
@tag :pending
25+
test "Exactly on centre" do
26+
assert Darts.score({0, 0}) == 10
27+
end
28+
29+
@tag :pending
30+
test "Near the centre" do
31+
assert Darts.score({-0.1, -0.1}) == 10
32+
end
33+
34+
@tag :pending
35+
test "Just within the inner circle" do
36+
assert Darts.score({0.7, 0.7}) == 10
37+
end
38+
39+
@tag :pending
40+
test "Just outside the inner circle" do
41+
assert Darts.score({0.8, -0.8}) == 5
42+
end
43+
44+
@tag :pending
45+
test "Just within the middle circle" do
46+
assert Darts.score({-3.5, 3.5}) == 5
47+
end
48+
49+
@tag :pending
50+
test "Just outside the middle circle" do
51+
assert Darts.score({-3.6, -3.6}) == 1
52+
end
53+
54+
@tag :pending
55+
test "Just within the outer circle" do
56+
assert Darts.score({-7.0, 7.0}) == 1
57+
end
58+
59+
@tag :pending
60+
test "Just outside the outer circle" do
61+
assert Darts.score({7.1, -7.1}) == 0
62+
end
63+
64+
@tag :pending
65+
test "Asymmetric position between the inner and middle circles" do
66+
assert Darts.score({0.5, -4}) == 5
67+
end
68+
end

0 commit comments

Comments
 (0)