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
Closed

Conversation

Tuxified
Copy link
Contributor

@Tuxified Tuxified commented Oct 31, 2018

canonical-data

As this is my first stab at a Ruby exercise I didn't use the test generator, hope that's ok.

The solution is not using Integer.digits as that is available on Ruby 2.4, not previous ones.
Let me know what you think :)

TODO:

  • update config.json

As for the config .json metadata, difficulty 1 or 2, categories: integers, loops ?

- not using Integer.digits as that is only available on Ruby 2.4
complying with name convention
@Insti
Copy link
Contributor

Insti commented Nov 1, 2018

Great. thanks!
@F3PiX can you give your estimation of difficulty and where this should fit in the tree?

@Insti Insti requested a review from emcoding November 1, 2018 18:17
@@ -0,0 +1,10 @@
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.

@Insti
Copy link
Contributor

Insti commented Nov 1, 2018

Looks good, I have noted a couple of things that need to be looked at. But it's overall a suitable implementation.
Not making a test generator is fine.

@emcoding
Copy link
Contributor

emcoding commented Nov 1, 2018

Hi @Tuxified , @Insti!

What I like most is that it's an math exercise that doesn't ask to reimplement an existing Ruby method 👍

I feel using digits and sum would add value regarding the Ruby idiom, and make it a bit more about Ruby than about maths.
number.digits.sum { |digit| digit**exponent } == number

I don't think we should avoid Ruby 2.4 at all.
(Ruby 2.5 is adding fun: number.digits.sum { |digit| digit.pow(exponent) } == number , but let's not go there.)

It's an easy exercise regarding the math, but the Enumerable makes it more of a level unlocked by Isogram.
So:
Difficulty 3 seems fit, due to the Enumerable, topic: math, unlocked by Isogram

Note

  • I think I liked the singular class name better. Because it's a check, for a single number, not a collection of sorts. ??

Thoughts?

@Insti
Copy link
Contributor

Insti commented Nov 2, 2018

I think I liked the singular class name better. Because it's a check, for a single number, not a collection of sorts. ??

I really agree with your logic here.

However the convention is that the class name always matches the exercise slug, so the s needs to be there.

See also: #854

@emcoding
Copy link
Contributor

emcoding commented Nov 2, 2018

However the convention is that the class name always matches the exercise slug, so the s needs to be there.

It's a new exercise, so we can change the slug? No, it's not. Sorry, didn't realize that.

@emcoding
Copy link
Contributor

Hey @Tuxified What's the status?

@Tuxified
Copy link
Contributor Author

Tuxified commented Dec 10, 2018

Apologies it took too long to respond, wasn't my intention to neglect your efforts. I've updated to include your comments, however I'm still unsure about the name of method and/or implementing it as an Enumerable.

Are you adamant on the Enumerable implementation or are you open for a different method name?

[edit]
I just saw my specs failed as Travies is running with Ruby 2.1.2, which doesn't include Integer.digits and Enumerable.sum .. so revert to old version with reduce and all?

@emcoding
Copy link
Contributor

emcoding commented Dec 15, 2018

Hey Toni, thanks for the next round!

Looks to me that there's some confusion in the Enumerable discussion.
Let me try to recap and see if we're all talking about the same:

  • Candidates for name of the method:
    • is_valid? : let's follow the styleguide and don't prefix predicate methods with is_...
    • include? : does cover the meaning, but if I understand @petertseng 's comment correctly: as include? is also a method in Enumerable, using this name suggests that ArmstrongNumbers responds to other Enumerable methods as well. It's not about using Enumerable methods in the implementation of the method_to_be_named.
  • valid? is also a bit misleading, because it's mostly use for validations, and that's not exactly what we're doing; a non-armstrong-number is not per se invalid, it's just not an armstrong number.
  • I feel the slug = class name convention is making naming things even harder, but as we're stuck with it (for now), I got giggly and thought of: really? ?

Concluding:

  • Let's avoid names that start with is_, and names that have a meaning in and/or are part of Ruby.
  • I was thinking: maybe use a synonym for the english 'include', like contain? , or more like valid: confirm? ,affirm?, or more like checking: check?, proven?.

I'm okay of any of those, and I'd say let's just 'vote' for our preferences and let Tony decide.
(- Unless, of course, my recap of the discussion is off. -)

@Insti will be better at assessing the tests thing. All I know: there are exercises that use Ruby 2.4 and they work. I didn't look into that.

@emcoding emcoding mentioned this pull request Dec 15, 2018
9 tasks
@Insti
Copy link
Contributor

Insti commented Dec 16, 2018

The tests and solution need to work with Ruby 2.1.2 so you'll need to either "downgrade" the code or write a "polyfill" to support the new syntax. Or create a new issue/PR to upgrade the minimum version of ruby we support.

@Insti
Copy link
Contributor

Insti commented Dec 16, 2018

@Tuxified
For the method name, instead of is_valid? use include?.
(Don't worry about the Enumerable stuff.)

@guygastineau
Copy link
Contributor

Is this abandoned? I am willing to take over implementation of this exercise.
It has been a long time since I contributed here, but I am coming back to Ruby, so I figured helping here will help me ramp back up 😄

@Tuxified I don't want to step on your toes... If you aren't interested in finishing this I could build off of your work, so that you are still listed in the git history.

@Tuxified
Copy link
Contributor Author

Tuxified commented Aug 6, 2019

Hi Guy,

No problem, I lost track of this PR, so if you're willing to pick it up, plz do. No need to keep this PR BTW, I don't care that much about keeping git history :P

@guygastineau
Copy link
Contributor

Cool. I'm on it.

@guygastineau
Copy link
Contributor

@Tuxified I went ahead and rebased your work onto the current master, so I could keep your commits in #988 . I figured there was no reason to redo all the work from scratch, but I didn't want to take your work without credit 😈

@Tuxified
Copy link
Contributor Author

Tuxified commented Aug 8, 2019

Closing this, as #988 is a better choice :P

@Tuxified Tuxified closed this Aug 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants