Skip to content

[#767] New practice exercise darts #777

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2465,6 +2465,20 @@
],
"difficulty": 5
},
{
"slug": "darts",
"name": "Darts",
"uuid": "a68113f8-92be-40e0-a97f-eb4239303ef5",
"prerequisites": [
"cond",
"integers",
"floating-point-numbers"
],
"practices": [
"cond"
],
"difficulty": 2
},
{
"slug": "affine-cipher",
"name": "Affine Cipher",
Expand Down
5 changes: 5 additions & 0 deletions exercises/practice/darts/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## General

- The distance of a point `(x, y)` from the center of the target `(0, 0)` can be calculated with `sqrt(x*x + y *y)`

- 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)`
17 changes: 17 additions & 0 deletions exercises/practice/darts/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Description

Write a function that returns the earned points in a single toss of a Darts game.

[Darts](https://en.wikipedia.org/wiki/Darts) is a game where players
throw darts to a [target](https://en.wikipedia.org/wiki/Darts#/media/File:Darts_in_a_dartboard.jpg).

In our particular instance of the game, the target rewards with 4 different amounts of points, depending on where the dart lands:

* If the dart lands outside the target, player earns no points (0 points).
* If the dart lands in the outer circle of the target, player earns 1 point.
* If the dart lands in the middle circle of the target, player earns 5 points.
* If the dart lands in the inner circle of the target, player earns 10 points.

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).

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.
4 changes: 4 additions & 0 deletions exercises/practice/darts/.formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
19 changes: 19 additions & 0 deletions exercises/practice/darts/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": ["jiegillet"],
"contributors": [
"angelikatyborska"
],
"files": {
"example": [
".meta/example.ex"
],
"solution": [
"lib/darts.ex"
],
"test": [
"test/darts_test.exs"
]
},
"blurb": "Write a function that returns the earned points in a single toss of a Darts game.",
"source": "Inspired by an exercise created by a professor Della Paolera in Argentina"
}
18 changes: 18 additions & 0 deletions exercises/practice/darts/.meta/example.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule Darts do
@type position :: {number, number}

@doc """
Calculate the score of a single dart hitting a target
"""
@spec score(position :: position) :: integer
def score({x, y}) do
radius = :math.sqrt(x * x + y * y)

cond do
radius > 10 -> 0
radius > 5 -> 1
radius > 1 -> 5
true -> 10
end
end
end
48 changes: 48 additions & 0 deletions exercises/practice/darts/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.
[9033f731-0a3a-4d9c-b1c0-34a1c8362afb]
description = "Missed target"

[4c9f6ff4-c489-45fd-be8a-1fcb08b4d0ba]
description = "On the outer circle"

[14378687-ee58-4c9b-a323-b089d5274be8]
description = "On the middle circle"

[849e2e63-85bd-4fed-bc3b-781ae962e2c9]
description = "On the inner circle"

[1c5ffd9f-ea66-462f-9f06-a1303de5a226]
description = "Exactly on centre"

[b65abce3-a679-4550-8115-4b74bda06088]
description = "Near the centre"

[66c29c1d-44f5-40cf-9927-e09a1305b399]
description = "Just within the inner circle"

[d1012f63-c97c-4394-b944-7beb3d0b141a]
description = "Just outside the inner circle"

[ab2b5666-b0b4-49c3-9b27-205e790ed945]
description = "Just within the middle circle"

[70f1424e-d690-4860-8caf-9740a52c0161]
description = "Just outside the middle circle"

[a7dbf8db-419c-4712-8a7f-67602b69b293]
description = "Just within the outer circle"

[e0f39315-9f9a-4546-96e4-a9475b885aa7]
description = "Just outside the outer circle"

[045d7d18-d863-4229-818e-b50828c75d19]
description = "Asymmetric position between the inner and middle circles"
10 changes: 10 additions & 0 deletions exercises/practice/darts/lib/darts.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
defmodule Darts do
@type position :: {number, number}

@doc """
Calculate the score of a single dart hitting a target
"""
@spec score(position :: position) :: integer
def score({x, y}) do
end
end
28 changes: 28 additions & 0 deletions exercises/practice/darts/mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule Darts.MixProject do
use Mix.Project

def project do
[
app: :darts,
version: "0.1.0",
# elixir: "~> 1.8",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end

# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end

# Run "mix help deps" to learn about dependencies.
defp deps do
[
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
]
end
end
68 changes: 68 additions & 0 deletions exercises/practice/darts/test/darts_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
defmodule DartsTest do
use ExUnit.Case

# @tag :pending
test "Missed target" do
assert Darts.score({-9, 9}) == 0
end

@tag :pending
test "On the outer circle" do
assert Darts.score({0, 10}) == 1
end

@tag :pending
test "On the middle circle" do
assert Darts.score({-5, 0}) == 5
end

@tag :pending
test "On the inner circle" do
assert Darts.score({0, -1}) == 10
end

@tag :pending
test "Exactly on centre" do
assert Darts.score({0, 0}) == 10
end

@tag :pending
test "Near the centre" do
assert Darts.score({-0.1, -0.1}) == 10
end

@tag :pending
test "Just within the inner circle" do
assert Darts.score({0.7, 0.7}) == 10
end

@tag :pending
test "Just outside the inner circle" do
assert Darts.score({0.8, -0.8}) == 5
end

@tag :pending
test "Just within the middle circle" do
assert Darts.score({-3.5, 3.5}) == 5
end

@tag :pending
test "Just outside the middle circle" do
assert Darts.score({-3.6, -3.6}) == 1
end

@tag :pending
test "Just within the outer circle" do
assert Darts.score({-7.0, 7.0}) == 1
end

@tag :pending
test "Just outside the outer circle" do
assert Darts.score({7.1, -7.1}) == 0
end

@tag :pending
test "Asymmetric position between the inner and middle circles" do
assert Darts.score({0.5, -4}) == 5
end
end
2 changes: 2 additions & 0 deletions exercises/practice/darts/test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)