Skip to content

Optimised palindrome-products solution #1398

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 5 commits into from
Closed

Optimised palindrome-products solution #1398

wants to merge 5 commits into from

Conversation

qianzhong516
Copy link
Contributor

Hi, the original solution takes around 30s to pass all the tests on my machine. I have greatly optimized the solution and it only takes 1.5s to pass all the tests now.

@SleeplessByte
Copy link
Member

(@junedev added you for your eyes. @joshgoebel I know you find these really interesting)

Copy link

@surya-11dev surya-11dev left a comment

Choose a reason for hiding this comment

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

No its not working. still cant pass the largest number.
Screenshot from 2021-10-04 23-49-50

@joshgoebel
Copy link
Contributor

How so? Passes ALL tests here locally.

@SleeplessByte
Copy link
Member

@surya-11dev, that's a different issue. That's related to #898, and the test-runner trying to run the skipped test.

@surya-11dev
Copy link

@surya-11dev, that's a different issue. That's related to #898, and the test-runner trying to run the skipped test.

Thank u very much sir for making time to explain things.

@surya-11dev
Copy link

surya-11dev commented Oct 4, 2021

How so? Passes ALL tests here locally.

sorry sir, nothing but some misunderstanding. Thanks for the explanation. Kind of u.

@joshgoebel
Copy link
Contributor

@SleeplessByte Thanks for pinging me, I was mentoring this student so I had seen this early!

@junedev
Copy link
Member

junedev commented Oct 4, 2021

@joshgoebel @SleeplessByte Could you two decide whether this can be merged please? I am not very familiar with this exercise.

Copy link
Contributor

@joshgoebel joshgoebel left a comment

Choose a reason for hiding this comment

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

Has my vote.

@@ -79,7 +79,7 @@ describe('Palindromes', () => {
expect(sortFactors(smallest.factors)).toEqual(expected.factors);
});

test.skip('largest palindrome from four digit factors', () => {
xtest('largest palindrome from four digit factors', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Pretty sure this was intentional.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the previous solution caused some error when ran into this test case, so the maintainer skipped this test by test.skip. After the changes, it should work out fine, so I reverted test.skip to xtest.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is on purpose the test runner on the website is not supposed to run this because it can take too long. There is a bug now where instead it fails. That's a separate issue.

@@ -70,14 +68,22 @@ export class Palindromes {
}

get smallest() {
for (let m = this.minFactor; m <= this.maxFactor; m += 1) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't this the same exact algorithm just written in a much more complex manner? Isn't the loop still covering exactly the same territory? Since you're just finishing the first item you can't get the same benefits you could by optimization the max loop.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please see #1407. I have updated the code after our discussion on exercism.io

Copy link
Contributor

@joshgoebel joshgoebel Oct 6, 2021

Choose a reason for hiding this comment

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

I'll repeat: How is the behavior of this code any different than the previous code? (in regards to smallest) Please explain? From what I can see the for loops do the exact same thing - cover the exact same ground, etc, but they were clearer before with nested for loops.

Copy link
Contributor Author

@qianzhong516 qianzhong516 Oct 6, 2021

Choose a reason for hiding this comment

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

The difference is that they don't generate the "same" pairs. For instance, the for loop solution will generate both 3x5 and 5x3. In the updated solution, there will only be 3x5.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can't this be resolved by simply having the second for loop start with m instead of minFactor, avoiding looping over the same territory as prior passes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, for loops can achieve the same. I was following the logic and coding style I had in the get largest

Copy link
Contributor

Choose a reason for hiding this comment

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

Adding complexity solely for symmetry is not a win IMHO. If the simpler for loop solution work for minimum, lets not complicate it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the comment. I have updated the code accordingly to the above two points in #1407. The reason I started another pull request is that I realized I mixed a few other previous merged commits in this pull request, and I wasn't sure if that would make the process more complicated when you guys merge the changes

@junedev
Copy link
Member

junedev commented Oct 6, 2021

Closing this as it is replaced by #1407

@junedev junedev closed this Oct 6, 2021
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