Programming Brain Teaser

i is smart!

Posted: July 09, 2008 Tagged: ruby and programming 0 Comments

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. He said any language, so the quickest to get running for me is Ruby (and Command-R 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:


            # 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 > 0 && arr[i] != arr[i-1]
                if count > 2
                  arr[i-1] = arr[i-1] + "</span>" 
                  arr[i-count+2] = "<span>" + arr[i-count+2]
                end
                count = 0
                put_tag = false
              end
              # increment count
              count += 1
            end
            if count > 2
              arr[-1] = arr[-1] + "</span>" 
              arr[-count+2] = "<span>" + arr[-count+2]
            end
            
            puts arr.join(" ")
            

Thinking back, I should have gone for some JavaScript, since that’s mostly what I’m writing these days. Oh well.

Comments

Nobody has commented on this article yet!

© Copyright 2004-2008 Paul O’Shannessy Powered by Blarg & hosted on Dreamhost

Blog | Playground | Twitter | Flickr | Last.fm | Del.icio.us | Facebook | LinkedIn

Exploration Panda