Skip to content

Instantly share code, notes, and snippets.

@jlsync
Forked from knaveofdiamonds/letters.rb
Last active December 26, 2015 14:08
Show Gist options
  • Save jlsync/7162908 to your computer and use it in GitHub Desktop.
Save jlsync/7162908 to your computer and use it in GitHub Desktop.
require 'rspec/autorun'
def get_results(string, s_i, chars, c_i )
finds = []
while i = string.index(chars[c_i], s_i)
if chars[c_i + 1]
if more_finds = get_results(string, i + 1, chars , c_i + 1)
finds += more_finds.map{|a| a.unshift(i)}
else
return nil
end
else
finds << [i]
end
s_i = i + 1
end
return finds
end
def letters(a, b)
return get_results(a, 0, b.split(//), 0 )
end
describe "letters" do
let(:a) { "hello world" }
specify "example 1" do
letters(a, "e").should == [[1]]
end
specify "example 2" do
letters(a, "l").should == [[2],[3],[9]]
end
specify "example 3" do
letters(a, "el").should == [[1,2], [1,3], [1,9]]
end
specify "example 4" do
letters(a, "lo").should == [[2,4], [2,7], [3,4], [3,7]]
end
specify "example 5" do
letters(a, "lod").should == [ [2,4,10], [2,7,10], [3,4,10], [3,7,10] ]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment