Live data from Hacker News

How do I find the longest palindrome in a string?

codegolf.stackexchange.com

1–10 of 12 posts

Re: How do I find the longest palindrome in a string?

#3
post #2

For anyone interested in how to actually do this efficiently, without building a suffix tree, here's a good explanation: http://www.akalin.cx/longest-palindrome-linear-time

Cool, but unfortunately also a great example of when excessive commenting actually makes the code less digestible.

Re: How do I find the longest palindrome in a string?

#4
post #3
post #2

For anyone interested in how to actually do this efficiently, without building a suffix tree, here's a good explanation: http://www.akalin.cx/longest-palindrome-linear-time

Cool, but unfortunately also a great example of when excessive commenting actually makes the code less digestible.

Right. A figure could be more intuitive. But the algorithm is tricky itself anyway...

Re: How do I find the longest palindrome in a string?

#5
post #3
post #2

For anyone interested in how to actually do this efficiently, without building a suffix tree, here's a good explanation: http://www.akalin.cx/longest-palindrome-linear-time

Cool, but unfortunately also a great example of when excessive commenting actually makes the code less digestible.

In addition, the use of cryptic variable names like s, e, d or j with big block comments describing what each one stands for is particularly irritating.

Re: How do I find the longest palindrome in a string?

#6
"generate a set of every single possible Palindrome as large as the string you're testing"

This kind of memoization could well be the most efficient solution in the real world, albeit not one that will pass your CS 101 exam.

More generally #codetrolling is like putting the creative hat on and will probably help people to come up with out-of-the-box tricks that could in some cases be quite useful.

Re: How do I find the longest palindrome in a string?

#10
post #3

Earlier quoted context omitted.

Cool, but unfortunately also a great example of when excessive commenting actually makes the code less digestible.

In addition, the use of cryptic variable names like s, e, d or j with big block comments describing what each one stands for is particularly irritating.

Thanks for the feedback! In my defense, I can only say that I was a less-experienced coder when I wrote that. :)

The comments were intended to extend the preceding prose explanation rather than being comments per se. The actual code is not much longer than the naive version, though, so if I get around to writing an updated version, I'd probably find some other method of conveying that info.

Some other things I'd do differently if I were to update it today:

- I'd write it in Javascript and provide a little interactive applet to play with it. (Similar to my posts on primality testing: http://www.akalin.cx/intro-primality-testing and http://www.akalin.cx/primality-testing-polynomial-time-part-... .)

- I'd upload the code to GitHub.

- I'd add unit tests.

Cheers!

Post reply on HN