Skip to content

Instantly share code, notes, and snippets.

@andrey-skat
Created September 2, 2013 01:05
Show Gist options
  • Save andrey-skat/6408385 to your computer and use it in GitHub Desktop.
Save andrey-skat/6408385 to your computer and use it in GitHub Desktop.
1 11 21 1211
require 'benchmark'
TIMES = 12
Benchmark.bm do |b|
b.report "mine" do
def next_m(m)
r, prev, sum = "", m[0], 0
m.each_char do |c|
if prev != c
r << "#{sum}#{prev}"
sum, prev = 0, c
end
sum += 1
end
r << "#{sum}#{prev}"
end
s = '1'
TIMES.times {s = next_m(s)}
end
b.report "their" do
def look_and_say(m)
m.gsub(/(.)\1*/){|s| "#{s.size}#{s[0]}"}
end
s = "1"
TIMES.times {s = look_and_say(s)}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment