Skip to content

Instantly share code, notes, and snippets.

@Dimanaux
Last active December 17, 2019 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dimanaux/caf4df00de7138fa217f7b629b6dafd9 to your computer and use it in GitHub Desktop.
Save Dimanaux/caf4df00de7138fa217f7b629b6dafd9 to your computer and use it in GitHub Desktop.
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