🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of angelikatyborska

angelikatyborska's solution

to Darts in the Erlang Track

Published at Jan 19 2020 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

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

Darts is a game where players throw darts to a target.

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

Running tests

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

Questions?

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.

Source

Inspired by an excersie created by a professor Della Paolera in Argentina

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

darts_tests.erl

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

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

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.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?