Last active
May 4, 2016 15:02
-
-
Save serghost/30fb0130a31b468e59c939f027930be9 to your computer and use it in GitHub Desktop.
Look-and-say ruby variant
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def next_node(current_node) | |
array_of_digits = current_node.to_s.split('') | |
result = [] | |
array_of_digits.chunk { |n| n }.each do |digit, ary| | |
result << ary.size | |
result << digit | |
end | |
result.join.to_i | |
end | |
def print_sequence(depth, start_node) | |
node = start_node | |
p node | |
1.upto(depth) do | |
node = next_node(node) | |
p node | |
end | |
end | |
print_sequence(10, 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment