Last active
March 15, 2016 11:21
-
-
Save snicky/12148253fc23af5e1c38 to your computer and use it in GitHub Desktop.
This file contains 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 String | |
def constitutes_of?(phrases) | |
_constitutes_of?(phrases.sort_by!(&:length)) | |
end | |
protected | |
def _constitutes_of?(phrases) | |
return false if phrases.empty? | |
absent_phrases = [] | |
phrases.each do |phrase| | |
remainders = [] | |
if (i = index(phrase)) | |
remainders << self[0..i-1] if i > 0 | |
remainders << self[i+phrase.length..-1] | |
else | |
absent_phrases << phrase | |
next | |
end | |
remainders.sort_by!(&:length) | |
phrases_left = phrases - absent_phrases | |
return true if remainders.all? { |r| r.empty? || r._constitutes_of?(phrases_left) } | |
end | |
false | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment