Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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).



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: