ruby challenge 10

what are you looking at?

This challenge has a picture containing an image mapped link. Under the picture is the question;

len(a[30])=?

The link in the picture will bring you to a page containing the first part of an array, this;

a = [1, 11, 21, 1211, 111221,

The sequence isn’t too complicated, I learned it my first year back in college. When I saw it I had a pretty good idea how to solve it. It didn’t take me long to come up with this;

#!/usr/bin/ruby
a, x = ['1'], 0
while x <= 30
b, y, z, n = '', a[x,1].to_s, 0, 1
  while z <= y.length
    if y[z] == y[z+1]
      n, z = n+1, z+1
    else
      b, n, z = b + n.to_s + y[z,1], 1, z+1
    end
    a[(x+1),1] = b
  end
  x += 1
end 
puts "$a[30].length = " + a[30,1].to_s.length.to_s

There are plenty of shorter ways of doing this, using regex’s and such, but this is the that way made the most sense to me. With challenge 10 completed I am now in uncharted territory, and the next challenge has no hints in the source code or on the page only a title and a blurry image. Hmm, I sense more RMagick use ahead.

Next: challenge 11; odd even

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s