darts exercise

This commit is contained in:
Xevion
2019-07-14 02:53:41 -05:00
parent fed57ab7ec
commit 25c4dffd84
4 changed files with 123 additions and 0 deletions

View File

@@ -0,0 +1 @@
{"track":"python","exercise":"darts","id":"dda1870d13b645768af9267f8beb8514","url":"https://exercism.io/my/solutions/dda1870d13b645768af9267f8beb8514","handle":"Xevion","is_requester":true,"auto_approve":false}

65
python/darts/README.md Normal file
View File

@@ -0,0 +1,65 @@
# Darts
Write a function that returns the earned points in a single toss of a Darts game.
[Darts](https://en.wikipedia.org/wiki/Darts) is a game where players
throw darts to a [target](https://en.wikipedia.org/wiki/Darts#/media/File:Darts_in_a_dartboard.jpg).
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](http://mathworld.wolfram.com/ConcentricCircles.html)) 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.
## Exception messages
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to
indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
a message.
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
`raise Exception`, you should write:
```python
raise Exception("Meaningful message indicating the source of the error")
```
## Running the tests
To run the tests, run the appropriate command below ([why they are different](https://github.com/pytest-dev/pytest/issues/1629#issue-161422224)):
- Python 2.7: `py.test darts_test.py`
- Python 3.4+: `pytest darts_test.py`
Alternatively, you can tell Python to run the pytest module (allowing the same command to be used regardless of Python version):
`python -m pytest darts_test.py`
### Common `pytest` options
- `-v` : enable verbose output
- `-x` : stop running tests on first failure
- `--ff` : run failures from previous test before running other test cases
For other options, see `python -m pytest -h`
## Submitting Exercises
Note that, when trying to submit an exercise, make sure the solution is in the `$EXERCISM_WORKSPACE/python/darts` directory.
You can find your Exercism workspace by running `exercism debug` and looking for the line that starts with `Workspace`.
For more detailed information about running tests, code style and linting,
please see [Running the Tests](http://exercism.io/tracks/python/tests).
## 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.

8
python/darts/darts.py Normal file
View File

@@ -0,0 +1,8 @@
from math import sqrt
def dist(x1, y1, x2=0, y2=0):
return sqrt(((x2 - x1) ** 2) + ((y2 - y1) ** 2))
def score(x, y):
distance = dist(x, y)
return 10 if distance <= 1 else 5 if distance <= 5 else 1 if distance <= 10 else 0

View File

@@ -0,0 +1,49 @@
import unittest
from darts import score
# Tests adapted from `problem-specifications//canonical-data.json` @ v2.2.0
class darts_test(unittest.TestCase):
def test_missed_target(self):
self.assertEqual(score(-9, 9), 0)
def test_on_the_outer_circle(self):
self.assertEqual(score(0, 10), 1)
def test_on_the_middle_circle(self):
self.assertEqual(score(-5, 0), 5)
def test_on_the_inner_circle(self):
self.assertEqual(score(0, -1), 10)
def test_exactly_on_centre(self):
self.assertEqual(score(0, 0), 10)
def test_near_the_centre(self):
self.assertEqual(score(-0.1, -0.1), 10)
def test_just_within_the_inner_circle(self):
self.assertEqual(score(0.7, 0.7), 10)
def test_just_outside_the_inner_circle(self):
self.assertEqual(score(0.8, -0.8), 5)
def test_just_within_the_middle_circle(self):
self.assertAlmostEqual(score(-3.5, 3.5), 5)
def test_just_outside_the_middle_circle(self):
self.assertAlmostEqual(score(-3.6, -3.6), 1)
def test_just_within_the_outer_circle(self):
self.assertAlmostEqual(score(-7.0, 7.0), 1)
def test_just_outside_the_outer_circle(self):
self.assertAlmostEqual(score(7.1, -7.1), 0)
def test_asymmetric_position_between_the_inner_and_middle_circles(self):
self.assertAlmostEqual(score(0.5, -4), 5)
if __name__ == '__main__':
unittest.main()