with Imagination: by Dustin Diaz

./with Imagination

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

Programming Brain Teaser

Monday, July 7th, 2008

I’ve titled this post as a “Programming” brain teaser because ideally you could solve it in any language. For the sake of the exercise, I’m going to show sample code in JavaScript, but feel free to use your language of choice.
But first and foremost, the reason I’m writing about this is because I ran into a logic problem last week which I thought I would be able to solve in two seconds. Sadly it wasn’t the case so I’d like to share that same problem (in obfuscated form) with you, the readers. I know what you’re thinking. You’re at work, and you’ve got a few minutes to whip up a simple answer. Cool then, go for it. Be sure to share your answer with the rest of us by linking to it offsite, but do not share code in the comments directly as others will most likely want to solve it themselves.

The problem is simple

First, you have an array. It looks like this:

The array


var arr = ['a', 'b', 'c', 'c', 'd','e', 'e',
'e', 'e', 'e', 'f', 'e', 'f', 'e',
'f', 'a', 'a', 'a', 'f', 'f', 'f'];

Be sure to use the array above in your example. You can iterate through an array easily with a batch function like forEach.

using forEach

arr.forEach(funciton(item, index, ar) {
  // do stuff here
});

In the end, we’d like to have an output that looks like the following:

The final output


a b c c d e e <span>e e e</span> f e f e f a a <span>a</span> f f <span>f</span>

You can use basic string concatenation while looping through the items to build your final output. Be sure to test and compare your output results with the actual results. And last but not least, the rule.

So the rule is this

In English: Group together all duplicate items that occur anytime beyond twice by wrapping them with a tag, naturally “bookending” them.
Simple, right? No, really. Tease your brain for a few minutes, you can fix that bug after lunch.

66 Responses to “Programming Brain Teaser”

  1. Dmitry Baranovskiy

    Is it mandatory to use “forEach” method or we can use simple “for” loop?

  2. Dmitry Baranovskiy

    Here is my solution if I got the task right: http://dmitry.baranovskiy.com/post/41419429

  3. Onar

    Can’t make the code look, right, look here:
    http://pastebin.com/m67157328

  4. James Coglan

    Hmm, seems the indentation’s gone from my code and the spans have gone from the result. Trust me, it does work!

  5. Jaisen Mathai

    http://www.jaisenmathai.com/blog/2008/07/07/solving-dustin-diaz-programming-brain-teaser/

  6. Gyran

    I’ve made it work in PHP.
    http://pastebin.com/m30a802e0

    Don’t know if it is a good solution. But it works. I’m sure you can have it done alot easyer.

  7. lvanbass

    Did it in Java, cause that’s what I’m working on right now…
    http://paste.bradleygill.com/index.php?paste_id=598

  8. Matt Ryall

    Shame on you, RStankov. Didn’t you read the part where he said, “Be sure to share your answer with the rest of us by linking to it offsite”?.

    My solution:

    http://www.mattryall.net/blog/2008/07/dustins-programming-problem

  9. Remy Sharp

    Pah! [in his best Monty Python accent] I laugh in the face of your brain teaser - took 10 minutes :-P

    Since you asked for the solution off site (i.e. not in these comments) - here you go:

    http://remysharp.com/downloads/brain-teaser-solution.js

    I’ve taken a slightly different approach to solving this - in particular, I ignored your advice on the forEach.

    Enjoy.

  10. Harmen Janssen

    Here you go: http://dutchinternetworks.nl/dustins-brain-teaser.html
    It took me not very long, but I think I could’ve done it with a little less code. Ah well :)
    I like brain teasers like this, Dustin, post some more if you have ‘em!

  11. Matthew Pennell

    My solution:

    http://www.29digital.net/brain-teaser.html

    Took about 10 minutes - it’s too early in the morning for regular expressions, though. ;)

  12. Programming Brain Teaser -- Jeroen Smeets

    [...] Diaz told a programmer’s riddle — impossible to leave these things alone. So I wrote a solution. One thing bugs me: the itch [...]

  13. The If Works » Blog Archive » Brain teaser

    [...] response to Dustin Diaz (you’ll need a JavaScript 1.8 capable browser, or [...]

  14. James Coglan

    Okay, seeing as how I can’t get my code to render here (and I just noticed you want solutions off-site, my bad):

    http://blog.jcoglan.com/2008/07/08/brain-teaser/

  15. Jeroen Smeets

    Would have loved to write some beautiful js and avoid looping through the array. That proved more difficult than I thought at first. Follow the link to my site for my attempt at solving this.

  16. Jeroen Smeets

    @Remy: that’s what I meant by avoiding the loop. Nice!

  17. kentaromiura

    http://www.dustindiaz.com/programming-brain-teaser/

    changing the first captured group from “.” to another more complex thing teoretically made this code work with words or other token :D

  18. kentaromiura

    lol, i pasted the wrong link :)
    corrected one:

    http://mykenta.blogspot.com/2008/07/dustin-diaz-programming-brain-teaser.html

  19. Julien Royer

    Hi,

    My solution: http://eldebaran.free.fr/miscellaneous/programming-brain-teaser.js

  20. chessguy

    Haskell yawns:

    http://hpaste.org/8784

  21. Baughn

    Haskell makes this almost too easy, with its built-in group function. Someone else already wrote it, though, so I’ll link to that one: http://hpaste.org/8784

  22. Stephen Sorensen

    Good challenge. took about 10 mins using Array.reduce function as documented on mozilla’s site.

    http://spudly.shuoink.com/brainteaser.html

  23. Aristotle Pagaltzis

    arr.join(' ').replace(/((\S) \2 )(\2(?: \2)*)/g, '$1<span>$3</span>')

  24. TJSingleton

    I am sure there is a better way, but here is my quickie.
    http://bin.cakephp.org/view/112169537

  25. Jonathan Snook

    I took the same approach that remy did but I feel like kenta’s solution could be refined into a two function call (join/replace).

  26. Dustin Diaz

    Hi All,
    Wow. Such amazing responses! For anyone that noticed their response has been removed, it’s because you’ve broken the rule of posting code on the site. Please take them offsite :)

    For those that came up with regular expressions, I commend you. You rock in at least one more way than I do :)

    And those that tried Array reduce… it hadn’t even occurred to me. Great job!

    It’s funny that after spelling out the problem in plain English, it soon became clear what I had to do. It took me “hours” because I was looking within the context of 200 lines of code, and I didn’t quite know what I was solving mentally :/
    Perhaps next time I’ll try and come up with a harder one that puts us all on the same playing field :)

  27. jaredmellentine

    Remy Sharp, you are a genius.

  28. kentaromiura

    @JSnook:
    My code is write in that way to make clear the separation from grouping and presentation parts
    (even if i had to use a pair of placeholders ;D ),

    Btw if you don’t mind that separation, look at Dmitry solution that’s do a join with a space before and thus is 5x faster than mine
    (solving 10000 times this problem take 1s with my method and only 0.2s with Dimitry one)

    Actually it can be devided into 3 part
    grouping, adding space (presenting) and replace the placeholders :D

  29. Programming Brain Teaser - enrii.blog

    [...] joined the fun to work on a programming brain teaser by Dustin Diaz (read the original post for details). The general rule is as follows: Group together all duplicate [...]

  30. EngLee

    My solution:
    http://blog.enrii.com/2008/07/09/programming-brain-teaser/

  31. Jakob Heuser

    What a great thing to get started with in the morning. Not knowing if you intended the letters to always be 1-word tokens, I added a bit of defensive code to the structure.

    http://www.felocity.org/tmp/ded.html

  32. Eduardo Antunez

    I came up with a very very simple algorithm.
    I think it’s as simple as it gets without using regular expressions.

    http://antunez.no-ip.org/brain-teaser.js

  33. Matt Tarbit

    Looks like I’m late to the party, but here’s yet another to add to the pile:

    http://matt.tarbit.org/2008/07/08/a-minor-morsel

  34. Darkness Productions’ Blog » Blog Archive » Programming Brain Teaser

    [...] requested by Dustin, I’ve got a solution, in [...]

  35. Binny V A

    The brute force approach. Nothing very clever in this code.
    http://pastebin.ca/1065805

  36. Val

    Well, I haven’t tried to tackle the problem yet, but I think your requirements are not quite accurate:

    Group together all duplicate items that occur anytime beyond twice by wrapping them with a tag, naturally “bookending” them.

    But your example only shows the bookends when the items appear more than twice in a row:

    a b c c d e e e e e f e f e f a a a f f f

    If the requirements were followed, and not the example, the ‘e’s interspersed between the ‘f’s would also get wrapped in spans, as would the ‘f’s following the sequence ‘f e f’, and an extra ‘a’ would get captured in a span:

    a b c c d e e e e e f e f e f a a a f f f

    That looks a bit awkward, and would lead me to ask if the list of items must remain in its original order or if it’s OK that the output be sorted.

    It’s easier to fix the requirements:

    Group together all duplicate items that occur anytime beyond twice in a row by wrapping them with a tag, naturally “bookending” them.

  37. JavaScript programming brain teaser | NeXt

    [...] http://www.dustindiaz.com/programming-brain-teaser/ [...]

  38. RStankov

    I’m really sorry for the first comment with code on it :( , but it was in the early morning :)
    So here is my original solution:
    http://next.pixeldepo.com/files/programming_brain_teaser.js

  39. Dustin Diaz

    @Val: ‘in a row‘ is implied when saying it’s a ‘duplicate’. However this is also why I showed the final desired output so there would be no confusion.
    As far as ordering. No sorting allowed.

  40. Kanashii

    Here’s another to add to the list.

    http://pastebin.com/f2264724c

  41. Jim A.

    http://pastie.textmate.org/230365

    This only took about 20 minutes because I recently worked on a regular expression that checked for repeating characters in a string. JavaScript made this easier than any PCRE implementation of regular expression because it allows for dynamic counting of back-references. I also learned some new stuff about JavaScript’s string.replace() method and how it allows for references to matched portions.

  42. Jim A.

    doh…had a bit of an error in the first…this is better:
    http://pastie.textmate.org/230373

  43. thirstymind.org: andrew watts’ blog » Dustin Diaz’s Brain Teaser

    [...] last night Dustin Diaz posted a short brain teaser, there are lots of solutions already posted, but I didn’t see any with a similar approach to [...]

  44. Andrew Watts

    didn’t really see an approach like mine, so i thought i would also share my solution

  45. Paul O'Shannessy

    Mine is in Ruby, though it would look pretty similar in JavaScript - http://zpao.com/articles/6-programming_brain_teaser

  46. Matt Snider

    That last ‘f’ was tricky, but here is a regex solution:

    arr.join(’ ‘).replace(/((\w)\s)(\1{1,}\2)/g, ‘$3 $2′).replace(/((\w)\s)(\1)(\2)/g, ‘$2 $3$4′);

  47. Matt Snider

    Jim A,

    I tried a regex very similar to yours at first, but it doesn’t capture that last ‘f’, because there is no ‘ ‘ after ‘f’. My regex doesn’t have that limitation, but you could fix yours by adding a space to the end of ’str’ after doing your join(’ ‘), and removing that space after your replace.

  48. The real Mr. Funk

    One line of actionscript (and a bunch of MXML so it compiles):

    http://flex.joshmcdonald.info/2008/07/what-better-way-to-kick-off-new-blog.html

  49. hackademix.net » Programming Brain Teaser

    [...] Dustin Diaz (via http://zpao.com/articles/6-programming_brain_teaser”; target=”_blank” [...]

  50. Giorgio Maone

    You didn’t manage to ruin my lunch :)
    http://hackademix.net/2008/07/10/programming-brain-teaser/

  51. Re: Dustin’s Brain Teaser at weboholic.de

    [...] is my response to Dustin’s brain teaser. I’m offering a solution without regular expressions. Though after reading the problem I [...]

  52. Steven Levithan

    Remy Sharp’s solution is obvious, since I’m a regex nut. You can save a few chars and cycles like this:

    http://stevenlevithan.com/downloads/brain_teaser_solution.js

    (I haven’t read through the other responses in this thread.)

  53. Jim A.

    Matt Snider,

    Great catch, I didn’t even notice. I also don’t like that mine still leaves the trailing space inside the resulting “bookends”. Both could be worked out though.

    Thanks,
    Jim

  54. iratik

    I thought of the algorithm so sequentially that it didn’t even occur to me to use regex! What a great idea. Here’s my shot at it.
    http://rafb.net/p/hf8FjF47.html

  55. M

    don’t think you can beat @Aristotle’s solution.
    but if you’re grouping the duplicate items of more than 3, don’t you want them in a group?

    Like this:
    a b c c d e e e e e f e f e f a a a f f f

  56. M

    or rather like this:
    a b c c d <span>e e e e e</span> f e f e f <span>a a a</span><span> f f f</span>

  57. n0xie

    In php, in less than 10 min. very dirty…

    http://pastebin.com/m544fd9bc

  58. Solving Dustin Diaz’ programming brain teaser :: Jaisen Mathai

    [...] Diaz posted a programming brain teaser on his blog. I decided to take a shot at it and came up with this. The solution took me just under [...]

  59. victori

    Why not just concatenate the whole array into a string and use regexp to capture and replace a repeated group? problem solved.

  60. Rory Fitzpatrick

    My solution in Java (just cos I roxors):

    http://pastebin.com/m78a7b892

    Its ugly and long and I much prefer some of the elegant Javascript solutions, but then I’m not paid to make Java more elegant!

    I would like to note that several of the other solutions don’t take account of spaces in the output, wasn’t that part of the task? (its what took most of my time)

  61. Ciprian Amariei

    Here is my solution in js: http://2wit.eu/ciprian-amariei-solution.html
    It would be interesting to do some performance tests on all solutions ( for each language of course ) :D.

  62. Fabrice

    A very simple and short solution (Javascript) without regexp.
    http://pastebin.com/m23c326d4

  63. Fabrice

    Note : my code above works if you replace letters with words. Would it be possible to use a simple regexp that accepts words in the array instead of single letters ?

  64. Fabrice3

    http://pastebin.com/m23c326d4

    The above works if you replace letters with words. Would it be possible to use a simple regexp that accepts words in the array instead of single letters ?

    (@Dustin sorry for spam, I wanted to add a 2nd comment, didn’t work, then lost my 1st comment, so reposted a 3rd time >< )

  65. Eric Jardas

    Here is my solution in ruby (no regex):
    http://pastie.org/248575

  66. Tim McCormack

    @Aristotle’s is better than mine, but I basically took the same approach:

    http://pastebin.ca/1167177

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

Submit a Prototype

All content copyright © 2003 - 2007 under the Creative Commons License. Wanna know something? Just ask.

About | Archives | Blog Search

[x] close

Loading...

Submit a prototype

By checking this prototype I agree that I am not submitting false credentials, pornography, or a hate crime website. I also understand that by submitting my entry I may or may not be accepted, and if accepted, my entry may be taken down at any given time if I violate these terms.