Other Elixir solutions.
defmodule Darts do
@type position :: {number, number}
@doc """
Calculate the score of a single dart hitting a target
"""
@spec score(position) :: integer
def score({x, y}) do
r = (x ** 2 + y ** 2) ** 0.5
cond do
r <= 1 -> 10
r <= 5 -> 5
r <= 10 -> 1
true -> 0
end
end
end
Other Roc solutions.
module [score]
score : F64, F64 -> U64
score = \x, y ->
r = Num.sqrt (x ^ 2 + y ^ 2)
when r is
_ if r <= 1 -> 10
_ if r <= 5 -> 5
_ if r <= 10 -> 1
_ -> 0
Other C solutions.
#include "darts.h"
#include <math.h>
uint8_t score(coordinate_t coord)
{
float hyp = sqrt(pow(coord.x, 2) + pow(coord.y, 2));
int score = 0;
if (hyp <= 1.0F)
{
score = 10;
}
else if (hyp <= 5.0F)
{
score = 5;
}
else if (hyp <= 10.0F)
{
score = 1;
}
return score;
}