Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions exercises/practice/darts/.approaches/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"introduction": {
"authors": ["bobahop"],
"contributors": []
},
"approaches": [
{
"uuid": "40396c67-efdd-4031-bbfd-12e92e7448c8",
"slug": "hypot-for-radius",
"title": "hypot for radius",
"blurb": "Use the hypot function to get the radius of the throw.",
"authors": ["bobahop"]
}
]
}
50 changes: 50 additions & 0 deletions exercises/practice/darts/.approaches/hypot-for-radius/content.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# `hypot` for radius

```kotlin
import kotlin.math.hypot

object Darts {
private const val innerRing = 1.0
private const val middleRing = 5.0
private const val outerRing = 10.0

fun score(x: Number, y: Number): Int {
val toss = hypot(x.toDouble(), y.toDouble())
fun throwWithin(ring: Double) = toss <= ring

if (throwWithin(innerRing)) return 10
if (throwWithin(middleRing)) return 5
if (throwWithin(outerRing)) return 1
return 0
}
}
```

An [object declaration][object] is used to define `Darts` as essentially a [singleton][singleton] object instantiation of the class.
This is sufficient, since there is no object state that needs to change with each call of the `score` method.

The object defines [private][visibility] [`const`][const] [val][variables]s for the rings.
The `const` values are given meaningful names instead of using the float literals as [magic numbers][magic-numbers].
A `val` is immutable, as is a `const`.
A `const` `val` means that the value of the `val` is known at compile time.

The [`hypot`][hypot] function is used to calculate the [radius][radius] of the dart throw from the `x` and `y` coordinates.

The `throwWithin` function returns if the radius is within the ring passed in.

Due to the naming of the function and varables, the `if` statements read much like natural language.

The `throwWithin` function is passed the ring.
If it returns `true`, then the function returns with the score for throwing within that ring.

If the throw is not within a defined ring, then the function returns `0`.

[object]: https://kotlinlang.org/docs/object-declarations.html#object-declarations-overview
[singleton]: https://en.wikipedia.org/wiki/Singleton_pattern
[visibility]: https://kotlinlang.org/docs/visibility-modifiers.html
[const]: https://www.geeksforgeeks.org/whats-the-difference-between-const-and-val-in-kotlin/
[variables]: https://kotlinlang.org/docs/basic-syntax.html#variables
[magic-numbers]: https://en.wikipedia.org/wiki/Magic_number_(programming)
[hypot]: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.math/hypot.html
[radius]: https://www.mathopenref.com/coordbasiccircle.html

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
val toss = hypot(x.toDouble(), y.toDouble())
fun throwWithin(ring: Double) = toss <= ring

if (throwWithin(innerRing)) return 10
if (throwWithin(middleRing)) return 5
if (throwWithin(outerRing)) return 1
return 0
32 changes: 32 additions & 0 deletions exercises/practice/darts/.approaches/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Introduction

There are several ways to solve Darts.
One approach is to use the [`hypot`][hypot] function to get the [radius][radius] of the throw.

## Approach: `hypot` for radius

```kotlin
import kotlin.math.hypot

object Darts {
private const val innerRing = 1.0
private const val middleRing = 5.0
private const val outerRing = 10.0

fun score(x: Number, y: Number): Int {
val toss = hypot(x.toDouble(), y.toDouble())
fun throwWithin(ring: Double) = toss <= ring

if (throwWithin(innerRing)) return 10
if (throwWithin(middleRing)) return 5
if (throwWithin(outerRing)) return 1
return 0
}
}
```

For more information, check the [`hypot` for radius approach][approach-hypot-for-radius].

[approach-hypot-for-radius]: https://exercism.org/tracks/kotlin/exercises/darts/approaches/hypot-for-radius
[hypot]: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.math/hypot.html
[radius]: https://www.mathopenref.com/coordbasiccircle.html