-
-
Notifications
You must be signed in to change notification settings - Fork 525
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
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
40af229
Add Armstrong Numbers exercise
Tuxified 9343e19
Update and rename armstrong_number.rb to armstrong_numbers.rb
Tuxified ad27c0b
ArmstrongNumber -> ArmstrongNumbers
Tuxified 9fb5995
use underscore for large numbers
Tuxified fde0150
add skip to all but first test
Tuxified 0c6963a
rename tests for better English
Tuxified 19e3900
entry for config.json
Tuxified eb5a5dd
use chars instead of split('')
Tuxified da27025
correct config.json syntax error
Tuxified 96910d4
more idiomatic by using sum instead of reduce
Tuxified 9da8b9b
correct slug to armstrong-numbers
Tuxified File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
exercises/armstrong-numbers/.meta/solutions/armstrong_numbers.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class ArmstrongNumbers | ||
def self.is_valid?(number) | ||
exponent = number.digits.size | ||
|
||
number.digits.sum { |digit| digit**exponent } == number | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
Tuxified marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
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 thanself.armstrong?
, although it's not really about validity, is it? I can't think of a better name though. 🤔There was a problem hiding this comment.
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... butArmstrongNumbers.is?(10)
actually works in the opposite direction compared tois_a?
so it would be unconventional.If using
include?
, thenArmstrongNumbers.include?(10)
does read naturally, but I think it would be surprising because it hints that you can use other Enumerable methods on ArmstrongNumbers (becauseinclude?
is on Enumerable) but you cannot.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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)
?There was a problem hiding this comment.
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.