Last active
December 26, 2015 12:59
-
-
Save knaveofdiamonds/7155189 to your computer and use it in GitHub Desktop.
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
require 'rspec/autorun' | |
def letters(a, b) | |
indexes = b.each_char.map do |letter| | |
a.each_char.each_with_index.select {|l, _| l == letter }.map {|_, i| i } | |
end | |
start = indexes.shift | |
if indexes.empty? | |
[start] | |
else | |
start.product(*indexes).select {|x| x == x.sort } | |
end | |
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
Wanted to try my luck, and it turned out very similar to other solutions. It was lots of fun, thanks! :-)
Specs used: