diff --git a/exercises/practice/darts/.approaches/config.json b/exercises/practice/darts/.approaches/config.json new file mode 100644 index 00000000..8d6fae7b --- /dev/null +++ b/exercises/practice/darts/.approaches/config.json @@ -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"] + } + ] +} diff --git a/exercises/practice/darts/.approaches/hypot-for-radius/content.md b/exercises/practice/darts/.approaches/hypot-for-radius/content.md new file mode 100644 index 00000000..a1311f5f --- /dev/null +++ b/exercises/practice/darts/.approaches/hypot-for-radius/content.md @@ -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 + diff --git a/exercises/practice/darts/.approaches/hypot-for-radius/snippet.txt b/exercises/practice/darts/.approaches/hypot-for-radius/snippet.txt new file mode 100644 index 00000000..c53623fc --- /dev/null +++ b/exercises/practice/darts/.approaches/hypot-for-radius/snippet.txt @@ -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 diff --git a/exercises/practice/darts/.approaches/introduction.md b/exercises/practice/darts/.approaches/introduction.md new file mode 100644 index 00000000..070e757b --- /dev/null +++ b/exercises/practice/darts/.approaches/introduction.md @@ -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