Created
September 14, 2017 17:59
-
-
Save tadman/638f7ee951238b25ebaab039ed11068e 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 substrings | |
((0...length).to_a.combination(2).map do |from,to| | |
# Extract substrings | |
self[from, to] | |
end + self.chars).map do |s| | |
# Trim off any unwanted characters | |
s.sub(/\A\W+/, '').sub(/\W+\z/, '') | |
end.sort.uniq.reject do |s| | |
# Remove empty strings | |
s.length == 0 | |
end | |
end | |
def phrase_in_common(other) | |
[ self, other ].map(&:downcase).map(&:substrings).reduce(&:&).max_by(&:length) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment