Write a function that returns the earned points in a single toss of a Darts game.
In our particular instance of the game, the target rewards with 4 different amounts of points, depending on where the dart lands:
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) defined by the coordinates (0, 0).
Write a function that given a point in the target (defined by its
real cartesian coordinates
y), returns the correct amount earned by a dart landing in that point.
In order to run the tests, issue the following command from the exercise directory:
For running the tests provided,
rebar3 is used as it is the official build and
dependency management tool for erlang now. Please refer to the tracks installation
instructions on how to do that.
In order to run the tests, you can issue the following command from the exercise directory.
$ rebar3 eunit
For detailed information about the Erlang track, please refer to the help page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.
Inspired by an excersie created by a professor Della Paolera in Argentina
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
%% Based on canonical data version 1.0.0 %% https://github.com/exercism/problem-specifications/raw/master/exercises/darts/canonical-data.json %% This file is automatically generated from the exercises canonical data. -module(darts_tests). -include_lib("erl_exercism/include/exercism.hrl"). -include_lib("eunit/include/eunit.hrl"). '1_a_dart_lands_outside_the_target_test'() -> ?assertEqual(0, darts:score(1.53e+1, 1.31999999999999992895e+1)). '2_a_dart_lands_just_in_the_border_of_the_target_test'() -> ?assertEqual(1, darts:score(10, 0)). '3_a_dart_lands_in_the_middle_circle_test'() -> ?assertEqual(5, darts:score(3, 3.7)). '4_a_dart_lands_right_in_the_border_between_outside_and_middle_circles_test'() -> ?assertEqual(5, darts:score(0, 5)). '5_a_dart_lands_in_the_inner_circle_test'() -> ?assertEqual(10, darts:score(0, 0)).
-module(darts). -export([score/2]). score(X, Y) -> score(math:sqrt(X * X + Y * Y)). score(R) when R =< 1 -> 10; score(R) when R =< 5 -> 5; score(R) when R =< 10 -> 1; score(_R) -> 0.
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.