Re:Challenging Programming Exercises Published Weekly
by peteretep on Tuesday December 11, 2012 @03:12PM
(#4906551)
And in some rather nicer Perl:
eg:
[ Parent
| on HN ]
Re:Challenging Programming Exercises Published Weekly
by JasonFruit on Tuesday December 11, 2012 @03:14PM
(#4906561)
That's quite pretty Perl. You've lessened my prejudice against the language by writing code that can be read.
Re:Challenging Programming Exercises Published Weekly
by finnh on Tuesday December 11, 2012 @06:25PM
(#4907614)
Is that accurate? This looks like it only checks for sequential runs of two characters apiece.
The challenge calls for sequences "such that no two immediately adjacent subsequences are equal".
This, to me, implies subsequences of any length > 1.
For a $target_length of 5, your solution is correct as 2 is the longest repeatable subsequence.
Easy fix, of course:
grep { ! /(.{2})\1/ }
becomes grep { ! /(.{2,})\1/ }
I wonder if it would help performance to say: grep { $max = length()/2; $max < 2 || ! /(.{2,$max})\1/ }
Or if the regexp engine manages to do that automatically.[UPDATE: it is slower to provide your own max length. the perl regexp engine is badass. repeating timings on my mac pro with a target_length of 12 show the plain {2,} version is 15% quicker].
(also, you've a minor typo as you recurse into "generate" instead of "solutions", I assume b/c you renamed the function at the very end to make the eg: code scan better).