jumpstart step 2 hw even splitters
Define a method, #even_splitters, that accepts a string and returns an array of characters on which the string can be split to produce substrings of the same length.
Don't count empty strings after the split.
Here's an example for "banana":
"banana".split("a") # => ["b", "n", "n"] (all elements same length)
"banana".split("b") # => ["", anana"] (all elements same length - there's only
one element "anana" because the empty string doesn't count)
"banana".split("n") # => ["ba", "a", "a"] (all elements NOT same length)
result: ["b", "a"].
def even_splitters(string)
chrs = string.split("")
chrs_split_evenly = chrs.select do |chr|
split_at_chr = string.split(chr)
if split_at_chr.all? {|seg| seg == "" || seg.length == split_at_chr[-1].length}
chr
end
end
chrs_split_evenly.uniq
end
# test cases
puts "-----Even Splitters----"
puts even_splitters("") == []
puts even_splitters("t") == ["t"]
puts even_splitters("jk") == ["j", "k"]
puts even_splitters("xoxo") == ["x", "o"]
puts even_splitters("banana") == ["b","a"]
puts even_splitters("mishmash") == ["m","h"]