Skip to content

Instantly share code, notes, and snippets.

@Olefine
Last active August 29, 2015 13:56
Show Gist options
  • Save Olefine/9267413 to your computer and use it in GitHub Desktop.
Save Olefine/9267413 to your computer and use it in GitHub Desktop.
1,11,21,1211 ... sequence
def generate(cardinality=1, result=[1])
if cardinality == 0
return result
else
elem = result.last
sub_ary = elem.to_s.split('').map(&:to_i)
result << group(sub_ary)
generate(cardinality - 1, result)
end
end
def group(ary)
len = ary.length
counter = 0
res = []
while counter < len
tmp_count = 0
first_element_in_seq = ary[counter]
100.times do |i|
if first_element_in_seq == ary[counter + i]
tmp_count += 1
else
break
end
end
res << "#{tmp_count}#{first_element_in_seq}".to_i
counter += tmp_count
end
res.join.to_i
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment