Skip to content

feat: Added implementation for ugly numbers #146

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 9 commits into from
Oct 4, 2023

Conversation

SpiderMath
Copy link
Contributor

@SpiderMath SpiderMath commented Jul 16, 2023

This pull request aims to add the implementation for Ugly Numbers. Lmk if there is any issue in this pull request

Copy link
Contributor

@appgurueu appgurueu left a comment

Choose a reason for hiding this comment

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

Could you add comments to the code?

I'm also not sure whether this should necessarily be a function giving you only the n-th number of the series. An iterator or returning the list of ugly numbers up to the n-th one might be a better design.

@SpiderMath
Copy link
Contributor Author

SpiderMath commented Jul 19, 2023

Could you add comments to the code?

Okay, I'll add some comments to the code (though I thought adding the bits above the file was enough but I can add some more if required)

I'm also not sure whether this should necessarily be a function giving you only the n-th number of the series. An iterator or returning the list of ugly numbers up to the n-th one might be a better design.

But the accompanying implementations in python, java and dart (though dart implementation also made a function isUgly) on the website do the same thing of returning the Nth ugly number, which is why I decided to code it in this way.

@appgurueu
Copy link
Contributor

But the accompanying implementations in python, java and dart (though dart implementation also made a function isUgly) on the website do the same thing of returning the Nth ugly number, which is why I decided to code it in this way.

I think you should choose the best design for this implementation, not mirror the poor design decisions of the other implementations. If I want the first ten ugly numbers, an iterator is much preferable over such a "getter".

@raklaptudirm
Copy link
Member

@appgurueu I would argue that getting the nth number of the series is also good design, because you can simply define a wrapper to convert it into an iterator or return the first n numbers. On the other hand, doing it the opposite way (nth number from iterator or range) is simply doing more work than necessary. Let me know what you think.

@appgurueu
Copy link
Contributor

This algorithm generates a "stream" of numbers, since it needs the previous numbers to calculate the next ones. I think it is only reasonable to use an array or a generator to map this to code. The array may be wasteful though if only, say, the last number is needed, so I think a generator is better. Getting the n-th number from an array is easy; getting it from the generator would be slightly more work, but is pretty standard, at least in non-javascript spheres (Rust's iterators have an nth method for example).

I would argue that getting the nth number of the series is also good design, because you can simply define a wrapper to convert it into an iterator or return the first n numbers.

This is fine from a pure "design"/"correctness" perspective, but it makes an otherwise linear time operation quadratic time.

@SpiderMath
Copy link
Contributor Author

Sorry for the long hiatus (for personal reasons), I'm finally back.
I read the points, and understood your suggestions.
I'll restructure the code as a generator, since it seems to be a better idea from a design perspective. I'll push the code in a day or so.

@SpiderMath
Copy link
Contributor Author

I've made the required changes (I'm not sure on whether the writing of the tests was optimal however, since I don't know how exactly I'm supposed to write tests for generators)

@SpiderMath SpiderMath requested a review from appgurueu October 3, 2023 12:36
Copy link
Contributor

@appgurueu appgurueu left a comment

Choose a reason for hiding this comment

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

I don't know how exactly I'm supposed to write tests for generators

I rewrote your test to just generate the first 11 numbers and compare them against the expected numbers.

(The failing CI is due to something unrelated.)

@SpiderMath
Copy link
Contributor Author

I don't know how exactly I'm supposed to write tests for generators

I rewrote your test to just generate the first 11 numbers and compare them against the expected numbers.

That looks a lot cleaner than what I did, thanks!

(The failing CI is due to something unrelated.)

Ah, understood. Thanks for the clarification! 😄

@raklaptudirm raklaptudirm merged commit 6be6a4b into TheAlgorithms:master Oct 4, 2023
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.

4 participants