Darts
From Exercism PythonMITOriginal pagePython1 class period1 exercise13 graded tests
Darts
Instructions
Calculate the points scored in a single toss of a Darts game.
Darts is a game where players throw darts at a target.
In our particular instance of the game, the target rewards 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 at the same point — that is, the circles are concentric defined by the coordinates (0, 0).
Given a point in the target (defined by its Cartesian coordinates x and y, where x and y are real), calculate the correct score earned by a dart landing at that point.
Credit
The scoreboard image was created by habere-et-dispertire using Inkscape.
Open the
01folder and editmain.py. Run Tests checks it.
Hints
General
- This challenge is all about calculating if a point falls on, in, or outside a given circle.
- There are two different ways of calculating if a point falls on, in, or outside a circle.
- This Stack Overflow Post: Equation for Testing if a Point is Inside a Circle outlines one method.
- This DoubleRoot post Position of a point relative to a circle outlines a different one.
- This Math is Fun post covers a more general Distance Between 2 Points calculation.
- Because the dart board is a set of concentric circles, the order in which you calculate points could change the answer significantly. You should pay attention to which direction your calculations "move" in.
- Remember that this exercise has many potential solutions and many paths you can take along the way. No path is manifestly "better" than another, although a particular path may be more interesting or better suited to what you want to learn or explore right now.
- Some paths may trade speed for clarity. Others might take up more memory but be more scalable or maintainable.
- We encourage you to try out more than one programming strategy to see what happens.
For teachers
Darts
Students solve Exercism's “Darts” Python exercise in CS Room, with Exercism's own tests.
- Time: 1 class period
- Tests: 13, set up for you when you import the lesson
- Practices: comparisons