From baf2a6b0961c3368d6d1af835e1acb22aa729c21 Mon Sep 17 00:00:00 2001 From: Eric Tillberg Date: Sun, 12 Jan 2020 11:12:08 -0500 Subject: [PATCH 1/4] darts: Port Darts Exercise --- exercises/darts/.meta/generator/darts_case.rb | 10 +++ exercises/darts/.meta/solutions/darts.rb | 29 +++++++ exercises/darts/README.md | 46 ++++++++++ exercises/darts/darts_test.rb | 83 +++++++++++++++++++ 4 files changed, 168 insertions(+) create mode 100644 exercises/darts/.meta/generator/darts_case.rb create mode 100644 exercises/darts/.meta/solutions/darts.rb create mode 100644 exercises/darts/README.md create mode 100644 exercises/darts/darts_test.rb diff --git a/exercises/darts/.meta/generator/darts_case.rb b/exercises/darts/.meta/generator/darts_case.rb new file mode 100644 index 0000000000..f7cec1aaa4 --- /dev/null +++ b/exercises/darts/.meta/generator/darts_case.rb @@ -0,0 +1,10 @@ +require 'generator/exercise_case' + +class DartsCase < Generator::ExerciseCase + def workload + [ + "darts = Darts.new(#{input["x"]}, #{input["y"]})\n", + "assert_equal(#{expected.inspect}, darts.score)\n" + ].join + end +end diff --git a/exercises/darts/.meta/solutions/darts.rb b/exercises/darts/.meta/solutions/darts.rb new file mode 100644 index 0000000000..6524d81ae6 --- /dev/null +++ b/exercises/darts/.meta/solutions/darts.rb @@ -0,0 +1,29 @@ +class Darts + attr_reader :x, :y + + def initialize(x, y) + @x = x + @y = y + end + + def score + scores.detect do |range, _| + range.cover? distance_to_bullseye + end[1] + end + + private + + def scores + { + 0.0..1.0 => 10, + 1.0..5.0 => 5, + 5.0..10.0 => 1, + 10.0.. => 0, + } + end + + def distance_to_bullseye + Math.sqrt(x * x + y * y) + end +end diff --git a/exercises/darts/README.md b/exercises/darts/README.md new file mode 100644 index 0000000000..42a5d4e58e --- /dev/null +++ b/exercises/darts/README.md @@ -0,0 +1,46 @@ +# 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. +* * * * + +For installation and learning resources, refer to the +[Ruby resources page](http://exercism.io/languages/ruby/resources). + +For running the tests provided, you will need the Minitest gem. Open a +terminal window and run the following command to install minitest: + + gem install minitest + +If you would like color output, you can `require 'minitest/pride'` in +the test file, or note the alternative instruction, below, for running +the test file. + +Run the tests from the exercise directory using the following command: + + ruby darts_test.rb + +To include color from the command line: + + ruby -r minitest/pride darts_test.rb + + +## Source + +Inspired by an exercise 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. diff --git a/exercises/darts/darts_test.rb b/exercises/darts/darts_test.rb new file mode 100644 index 0000000000..f13419682e --- /dev/null +++ b/exercises/darts/darts_test.rb @@ -0,0 +1,83 @@ +require 'minitest/autorun' +require_relative 'darts' + +# Common test data version: 2.2.0 f60c43b +class DartsTest < Minitest::Test + def test_missed_target + # skip + darts = Darts.new(-9, 9) + assert_equal(0, darts.score) + end + + def test_on_the_outer_circle + skip + darts = Darts.new(0, 10) + assert_equal(1, darts.score) + end + + def test_on_the_middle_circle + skip + darts = Darts.new(-5, 0) + assert_equal(5, darts.score) + end + + def test_on_the_inner_circle + skip + darts = Darts.new(0, -1) + assert_equal(10, darts.score) + end + + def test_exactly_on_centre + skip + darts = Darts.new(0, 0) + assert_equal(10, darts.score) + end + + def test_near_the_centre + skip + darts = Darts.new(-0.1, -0.1) + assert_equal(10, darts.score) + end + + def test_just_within_the_inner_circle + skip + darts = Darts.new(0.7, 0.7) + assert_equal(10, darts.score) + end + + def test_just_outside_the_inner_circle + skip + darts = Darts.new(0.8, -0.8) + assert_equal(5, darts.score) + end + + def test_just_within_the_middle_circle + skip + darts = Darts.new(-3.5, 3.5) + assert_equal(5, darts.score) + end + + def test_just_outside_the_middle_circle + skip + darts = Darts.new(-3.6, -3.6) + assert_equal(1, darts.score) + end + + def test_just_within_the_outer_circle + skip + darts = Darts.new(-7.0, 7.0) + assert_equal(1, darts.score) + end + + def test_just_outside_the_outer_circle + skip + darts = Darts.new(7.1, -7.1) + assert_equal(0, darts.score) + end + + def test_asymmetric_position_between_the_inner_and_middle_circles + skip + darts = Darts.new(0.5, -4) + assert_equal(5, darts.score) + end +end From 07d4bb6da5a0748c48db4429cce86ca509ab4237 Mon Sep 17 00:00:00 2001 From: Eric Tillberg Date: Sun, 19 Jan 2020 17:14:07 -0500 Subject: [PATCH 2/4] add entry to config.json --- config.json | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/config.json b/config.json index 25f6c60190..c977abeab4 100644 --- a/config.json +++ b/config.json @@ -1222,6 +1222,17 @@ "topics": [ "loops" ] + }, + { + "slug": "darts", + "uuid": "15b73808-78a4-4854-b7cf-82a478107024", + "core": false, + "unlocked_by": null, + "difficulty": 3, + "topics": [ + "math", + "geometry" + ] } ] } From a7a11971e07b6f65c352784b1bcd5cc4d66eb48e Mon Sep 17 00:00:00 2001 From: Eric Tillberg Date: Wed, 22 Jan 2020 08:43:31 -0500 Subject: [PATCH 3/4] Make solution Ruby 2.5 friendly Co-Authored-By: Victor Goff --- exercises/darts/.meta/solutions/darts.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/darts/.meta/solutions/darts.rb b/exercises/darts/.meta/solutions/darts.rb index 6524d81ae6..32b0196e04 100644 --- a/exercises/darts/.meta/solutions/darts.rb +++ b/exercises/darts/.meta/solutions/darts.rb @@ -19,7 +19,7 @@ def scores 0.0..1.0 => 10, 1.0..5.0 => 5, 5.0..10.0 => 1, - 10.0.. => 0, + 10.0..1.0/0 => 0, } end From 59a545db13a5125abea100df88209534a7bf5036 Mon Sep 17 00:00:00 2001 From: Eric Tillberg Date: Sat, 25 Jan 2020 17:29:11 -0500 Subject: [PATCH 4/4] Improve wording in Darts README --- exercises/darts/README.md | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/exercises/darts/README.md b/exercises/darts/README.md index 42a5d4e58e..21e842aaaf 100644 --- a/exercises/darts/README.md +++ b/exercises/darts/README.md @@ -1,20 +1,22 @@ # Darts -Write a function that returns the earned points in a single toss of a Darts game. +Write a method 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: +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. +If the dart lands: -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). +* outside the target: 0 points. +* in the outer circle of the target: 1 point. +* in the middle circle of the target: 5 points. +* in the inner circle of the target: 10 points. -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. +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. 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 method 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. * * * * For installation and learning resources, refer to the