Created
August 20, 2014 09:53
-
-
Save llacroix/270a1a41766b8a0d55dc to your computer and use it in GitHub Desktop.
Number serie
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 split_num(arr) | |
arr.split("").map {|c| | |
c.to_i | |
} | |
.inject([]){|accumulator, value| | |
# puts "[#{accumulator}, #{value}]" | |
if accumulator.size > 0 and accumulator[-1][0] == value | |
accumulator[-1][1] += 1 | |
else | |
accumulator.push([value, 1]) | |
end | |
accumulator | |
} | |
.map {|value| | |
"#{value[1]}#{value[0]}" | |
} | |
.join("") | |
end | |
def loop (steps, init) | |
steps.times.inject([init]) {|c, r| | |
c.push(split_num(c[-1])) | |
} | |
end | |
puts loop(10, "1") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment