<?xml version="1.0" encoding="UTF-8"?>
<article>
  <blurb>i is smart!</blurb>
  <blurb-html>&lt;p&gt;i is smart!&lt;/p&gt;</blurb-html>
  <body>Since I was at a presentation during lunch, I took some time for myself and took a quick crack at Dustin Diaz's "programming brain teaser":http://www.dustindiaz.com/programming-brain-teaser/.  He said any language, so the quickest to get running for me is Ruby (and &lt;code&gt;Command-R&lt;/code&gt; in Textmate is so easy).

It's not terribly clever, but it takes a different approach from some of the other solutions in that it modifies the array elements then just joins them, instead of creating a single output string and concatenating as time goes.  My favorite solution in the comments used a regular expression, which I thought was particularly clever.  Anyway, here's mine:

&lt;pre&gt;&lt;code class="ruby"&gt;
# the array
arr = ['a', 'b', 'c', 'c', 'd', 'e', 'e',
       'e', 'e', 'e', 'f', 'e', 'f', 'e',
       'f', 'a', 'a', 'a', 'f', 'f', 'f']

# some variables
count = 0
arr.each_index do |i|
  # not the same as previous letter, so do some things
  if i &gt; 0 &amp;&amp; arr[i] != arr[i-1]
    if count &gt; 2
      arr[i-1] = arr[i-1] + "&lt;/span&gt;"
      arr[i-count+2] = "&lt;span&gt;" + arr[i-count+2]
    end
    count = 0
    put_tag = false
  end
  # increment count
  count += 1
end
if count &gt; 2
  arr[-1] = arr[-1] + "&lt;/span&gt;"
  arr[-count+2] = "&lt;span&gt;" + arr[-count+2]
end

puts arr.join(" ")
&lt;/code&gt;&lt;/pre&gt;

Thinking back, I should have gone for some JavaScript, since that's mostly what I'm writing these days. Oh well.</body>
  <body-html>&lt;p&gt;Since I was at a presentation during lunch, I took some time for myself and took a quick crack at Dustin Diaz&amp;#8217;s &lt;a href="http://www.dustindiaz.com/programming-brain-teaser/"&gt;programming brain teaser&lt;/a&gt;.  He said any language, so the quickest to get running for me is Ruby (and &lt;code&gt;Command-R&lt;/code&gt; in Textmate is so easy).&lt;/p&gt;


	&lt;p&gt;It&amp;#8217;s not terribly clever, but it takes a different approach from some of the other solutions in that it modifies the array elements then just joins them, instead of creating a single output string and concatenating as time goes.  My favorite solution in the comments used a regular expression, which I thought was particularly clever.  Anyway, here&amp;#8217;s mine:&lt;/p&gt;


&lt;pre&gt;&lt;code class="ruby"&gt;
# the array
arr = ['a', 'b', 'c', 'c', 'd', 'e', 'e',
       'e', 'e', 'e', 'f', 'e', 'f', 'e',
       'f', 'a', 'a', 'a', 'f', 'f', 'f']

# some variables
count = 0
arr.each_index do |i|
  # not the same as previous letter, so do some things
  if i &amp;gt; 0 &amp;#38;&amp;#38; arr[i] != arr[i-1]
    if count &amp;gt; 2
      arr[i-1] = arr[i-1] + "&amp;lt;/span&amp;gt;" 
      arr[i-count+2] = "&amp;lt;span&amp;gt;" + arr[i-count+2]
    end
    count = 0
    put_tag = false
  end
  # increment count
  count += 1
end
if count &amp;gt; 2
  arr[-1] = arr[-1] + "&amp;lt;/span&amp;gt;" 
  arr[-count+2] = "&amp;lt;span&amp;gt;" + arr[-count+2]
end

puts arr.join(" ")
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Thinking back, I should have gone for some JavaScript, since that&amp;#8217;s mostly what I&amp;#8217;m writing these days. Oh well.&lt;/p&gt;</body-html>
  <created-at type="datetime">2008-07-09T14:32:51-07:00</created-at>
  <id type="integer">6</id>
  <permalink>programming_brain_teaser</permalink>
  <published type="boolean">true</published>
  <published-at type="datetime">2008-07-09T14:32:00-07:00</published-at>
  <title>Programming Brain Teaser</title>
  <updated-at type="datetime">2008-07-14T13:18:07-07:00</updated-at>
</article>
