Last active
November 26, 2015 17:18
-
-
Save mkweick/22115ebe200c23b4e31d to your computer and use it in GitHub Desktop.
Series
This file contains hidden or 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 Series | |
attr_reader :series | |
def initialize(series) | |
@series = series | |
end | |
def slices(slice) | |
valid?(slice) | |
last_valid_index = series.length - slice | |
final_combos = [] | |
series.each_char.with_index do |char, index| | |
if index <= last_valid_index | |
combination = [] | |
n = 0 | |
while n < slice do | |
combination << series[index + n].to_i | |
n += 1 | |
end | |
final_combos << combination | |
end | |
end | |
final_combos | |
end | |
private | |
def valid?(slice) | |
fail ArgumentError if series.length - slice < 0 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment