Skip to content

armstrong-numbers: port #988

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

Merged
merged 2 commits into from
Aug 8, 2019
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
10 changes: 10 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@
"arrays"
]
},
{
"slug": "armstrong-numbers",
"uuid": "77c5c7e6-265a-4f1a-9bc4-851f8f825420",
"core": false,
"unlocked_by": "series",
"difficulty": 3,
"topics": [
"math"
]
},
{
"slug": "matrix",
"uuid": "3de21c18-a533-4150-8a73-49df8fcb8c61",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'generator/exercise_case'

class ArmstrongNumbersCase < Generator::ExerciseCase

def workload
assert_or_refute(expected, call_armstrong)
end

def call_armstrong
"ArmstrongNumbers.include?(#{underscore(input['number'])})"
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ArmstrongNumbers
def self.include?(number)
exponent = number.digits.size

number.digits.sum { |digit| digit**exponent } == number
end
Copy link
Contributor

@emcoding emcoding Aug 8, 2019

Choose a reason for hiding this comment

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

:-)
Since the solution file is sometimes consulted by (new) mentors and students, and to avoid miscommunications in general, I'd prefer to have a solution that is in sync with the solution that we strive for.
That's mostly a matter of opinion, I don't think we have a policy for this.

And I'm slightly confused too. ;-) Does this mean that you think the original solution is not good enough? Or?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I just really wanted to use stabby lambda for the hell of it, and the guide for porting exercises on this track says the solution doesn't need to be ideal (although I think this one is fun 😃)

I have no problem reverting it. That is a simple fix 😃

(I think that porting guide was written before the profound track anatomy project happened, so wanting example solutions that fit the level of the exercise is a reasonable desire given all that you all have done to polish this track 😃)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reverted.

Copy link
Member

Choose a reason for hiding this comment

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

The example solution is a proof of concept, and if it is not "normal" this means that we will likely spot it quickly, if for some reason it comes to be a solution submitted by a student. So there are some advantages to having a non ideal solution, as long as it "proves the concept".

Copy link
Member

Choose a reason for hiding this comment

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

On the other hand, since this isn't example.rb it may be being presented in the note as an acceptable solution... one to approve. In that situation, the story changes.

Copy link
Contributor

@emcoding emcoding Aug 8, 2019

Choose a reason for hiding this comment

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

Oh, I just really wanted to use stabby lambda for the hell of it

Yeah, you mentioned the lambda fun somewhere else, so that's what I thought it was 🤓 .

FYI:
My thinking is: The beauty of this exercise to me is that it does 'light math', that can be solved with 'early' Ruby methods without being scary mathy. And then it can be refactored to use digits.sum.
For other purposes, this exercise would need more 'progressions' in other aspects, to make the learning curve between exercises steep enough on the higher levels.

Thanks for reverting :-)

end
42 changes: 42 additions & 0 deletions exercises/armstrong-numbers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# 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.
50 changes: 50 additions & 0 deletions exercises/armstrong-numbers/armstrong_numbers_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'minitest/autorun'
require_relative 'armstrong_numbers'

# Common test data version: 1.1.0 b3c2522
class ArmstrongNumbersTest < Minitest::Test
def test_zero_is_an_armstrong_number
# skip
assert ArmstrongNumbers.include?(0)
end

def test_single_digit_numbers_are_armstrong_numbers
skip
assert ArmstrongNumbers.include?(5)
end

def test_there_are_no_2_digit_armstrong_numbers
skip
refute ArmstrongNumbers.include?(10)
end

def test_three_digit_number_that_is_an_armstrong_number
skip
assert ArmstrongNumbers.include?(153)
end

def test_three_digit_number_that_is_not_an_armstrong_number
skip
refute ArmstrongNumbers.include?(100)
end

def test_four_digit_number_that_is_an_armstrong_number
skip
assert ArmstrongNumbers.include?(9_474)
end

def test_four_digit_number_that_is_not_an_armstrong_number
skip
refute ArmstrongNumbers.include?(9_475)
end

def test_seven_digit_number_that_is_an_armstrong_number
skip
assert ArmstrongNumbers.include?(9_926_315)
end

def test_seven_digit_number_that_is_not_an_armstrong_number
skip
refute ArmstrongNumbers.include?(9_926_314)
end
end