Last active
December 17, 2019 18:56
-
-
Save Dimanaux/caf4df00de7138fa217f7b629b6dafd9 to your computer and use it in GitHub Desktop.
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
class Sequence | |
def initialize(str) | |
@digits = str.chars.map(&:to_i) | |
end | |
def next | |
new_digits = [] | |
digit_repeats = 1 | |
@digits.each_cons(2) do |prev, curr| | |
if prev == curr | |
digit_repeats += 1 | |
else | |
new_digits << digit_repeats << prev | |
digit_repeats = 1 | |
end | |
end | |
new_digits << digit_repeats << @digits.last | |
Sequence.new(new_digits.join) | |
end | |
def to_s | |
@digits.join | |
end | |
end | |
s = Sequence.new("1") | |
s.next.to_s #=> "11" | |
s.next.next.to_s #=> "1211" | |
s.next.next.next.to_s #=> "111221" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment