Skip to content

Add Armstrong Numbers exercise #893

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
14 changes: 12 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,20 @@
"difficulty": 2,
"topics": [
"sequences",
"strings",
"strings",
"regular_expressions"
]
},
{
"slug": "armstrong-numbers",
"uuid": "77c5c7e6-265a-4f1a-9bc4-851f8f825420",
"core": false,
"unlocked_by": "isogram",
"difficulty": 3,
"topics": [
"math"
]
},
{
"slug": "matrix",
"uuid": "3de21c18-a533-4150-8a73-49df8fcb8c61",
Expand Down Expand Up @@ -209,7 +219,7 @@
"maps",
"transforming"
]
},
},
{
"slug": "pangram",
"uuid": "fcf07149-b2cb-4042-90e6-fb3350e0fdf6",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ArmstrongNumbers
def self.is_valid?(number)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in Ruby I try not to use 'is_' prefixes. See: https://github.com/rubocop-hq/ruby-style-guide#bool-methods-prefix

What about: def self.armstrong?

(The name used in the tests will also need updating.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like self.valid? better than self.armstrong?, although it's not really about validity, is it? I can't think of a better name though. 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In response to this comment I heavily considered suggesting is? because I thought it would be slightly amusing... but ArmstrongNumbers.is?(10) actually works in the opposite direction compared to is_a? so it would be unconventional.

If using include?, then ArmstrongNumbers.include?(10) does read naturally, but I think it would be surprising because it hints that you can use other Enumerable methods on ArmstrongNumbers (because include? is on Enumerable) but you cannot.

Copy link
Contributor

@Insti Insti Nov 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like include? 👍

It would be possible to implement this in a way that you could use Enumerable if you wanted to.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

include? sounds nice, but I wonder how you'd like to implement it to use Enumerable? Even if we choose to do this using Enumerable I wonder (and if) students would use other methods from Enumerable? Perhaps we can settle on a better name / other implementation? I saw the Python example defines just one function: def armstrong_number?(number) ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Including Enumerable is not really relevant to this PR but is something that could be interesting to explore as part of a solution.

exponent = number.digits.size

number.digits.sum { |digit| digit**exponent } == number
end
end
41 changes: 41 additions & 0 deletions exercises/armstrong-numbers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Armstrong Numbers

An [Armstrong number](https://en.wikipedia.org/wiki/Narcissistic_number) is a number that is the sum of its own digits each raised to the power of the number of digits.

For example:

- 9 is an Armstrong number, because `9 = 9^1 = 9`
- 10 is *not* an Armstrong number, because `10 != 1^2 + 0^2 = 1`
- 153 is an Armstrong number, because: `153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153`
- 154 is *not* an Armstrong number, because: `154 != 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190`

Write some code to determine whether a number is an Armstrong number.

* * * *

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 armstrong_numbers_test.rb

To include color from the command line:

ruby -r minitest/pride armstrong_numbers_test.rb

## Source

Wikipedia [https://en.wikipedia.org/wiki/Narcissistic_number](https://en.wikipedia.org/wiki/Narcissistic_number)

## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
59 changes: 59 additions & 0 deletions exercises/armstrong-numbers/armstrong_numbers_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require 'minitest/autorun'
require_relative 'armstrong_numbers'

class ArmstrongNumbersTest < Minitest::Test
def test_one_digit_numbers_are_armstrong_numbers
assert ArmstrongNumbers.is_valid?(1)
assert ArmstrongNumbers.is_valid?(6)
assert ArmstrongNumbers.is_valid?(9)
end

def test_two_digit_numbers_aren_t_armstrong_numbers
skip
refute ArmstrongNumbers.is_valid?(11)
refute ArmstrongNumbers.is_valid?(89)
refute ArmstrongNumbers.is_valid?(44)
end

def test_three_digit_numbers_that_are_armstrong_numbers
skip
assert ArmstrongNumbers.is_valid?(153)
assert ArmstrongNumbers.is_valid?(370)
end

def test_three_digit_numbers_that_aren_t_armstrong_numbers
skip
refute ArmstrongNumbers.is_valid?(555)
refute ArmstrongNumbers.is_valid?(662)
end

def test_four_digit_number_that_is_armstrong_number
skip
assert ArmstrongNumbers.is_valid?(1_634)
end

def test_four_digit_number_that_is_not_armstrong_number
skip
refute ArmstrongNumbers.is_valid?(9_989)
end

def test_seven_digit_number_that_is_armstrong_number
skip
assert ArmstrongNumbers.is_valid?(9_926_315)
end

def test_seven_digit_number_that_is_not_armstrong_number
skip
refute ArmstrongNumbers.is_valid?(2_369_989)
end

def test_ten_digit_number_that_is_armstrong_number
skip
assert ArmstrongNumbers.is_valid?(4_679_307_774)
end

def test_fourteen_digit_number_that_is_armstrong_number
skip
assert ArmstrongNumbers.is_valid?(28_116_440_335_967)
end
end