From 5e5f72e67a6e3f8ef3c13f7ece9ff0e1d79730bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Mon, 15 Apr 2024 23:58:17 -0700 Subject: [PATCH] Add darts exercise (#219) --- config.json | 8 +++ .../practice/darts/.docs/instructions.md | 31 ++++++++++++ exercises/practice/darts/.gitignore | 11 +++++ exercises/practice/darts/.meta/config.json | 18 +++++++ exercises/practice/darts/.meta/example.lfe | 12 +++++ exercises/practice/darts/.meta/tests.toml | 49 +++++++++++++++++++ exercises/practice/darts/Makefile | 21 ++++++++ exercises/practice/darts/rebar.config | 11 +++++ exercises/practice/darts/rebar.lock | 8 +++ exercises/practice/darts/src/darts.app.src | 11 +++++ exercises/practice/darts/src/darts.lfe | 4 ++ exercises/practice/darts/test/darts-tests.lfe | 44 +++++++++++++++++ 12 files changed, 228 insertions(+) create mode 100644 exercises/practice/darts/.docs/instructions.md create mode 100644 exercises/practice/darts/.gitignore create mode 100644 exercises/practice/darts/.meta/config.json create mode 100644 exercises/practice/darts/.meta/example.lfe create mode 100644 exercises/practice/darts/.meta/tests.toml create mode 100644 exercises/practice/darts/Makefile create mode 100644 exercises/practice/darts/rebar.config create mode 100644 exercises/practice/darts/rebar.lock create mode 100644 exercises/practice/darts/src/darts.app.src create mode 100644 exercises/practice/darts/src/darts.lfe create mode 100644 exercises/practice/darts/test/darts-tests.lfe diff --git a/config.json b/config.json index 2c2724ad..b53e6e4b 100644 --- a/config.json +++ b/config.json @@ -105,6 +105,14 @@ "prerequisites": [], "difficulty": 2 }, + { + "slug": "darts", + "name": "Darts", + "uuid": "6b18a69a-2a24-4db1-bd84-fe599e74443f", + "practices": [], + "prerequisites": [], + "difficulty": 2 + }, { "slug": "difference-of-squares", "name": "Difference of Squares", diff --git a/exercises/practice/darts/.docs/instructions.md b/exercises/practice/darts/.docs/instructions.md new file mode 100644 index 00000000..6518201c --- /dev/null +++ b/exercises/practice/darts/.docs/instructions.md @@ -0,0 +1,31 @@ +# Instructions + +Calculate the points scored in a single toss of a Darts game. + +[Darts][darts] is a game where players throw darts at a [target][darts-target]. + +In our particular instance of the game, the target rewards 4 different amounts of points, depending on where the dart lands: + +![Our dart scoreboard with values from a complete miss to a bullseye](https://assets.exercism.org/images/exercises/darts/darts-scoreboard.svg) + +- 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 at the same point — that is, the circles are [concentric][] defined by the coordinates (0, 0). + +Given a point in the target (defined by its [Cartesian coordinates][cartesian-coordinates] `x` and `y`, where `x` and `y` are [real][real-numbers]), calculate the correct score earned by a dart landing at that point. + +## Credit + +The scoreboard image was created by [habere-et-dispertire][habere-et-dispertire] using [Inkscape][inkscape]. + +[darts]: https://en.wikipedia.org/wiki/Darts +[darts-target]: https://en.wikipedia.org/wiki/Darts#/media/File:Darts_in_a_dartboard.jpg +[concentric]: https://mathworld.wolfram.com/ConcentricCircles.html +[cartesian-coordinates]: https://www.mathsisfun.com/data/cartesian-coordinates.html +[real-numbers]: https://www.mathsisfun.com/numbers/real-numbers.html +[habere-et-dispertire]: https://exercism.org/profiles/habere-et-dispertire +[inkscape]: https://en.wikipedia.org/wiki/Inkscape diff --git a/exercises/practice/darts/.gitignore b/exercises/practice/darts/.gitignore new file mode 100644 index 00000000..6dd20ff0 --- /dev/null +++ b/exercises/practice/darts/.gitignore @@ -0,0 +1,11 @@ +## -*- conf -*- +.rebar3 +_build/ +ebin/ +erl_crash.dump +rebar3.crashdump + +tmp +bin/configlet +bin/configlet.exe +CHECKLIST diff --git a/exercises/practice/darts/.meta/config.json b/exercises/practice/darts/.meta/config.json new file mode 100644 index 00000000..2444d2ca --- /dev/null +++ b/exercises/practice/darts/.meta/config.json @@ -0,0 +1,18 @@ +{ + "authors": [ + "BNAndras" + ], + "files": { + "solution": [ + "src/darts.lfe" + ], + "test": [ + "test/darts-tests.lfe" + ], + "example": [ + ".meta/example.lfe" + ] + }, + "blurb": "Calculate the points scored in a single toss of a Darts game.", + "source": "Inspired by an exercise created by a professor Della Paolera in Argentina" +} diff --git a/exercises/practice/darts/.meta/example.lfe b/exercises/practice/darts/.meta/example.lfe new file mode 100644 index 00000000..eb9a6462 --- /dev/null +++ b/exercises/practice/darts/.meta/example.lfe @@ -0,0 +1,12 @@ +(defmodule darts + (export (score 2))) + +(defun score (x y) + (assign-score (math:sqrt (+ (* x x) + (* y y))))) + +(defun assign-score + ((dist) (when (=< dist 1)) 10) + ((dist) (when (=< dist 5)) 5) + ((dist) (when (=< dist 10)) 1) + ((_) 0)) diff --git a/exercises/practice/darts/.meta/tests.toml b/exercises/practice/darts/.meta/tests.toml new file mode 100644 index 00000000..fbe2976d --- /dev/null +++ b/exercises/practice/darts/.meta/tests.toml @@ -0,0 +1,49 @@ +# 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 center" + +[b65abce3-a679-4550-8115-4b74bda06088] +description = "Near the center" + +[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" diff --git a/exercises/practice/darts/Makefile b/exercises/practice/darts/Makefile new file mode 100644 index 00000000..fbb5a7de --- /dev/null +++ b/exercises/practice/darts/Makefile @@ -0,0 +1,21 @@ +ERL := $(shell which erl) +REBAR3 := $(shell which rebar3) + +null := +space := $(null) # +comma := , + +ifeq ($(ERL),) + $(error Can't find Erlang executable 'erl') +else ifeq ($(REBAR3),) + $(error Can't find rebar3) +endif + +compile: ; $(REBAR3) compile + +clean: ; $(REBAR3) clean + +.PHONY: test +test: + $(REBAR3) eunit \ + -m $(subst $(space),$(comma),$(basename $(notdir $(wildcard test/*.lfe)))) diff --git a/exercises/practice/darts/rebar.config b/exercises/practice/darts/rebar.config new file mode 100644 index 00000000..d53487ac --- /dev/null +++ b/exercises/practice/darts/rebar.config @@ -0,0 +1,11 @@ +{plugins, [{rebar3_lfe, "0.4.3"}]}. + +{provider_hooks, [{post, [{compile, {lfe, compile}}]}]}. + +{deps, [{lfe, "2.1.1"}]}. + +{profiles, + [{test, + [{eunit_compile_opts, [{src_dirs, ["src", "test"]}]}, + {deps, + [{ltest, "0.13.3"}]}]}]}. diff --git a/exercises/practice/darts/rebar.lock b/exercises/practice/darts/rebar.lock new file mode 100644 index 00000000..d5a6b3b9 --- /dev/null +++ b/exercises/practice/darts/rebar.lock @@ -0,0 +1,8 @@ +{"1.2.0", +[{<<"lfe">>,{pkg,<<"lfe">>,<<"2.1.1">>},0}]}. +[ +{pkg_hash,[ + {<<"lfe">>, <<"4A888B26172D198DC7A5AFEB897E8248AF7D56E1638D9C8249AAF933AE811B96">>}]}, +{pkg_hash_ext,[ + {<<"lfe">>, <<"C484D3B655D40DED58BC41B17B22F173711C681BF36063A234A9BAA9506947E1">>}]} +]. diff --git a/exercises/practice/darts/src/darts.app.src b/exercises/practice/darts/src/darts.app.src new file mode 100644 index 00000000..d07a7952 --- /dev/null +++ b/exercises/practice/darts/src/darts.app.src @@ -0,0 +1,11 @@ +%% -*- erlang -*- +{application, 'darts', + [{description, ""}, + {vsn, "0.0.1"}, + {modules, + ['darts']}, + {registered, []}, + {applications, + [kernel, stdlib]}, + {included_applications, []}, + {env, []}]}. diff --git a/exercises/practice/darts/src/darts.lfe b/exercises/practice/darts/src/darts.lfe new file mode 100644 index 00000000..fdfb9774 --- /dev/null +++ b/exercises/practice/darts/src/darts.lfe @@ -0,0 +1,4 @@ +(defmodule darts + (export (score 2))) + +; Please implement the score function. diff --git a/exercises/practice/darts/test/darts-tests.lfe b/exercises/practice/darts/test/darts-tests.lfe new file mode 100644 index 00000000..5ceaeb23 --- /dev/null +++ b/exercises/practice/darts/test/darts-tests.lfe @@ -0,0 +1,44 @@ +(defmodule darts-tests + (behaviour ltest-unit) + (export all)) + +(include-lib "ltest/include/ltest-macros.lfe") + +(deftest missed-target + (is-equal 0 (darts:score -9 9))) + +(deftest on-the-outer-circle + (is-equal 1 (darts:score 0 10))) + +(deftest on-the-middle-circle + (is-equal 5 (darts:score -5 0))) + +(deftest on-the-inner-circle + (is-equal 10 (darts:score 0 -1))) + +(deftest exactly-on-center + (is-equal 10 (darts:score 0 0))) + +(deftest near-the-center + (is-equal 10 (darts:score -0.1 -0.1))) + +(deftest just-within-the-inner-circle + (is-equal 10 (darts:score 0.7 0.7))) + +(deftest just-outside-the-inner-circle + (is-equal 5 (darts:score 0.8 -0.8))) + +(deftest just-within-the-middle-circle + (is-equal 5 (darts:score -3.5 3.5))) + +(deftest just-outside-the-middle-circle + (is-equal 1 (darts:score -3.6 -3.6))) + +(deftest just-within-the-outer-circle + (is-equal 1 (darts:score -7.0 7.0))) + +(deftest just-outside-the-outer-circle + (is-equal 0 (darts:score 7.1 -7.1))) + +(deftest asymmetric-position-between-the-inner-and-middle-circles + (is-equal 5 (darts:score 0.5 -4)))