Skip to content

Instantly share code, notes, and snippets.

@Pavling
Last active December 26, 2015 12:59
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 Pavling/7155370 to your computer and use it in GitHub Desktop.
Save Pavling/7155370 to your computer and use it in GitHub Desktop.
require 'rspec/autorun'
def get_indexes(needle, haystack)
(0...haystack.length).find_all { |i| haystack[i,1] == needle }
end
def map_indexes(needle, haystack)
needle.split('').map do |nee|
get_indexes nee, haystack
end
end
def letters(haystack, needle)
indexes = map_indexes(needle, haystack)
first_index = indexes.shift
first_index.product(*indexes).select{|match| match == match.sort}
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