For fun, I thought I'd see what it looked like to implement the
integer-based algorithm that this quiz is based on. Here's what I came
up with. (I'm a big fan of monkeypatching. :)
class Integer
def each_digit
to_s.each_byte{ |b| yield( b - ?0 ) }
end
def look_and_say
digits, counts = [], []
each_digit{ |d|
if digits.last == d
counts[ counts.size - 1 ] += 1
else
digits << d
counts << 1
end
}
counts.zip( digits ).join.to_i
end
end
n = 1
12.times{ p n; n = n.look_and_say }
#=> 1
#=> 11
#=> 21
#=> 1211
#=> 111221
#=> 312211
#=> 13112221
#=> 1113213211
#=> 31131211131221
#=> 13211311123113112211
#=> 11131221133112132113212221
#=> 3113112221232112111312211312113211