Skip to content

Commit ac1cb36

Browse files
authored
Darts approach (#582)
* Create config.json * Create introduction.md * Create snippet.txt * Create content.md
1 parent b5dcbfd commit ac1cb36

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"introduction": {
3+
"authors": ["bobahop"],
4+
"contributors": []
5+
},
6+
"approaches": [
7+
{
8+
"uuid": "40396c67-efdd-4031-bbfd-12e92e7448c8",
9+
"slug": "hypot-for-radius",
10+
"title": "hypot for radius",
11+
"blurb": "Use the hypot function to get the radius of the throw.",
12+
"authors": ["bobahop"]
13+
}
14+
]
15+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# `hypot` for radius
2+
3+
```kotlin
4+
import kotlin.math.hypot
5+
6+
object Darts {
7+
private const val innerRing = 1.0
8+
private const val middleRing = 5.0
9+
private const val outerRing = 10.0
10+
11+
fun score(x: Number, y: Number): Int {
12+
val toss = hypot(x.toDouble(), y.toDouble())
13+
fun throwWithin(ring: Double) = toss <= ring
14+
15+
if (throwWithin(innerRing)) return 10
16+
if (throwWithin(middleRing)) return 5
17+
if (throwWithin(outerRing)) return 1
18+
return 0
19+
}
20+
}
21+
```
22+
23+
An [object declaration][object] is used to define `Darts` as essentially a [singleton][singleton] object instantiation of the class.
24+
This is sufficient, since there is no object state that needs to change with each call of the `score` method.
25+
26+
The object defines [private][visibility] [`const`][const] [val][variables]s for the rings.
27+
The `const` values are given meaningful names instead of using the float literals as [magic numbers][magic-numbers].
28+
A `val` is immutable, as is a `const`.
29+
A `const` `val` means that the value of the `val` is known at compile time.
30+
31+
The [`hypot`][hypot] function is used to calculate the [radius][radius] of the dart throw from the `x` and `y` coordinates.
32+
33+
The `throwWithin` function returns if the radius is within the ring passed in.
34+
35+
Due to the naming of the function and varables, the `if` statements read much like natural language.
36+
37+
The `throwWithin` function is passed the ring.
38+
If it returns `true`, then the function returns with the score for throwing within that ring.
39+
40+
If the throw is not within a defined ring, then the function returns `0`.
41+
42+
[object]: https://kotlinlang.org/docs/object-declarations.html#object-declarations-overview
43+
[singleton]: https://en.wikipedia.org/wiki/Singleton_pattern
44+
[visibility]: https://kotlinlang.org/docs/visibility-modifiers.html
45+
[const]: https://www.geeksforgeeks.org/whats-the-difference-between-const-and-val-in-kotlin/
46+
[variables]: https://kotlinlang.org/docs/basic-syntax.html#variables
47+
[magic-numbers]: https://en.wikipedia.org/wiki/Magic_number_(programming)
48+
[hypot]: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.math/hypot.html
49+
[radius]: https://www.mathopenref.com/coordbasiccircle.html
50+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
val toss = hypot(x.toDouble(), y.toDouble())
2+
fun throwWithin(ring: Double) = toss <= ring
3+
4+
if (throwWithin(innerRing)) return 10
5+
if (throwWithin(middleRing)) return 5
6+
if (throwWithin(outerRing)) return 1
7+
return 0
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Introduction
2+
3+
There are several ways to solve Darts.
4+
One approach is to use the [`hypot`][hypot] function to get the [radius][radius] of the throw.
5+
6+
## Approach: `hypot` for radius
7+
8+
```kotlin
9+
import kotlin.math.hypot
10+
11+
object Darts {
12+
private const val innerRing = 1.0
13+
private const val middleRing = 5.0
14+
private const val outerRing = 10.0
15+
16+
fun score(x: Number, y: Number): Int {
17+
val toss = hypot(x.toDouble(), y.toDouble())
18+
fun throwWithin(ring: Double) = toss <= ring
19+
20+
if (throwWithin(innerRing)) return 10
21+
if (throwWithin(middleRing)) return 5
22+
if (throwWithin(outerRing)) return 1
23+
return 0
24+
}
25+
}
26+
```
27+
28+
For more information, check the [`hypot` for radius approach][approach-hypot-for-radius].
29+
30+
[approach-hypot-for-radius]: https://exercism.org/tracks/kotlin/exercises/darts/approaches/hypot-for-radius
31+
[hypot]: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.math/hypot.html
32+
[radius]: https://www.mathopenref.com/coordbasiccircle.html

0 commit comments

Comments
 (0)