Skip to content

Commit 4a032c1

Browse files
committed
feat: add Darts exercise
1 parent e5dad3f commit 4a032c1

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

exercism/darts/darts.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* <--Introduction-->
3+
* Refer to this exercise for full details:
4+
* https://exercism.org/tracks/c/exercises/darts
5+
*
6+
*/
7+
8+
#include "darts.h"
9+
10+
unsigned int score(coordinate_t landing_position){
11+
12+
// We used the Pythagorean theorem to get the radius by using the two coordinates:
13+
// 1. landing_position.x
14+
// 2. landing_position.y
15+
float radius = sqrt(pow(landing_position.x, 2) + pow(landing_position.y, 2));
16+
17+
/*
18+
* Note: Interval notation is used to show the range of the radius to
19+
* determine the score
20+
*
21+
* If radius is [0, 1] , return a score of 10
22+
* If radius is (1, 5] , return a score of 5
23+
* If radius is (5, 10] , return a score of 1
24+
* If radius is (-inf, 0) or (10, +inf), return a score of 0
25+
*
26+
*/
27+
return radius >= 0 && radius <= 1 ? 10 : radius > 1 && radius <= 5 ? 5 : radius > 5 && radius <= 10 ? 1 : 0;
28+
}

exercism/darts/darts.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef DARTS_H
2+
#define DARTS_H
3+
4+
#include <math.h>
5+
#include <stdlib.h>
6+
7+
typedef struct
8+
{
9+
float x;
10+
float y;
11+
} coordinate_t;
12+
13+
unsigned int score(coordinate_t landing_position);
14+
15+
#endif

0 commit comments

Comments
 (0)