with Imagination: by Dustin Diaz

./with Imagination

A JavaScript, CSS, XHTML web log focusing on usability and accessibility by Dustin Diaz

RegEx Brain Teaser Part II

Monday, December 8th, 2008

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!

19 Responses to “RegEx Brain Teaser Part II”

  1. Adrien Friggeri

    http://pastie.org/334609

    This should work fine. ;)

  2. WindPower

    Perhaps not the most elegant, but it works.
    http://pastie.org/334612

  3. Dmitry Baranovskiy

    Easy-peasy :) http://pastie.org/334614

  4. Jake

    Here ya go….

    http://pastie.org/334615

  5. Denis

    This is easy.
    Sorry for using PHP, it was easier to test for me.

    http://pastie.org/334691

  6. Sam

    http://pastie.org/334848

  7. Sam

    Yes, I’m lazy ;)

  8. Daniel Vandersluis

    Here’s mine: http://pastie.org/335012

  9. Julian

    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

  10. Dustin Diaz

    Well done everyone!
    Julian, yours is interesting since with ‘0′ it bookends all the duplicates. well done!

  11. Matthew Crumley

    This is probably more complicated than it needs to be, but it works (even with zero).

    http://pastie.org/335327

  12. Solving Dustin Diaz RegEx Brain Teaser Part II | Matt Snider JavaScript Resource

    [...] Diaz of “./with Imagination” blog and Google, wrote a regex challenge. Basically, write a regular expression that wraps any alpha sequence (”w+”) occurring [...]

  13. Matt Snider

    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

  14. manel.villar

    A very simple approach, using a basic syntax:

    http://pastie.org/337151

  15. ARTI

    Thanks you very much again. This is very nice and stable working in PHP.

  16. Syd Hoover

    I couldn’t get the example, must require a regulard expression? I keep getting errors. I guess my brain is teased too much.

    Syd

  17. sep

    Thanks!

  18. Ciprian Amariei

    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.

  19. sigara b?rakma

    Thats perfect man , simple and clever. Thank you

Get "JavaScript Design Patterns"

"As a web developer, you'll already know that JavaScript™ is a powerful language, allowing you to add an impressive array of dynamic functionality to otherwise static web sites. But there is more power waiting to be unlocked--JavaScript is capable of full object-oriented capabilities, and by applying OOP principles, best practices, and design patterns to your code, you can make it more powerful, more efficient, and easier to work with alone or as part of a team."

Buy JS Design Patterns from Amazon.com Buy JS Design Patterns from Apress

All content copyright © 2003 - 2009 under the Creative Commons License.

Archives | Blog Search