RegEx Brain Teaser Part II
In July I published a post calling out puzzle enthusiasts to solve a programming brain teaser that involved grouping duplicates. Some solved it with a hefty amount of code, others used a savvy regular expression.
Now I’d like to invite you to yet another brain teaser, except this time your answer must require a regular expression. If you solve it, I would urge you to buy yourself a t-shirt ;)
The Problem
Write a function that takes a text input (first argument), and an integer specifying the amount of duplicates (second argument) to begin the grouping. View the following code as our skeleton:
empty function
function bookend(text, start) {
// return modified text
}
var example = 'a a a a a a a a a bb bb c c c c d a dd dd dd dd dd dd';
var result = bookend(example, 3);
// result is 'a a a (a a a a a a) bb bb c c c (c) d a dd dd dd (dd dd dd)'
var result2 = bookend(example, 4);
// result2 is 'a a a a (a a a a a) bb bb c c c c d a dd dd dd dd (dd dd)'
In English
Bookend all duplicates that begin after the specific amount of minimum duplicates. Therefore if you specify the offset of ‘4′ as your argument, and there are six duplicates in a row, then bookend the final two. Also take special note of the spaces in the examples.
Assumptions
You can assume your text input will only be a mix of letters (eg: “\w+”), and that nothing is separated by more than a single space.
Sharing
If you come up with a solution, post them offsite. I’d recommend Pastie.org.
Happy RegExing!
Cheers!




December 8th, 2008 at 9:08 pm
http://pastie.org/334609
This should work fine. ;)
December 8th, 2008 at 9:29 pm
Perhaps not the most elegant, but it works.
http://pastie.org/334612
December 8th, 2008 at 9:38 pm
Easy-peasy :) http://pastie.org/334614
December 8th, 2008 at 9:39 pm
Here ya go….
http://pastie.org/334615
December 9th, 2008 at 12:45 am
This is easy.
Sorry for using PHP, it was easier to test for me.
http://pastie.org/334691
December 9th, 2008 at 6:47 am
http://pastie.org/334848
December 9th, 2008 at 6:48 am
Yes, I’m lazy ;)
December 9th, 2008 at 9:41 am
Here’s mine: http://pastie.org/335012
December 9th, 2008 at 2:30 pm
Here is my attempt. I have tried to allow for a start point of ‘0′ as well (the entire group as the bookend) and do not use any lookaheads.
http://pastie.org/335273
December 9th, 2008 at 2:43 pm
Well done everyone!
Julian, yours is interesting since with ‘0′ it bookends all the duplicates. well done!
December 9th, 2008 at 3:20 pm
This is probably more complicated than it needs to be, but it works (even with zero).
http://pastie.org/335327
December 10th, 2008 at 10:07 am
[...] Diaz of “./with Imagination” blog and Google, wrote a regex challenge. Basically, write a regular expression that wraps any alpha sequence (”w+”) occurring [...]
December 10th, 2008 at 10:08 am
It was pretty easy. No need to do any look aheads or anything more complicated than a backreference. Here is my writeup:
http://mattsnider.com/uncategorized/solving-dustin-diaz-regex-brain-teaser-part-ii/
And here is a demo:
http://www.mattsnider.com/tests/test_brainTeaser2.html
December 11th, 2008 at 2:58 pm
A very simple approach, using a basic syntax:
http://pastie.org/337151
December 27th, 2008 at 2:25 am
Thanks you very much again. This is very nice and stable working in PHP.
January 4th, 2009 at 11:48 pm
I couldn’t get the example, must require a regulard expression? I keep getting errors. I guess my brain is teased too much.
Syd
January 5th, 2009 at 5:27 pm
Thanks!
January 8th, 2009 at 3:19 pm
Hi all,
here is my solution: http://2wit.eu/ciprian-amariei-dustin-teaser2-solution.html
I’ve noticed that there is a general bug on previous solutions ( except for Dmitry’s I guess, sorry if I missed others ) when the next word has the previous one as prefix eg. a a a a ab should return a a a (a) ab and not a a a (a a)b for n=3.
My solution handles this situation and I played a little with spaces so it doesn’t matter the kind or the number of spaces between words.
Thanks Dustin for this delight.
January 10th, 2009 at 8:06 am
Thats perfect man , simple and clever. Thank you